block_interface.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 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 <starpu_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 STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  27. #ifdef STARPU_USE_CUDA
  28. static int copy_ram_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  29. static int copy_cuda_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  30. static int copy_ram_to_cuda_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cudaStream_t stream);
  31. static int copy_cuda_to_ram_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cudaStream_t stream);
  32. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  33. #endif
  34. #ifdef STARPU_USE_OPENCL
  35. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  36. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  37. static int copy_opencl_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  38. static int copy_ram_to_opencl_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cl_event *event);
  39. static int copy_opencl_to_ram_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cl_event *event);
  40. static int copy_opencl_to_opencl_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cl_event *event);
  41. #endif
  42. static struct starpu_data_copy_methods block_copy_data_methods_s =
  43. {
  44. .ram_to_ram = copy_ram_to_ram,
  45. #ifdef STARPU_USE_CUDA
  46. .ram_to_cuda = copy_ram_to_cuda,
  47. .cuda_to_ram = copy_cuda_to_ram,
  48. .ram_to_cuda_async = copy_ram_to_cuda_async,
  49. .cuda_to_ram_async = copy_cuda_to_ram_async,
  50. .cuda_to_cuda = copy_cuda_to_cuda,
  51. #endif
  52. #ifdef STARPU_USE_OPENCL
  53. .ram_to_opencl = copy_ram_to_opencl,
  54. .opencl_to_ram = copy_opencl_to_ram,
  55. .opencl_to_opencl = copy_opencl_to_opencl,
  56. .ram_to_opencl_async = copy_ram_to_opencl_async,
  57. .opencl_to_ram_async = copy_opencl_to_ram_async,
  58. .opencl_to_opencl_async = copy_opencl_to_opencl_async,
  59. #endif
  60. };
  61. static void register_block_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  62. static void *block_handle_to_pointer(starpu_data_handle_t data_handle, unsigned node);
  63. static ssize_t allocate_block_buffer_on_node(void *data_interface_, unsigned dst_node);
  64. static void free_block_buffer_on_node(void *data_interface, unsigned node);
  65. static size_t block_interface_get_size(starpu_data_handle_t handle);
  66. static uint32_t footprint_block_interface_crc32(starpu_data_handle_t handle);
  67. static int block_compare(void *data_interface_a, void *data_interface_b);
  68. static void display_block_interface(starpu_data_handle_t handle, FILE *f);
  69. static struct starpu_data_interface_ops interface_block_ops =
  70. {
  71. .register_data_handle = register_block_handle,
  72. .allocate_data_on_node = allocate_block_buffer_on_node,
  73. .handle_to_pointer = block_handle_to_pointer,
  74. .free_data_on_node = free_block_buffer_on_node,
  75. .copy_methods = &block_copy_data_methods_s,
  76. .get_size = block_interface_get_size,
  77. .footprint = footprint_block_interface_crc32,
  78. .compare = block_compare,
  79. .interfaceid = STARPU_BLOCK_INTERFACE_ID,
  80. .interface_size = sizeof(struct starpu_block_interface),
  81. .display = display_block_interface,
  82. };
  83. static void *block_handle_to_pointer(starpu_data_handle_t handle, unsigned node)
  84. {
  85. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  86. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  87. starpu_data_get_interface_on_node(handle, node);
  88. return (void*) block_interface->ptr;
  89. }
  90. static void register_block_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  91. {
  92. struct starpu_block_interface *block_interface = (struct starpu_block_interface *) data_interface;
  93. unsigned node;
  94. for (node = 0; node < STARPU_MAXNODES; node++)
  95. {
  96. struct starpu_block_interface *local_interface = (struct starpu_block_interface *)
  97. starpu_data_get_interface_on_node(handle, node);
  98. if (node == home_node)
  99. {
  100. local_interface->ptr = block_interface->ptr;
  101. local_interface->dev_handle = block_interface->dev_handle;
  102. local_interface->offset = block_interface->offset;
  103. local_interface->ldy = block_interface->ldy;
  104. local_interface->ldz = block_interface->ldz;
  105. }
  106. else
  107. {
  108. local_interface->ptr = 0;
  109. local_interface->dev_handle = 0;
  110. local_interface->offset = 0;
  111. local_interface->ldy = 0;
  112. local_interface->ldz = 0;
  113. }
  114. local_interface->nx = block_interface->nx;
  115. local_interface->ny = block_interface->ny;
  116. local_interface->nz = block_interface->nz;
  117. local_interface->elemsize = block_interface->elemsize;
  118. }
  119. }
  120. /* declare a new data with the BLAS interface */
  121. void starpu_block_data_register(starpu_data_handle_t *handleptr, unsigned home_node,
  122. uintptr_t ptr, uint32_t ldy, uint32_t ldz, uint32_t nx,
  123. uint32_t ny, uint32_t nz, size_t elemsize)
  124. {
  125. struct starpu_block_interface block_interface =
  126. {
  127. .ptr = ptr,
  128. .dev_handle = ptr,
  129. .offset = 0,
  130. .ldy = ldy,
  131. .ldz = ldz,
  132. .nx = nx,
  133. .ny = ny,
  134. .nz = nz,
  135. .elemsize = elemsize
  136. };
  137. starpu_data_register(handleptr, home_node, &block_interface, &interface_block_ops);
  138. }
  139. static uint32_t footprint_block_interface_crc32(starpu_data_handle_t handle)
  140. {
  141. uint32_t hash;
  142. hash = starpu_crc32_be(starpu_block_get_nx(handle), 0);
  143. hash = starpu_crc32_be(starpu_block_get_ny(handle), hash);
  144. hash = starpu_crc32_be(starpu_block_get_nz(handle), hash);
  145. return hash;
  146. }
  147. static int block_compare(void *data_interface_a, void *data_interface_b)
  148. {
  149. struct starpu_block_interface *block_a = (struct starpu_block_interface *) data_interface_a;
  150. struct starpu_block_interface *block_b = (struct starpu_block_interface *) data_interface_b;
  151. /* Two matricess are considered compatible if they have the same size */
  152. return ((block_a->nx == block_b->nx)
  153. && (block_a->ny == block_b->ny)
  154. && (block_a->nz == block_b->nz)
  155. && (block_a->elemsize == block_b->elemsize));
  156. }
  157. static void display_block_interface(starpu_data_handle_t handle, FILE *f)
  158. {
  159. struct starpu_block_interface *block_interface;
  160. block_interface = (struct starpu_block_interface *) starpu_data_get_interface_on_node(handle, 0);
  161. fprintf(f, "%u\t%u\t%u\t", block_interface->nx, block_interface->ny, block_interface->nz);
  162. }
  163. static size_t block_interface_get_size(starpu_data_handle_t handle)
  164. {
  165. size_t size;
  166. struct starpu_block_interface *block_interface;
  167. block_interface = (struct starpu_block_interface *) starpu_data_get_interface_on_node(handle, 0);
  168. size = block_interface->nx*block_interface->ny*block_interface->nz*block_interface->elemsize;
  169. return size;
  170. }
  171. /* offer an access to the data parameters */
  172. uint32_t starpu_block_get_nx(starpu_data_handle_t handle)
  173. {
  174. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  175. starpu_data_get_interface_on_node(handle, 0);
  176. return block_interface->nx;
  177. }
  178. uint32_t starpu_block_get_ny(starpu_data_handle_t handle)
  179. {
  180. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  181. starpu_data_get_interface_on_node(handle, 0);
  182. return block_interface->ny;
  183. }
  184. uint32_t starpu_block_get_nz(starpu_data_handle_t handle)
  185. {
  186. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  187. starpu_data_get_interface_on_node(handle, 0);
  188. return block_interface->nz;
  189. }
  190. uint32_t starpu_block_get_local_ldy(starpu_data_handle_t handle)
  191. {
  192. unsigned node;
  193. node = _starpu_memory_node_get_local_key();
  194. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  195. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  196. starpu_data_get_interface_on_node(handle, node);
  197. return block_interface->ldy;
  198. }
  199. uint32_t starpu_block_get_local_ldz(starpu_data_handle_t handle)
  200. {
  201. unsigned node;
  202. node = _starpu_memory_node_get_local_key();
  203. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  204. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  205. starpu_data_get_interface_on_node(handle, node);
  206. return block_interface->ldz;
  207. }
  208. uintptr_t starpu_block_get_local_ptr(starpu_data_handle_t handle)
  209. {
  210. unsigned node;
  211. node = _starpu_memory_node_get_local_key();
  212. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  213. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  214. starpu_data_get_interface_on_node(handle, node);
  215. return block_interface->ptr;
  216. }
  217. size_t starpu_block_get_elemsize(starpu_data_handle_t handle)
  218. {
  219. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  220. starpu_data_get_interface_on_node(handle, 0);
  221. return block_interface->elemsize;
  222. }
  223. /* memory allocation/deallocation primitives for the BLOCK interface */
  224. /* returns the size of the allocated area */
  225. static ssize_t allocate_block_buffer_on_node(void *data_interface_, unsigned dst_node)
  226. {
  227. uintptr_t addr = 0, handle;
  228. struct starpu_block_interface *dst_block = (struct starpu_block_interface *) data_interface_;
  229. uint32_t nx = dst_block->nx;
  230. uint32_t ny = dst_block->ny;
  231. uint32_t nz = dst_block->nz;
  232. size_t elemsize = dst_block->elemsize;
  233. ssize_t allocated_memory;
  234. handle = starpu_allocate_buffer_on_node(dst_node, nx*ny*nz*elemsize);
  235. if (!handle)
  236. return -ENOMEM;
  237. if (starpu_node_get_kind(dst_node) != STARPU_OPENCL_RAM)
  238. addr = handle;
  239. allocated_memory = nx*ny*nz*elemsize;
  240. /* update the data properly in consequence */
  241. dst_block->ptr = addr;
  242. dst_block->dev_handle = handle;
  243. dst_block->offset = 0;
  244. dst_block->ldy = nx;
  245. dst_block->ldz = nx*ny;
  246. return allocated_memory;
  247. }
  248. static void free_block_buffer_on_node(void *data_interface, unsigned node)
  249. {
  250. struct starpu_block_interface *block_interface = (struct starpu_block_interface *) data_interface;
  251. uint32_t nx = block_interface->nx;
  252. uint32_t ny = block_interface->ny;
  253. uint32_t nz = block_interface->nz;
  254. size_t elemsize = block_interface->elemsize;
  255. starpu_free_buffer_on_node(node, block_interface->ptr, nx*ny*nz*elemsize);
  256. }
  257. #ifdef STARPU_USE_CUDA
  258. static int copy_cuda_common(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, enum cudaMemcpyKind kind)
  259. {
  260. struct starpu_block_interface *src_block = src_interface;
  261. struct starpu_block_interface *dst_block = dst_interface;
  262. uint32_t nx = src_block->nx;
  263. uint32_t ny = src_block->ny;
  264. uint32_t nz = src_block->nz;
  265. size_t elemsize = src_block->elemsize;
  266. cudaError_t cures;
  267. if ((nx == src_block->ldy) && (src_block->ldy == dst_block->ldy))
  268. {
  269. /* Is that a single contiguous buffer ? */
  270. if (((nx*ny) == src_block->ldz) && (src_block->ldz == dst_block->ldz))
  271. {
  272. starpu_cuda_copy_async_sync((void *)src_block->ptr, src_node, (void *)dst_block->ptr, dst_node, nx*ny*nz*elemsize, NULL, kind);
  273. }
  274. else
  275. {
  276. /* Are all plans contiguous */
  277. cures = cudaMemcpy2D((char *)dst_block->ptr, dst_block->ldz*elemsize,
  278. (char *)src_block->ptr, src_block->ldz*elemsize,
  279. nx*ny*elemsize, nz, kind);
  280. if (STARPU_UNLIKELY(cures))
  281. STARPU_CUDA_REPORT_ERROR(cures);
  282. }
  283. }
  284. else
  285. {
  286. /* Default case: we transfer all lines one by one: ny*nz transfers */
  287. unsigned layer;
  288. for (layer = 0; layer < src_block->nz; layer++)
  289. {
  290. uint8_t *src_ptr = ((uint8_t *)src_block->ptr) + layer*src_block->ldz*src_block->elemsize;
  291. uint8_t *dst_ptr = ((uint8_t *)dst_block->ptr) + layer*dst_block->ldz*dst_block->elemsize;
  292. cures = cudaMemcpy2D((char *)dst_ptr, dst_block->ldy*elemsize,
  293. (char *)src_ptr, src_block->ldy*elemsize,
  294. nx*elemsize, ny, kind);
  295. if (STARPU_UNLIKELY(cures))
  296. STARPU_CUDA_REPORT_ERROR(cures);
  297. }
  298. }
  299. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_block->nx*src_block->ny*src_block->elemsize*src_block->elemsize);
  300. return 0;
  301. }
  302. static int copy_cuda_async_common(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cudaStream_t stream, enum cudaMemcpyKind kind)
  303. {
  304. struct starpu_block_interface *src_block = src_interface;
  305. struct starpu_block_interface *dst_block = dst_interface;
  306. uint32_t nx = src_block->nx;
  307. uint32_t ny = src_block->ny;
  308. uint32_t nz = src_block->nz;
  309. size_t elemsize = src_block->elemsize;
  310. cudaError_t cures;
  311. int ret;
  312. /* We may have a contiguous buffer for the entire block, or contiguous
  313. * plans within the block, we can avoid many small transfers that way */
  314. if ((nx == src_block->ldy) && (src_block->ldy == dst_block->ldy))
  315. {
  316. /* Is that a single contiguous buffer ? */
  317. if (((nx*ny) == src_block->ldz) && (src_block->ldz == dst_block->ldz))
  318. {
  319. ret = starpu_cuda_copy_async_sync((void *)src_block->ptr, src_node, (void *)dst_block->ptr, dst_node, nx*ny*nz*elemsize, stream, kind);
  320. }
  321. else
  322. {
  323. /* Are all plans contiguous */
  324. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  325. cures = cudaMemcpy2DAsync((char *)dst_block->ptr, dst_block->ldz*elemsize,
  326. (char *)src_block->ptr, src_block->ldz*elemsize,
  327. nx*ny*elemsize, nz, kind, stream);
  328. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  329. if (STARPU_UNLIKELY(cures))
  330. {
  331. cures = cudaMemcpy2D((char *)dst_block->ptr, dst_block->ldz*elemsize,
  332. (char *)src_block->ptr, src_block->ldz*elemsize,
  333. nx*ny*elemsize, nz, kind);
  334. if (STARPU_UNLIKELY(cures))
  335. STARPU_CUDA_REPORT_ERROR(cures);
  336. ret = 0;
  337. }
  338. else
  339. {
  340. ret = -EAGAIN;
  341. }
  342. }
  343. }
  344. else
  345. {
  346. /* Default case: we transfer all blocks one by one: nz 2D transfers */
  347. unsigned layer;
  348. for (layer = 0; layer < src_block->nz; layer++)
  349. {
  350. uint8_t *src_ptr = ((uint8_t *)src_block->ptr) + layer*src_block->ldz*src_block->elemsize;
  351. uint8_t *dst_ptr = ((uint8_t *)dst_block->ptr) + layer*dst_block->ldz*dst_block->elemsize;
  352. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  353. cures = cudaMemcpy2DAsync((char *)dst_ptr, dst_block->ldy*elemsize,
  354. (char *)src_ptr, src_block->ldy*elemsize,
  355. nx*elemsize, ny, kind, stream);
  356. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  357. if (STARPU_UNLIKELY(cures))
  358. {
  359. /* I don't know how to do that "better" */
  360. goto no_async_default;
  361. }
  362. }
  363. ret = -EAGAIN;
  364. }
  365. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_block->nx*src_block->ny*src_block->nz*src_block->elemsize);
  366. return ret;
  367. no_async_default:
  368. {
  369. unsigned layer;
  370. for (layer = 0; layer < src_block->nz; layer++)
  371. {
  372. uint8_t *src_ptr = ((uint8_t *)src_block->ptr) + layer*src_block->ldz*src_block->elemsize;
  373. uint8_t *dst_ptr = ((uint8_t *)dst_block->ptr) + layer*dst_block->ldz*dst_block->elemsize;
  374. cures = cudaMemcpy2D((char *)dst_ptr, dst_block->ldy*elemsize,
  375. (char *)src_ptr, src_block->ldy*elemsize,
  376. nx*elemsize, ny, kind);
  377. if (STARPU_UNLIKELY(cures))
  378. STARPU_CUDA_REPORT_ERROR(cures);
  379. }
  380. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_block->nx*src_block->ny*src_block->nz*src_block->elemsize);
  381. return 0;
  382. }
  383. }
  384. static int copy_cuda_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node)
  385. {
  386. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost);
  387. }
  388. static int copy_ram_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  389. {
  390. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice);
  391. }
  392. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  393. {
  394. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToDevice);
  395. }
  396. static int copy_cuda_to_ram_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cudaStream_t stream)
  397. {
  398. return copy_cuda_async_common(src_interface, src_node, dst_interface, dst_node, stream, cudaMemcpyDeviceToHost);
  399. }
  400. static int copy_ram_to_cuda_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cudaStream_t stream)
  401. {
  402. return copy_cuda_async_common(src_interface, src_node, dst_interface, dst_node, stream, cudaMemcpyHostToDevice);
  403. }
  404. #endif // STARPU_USE_CUDA
  405. #ifdef STARPU_USE_OPENCL
  406. static int copy_opencl_common(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  407. {
  408. struct starpu_block_interface *src_block = src_interface;
  409. struct starpu_block_interface *dst_block = dst_interface;
  410. int ret = 0;
  411. uint32_t nx = src_block->nx;
  412. uint32_t ny = src_block->ny;
  413. /* We may have a contiguous buffer for the entire block, or contiguous
  414. * plans within the block, we can avoid many small transfers that way */
  415. if ((nx == src_block->ldy) && (src_block->ldy == dst_block->ldy))
  416. {
  417. /* Is that a single contiguous buffer ? */
  418. if (((nx*ny) == src_block->ldz) && (src_block->ldz == dst_block->ldz))
  419. {
  420. ret = starpu_opencl_copy_async_sync(src_block->dev_handle, src_block->offset, src_node,
  421. dst_block->dev_handle, dst_block->offset, dst_node,
  422. src_block->nx*src_block->ny*src_block->nz*src_block->elemsize,
  423. event);
  424. }
  425. else
  426. {
  427. /* Are all plans contiguous */
  428. STARPU_ASSERT_MSG(0, "XXX non contiguous buffers are not properly supported in OpenCL yet. (TODO)");
  429. }
  430. }
  431. else
  432. {
  433. /* Default case: we transfer all lines one by one: ny*nz transfers */
  434. /* TODO: rect support */
  435. unsigned layer;
  436. for (layer = 0; layer < src_block->nz; layer++)
  437. {
  438. unsigned j;
  439. for(j=0 ; j<src_block->ny ; j++)
  440. {
  441. ret = starpu_opencl_copy_async_sync(src_block->dev_handle,
  442. src_block->offset + layer*src_block->ldz*src_block->elemsize + j*src_block->ldy*src_block->elemsize,
  443. src_node,
  444. dst_block->dev_handle,
  445. dst_block->offset + layer*dst_block->ldz*dst_block->elemsize + j*dst_block->ldy*dst_block->elemsize,
  446. dst_node,
  447. src_block->nx*src_block->elemsize,
  448. event);
  449. }
  450. }
  451. }
  452. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_block->nx*src_block->ny*src_block->nz*src_block->elemsize);
  453. return ret;
  454. }
  455. static int copy_ram_to_opencl_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  456. {
  457. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  458. }
  459. static int copy_opencl_to_ram_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  460. {
  461. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  462. }
  463. static int copy_opencl_to_opencl_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  464. {
  465. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  466. }
  467. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  468. {
  469. return copy_ram_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  470. }
  471. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  472. {
  473. return copy_opencl_to_ram_async(src_interface, src_node, dst_interface, dst_node, NULL);
  474. }
  475. static int copy_opencl_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  476. {
  477. return copy_opencl_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  478. }
  479. #endif
  480. /* as not all platform easily have a BLAS lib installed ... */
  481. static int copy_ram_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  482. {
  483. struct starpu_block_interface *src_block = (struct starpu_block_interface *) src_interface;
  484. struct starpu_block_interface *dst_block = (struct starpu_block_interface *) dst_interface;
  485. uint32_t nx = dst_block->nx;
  486. uint32_t ny = dst_block->ny;
  487. uint32_t nz = dst_block->nz;
  488. size_t elemsize = dst_block->elemsize;
  489. uint32_t ldy_src = src_block->ldy;
  490. uint32_t ldz_src = src_block->ldz;
  491. uint32_t ldy_dst = dst_block->ldy;
  492. uint32_t ldz_dst = dst_block->ldz;
  493. uintptr_t ptr_src = src_block->ptr;
  494. uintptr_t ptr_dst = dst_block->ptr;
  495. unsigned y, z;
  496. for (z = 0; z < nz; z++)
  497. {
  498. for (y = 0; y < ny; y++)
  499. {
  500. uint32_t src_offset = (y*ldy_src + z*ldz_src)*elemsize;
  501. uint32_t dst_offset = (y*ldy_dst + z*ldz_dst)*elemsize;
  502. memcpy((void *)(ptr_dst + dst_offset),
  503. (void *)(ptr_src + src_offset), nx*elemsize);
  504. }
  505. }
  506. _STARPU_TRACE_DATA_COPY(src_node, dst_node, nx*ny*nz*elemsize);
  507. return 0;
  508. }