vector_interface.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  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 <starpu_hash.h>
  23. #include <starpu_cuda.h>
  24. #include <starpu_opencl.h>
  25. #include <drivers/opencl/driver_opencl.h>
  26. #include <drivers/mic/driver_mic_source.h>
  27. #include <drivers/scc/driver_scc_source.h>
  28. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data);
  29. static const struct starpu_data_copy_methods vector_copy_data_methods_s =
  30. {
  31. .any_to_any = copy_any_to_any,
  32. };
  33. static void register_vector_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  34. static starpu_ssize_t allocate_vector_buffer_on_node(void *data_interface_, unsigned dst_node);
  35. static void *vector_handle_to_pointer(starpu_data_handle_t data_handle, unsigned node);
  36. static void free_vector_buffer_on_node(void *data_interface, unsigned node);
  37. static size_t vector_interface_get_size(starpu_data_handle_t handle);
  38. static uint32_t footprint_vector_interface_crc32(starpu_data_handle_t handle);
  39. static int vector_compare(void *data_interface_a, void *data_interface_b);
  40. static void display_vector_interface(starpu_data_handle_t handle, FILE *f);
  41. static int pack_vector_handle(starpu_data_handle_t handle, unsigned node, void **ptr, ssize_t *count);
  42. static int unpack_vector_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count);
  43. struct starpu_data_interface_ops starpu_interface_vector_ops =
  44. {
  45. .register_data_handle = register_vector_handle,
  46. .allocate_data_on_node = allocate_vector_buffer_on_node,
  47. .handle_to_pointer = vector_handle_to_pointer,
  48. .free_data_on_node = free_vector_buffer_on_node,
  49. .copy_methods = &vector_copy_data_methods_s,
  50. .get_size = vector_interface_get_size,
  51. .footprint = footprint_vector_interface_crc32,
  52. .compare = vector_compare,
  53. .interfaceid = STARPU_VECTOR_INTERFACE_ID,
  54. .interface_size = sizeof(struct starpu_vector_interface),
  55. .display = display_vector_interface,
  56. .pack_data = pack_vector_handle,
  57. .unpack_data = unpack_vector_handle
  58. };
  59. static void *vector_handle_to_pointer(starpu_data_handle_t handle, unsigned node)
  60. {
  61. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  62. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  63. starpu_data_get_interface_on_node(handle, node);
  64. return (void*) vector_interface->ptr;
  65. }
  66. static void register_vector_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  67. {
  68. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *) data_interface;
  69. unsigned node;
  70. for (node = 0; node < STARPU_MAXNODES; node++)
  71. {
  72. struct starpu_vector_interface *local_interface = (struct starpu_vector_interface *)
  73. starpu_data_get_interface_on_node(handle, node);
  74. if (node == home_node)
  75. {
  76. local_interface->ptr = vector_interface->ptr;
  77. local_interface->dev_handle = vector_interface->dev_handle;
  78. local_interface->offset = vector_interface->offset;
  79. }
  80. else
  81. {
  82. local_interface->ptr = 0;
  83. local_interface->dev_handle = 0;
  84. local_interface->offset = 0;
  85. }
  86. local_interface->id = vector_interface->id;
  87. local_interface->nx = vector_interface->nx;
  88. local_interface->elemsize = vector_interface->elemsize;
  89. }
  90. }
  91. /* declare a new data with the vector interface */
  92. void starpu_vector_data_register(starpu_data_handle_t *handleptr, unsigned home_node,
  93. uintptr_t ptr, uint32_t nx, size_t elemsize)
  94. {
  95. struct starpu_vector_interface vector =
  96. {
  97. .id = STARPU_VECTOR_INTERFACE_ID,
  98. .ptr = ptr,
  99. .nx = nx,
  100. .elemsize = elemsize,
  101. .dev_handle = ptr,
  102. .offset = 0
  103. };
  104. #ifdef STARPU_USE_SCC
  105. _starpu_scc_set_offset_in_shared_memory((void*)vector.ptr, (void**)&(vector.dev_handle), &(vector.offset));
  106. #endif
  107. starpu_data_register(handleptr, home_node, &vector, &starpu_interface_vector_ops);
  108. }
  109. static uint32_t footprint_vector_interface_crc32(starpu_data_handle_t handle)
  110. {
  111. return starpu_hash_crc32c_be(starpu_vector_get_nx(handle), 0);
  112. }
  113. static int vector_compare(void *data_interface_a, void *data_interface_b)
  114. {
  115. struct starpu_vector_interface *vector_a = (struct starpu_vector_interface *) data_interface_a;
  116. struct starpu_vector_interface *vector_b = (struct starpu_vector_interface *) data_interface_b;
  117. /* Two vectors are considered compatible if they have the same size */
  118. return ((vector_a->nx == vector_b->nx)
  119. && (vector_a->elemsize == vector_b->elemsize));
  120. }
  121. static void display_vector_interface(starpu_data_handle_t handle, FILE *f)
  122. {
  123. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  124. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  125. fprintf(f, "%u\t", vector_interface->nx);
  126. }
  127. static int pack_vector_handle(starpu_data_handle_t handle, unsigned node, void **ptr, ssize_t *count)
  128. {
  129. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  130. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  131. starpu_data_get_interface_on_node(handle, node);
  132. *count = vector_interface->nx*vector_interface->elemsize;
  133. if (ptr != NULL)
  134. {
  135. *ptr = malloc(*count);
  136. memcpy(*ptr, (void*)vector_interface->ptr, vector_interface->elemsize*vector_interface->nx);
  137. }
  138. return 0;
  139. }
  140. static int unpack_vector_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  141. {
  142. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  143. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  144. starpu_data_get_interface_on_node(handle, node);
  145. STARPU_ASSERT(count == vector_interface->elemsize * vector_interface->nx);
  146. memcpy((void*)vector_interface->ptr, ptr, count);
  147. return 0;
  148. }
  149. static size_t vector_interface_get_size(starpu_data_handle_t handle)
  150. {
  151. size_t size;
  152. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  153. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  154. size = vector_interface->nx*vector_interface->elemsize;
  155. return size;
  156. }
  157. /* offer an access to the data parameters */
  158. uint32_t starpu_vector_get_nx(starpu_data_handle_t handle)
  159. {
  160. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  161. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  162. return vector_interface->nx;
  163. }
  164. uintptr_t starpu_vector_get_local_ptr(starpu_data_handle_t handle)
  165. {
  166. unsigned node;
  167. node = _starpu_memory_node_get_local_key();
  168. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  169. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  170. starpu_data_get_interface_on_node(handle, node);
  171. return vector_interface->ptr;
  172. }
  173. size_t starpu_vector_get_elemsize(starpu_data_handle_t handle)
  174. {
  175. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *)
  176. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  177. return vector_interface->elemsize;
  178. }
  179. /* memory allocation/deallocation primitives for the vector interface */
  180. /* returns the size of the allocated area */
  181. static starpu_ssize_t allocate_vector_buffer_on_node(void *data_interface_, unsigned dst_node)
  182. {
  183. uintptr_t addr = 0, handle;
  184. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *) data_interface_;
  185. uint32_t nx = vector_interface->nx;
  186. size_t elemsize = vector_interface->elemsize;
  187. starpu_ssize_t allocated_memory;
  188. handle = starpu_malloc_on_node(dst_node, nx*elemsize);
  189. if (!handle)
  190. return -ENOMEM;
  191. if (starpu_node_get_kind(dst_node) != STARPU_OPENCL_RAM)
  192. addr = handle;
  193. allocated_memory = nx*elemsize;
  194. /* update the data properly in consequence */
  195. vector_interface->ptr = addr;
  196. vector_interface->dev_handle = handle;
  197. vector_interface->offset = 0;
  198. return allocated_memory;
  199. }
  200. static void free_vector_buffer_on_node(void *data_interface, unsigned node)
  201. {
  202. struct starpu_vector_interface *vector_interface = (struct starpu_vector_interface *) data_interface;
  203. uint32_t nx = vector_interface->nx;
  204. size_t elemsize = vector_interface->elemsize;
  205. starpu_free_on_node(node, vector_interface->dev_handle, nx*elemsize);
  206. }
  207. static int copy_any_to_any(void *src_interface, unsigned src_node,
  208. void *dst_interface, unsigned dst_node, void *async_data)
  209. {
  210. struct starpu_vector_interface *src_vector = src_interface;
  211. struct starpu_vector_interface *dst_vector = dst_interface;
  212. int ret;
  213. ret = starpu_interface_copy(src_vector->dev_handle, src_vector->offset, src_node,
  214. dst_vector->dev_handle, dst_vector->offset, dst_node,
  215. src_vector->nx*src_vector->elemsize, async_data);
  216. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_vector->nx*src_vector->elemsize);
  217. return ret;
  218. }