variable_interface.c 14 KB

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