vector_interface.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2008-2019 Université de Bordeaux
  4. * Copyright (C) 2011,2012,2014,2017 Inria
  5. * Copyright (C) 2010-2015,2017,2019 CNRS
  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 vector_init(void *data_interface);
  37. static void register_vector_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  38. static starpu_ssize_t allocate_vector_buffer_on_node(void *data_interface_, unsigned dst_node);
  39. static void *vector_to_pointer(void *data_interface, unsigned node);
  40. static int vector_pointer_is_inside(void *data_interface, unsigned node, void *ptr);
  41. static void free_vector_buffer_on_node(void *data_interface, unsigned node);
  42. static size_t vector_interface_get_size(starpu_data_handle_t handle);
  43. static size_t vector_interface_get_alloc_size(starpu_data_handle_t handle);
  44. static uint32_t footprint_vector_interface_crc32(starpu_data_handle_t handle);
  45. static uint32_t alloc_footprint_vector_interface_crc32(starpu_data_handle_t handle);
  46. static int vector_compare(void *data_interface_a, void *data_interface_b);
  47. static int vector_alloc_compare(void *data_interface_a, void *data_interface_b);
  48. static void display_vector_interface(starpu_data_handle_t handle, FILE *f);
  49. static int pack_vector_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count);
  50. static int unpack_vector_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count);
  51. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size);
  52. struct starpu_data_interface_ops starpu_interface_vector_ops =
  53. {
  54. .init = vector_init,
  55. .register_data_handle = register_vector_handle,
  56. .allocate_data_on_node = allocate_vector_buffer_on_node,
  57. .to_pointer = vector_to_pointer,
  58. .pointer_is_inside = vector_pointer_is_inside,
  59. .free_data_on_node = free_vector_buffer_on_node,
  60. .copy_methods = &vector_copy_data_methods_s,
  61. .get_size = vector_interface_get_size,
  62. .get_alloc_size = vector_interface_get_alloc_size,
  63. .footprint = footprint_vector_interface_crc32,
  64. .alloc_footprint = alloc_footprint_vector_interface_crc32,
  65. .compare = vector_compare,
  66. .alloc_compare = vector_alloc_compare,
  67. .interfaceid = STARPU_VECTOR_INTERFACE_ID,
  68. .interface_size = sizeof(struct starpu_vector_interface),
  69. .display = display_vector_interface,
  70. .pack_data = pack_vector_handle,
  71. .unpack_data = unpack_vector_handle,
  72. .describe = describe,
  73. .name = "STARPU_VECTOR_INTERFACE"
  74. };
  75. static void vector_init(void *data_interface)
  76. {
  77. struct starpu_vector_interface *vector_interface = data_interface;
  78. vector_interface->allocsize = -1;
  79. }
  80. static void *vector_to_pointer(void *data_interface, unsigned node)
  81. {
  82. (void) node;
  83. struct starpu_vector_interface *vector_interface = data_interface;
  84. return (void*) vector_interface->ptr;
  85. }
  86. static int vector_pointer_is_inside(void *data_interface, unsigned node, void *ptr)
  87. {
  88. (void) node;
  89. struct starpu_vector_interface *vector_interface = data_interface;
  90. return (char*) ptr >= (char*) vector_interface->ptr &&
  91. (char*) ptr < (char*) vector_interface->ptr + vector_interface->nx*vector_interface->elemsize;
  92. }
  93. static void register_vector_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  94. {
  95. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *) data_interface;
  96. unsigned node;
  97. for (node = 0; node < STARPU_MAXNODES; node++)
  98. {
  99. struct starpu_vector_interface *local_interface = (struct starpu_vector_interface *)
  100. starpu_data_get_interface_on_node(handle, node);
  101. if (node == home_node)
  102. {
  103. local_interface->ptr = vector_interface->ptr;
  104. local_interface->dev_handle = vector_interface->dev_handle;
  105. local_interface->offset = vector_interface->offset;
  106. }
  107. else
  108. {
  109. local_interface->ptr = 0;
  110. local_interface->dev_handle = 0;
  111. local_interface->offset = 0;
  112. }
  113. local_interface->id = vector_interface->id;
  114. local_interface->nx = vector_interface->nx;
  115. local_interface->elemsize = vector_interface->elemsize;
  116. local_interface->allocsize = vector_interface->allocsize;
  117. local_interface->slice_base = vector_interface->slice_base;
  118. }
  119. }
  120. /* declare a new data with the vector interface */
  121. void starpu_vector_data_register_allocsize(starpu_data_handle_t *handleptr, int home_node,
  122. uintptr_t ptr, uint32_t nx, size_t elemsize, size_t allocsize)
  123. {
  124. struct starpu_vector_interface vector =
  125. {
  126. .id = STARPU_VECTOR_INTERFACE_ID,
  127. .ptr = ptr,
  128. .nx = nx,
  129. .elemsize = elemsize,
  130. .dev_handle = ptr,
  131. .slice_base = 0,
  132. .offset = 0,
  133. .allocsize = allocsize,
  134. };
  135. #if (!defined(STARPU_SIMGRID) && !defined(STARPU_OPENMP))
  136. if (home_node >= 0 && starpu_node_get_kind(home_node) == STARPU_CPU_RAM)
  137. {
  138. STARPU_ASSERT_ACCESSIBLE(ptr);
  139. STARPU_ASSERT_ACCESSIBLE(ptr + nx*elemsize - 1);
  140. }
  141. #endif
  142. #ifdef STARPU_USE_SCC
  143. _starpu_scc_set_offset_in_shared_memory((void*)vector.ptr, (void**)&(vector.dev_handle), &(vector.offset));
  144. #endif
  145. starpu_data_register(handleptr, home_node, &vector, &starpu_interface_vector_ops);
  146. }
  147. void starpu_vector_data_register(starpu_data_handle_t *handleptr, int home_node,
  148. uintptr_t ptr, uint32_t nx, size_t elemsize)
  149. {
  150. starpu_vector_data_register_allocsize(handleptr, home_node, ptr, nx, elemsize, nx * elemsize);
  151. }
  152. void starpu_vector_ptr_register(starpu_data_handle_t handle, unsigned node,
  153. uintptr_t ptr, uintptr_t dev_handle, size_t offset)
  154. {
  155. struct starpu_vector_interface *vector_interface = starpu_data_get_interface_on_node(handle, node);
  156. starpu_data_ptr_register(handle, node);
  157. vector_interface->ptr = ptr;
  158. vector_interface->dev_handle = dev_handle;
  159. vector_interface->offset = offset;
  160. }
  161. static uint32_t footprint_vector_interface_crc32(starpu_data_handle_t handle)
  162. {
  163. return starpu_hash_crc32c_be(starpu_vector_get_nx(handle), 0);
  164. }
  165. static uint32_t alloc_footprint_vector_interface_crc32(starpu_data_handle_t handle)
  166. {
  167. return starpu_hash_crc32c_be(starpu_vector_get_allocsize(handle), 0);
  168. }
  169. static int vector_compare(void *data_interface_a, void *data_interface_b)
  170. {
  171. struct starpu_vector_interface *vector_a = (struct starpu_vector_interface *) data_interface_a;
  172. struct starpu_vector_interface *vector_b = (struct starpu_vector_interface *) data_interface_b;
  173. /* Two vectors are considered compatible if they have the same size */
  174. return (vector_a->nx == vector_b->nx)
  175. && (vector_a->elemsize == vector_b->elemsize);
  176. }
  177. static int vector_alloc_compare(void *data_interface_a, void *data_interface_b)
  178. {
  179. struct starpu_vector_interface *vector_a = (struct starpu_vector_interface *) data_interface_a;
  180. struct starpu_vector_interface *vector_b = (struct starpu_vector_interface *) data_interface_b;
  181. /* Two vectors are considered compatible if they have the same size */
  182. return (vector_a->allocsize == vector_b->allocsize);
  183. }
  184. static void display_vector_interface(starpu_data_handle_t handle, FILE *f)
  185. {
  186. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  187. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  188. fprintf(f, "%u\t", vector_interface->nx);
  189. }
  190. static int pack_vector_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count)
  191. {
  192. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  193. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  194. starpu_data_get_interface_on_node(handle, node);
  195. *count = vector_interface->nx*vector_interface->elemsize;
  196. if (ptr != NULL)
  197. {
  198. *ptr = (void *)starpu_malloc_on_node_flags(node, *count, 0);
  199. memcpy(*ptr, (void*)vector_interface->ptr, vector_interface->elemsize*vector_interface->nx);
  200. }
  201. return 0;
  202. }
  203. static int unpack_vector_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  204. {
  205. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  206. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  207. starpu_data_get_interface_on_node(handle, node);
  208. STARPU_ASSERT(count == vector_interface->elemsize * vector_interface->nx);
  209. memcpy((void*)vector_interface->ptr, ptr, count);
  210. starpu_free_on_node_flags(node, (uintptr_t)ptr, count, 0);
  211. return 0;
  212. }
  213. static size_t vector_interface_get_size(starpu_data_handle_t handle)
  214. {
  215. size_t size;
  216. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  217. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  218. #ifdef STARPU_DEBUG
  219. STARPU_ASSERT_MSG(vector_interface->id == STARPU_VECTOR_INTERFACE_ID, "Error. The given data is not a vector.");
  220. #endif
  221. size = vector_interface->nx * vector_interface->elemsize;
  222. return size;
  223. }
  224. static size_t vector_interface_get_alloc_size(starpu_data_handle_t handle)
  225. {
  226. size_t size;
  227. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  228. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  229. #ifdef STARPU_DEBUG
  230. STARPU_ASSERT_MSG(vector_interface->id == STARPU_VECTOR_INTERFACE_ID, "Error. The given data is not a vector.");
  231. #endif
  232. size = vector_interface->allocsize;
  233. STARPU_ASSERT_MSG(size != (size_t)-1, "The vector allocation size needs to be defined");
  234. return size;
  235. }
  236. /* offer an access to the data parameters */
  237. uint32_t starpu_vector_get_nx(starpu_data_handle_t handle)
  238. {
  239. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  240. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  241. #ifdef STARPU_DEBUG
  242. STARPU_ASSERT_MSG(vector_interface->id == STARPU_VECTOR_INTERFACE_ID, "Error. The given data is not a vector.");
  243. #endif
  244. return vector_interface->nx;
  245. }
  246. uintptr_t starpu_vector_get_local_ptr(starpu_data_handle_t handle)
  247. {
  248. unsigned node;
  249. node = _starpu_memory_node_get_local_key();
  250. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  251. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  252. starpu_data_get_interface_on_node(handle, node);
  253. #ifdef STARPU_DEBUG
  254. STARPU_ASSERT_MSG(vector_interface->id == STARPU_VECTOR_INTERFACE_ID, "Error. The given data is not a vector.");
  255. #endif
  256. return vector_interface->ptr;
  257. }
  258. size_t starpu_vector_get_elemsize(starpu_data_handle_t handle)
  259. {
  260. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  261. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  262. #ifdef STARPU_DEBUG
  263. STARPU_ASSERT_MSG(vector_interface->id == STARPU_VECTOR_INTERFACE_ID, "Error. The given data is not a vector.");
  264. #endif
  265. return vector_interface->elemsize;
  266. }
  267. size_t starpu_vector_get_allocsize(starpu_data_handle_t handle)
  268. {
  269. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  270. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  271. #ifdef STARPU_DEBUG
  272. STARPU_ASSERT_MSG(vector_interface->id == STARPU_VECTOR_INTERFACE_ID, "Error. The given data is not a vector.");
  273. #endif
  274. return vector_interface->allocsize;
  275. }
  276. /* memory allocation/deallocation primitives for the vector interface */
  277. /* returns the size of the allocated area */
  278. static starpu_ssize_t allocate_vector_buffer_on_node(void *data_interface_, unsigned dst_node)
  279. {
  280. uintptr_t addr = 0, handle;
  281. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *) data_interface_;
  282. starpu_ssize_t allocated_memory = vector_interface->allocsize;
  283. handle = starpu_malloc_on_node(dst_node, allocated_memory);
  284. if (!handle)
  285. return -ENOMEM;
  286. if (starpu_node_get_kind(dst_node) != STARPU_OPENCL_RAM)
  287. addr = handle;
  288. /* update the data properly in consequence */
  289. vector_interface->ptr = addr;
  290. vector_interface->dev_handle = handle;
  291. vector_interface->offset = 0;
  292. return allocated_memory;
  293. }
  294. static void free_vector_buffer_on_node(void *data_interface, unsigned node)
  295. {
  296. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *) data_interface;
  297. starpu_free_on_node(node, vector_interface->dev_handle, vector_interface->allocsize);
  298. }
  299. static int copy_any_to_any(void *src_interface, unsigned src_node,
  300. void *dst_interface, unsigned dst_node, void *async_data)
  301. {
  302. struct starpu_vector_interface *src_vector = src_interface;
  303. struct starpu_vector_interface *dst_vector = dst_interface;
  304. int ret;
  305. ret = starpu_interface_copy(src_vector->dev_handle, src_vector->offset, src_node,
  306. dst_vector->dev_handle, dst_vector->offset, dst_node,
  307. src_vector->nx*src_vector->elemsize, async_data);
  308. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_vector->nx*src_vector->elemsize);
  309. return ret;
  310. }
  311. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size)
  312. {
  313. struct starpu_vector_interface *vector = (struct starpu_vector_interface *) data_interface;
  314. return snprintf(buf, size, "V%ux%u",
  315. (unsigned) vector->nx,
  316. (unsigned) vector->elemsize);
  317. }