variable_interface.c 9.2 KB

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