variable_interface.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2018 Université de Bordeaux
  4. * Copyright (C) 2011,2012,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/scc/driver_scc_source.h>
  30. #include <drivers/mic/driver_mic_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 variable_copy_data_methods_s =
  33. {
  34. .any_to_any = copy_any_to_any,
  35. };
  36. static void register_variable_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  37. static starpu_ssize_t allocate_variable_buffer_on_node(void *data_interface_, unsigned dst_node);
  38. static void *variable_to_pointer(void *data_interface, unsigned node);
  39. static int variable_pointer_is_inside(void *data_interface, unsigned node, void *ptr);
  40. static void free_variable_buffer_on_node(void *data_interface, unsigned node);
  41. static size_t variable_interface_get_size(starpu_data_handle_t handle);
  42. static uint32_t footprint_variable_interface_crc32(starpu_data_handle_t handle);
  43. static int variable_compare(void *data_interface_a, void *data_interface_b);
  44. static void display_variable_interface(starpu_data_handle_t handle, FILE *f);
  45. static int pack_variable_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count);
  46. static int unpack_variable_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count);
  47. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size);
  48. struct starpu_data_interface_ops starpu_interface_variable_ops =
  49. {
  50. .register_data_handle = register_variable_handle,
  51. .allocate_data_on_node = allocate_variable_buffer_on_node,
  52. .to_pointer = variable_to_pointer,
  53. .pointer_is_inside = variable_pointer_is_inside,
  54. .free_data_on_node = free_variable_buffer_on_node,
  55. .copy_methods = &variable_copy_data_methods_s,
  56. .get_size = variable_interface_get_size,
  57. .footprint = footprint_variable_interface_crc32,
  58. .compare = variable_compare,
  59. .interfaceid = STARPU_VARIABLE_INTERFACE_ID,
  60. .interface_size = sizeof(struct starpu_variable_interface),
  61. .display = display_variable_interface,
  62. .pack_data = pack_variable_handle,
  63. .unpack_data = unpack_variable_handle,
  64. .describe = describe,
  65. .name = "STARPU_VARIABLE_INTERFACE"
  66. };
  67. static void *variable_to_pointer(void *data_interface, unsigned node)
  68. {
  69. (void) node;
  70. return (void*) STARPU_VARIABLE_GET_PTR(data_interface);
  71. }
  72. static int variable_pointer_is_inside(void *data_interface, unsigned node, void *ptr)
  73. {
  74. (void) node;
  75. struct starpu_variable_interface *variable_interface = data_interface;
  76. return (char*) ptr >= (char*) variable_interface->ptr &&
  77. (char*) ptr < (char*) variable_interface->ptr + variable_interface->elemsize;
  78. }
  79. static void register_variable_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  80. {
  81. struct starpu_variable_interface *variable_interface = (struct starpu_variable_interface *)data_interface;
  82. unsigned node;
  83. for (node = 0; node < STARPU_MAXNODES; node++)
  84. {
  85. struct starpu_variable_interface *local_interface = (struct starpu_variable_interface *)
  86. starpu_data_get_interface_on_node(handle, node);
  87. if (node == home_node)
  88. {
  89. local_interface->ptr = variable_interface->ptr;
  90. local_interface->dev_handle = variable_interface->dev_handle;
  91. local_interface->offset = variable_interface->offset;
  92. }
  93. else
  94. {
  95. local_interface->ptr = 0;
  96. local_interface->dev_handle = 0;
  97. local_interface->offset = 0;
  98. }
  99. local_interface->id = variable_interface->id;
  100. local_interface->elemsize = variable_interface->elemsize;
  101. }
  102. }
  103. /* declare a new data with the variable interface */
  104. void starpu_variable_data_register(starpu_data_handle_t *handleptr, int home_node,
  105. uintptr_t ptr, size_t elemsize)
  106. {
  107. struct starpu_variable_interface variable =
  108. {
  109. .id = STARPU_VARIABLE_INTERFACE_ID,
  110. .ptr = ptr,
  111. .dev_handle = ptr,
  112. .offset = 0,
  113. .elemsize = elemsize
  114. };
  115. #ifndef STARPU_SIMGRID
  116. if (home_node >= 0 && starpu_node_get_kind(home_node) == STARPU_CPU_RAM)
  117. {
  118. STARPU_ASSERT_ACCESSIBLE(ptr);
  119. STARPU_ASSERT_ACCESSIBLE(ptr + elemsize - 1);
  120. }
  121. #endif
  122. #ifdef STARPU_USE_SCC
  123. starpu_scc_get_offset_in_shared_memory((void*)variable.ptr, (void**)&(variable.dev_handle), &(variable.offset));
  124. #endif
  125. starpu_data_register(handleptr, home_node, &variable, &starpu_interface_variable_ops);
  126. }
  127. void starpu_variable_ptr_register(starpu_data_handle_t handle, unsigned node,
  128. uintptr_t ptr, uintptr_t dev_handle, size_t offset)
  129. {
  130. struct starpu_variable_interface *variable_interface = starpu_data_get_interface_on_node(handle, node);
  131. starpu_data_ptr_register(handle, node);
  132. variable_interface->ptr = ptr;
  133. variable_interface->dev_handle = dev_handle;
  134. variable_interface->offset = offset;
  135. }
  136. static uint32_t footprint_variable_interface_crc32(starpu_data_handle_t handle)
  137. {
  138. return starpu_hash_crc32c_be(starpu_variable_get_elemsize(handle), 0);
  139. }
  140. static int variable_compare(void *data_interface_a, void *data_interface_b)
  141. {
  142. struct starpu_variable_interface *variable_a = (struct starpu_variable_interface *) data_interface_a;
  143. struct starpu_variable_interface *variable_b = (struct starpu_variable_interface *) data_interface_b;
  144. /* Two variables are considered compatible if they have the same size */
  145. return variable_a->elemsize == variable_b->elemsize;
  146. }
  147. static void display_variable_interface(starpu_data_handle_t handle, FILE *f)
  148. {
  149. struct starpu_variable_interface *variable_interface = (struct starpu_variable_interface *)
  150. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  151. fprintf(f, "%ld\t", (long)variable_interface->elemsize);
  152. }
  153. static int pack_variable_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count)
  154. {
  155. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  156. struct starpu_variable_interface *variable_interface = (struct starpu_variable_interface *)
  157. starpu_data_get_interface_on_node(handle, node);
  158. *count = variable_interface->elemsize;
  159. if (ptr != NULL)
  160. {
  161. *ptr = (void *)starpu_malloc_on_node_flags(node, *count, 0);
  162. memcpy(*ptr, (void*)variable_interface->ptr, variable_interface->elemsize);
  163. }
  164. return 0;
  165. }
  166. static int unpack_variable_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  167. {
  168. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  169. struct starpu_variable_interface *variable_interface = (struct starpu_variable_interface *)
  170. starpu_data_get_interface_on_node(handle, node);
  171. STARPU_ASSERT(count == variable_interface->elemsize);
  172. memcpy((void*)variable_interface->ptr, ptr, variable_interface->elemsize);
  173. starpu_free_on_node_flags(node, (uintptr_t)ptr, count, 0);
  174. return 0;
  175. }
  176. static size_t variable_interface_get_size(starpu_data_handle_t handle)
  177. {
  178. struct starpu_variable_interface *variable_interface = (struct starpu_variable_interface *)
  179. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  180. #ifdef STARPU_DEBUG
  181. STARPU_ASSERT_MSG(variable_interface->id == STARPU_VARIABLE_INTERFACE_ID, "Error. The given data is not a variable.");
  182. #endif
  183. return variable_interface->elemsize;
  184. }
  185. uintptr_t starpu_variable_get_local_ptr(starpu_data_handle_t handle)
  186. {
  187. unsigned node;
  188. node = starpu_worker_get_local_memory_node();
  189. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  190. return STARPU_VARIABLE_GET_PTR(starpu_data_get_interface_on_node(handle, node));
  191. }
  192. size_t starpu_variable_get_elemsize(starpu_data_handle_t handle)
  193. {
  194. return STARPU_VARIABLE_GET_ELEMSIZE(starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM));
  195. }
  196. /* memory allocation/deallocation primitives for the variable interface */
  197. /* returns the size of the allocated area */
  198. static starpu_ssize_t allocate_variable_buffer_on_node(void *data_interface_, unsigned dst_node)
  199. {
  200. struct starpu_variable_interface *variable_interface = (struct starpu_variable_interface *) data_interface_;
  201. size_t elemsize = variable_interface->elemsize;
  202. uintptr_t addr = starpu_malloc_on_node(dst_node, elemsize);
  203. if (!addr)
  204. return -ENOMEM;
  205. /* update the data properly in consequence */
  206. variable_interface->ptr = addr;
  207. return elemsize;
  208. }
  209. static void free_variable_buffer_on_node(void *data_interface, unsigned node)
  210. {
  211. struct starpu_variable_interface *variable_interface = (struct starpu_variable_interface *) data_interface;
  212. starpu_free_on_node(node, variable_interface->ptr, variable_interface->elemsize);
  213. }
  214. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data)
  215. {
  216. struct starpu_variable_interface *src_variable = (struct starpu_variable_interface *) src_interface;
  217. struct starpu_variable_interface *dst_variable = (struct starpu_variable_interface *) dst_interface;
  218. size_t elemsize = dst_variable->elemsize;
  219. uintptr_t ptr_src = src_variable->ptr;
  220. uintptr_t ptr_dst = dst_variable->ptr;
  221. int ret;
  222. ret = starpu_interface_copy(ptr_src, 0, src_node, ptr_dst, 0, dst_node, elemsize, async_data);
  223. starpu_interface_data_copy(src_node, dst_node, elemsize);
  224. return ret;
  225. }
  226. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size)
  227. {
  228. struct starpu_variable_interface *variable = (struct starpu_variable_interface *) data_interface;
  229. return snprintf(buf, size, "v%u",
  230. (unsigned) variable->elemsize);
  231. }