variable_interface.c 15 KB

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