block_interface.c 27 KB

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