vector_interface.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2017 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2017 CNRS
  5. * Copyright (C) 2017 Inria
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include <common/config.h>
  20. #include <datawizard/coherency.h>
  21. #include <datawizard/copy_driver.h>
  22. #include <datawizard/filters.h>
  23. #include <datawizard/memory_nodes.h>
  24. #include <datawizard/malloc.h>
  25. #include <starpu_hash.h>
  26. #include <starpu_cuda.h>
  27. #include <starpu_opencl.h>
  28. #include <drivers/opencl/driver_opencl.h>
  29. #include <drivers/mic/driver_mic_source.h>
  30. #include <drivers/scc/driver_scc_source.h>
  31. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data);
  32. static const struct starpu_data_copy_methods vector_copy_data_methods_s =
  33. {
  34. .any_to_any = copy_any_to_any,
  35. };
  36. static void register_vector_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  37. static starpu_ssize_t allocate_vector_buffer_on_node(void *data_interface_, unsigned dst_node);
  38. static void *vector_handle_to_pointer(starpu_data_handle_t data_handle, unsigned node);
  39. static void free_vector_buffer_on_node(void *data_interface, unsigned node);
  40. static size_t vector_interface_get_size(starpu_data_handle_t handle);
  41. static uint32_t footprint_vector_interface_crc32(starpu_data_handle_t handle);
  42. static int vector_compare(void *data_interface_a, void *data_interface_b);
  43. static void display_vector_interface(starpu_data_handle_t handle, FILE *f);
  44. static int pack_vector_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count);
  45. static int unpack_vector_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count);
  46. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size);
  47. struct starpu_data_interface_ops starpu_interface_vector_ops =
  48. {
  49. .register_data_handle = register_vector_handle,
  50. .allocate_data_on_node = allocate_vector_buffer_on_node,
  51. .handle_to_pointer = vector_handle_to_pointer,
  52. .free_data_on_node = free_vector_buffer_on_node,
  53. .copy_methods = &vector_copy_data_methods_s,
  54. .get_size = vector_interface_get_size,
  55. .footprint = footprint_vector_interface_crc32,
  56. .compare = vector_compare,
  57. .interfaceid = STARPU_VECTOR_INTERFACE_ID,
  58. .interface_size = sizeof(struct starpu_vector_interface),
  59. .display = display_vector_interface,
  60. .pack_data = pack_vector_handle,
  61. .unpack_data = unpack_vector_handle,
  62. .describe = describe,
  63. .name = "STARPU_VECTOR_INTERFACE"
  64. };
  65. static void *vector_handle_to_pointer(starpu_data_handle_t handle, unsigned node)
  66. {
  67. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  68. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  69. starpu_data_get_interface_on_node(handle, node);
  70. return (void*) vector_interface->ptr;
  71. }
  72. static void register_vector_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  73. {
  74. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *) data_interface;
  75. unsigned node;
  76. for (node = 0; node < STARPU_MAXNODES; node++)
  77. {
  78. struct starpu_vector_interface *local_interface = (struct starpu_vector_interface *)
  79. starpu_data_get_interface_on_node(handle, node);
  80. if (node == home_node)
  81. {
  82. local_interface->ptr = vector_interface->ptr;
  83. local_interface->dev_handle = vector_interface->dev_handle;
  84. local_interface->offset = vector_interface->offset;
  85. }
  86. else
  87. {
  88. local_interface->ptr = 0;
  89. local_interface->dev_handle = 0;
  90. local_interface->offset = 0;
  91. }
  92. local_interface->id = vector_interface->id;
  93. local_interface->nx = vector_interface->nx;
  94. local_interface->elemsize = vector_interface->elemsize;
  95. local_interface->slice_base = vector_interface->slice_base;
  96. }
  97. }
  98. /* declare a new data with the vector interface */
  99. void starpu_vector_data_register(starpu_data_handle_t *handleptr, int home_node,
  100. uintptr_t ptr, uint32_t nx, size_t elemsize)
  101. {
  102. struct starpu_vector_interface vector =
  103. {
  104. .id = STARPU_VECTOR_INTERFACE_ID,
  105. .ptr = ptr,
  106. .nx = nx,
  107. .elemsize = elemsize,
  108. .dev_handle = ptr,
  109. .slice_base = 0,
  110. .offset = 0
  111. };
  112. #if (!defined(STARPU_SIMGRID) && !defined(STARPU_OPENMP))
  113. if (home_node >= 0 && starpu_node_get_kind(home_node) == STARPU_CPU_RAM)
  114. {
  115. STARPU_ASSERT_ACCESSIBLE(ptr);
  116. STARPU_ASSERT_ACCESSIBLE(ptr + nx*elemsize - 1);
  117. }
  118. #endif
  119. #ifdef STARPU_USE_SCC
  120. _starpu_scc_set_offset_in_shared_memory((void*)vector.ptr, (void**)&(vector.dev_handle), &(vector.offset));
  121. #endif
  122. starpu_data_register(handleptr, home_node, &vector, &starpu_interface_vector_ops);
  123. }
  124. void starpu_vector_ptr_register(starpu_data_handle_t handle, unsigned node,
  125. uintptr_t ptr, uintptr_t dev_handle, size_t offset)
  126. {
  127. struct starpu_vector_interface *vector_interface = starpu_data_get_interface_on_node(handle, node);
  128. starpu_data_ptr_register(handle, node);
  129. vector_interface->ptr = ptr;
  130. vector_interface->dev_handle = dev_handle;
  131. vector_interface->offset = offset;
  132. }
  133. static uint32_t footprint_vector_interface_crc32(starpu_data_handle_t handle)
  134. {
  135. return starpu_hash_crc32c_be(starpu_vector_get_nx(handle), 0);
  136. }
  137. static int vector_compare(void *data_interface_a, void *data_interface_b)
  138. {
  139. struct starpu_vector_interface *vector_a = (struct starpu_vector_interface *) data_interface_a;
  140. struct starpu_vector_interface *vector_b = (struct starpu_vector_interface *) data_interface_b;
  141. /* Two vectors are considered compatible if they have the same size */
  142. return (vector_a->nx == vector_b->nx)
  143. && (vector_a->elemsize == vector_b->elemsize);
  144. }
  145. static void display_vector_interface(starpu_data_handle_t handle, FILE *f)
  146. {
  147. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  148. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  149. fprintf(f, "%u\t", vector_interface->nx);
  150. }
  151. static int pack_vector_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count)
  152. {
  153. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  154. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  155. starpu_data_get_interface_on_node(handle, node);
  156. *count = vector_interface->nx*vector_interface->elemsize;
  157. if (ptr != NULL)
  158. {
  159. _starpu_malloc_flags_on_node(node, ptr, *count, 0);
  160. memcpy(*ptr, (void*)vector_interface->ptr, vector_interface->elemsize*vector_interface->nx);
  161. }
  162. return 0;
  163. }
  164. static int unpack_vector_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  165. {
  166. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  167. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  168. starpu_data_get_interface_on_node(handle, node);
  169. STARPU_ASSERT(count == vector_interface->elemsize * vector_interface->nx);
  170. memcpy((void*)vector_interface->ptr, ptr, count);
  171. return 0;
  172. }
  173. static size_t vector_interface_get_size(starpu_data_handle_t handle)
  174. {
  175. size_t size;
  176. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  177. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  178. #ifdef STARPU_DEBUG
  179. STARPU_ASSERT_MSG(vector_interface->id == STARPU_VECTOR_INTERFACE_ID, "Error. The given data is not a vector.");
  180. #endif
  181. size = vector_interface->nx*vector_interface->elemsize;
  182. return size;
  183. }
  184. /* offer an access to the data parameters */
  185. uint32_t starpu_vector_get_nx(starpu_data_handle_t handle)
  186. {
  187. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  188. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  189. #ifdef STARPU_DEBUG
  190. STARPU_ASSERT_MSG(vector_interface->id == STARPU_VECTOR_INTERFACE_ID, "Error. The given data is not a vector.");
  191. #endif
  192. return vector_interface->nx;
  193. }
  194. uintptr_t starpu_vector_get_local_ptr(starpu_data_handle_t handle)
  195. {
  196. unsigned node;
  197. node = _starpu_memory_node_get_local_key();
  198. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  199. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  200. starpu_data_get_interface_on_node(handle, node);
  201. #ifdef STARPU_DEBUG
  202. STARPU_ASSERT_MSG(vector_interface->id == STARPU_VECTOR_INTERFACE_ID, "Error. The given data is not a vector.");
  203. #endif
  204. return vector_interface->ptr;
  205. }
  206. size_t starpu_vector_get_elemsize(starpu_data_handle_t handle)
  207. {
  208. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  209. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  210. #ifdef STARPU_DEBUG
  211. STARPU_ASSERT_MSG(vector_interface->id == STARPU_VECTOR_INTERFACE_ID, "Error. The given data is not a vector.");
  212. #endif
  213. return vector_interface->elemsize;
  214. }
  215. /* memory allocation/deallocation primitives for the vector interface */
  216. /* returns the size of the allocated area */
  217. static starpu_ssize_t allocate_vector_buffer_on_node(void *data_interface_, unsigned dst_node)
  218. {
  219. uintptr_t addr = 0, handle;
  220. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *) data_interface_;
  221. uint32_t nx = vector_interface->nx;
  222. size_t elemsize = vector_interface->elemsize;
  223. starpu_ssize_t allocated_memory;
  224. handle = starpu_malloc_on_node(dst_node, nx*elemsize);
  225. if (!handle)
  226. return -ENOMEM;
  227. if (starpu_node_get_kind(dst_node) != STARPU_OPENCL_RAM)
  228. addr = handle;
  229. allocated_memory = nx*elemsize;
  230. /* update the data properly in consequence */
  231. vector_interface->ptr = addr;
  232. vector_interface->dev_handle = handle;
  233. vector_interface->offset = 0;
  234. return allocated_memory;
  235. }
  236. static void free_vector_buffer_on_node(void *data_interface, unsigned node)
  237. {
  238. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *) data_interface;
  239. uint32_t nx = vector_interface->nx;
  240. size_t elemsize = vector_interface->elemsize;
  241. starpu_free_on_node(node, vector_interface->dev_handle, nx*elemsize);
  242. }
  243. static int copy_any_to_any(void *src_interface, unsigned src_node,
  244. void *dst_interface, unsigned dst_node, void *async_data)
  245. {
  246. struct starpu_vector_interface *src_vector = src_interface;
  247. struct starpu_vector_interface *dst_vector = dst_interface;
  248. int ret;
  249. ret = starpu_interface_copy(src_vector->dev_handle, src_vector->offset, src_node,
  250. dst_vector->dev_handle, dst_vector->offset, dst_node,
  251. src_vector->nx*src_vector->elemsize, async_data);
  252. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_vector->nx*src_vector->elemsize);
  253. return ret;
  254. }
  255. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size)
  256. {
  257. struct starpu_vector_interface *vector = (struct starpu_vector_interface *) data_interface;
  258. return snprintf(buf, size, "V%ux%u",
  259. (unsigned) vector->nx,
  260. (unsigned) vector->elemsize);
  261. }