variable_interface.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. * Copyright (C) Sebastien Fremal 2010
  5. *
  6. * This program 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. * This program 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 <common/hash.h>
  23. #include <starpu_cuda.h>
  24. #include <starpu_opencl.h>
  25. #include <drivers/opencl/driver_opencl.h>
  26. static int copy_ram_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node __attribute__((unused)));
  27. #ifdef STARPU_USE_CUDA
  28. static int copy_ram_to_cuda(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node __attribute__((unused)));
  29. static int copy_cuda_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node __attribute__((unused)));
  30. static int copy_ram_to_cuda_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node __attribute__((unused)), cudaStream_t *stream);
  31. static int copy_cuda_to_ram_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node __attribute__((unused)), cudaStream_t *stream);
  32. #endif
  33. #ifdef STARPU_USE_OPENCL
  34. static int copy_ram_to_opencl(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node __attribute__((unused)));
  35. static int copy_opencl_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node __attribute__((unused)));
  36. static int copy_ram_to_opencl_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node __attribute__((unused)), void *_event);
  37. static int copy_opencl_to_ram_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node __attribute__((unused)), void *_event);
  38. #endif
  39. static const struct starpu_data_copy_methods variable_copy_data_methods_s = {
  40. .ram_to_ram = copy_ram_to_ram,
  41. .ram_to_spu = NULL,
  42. #ifdef STARPU_USE_CUDA
  43. .ram_to_cuda = copy_ram_to_cuda,
  44. .cuda_to_ram = copy_cuda_to_ram,
  45. .ram_to_cuda_async = copy_ram_to_cuda_async,
  46. .cuda_to_ram_async = copy_cuda_to_ram_async,
  47. #endif
  48. #ifdef STARPU_USE_OPENCL
  49. .ram_to_opencl = copy_ram_to_opencl,
  50. .opencl_to_ram = copy_opencl_to_ram,
  51. .ram_to_opencl_async = copy_ram_to_opencl_async,
  52. .opencl_to_ram_async = copy_opencl_to_ram_async,
  53. #endif
  54. .cuda_to_cuda = NULL,
  55. .cuda_to_spu = NULL,
  56. .spu_to_ram = NULL,
  57. .spu_to_cuda = NULL,
  58. .spu_to_spu = NULL
  59. };
  60. static void register_variable_handle(starpu_data_handle handle, uint32_t home_node, void *interface);
  61. static size_t allocate_variable_buffer_on_node(void *interface_, uint32_t dst_node);
  62. static void free_variable_buffer_on_node(void *interface, uint32_t node);
  63. static size_t variable_interface_get_size(starpu_data_handle handle);
  64. static uint32_t footprint_variable_interface_crc32(starpu_data_handle handle);
  65. static int variable_compare(void *interface_a, void *interface_b);
  66. static void display_variable_interface(starpu_data_handle handle, FILE *f);
  67. #ifdef STARPU_USE_GORDON
  68. static int convert_variable_to_gordon(void *interface, uint64_t *ptr, gordon_strideSize_t *ss);
  69. #endif
  70. static struct starpu_data_interface_ops_t interface_variable_ops = {
  71. .register_data_handle = register_variable_handle,
  72. .allocate_data_on_node = allocate_variable_buffer_on_node,
  73. .free_data_on_node = free_variable_buffer_on_node,
  74. .copy_methods = &variable_copy_data_methods_s,
  75. .get_size = variable_interface_get_size,
  76. .footprint = footprint_variable_interface_crc32,
  77. .compare = variable_compare,
  78. #ifdef STARPU_USE_GORDON
  79. .convert_to_gordon = convert_variable_to_gordon,
  80. #endif
  81. .interfaceid = STARPU_VARIABLE_INTERFACE_ID,
  82. .interface_size = sizeof(starpu_variable_interface_t),
  83. .display = display_variable_interface
  84. };
  85. static void register_variable_handle(starpu_data_handle handle, uint32_t home_node, void *interface)
  86. {
  87. unsigned node;
  88. for (node = 0; node < STARPU_MAXNODES; node++)
  89. {
  90. starpu_variable_interface_t *local_interface =
  91. starpu_data_get_interface_on_node(handle, node);
  92. if (node == home_node) {
  93. local_interface->ptr = STARPU_VARIABLE_GET_PTR(interface);
  94. }
  95. else {
  96. local_interface->ptr = 0;
  97. }
  98. local_interface->elemsize = STARPU_VARIABLE_GET_ELEMSIZE(interface);
  99. }
  100. }
  101. #ifdef STARPU_USE_GORDON
  102. int convert_variable_to_gordon(void *interface, uint64_t *ptr, gordon_strideSize_t *ss)
  103. {
  104. *ptr = STARPU_VARIABLE_GET_PTR(interface);
  105. (*ss).size = STARPU_VARIABLE_GET_ELEMSIZE(interface);
  106. return 0;
  107. }
  108. #endif
  109. /* declare a new data with the variable interface */
  110. void starpu_variable_data_register(starpu_data_handle *handleptr, uint32_t home_node,
  111. uintptr_t ptr, size_t elemsize)
  112. {
  113. starpu_variable_interface_t variable = {
  114. .ptr = ptr,
  115. .elemsize = elemsize
  116. };
  117. starpu_data_register(handleptr, home_node, &variable, &interface_variable_ops);
  118. }
  119. static uint32_t footprint_variable_interface_crc32(starpu_data_handle handle)
  120. {
  121. return _starpu_crc32_be(starpu_variable_get_elemsize(handle), 0);
  122. }
  123. static int variable_compare(void *interface_a, void *interface_b)
  124. {
  125. starpu_variable_interface_t *variable_a = interface_a;
  126. starpu_variable_interface_t *variable_b = interface_b;
  127. /* Two variables are considered compatible if they have the same size */
  128. return (variable_a->elemsize == variable_b->elemsize);
  129. }
  130. static void display_variable_interface(starpu_data_handle handle, FILE *f)
  131. {
  132. starpu_variable_interface_t *interface =
  133. starpu_data_get_interface_on_node(handle, 0);
  134. fprintf(f, "%ld\t", (long)interface->elemsize);
  135. }
  136. static size_t variable_interface_get_size(starpu_data_handle handle)
  137. {
  138. starpu_variable_interface_t *interface =
  139. starpu_data_get_interface_on_node(handle, 0);
  140. return interface->elemsize;
  141. }
  142. uintptr_t starpu_variable_get_local_ptr(starpu_data_handle handle)
  143. {
  144. unsigned node;
  145. node = _starpu_get_local_memory_node();
  146. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  147. return STARPU_VARIABLE_GET_PTR(starpu_data_get_interface_on_node(handle, node));
  148. }
  149. size_t starpu_variable_get_elemsize(starpu_data_handle handle)
  150. {
  151. return STARPU_VARIABLE_GET_ELEMSIZE(starpu_data_get_interface_on_node(handle, 0));
  152. }
  153. /* memory allocation/deallocation primitives for the variable interface */
  154. /* returns the size of the allocated area */
  155. static size_t allocate_variable_buffer_on_node(void *interface_, uint32_t dst_node)
  156. {
  157. starpu_variable_interface_t *interface = interface_;
  158. unsigned fail = 0;
  159. uintptr_t addr = 0;
  160. size_t allocated_memory;
  161. size_t elemsize = interface->elemsize;
  162. starpu_node_kind kind = _starpu_get_node_kind(dst_node);
  163. #ifdef STARPU_USE_CUDA
  164. cudaError_t status;
  165. #endif
  166. switch(kind) {
  167. case STARPU_CPU_RAM:
  168. addr = (uintptr_t)malloc(elemsize);
  169. if (!addr)
  170. fail = 1;
  171. break;
  172. #ifdef STARPU_USE_CUDA
  173. case STARPU_CUDA_RAM:
  174. status = cudaMalloc((void **)&addr, elemsize);
  175. if (!addr || (status != cudaSuccess))
  176. {
  177. if (STARPU_UNLIKELY(status != cudaErrorMemoryAllocation))
  178. STARPU_CUDA_REPORT_ERROR(status);
  179. fail = 1;
  180. }
  181. break;
  182. #endif
  183. #ifdef STARPU_USE_OPENCL
  184. case STARPU_OPENCL_RAM:
  185. {
  186. int ret;
  187. void *ptr;
  188. ret = _starpu_opencl_allocate_memory(&ptr, elemsize, CL_MEM_READ_WRITE);
  189. addr = (uintptr_t)ptr;
  190. if (ret) {
  191. fail = 1;
  192. }
  193. break;
  194. }
  195. #endif
  196. default:
  197. assert(0);
  198. }
  199. if (fail)
  200. return 0;
  201. /* allocation succeeded */
  202. allocated_memory = elemsize;
  203. /* update the data properly in consequence */
  204. interface->ptr = addr;
  205. return allocated_memory;
  206. }
  207. static void free_variable_buffer_on_node(void *interface, uint32_t node)
  208. {
  209. starpu_node_kind kind = _starpu_get_node_kind(node);
  210. switch(kind) {
  211. case STARPU_CPU_RAM:
  212. free((void*)STARPU_VARIABLE_GET_PTR(interface));
  213. break;
  214. #ifdef STARPU_USE_CUDA
  215. case STARPU_CUDA_RAM:
  216. cudaFree((void*)STARPU_VARIABLE_GET_PTR(interface));
  217. break;
  218. #endif
  219. #ifdef STARPU_USE_OPENCL
  220. case STARPU_OPENCL_RAM:
  221. clReleaseMemObject((void*)STARPU_VARIABLE_GET_PTR(interface));
  222. break;
  223. #endif
  224. default:
  225. assert(0);
  226. }
  227. }
  228. #ifdef STARPU_USE_CUDA
  229. static int copy_cuda_to_ram(void *src_interface, unsigned src_node __attribute__((unused)), void *dst_interface, unsigned dst_node __attribute__((unused)))
  230. {
  231. starpu_variable_interface_t *src_variable = src_interface;
  232. starpu_variable_interface_t *dst_variable = dst_interface;
  233. cudaError_t cures;
  234. cures = cudaMemcpy((char *)dst_variable->ptr, (char *)src_variable->ptr, src_variable->elemsize, cudaMemcpyDeviceToHost);
  235. cudaThreadSynchronize();
  236. if (STARPU_UNLIKELY(cures))
  237. STARPU_CUDA_REPORT_ERROR(cures);
  238. STARPU_TRACE_DATA_COPY(src_node, dst_node, src_variable->elemsize);
  239. return 0;
  240. }
  241. static int copy_ram_to_cuda(void *src_interface, unsigned src_node __attribute__((unused)), void *dst_interface, unsigned dst_node __attribute__((unused)))
  242. {
  243. starpu_variable_interface_t *src_variable = src_interface;
  244. starpu_variable_interface_t *dst_variable = dst_interface;
  245. cudaError_t cures;
  246. cures = cudaMemcpy((char *)dst_variable->ptr, (char *)src_variable->ptr, src_variable->elemsize, cudaMemcpyHostToDevice);
  247. cudaThreadSynchronize();
  248. if (STARPU_UNLIKELY(cures))
  249. STARPU_CUDA_REPORT_ERROR(cures);
  250. STARPU_TRACE_DATA_COPY(src_node, dst_node, src_variable->elemsize);
  251. return 0;
  252. }
  253. static int copy_cuda_to_ram_async(void *src_interface, unsigned src_node __attribute__((unused)), void *dst_interface, unsigned dst_node __attribute__((unused)), cudaStream_t *stream)
  254. {
  255. starpu_variable_interface_t *src_variable = src_interface;
  256. starpu_variable_interface_t *dst_variable = dst_interface;
  257. cudaError_t cures;
  258. cures = cudaMemcpyAsync((char *)dst_variable->ptr, (char *)src_variable->ptr, src_variable->elemsize, cudaMemcpyDeviceToHost, *stream);
  259. if (cures)
  260. {
  261. /* do it in a synchronous fashion */
  262. cures = cudaMemcpy((char *)dst_variable->ptr, (char *)src_variable->ptr, src_variable->elemsize, cudaMemcpyDeviceToHost);
  263. cudaThreadSynchronize();
  264. if (STARPU_UNLIKELY(cures))
  265. STARPU_CUDA_REPORT_ERROR(cures);
  266. return 0;
  267. }
  268. STARPU_TRACE_DATA_COPY(src_node, dst_node, src_variable->elemsize);
  269. return EAGAIN;
  270. }
  271. static int copy_ram_to_cuda_async(void *src_interface, unsigned src_node __attribute__((unused)), void *dst_interface, unsigned dst_node __attribute__((unused)), cudaStream_t *stream)
  272. {
  273. starpu_variable_interface_t *src_variable = src_interface;
  274. starpu_variable_interface_t *dst_variable = dst_interface;
  275. cudaError_t cures;
  276. cures = cudaMemcpyAsync((char *)dst_variable->ptr, (char *)src_variable->ptr, src_variable->elemsize, cudaMemcpyHostToDevice, *stream);
  277. if (cures)
  278. {
  279. /* do it in a synchronous fashion */
  280. cures = cudaMemcpy((char *)dst_variable->ptr, (char *)src_variable->ptr, src_variable->elemsize, cudaMemcpyHostToDevice);
  281. cudaThreadSynchronize();
  282. if (STARPU_UNLIKELY(cures))
  283. STARPU_CUDA_REPORT_ERROR(cures);
  284. return 0;
  285. }
  286. STARPU_TRACE_DATA_COPY(src_node, dst_node, src_variable->elemsize);
  287. return EAGAIN;
  288. }
  289. #endif // STARPU_USE_CUDA
  290. #ifdef STARPU_USE_OPENCL
  291. static int copy_ram_to_opencl_async(void *src_interface, unsigned src_node __attribute__((unused)), void *dst_interface,
  292. unsigned dst_node __attribute__((unused)), void *_event)
  293. {
  294. starpu_variable_interface_t *src_variable = src_interface;
  295. starpu_variable_interface_t *dst_variable = dst_interface;
  296. int err,ret;
  297. err = _starpu_opencl_copy_ram_to_opencl_async_sync((void*)src_variable->ptr, (cl_mem)dst_variable->ptr, src_variable->elemsize,
  298. 0, (cl_event*)_event, &ret);
  299. if (STARPU_UNLIKELY(err))
  300. STARPU_OPENCL_REPORT_ERROR(err);
  301. STARPU_TRACE_DATA_COPY(src_node, dst_node, src_variable->elemsize);
  302. return ret;
  303. }
  304. static int copy_opencl_to_ram_async(void *src_interface, unsigned src_node __attribute__((unused)), void *dst_interface, unsigned dst_node __attribute__((unused)), void *_event)
  305. {
  306. starpu_variable_interface_t *src_variable = src_interface;
  307. starpu_variable_interface_t *dst_variable = dst_interface;
  308. int err, ret;
  309. err = _starpu_opencl_copy_opencl_to_ram_async_sync((cl_mem)src_variable->ptr, (void*)dst_variable->ptr, src_variable->elemsize,
  310. 0, (cl_event*)_event, &ret);
  311. if (STARPU_UNLIKELY(err))
  312. STARPU_OPENCL_REPORT_ERROR(err);
  313. STARPU_TRACE_DATA_COPY(src_node, dst_node, src_variable->elemsize);
  314. return ret;
  315. }
  316. static int copy_ram_to_opencl(void *src_interface, unsigned src_node __attribute__((unused)), void *dst_interface, unsigned dst_node __attribute__((unused)))
  317. {
  318. return copy_ram_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  319. }
  320. static int copy_opencl_to_ram(void *src_interface, unsigned src_node __attribute__((unused)), void *dst_interface, unsigned dst_node __attribute__((unused)))
  321. {
  322. return copy_opencl_to_ram_async(src_interface, src_node, dst_interface, dst_node, NULL);
  323. }
  324. #endif
  325. static int copy_ram_to_ram(void *src_interface, unsigned src_node __attribute__((unused)), void *dst_interface, unsigned dst_node __attribute__((unused)))
  326. {
  327. starpu_variable_interface_t *src_variable = src_interface;
  328. starpu_variable_interface_t *dst_variable = dst_interface;
  329. size_t elemsize = dst_variable->elemsize;
  330. uintptr_t ptr_src = src_variable->ptr;
  331. uintptr_t ptr_dst = dst_variable->ptr;
  332. memcpy((void *)ptr_dst, (void *)ptr_src, elemsize);
  333. STARPU_TRACE_DATA_COPY(src_node, dst_node, elemsize);
  334. return 0;
  335. }