vector_interface.c 10 KB

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