block_interface.c 27 KB

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