vector_interface.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2008-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #ifdef BUILDING_STARPU
  18. #include <datawizard/memory_nodes.h>
  19. #endif
  20. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data);
  21. static const struct starpu_data_copy_methods vector_copy_data_methods_s =
  22. {
  23. .any_to_any = copy_any_to_any,
  24. };
  25. static void vector_init(void *data_interface);
  26. static void register_vector_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  27. static starpu_ssize_t allocate_vector_buffer_on_node(void *data_interface_, unsigned dst_node);
  28. static void *vector_to_pointer(void *data_interface, unsigned node);
  29. static int vector_pointer_is_inside(void *data_interface, unsigned node, void *ptr);
  30. static void free_vector_buffer_on_node(void *data_interface, unsigned node);
  31. static size_t vector_interface_get_size(starpu_data_handle_t handle);
  32. static size_t vector_interface_get_alloc_size(starpu_data_handle_t handle);
  33. static uint32_t footprint_vector_interface_crc32(starpu_data_handle_t handle);
  34. static uint32_t alloc_footprint_vector_interface_crc32(starpu_data_handle_t handle);
  35. static int vector_compare(void *data_interface_a, void *data_interface_b);
  36. static int vector_alloc_compare(void *data_interface_a, void *data_interface_b);
  37. static void display_vector_interface(starpu_data_handle_t handle, FILE *f);
  38. static int pack_vector_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count);
  39. static int peek_vector_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count);
  40. static int unpack_vector_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count);
  41. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size);
  42. struct starpu_data_interface_ops starpu_interface_vector_ops =
  43. {
  44. .init = vector_init,
  45. .register_data_handle = register_vector_handle,
  46. .allocate_data_on_node = allocate_vector_buffer_on_node,
  47. .to_pointer = vector_to_pointer,
  48. .pointer_is_inside = vector_pointer_is_inside,
  49. .free_data_on_node = free_vector_buffer_on_node,
  50. .copy_methods = &vector_copy_data_methods_s,
  51. .get_size = vector_interface_get_size,
  52. .get_alloc_size = vector_interface_get_alloc_size,
  53. .footprint = footprint_vector_interface_crc32,
  54. .alloc_footprint = alloc_footprint_vector_interface_crc32,
  55. .compare = vector_compare,
  56. .alloc_compare = vector_alloc_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. .peek_data = peek_vector_handle,
  62. .unpack_data = unpack_vector_handle,
  63. .describe = describe,
  64. .name = "STARPU_VECTOR_INTERFACE"
  65. };
  66. static void vector_init(void *data_interface)
  67. {
  68. struct starpu_vector_interface *vector_interface = data_interface;
  69. vector_interface->allocsize = -1;
  70. }
  71. static void *vector_to_pointer(void *data_interface, unsigned node)
  72. {
  73. (void) node;
  74. struct starpu_vector_interface *vector_interface = data_interface;
  75. return (void*) vector_interface->ptr;
  76. }
  77. static int vector_pointer_is_inside(void *data_interface, unsigned node, void *ptr)
  78. {
  79. (void) node;
  80. struct starpu_vector_interface *vector_interface = data_interface;
  81. return (char*) ptr >= (char*) vector_interface->ptr &&
  82. (char*) ptr < (char*) vector_interface->ptr + vector_interface->nx*vector_interface->elemsize;
  83. }
  84. static void register_vector_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  85. {
  86. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *) data_interface;
  87. unsigned node;
  88. for (node = 0; node < STARPU_MAXNODES; node++)
  89. {
  90. struct starpu_vector_interface *local_interface = (struct starpu_vector_interface *)
  91. starpu_data_get_interface_on_node(handle, node);
  92. if (node == home_node)
  93. {
  94. local_interface->ptr = vector_interface->ptr;
  95. local_interface->dev_handle = vector_interface->dev_handle;
  96. local_interface->offset = vector_interface->offset;
  97. }
  98. else
  99. {
  100. local_interface->ptr = 0;
  101. local_interface->dev_handle = 0;
  102. local_interface->offset = 0;
  103. }
  104. local_interface->id = vector_interface->id;
  105. local_interface->nx = vector_interface->nx;
  106. local_interface->elemsize = vector_interface->elemsize;
  107. local_interface->allocsize = vector_interface->allocsize;
  108. local_interface->slice_base = vector_interface->slice_base;
  109. }
  110. }
  111. /* declare a new data with the vector interface */
  112. void starpu_vector_data_register_allocsize(starpu_data_handle_t *handleptr, int home_node,
  113. uintptr_t ptr, uint32_t nx, size_t elemsize, size_t allocsize)
  114. {
  115. struct starpu_vector_interface vector =
  116. {
  117. .id = STARPU_VECTOR_INTERFACE_ID,
  118. .ptr = ptr,
  119. .nx = nx,
  120. .elemsize = elemsize,
  121. .dev_handle = ptr,
  122. .slice_base = 0,
  123. .offset = 0,
  124. .allocsize = allocsize,
  125. };
  126. #if (!defined(STARPU_SIMGRID) && !defined(STARPU_OPENMP))
  127. if (home_node >= 0 && starpu_node_get_kind(home_node) == STARPU_CPU_RAM)
  128. {
  129. if (nx && elemsize)
  130. {
  131. STARPU_ASSERT_ACCESSIBLE(ptr);
  132. STARPU_ASSERT_ACCESSIBLE(ptr + nx*elemsize - 1);
  133. }
  134. }
  135. #endif
  136. starpu_data_register(handleptr, home_node, &vector, &starpu_interface_vector_ops);
  137. }
  138. void starpu_vector_data_register(starpu_data_handle_t *handleptr, int home_node,
  139. uintptr_t ptr, uint32_t nx, size_t elemsize)
  140. {
  141. starpu_vector_data_register_allocsize(handleptr, home_node, ptr, nx, elemsize, nx * elemsize);
  142. }
  143. void starpu_vector_ptr_register(starpu_data_handle_t handle, unsigned node,
  144. uintptr_t ptr, uintptr_t dev_handle, size_t offset)
  145. {
  146. struct starpu_vector_interface *vector_interface = starpu_data_get_interface_on_node(handle, node);
  147. starpu_data_ptr_register(handle, node);
  148. vector_interface->ptr = ptr;
  149. vector_interface->dev_handle = dev_handle;
  150. vector_interface->offset = offset;
  151. }
  152. static uint32_t footprint_vector_interface_crc32(starpu_data_handle_t handle)
  153. {
  154. return starpu_hash_crc32c_be(starpu_vector_get_nx(handle), 0);
  155. }
  156. static uint32_t alloc_footprint_vector_interface_crc32(starpu_data_handle_t handle)
  157. {
  158. return starpu_hash_crc32c_be(starpu_vector_get_allocsize(handle), 0);
  159. }
  160. static int vector_compare(void *data_interface_a, void *data_interface_b)
  161. {
  162. struct starpu_vector_interface *vector_a = (struct starpu_vector_interface *) data_interface_a;
  163. struct starpu_vector_interface *vector_b = (struct starpu_vector_interface *) data_interface_b;
  164. /* Two vectors are considered compatible if they have the same size */
  165. return (vector_a->nx == vector_b->nx)
  166. && (vector_a->elemsize == vector_b->elemsize);
  167. }
  168. static int vector_alloc_compare(void *data_interface_a, void *data_interface_b)
  169. {
  170. struct starpu_vector_interface *vector_a = (struct starpu_vector_interface *) data_interface_a;
  171. struct starpu_vector_interface *vector_b = (struct starpu_vector_interface *) data_interface_b;
  172. /* Two vectors are considered allocation-compatible if they have the same size */
  173. return (vector_a->allocsize == vector_b->allocsize);
  174. }
  175. static void display_vector_interface(starpu_data_handle_t handle, FILE *f)
  176. {
  177. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  178. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  179. fprintf(f, "%u\t", vector_interface->nx);
  180. }
  181. static int pack_vector_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count)
  182. {
  183. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  184. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  185. starpu_data_get_interface_on_node(handle, node);
  186. *count = vector_interface->nx*vector_interface->elemsize;
  187. if (ptr != NULL)
  188. {
  189. *ptr = (void *)starpu_malloc_on_node_flags(node, *count, 0);
  190. memcpy(*ptr, (void*)vector_interface->ptr, vector_interface->elemsize*vector_interface->nx);
  191. }
  192. return 0;
  193. }
  194. static int peek_vector_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  195. {
  196. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  197. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  198. starpu_data_get_interface_on_node(handle, node);
  199. STARPU_ASSERT(count == vector_interface->elemsize * vector_interface->nx);
  200. memcpy((void*)vector_interface->ptr, ptr, count);
  201. return 0;
  202. }
  203. static int unpack_vector_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  204. {
  205. peek_vector_handle(handle, node, ptr, count);
  206. starpu_free_on_node_flags(node, (uintptr_t)ptr, count, 0);
  207. return 0;
  208. }
  209. static size_t vector_interface_get_size(starpu_data_handle_t handle)
  210. {
  211. size_t size;
  212. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  213. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  214. #ifdef STARPU_DEBUG
  215. STARPU_ASSERT_MSG(vector_interface->id == STARPU_VECTOR_INTERFACE_ID, "Error. The given data is not a vector.");
  216. #endif
  217. size = vector_interface->nx * vector_interface->elemsize;
  218. return size;
  219. }
  220. static size_t vector_interface_get_alloc_size(starpu_data_handle_t handle)
  221. {
  222. size_t size;
  223. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  224. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  225. #ifdef STARPU_DEBUG
  226. STARPU_ASSERT_MSG(vector_interface->id == STARPU_VECTOR_INTERFACE_ID, "Error. The given data is not a vector.");
  227. #endif
  228. size = vector_interface->allocsize;
  229. STARPU_ASSERT_MSG(size != (size_t)-1, "The vector allocation size needs to be defined");
  230. return size;
  231. }
  232. /* offer an access to the data parameters */
  233. uint32_t starpu_vector_get_nx(starpu_data_handle_t handle)
  234. {
  235. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  236. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  237. #ifdef STARPU_DEBUG
  238. STARPU_ASSERT_MSG(vector_interface->id == STARPU_VECTOR_INTERFACE_ID, "Error. The given data is not a vector.");
  239. #endif
  240. return vector_interface->nx;
  241. }
  242. uintptr_t starpu_vector_get_local_ptr(starpu_data_handle_t handle)
  243. {
  244. unsigned node;
  245. node = starpu_worker_get_local_memory_node();
  246. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  247. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  248. starpu_data_get_interface_on_node(handle, node);
  249. #ifdef STARPU_DEBUG
  250. STARPU_ASSERT_MSG(vector_interface->id == STARPU_VECTOR_INTERFACE_ID, "Error. The given data is not a vector.");
  251. #endif
  252. return vector_interface->ptr;
  253. }
  254. size_t starpu_vector_get_elemsize(starpu_data_handle_t handle)
  255. {
  256. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  257. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  258. #ifdef STARPU_DEBUG
  259. STARPU_ASSERT_MSG(vector_interface->id == STARPU_VECTOR_INTERFACE_ID, "Error. The given data is not a vector.");
  260. #endif
  261. return vector_interface->elemsize;
  262. }
  263. size_t starpu_vector_get_allocsize(starpu_data_handle_t handle)
  264. {
  265. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  266. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  267. #ifdef STARPU_DEBUG
  268. STARPU_ASSERT_MSG(vector_interface->id == STARPU_VECTOR_INTERFACE_ID, "Error. The given data is not a vector.");
  269. #endif
  270. return vector_interface->allocsize;
  271. }
  272. /* memory allocation/deallocation primitives for the vector interface */
  273. /* returns the size of the allocated area */
  274. static starpu_ssize_t allocate_vector_buffer_on_node(void *data_interface_, unsigned dst_node)
  275. {
  276. uintptr_t addr = 0, handle;
  277. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *) data_interface_;
  278. starpu_ssize_t allocated_memory = vector_interface->allocsize;
  279. handle = starpu_malloc_on_node(dst_node, allocated_memory);
  280. if (!handle)
  281. return -ENOMEM;
  282. if (starpu_node_get_kind(dst_node) != STARPU_OPENCL_RAM)
  283. addr = handle;
  284. /* update the data properly in consequence */
  285. vector_interface->ptr = addr;
  286. vector_interface->dev_handle = handle;
  287. vector_interface->offset = 0;
  288. return allocated_memory;
  289. }
  290. static void free_vector_buffer_on_node(void *data_interface, unsigned node)
  291. {
  292. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *) data_interface;
  293. starpu_free_on_node(node, vector_interface->dev_handle, vector_interface->allocsize);
  294. }
  295. static int copy_any_to_any(void *src_interface, unsigned src_node,
  296. void *dst_interface, unsigned dst_node, void *async_data)
  297. {
  298. struct starpu_vector_interface *src_vector = src_interface;
  299. struct starpu_vector_interface *dst_vector = dst_interface;
  300. int ret;
  301. ret = starpu_interface_copy(src_vector->dev_handle, src_vector->offset, src_node,
  302. dst_vector->dev_handle, dst_vector->offset, dst_node,
  303. src_vector->nx*src_vector->elemsize, async_data);
  304. starpu_interface_data_copy(src_node, dst_node, src_vector->nx*src_vector->elemsize);
  305. return ret;
  306. }
  307. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size)
  308. {
  309. struct starpu_vector_interface *vector = (struct starpu_vector_interface *) data_interface;
  310. return snprintf(buf, size, "V%ux%u",
  311. (unsigned) vector->nx,
  312. (unsigned) vector->elemsize);
  313. }