block_interface.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2019 Université de Bordeaux
  4. * Copyright (C) 2011,2012,2017 Inria
  5. * Copyright (C) 2010-2017,2019 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #ifdef STARPU_USE_CUDA
  20. static int copy_ram_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  21. static int copy_cuda_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  22. 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);
  23. 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);
  24. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  25. #endif
  26. #ifdef STARPU_USE_OPENCL
  27. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  28. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  29. static int copy_opencl_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  30. 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);
  31. 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);
  32. 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);
  33. #endif
  34. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data);
  35. static const struct starpu_data_copy_methods block_copy_data_methods_s =
  36. {
  37. #ifdef STARPU_USE_CUDA
  38. .ram_to_cuda = copy_ram_to_cuda,
  39. .cuda_to_ram = copy_cuda_to_ram,
  40. .ram_to_cuda_async = copy_ram_to_cuda_async,
  41. .cuda_to_ram_async = copy_cuda_to_ram_async,
  42. .cuda_to_cuda = copy_cuda_to_cuda,
  43. #endif
  44. #ifdef STARPU_USE_OPENCL
  45. .ram_to_opencl = copy_ram_to_opencl,
  46. .opencl_to_ram = copy_opencl_to_ram,
  47. .opencl_to_opencl = copy_opencl_to_opencl,
  48. .ram_to_opencl_async = copy_ram_to_opencl_async,
  49. .opencl_to_ram_async = copy_opencl_to_ram_async,
  50. .opencl_to_opencl_async = copy_opencl_to_opencl_async,
  51. #endif
  52. .any_to_any = copy_any_to_any,
  53. };
  54. static void register_block_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  55. static void *block_to_pointer(void *data_interface, unsigned node);
  56. static int block_pointer_is_inside(void *data_interface, unsigned node, void *ptr);
  57. static starpu_ssize_t allocate_block_buffer_on_node(void *data_interface_, unsigned dst_node);
  58. static void free_block_buffer_on_node(void *data_interface, unsigned node);
  59. static size_t block_interface_get_size(starpu_data_handle_t handle);
  60. static uint32_t footprint_block_interface_crc32(starpu_data_handle_t handle);
  61. static int block_compare(void *data_interface_a, void *data_interface_b);
  62. static void display_block_interface(starpu_data_handle_t handle, FILE *f);
  63. static int pack_block_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count);
  64. static int unpack_block_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count);
  65. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size);
  66. struct starpu_data_interface_ops starpu_interface_block_ops =
  67. {
  68. .register_data_handle = register_block_handle,
  69. .allocate_data_on_node = allocate_block_buffer_on_node,
  70. .to_pointer = block_to_pointer,
  71. .pointer_is_inside = block_pointer_is_inside,
  72. .free_data_on_node = free_block_buffer_on_node,
  73. .copy_methods = &block_copy_data_methods_s,
  74. .get_size = block_interface_get_size,
  75. .footprint = footprint_block_interface_crc32,
  76. .compare = block_compare,
  77. .interfaceid = STARPU_BLOCK_INTERFACE_ID,
  78. .interface_size = sizeof(struct starpu_block_interface),
  79. .display = display_block_interface,
  80. .pack_data = pack_block_handle,
  81. .unpack_data = unpack_block_handle,
  82. .describe = describe,
  83. .name = "STARPU_BLOCK_INTERFACE"
  84. };
  85. static void *block_to_pointer(void *data_interface, unsigned node)
  86. {
  87. (void) node;
  88. struct starpu_block_interface *block_interface = data_interface;
  89. return (void*) block_interface->ptr;
  90. }
  91. static int block_pointer_is_inside(void *data_interface, unsigned node, void *ptr)
  92. {
  93. (void) node;
  94. struct starpu_block_interface *block_interface = data_interface;
  95. return (char*) ptr >= (char*) block_interface->ptr &&
  96. (char*) ptr < (char*) block_interface->ptr + block_interface->nx*block_interface->ny*block_interface->nz*block_interface->elemsize;
  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, int 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. #ifndef STARPU_SIMGRID
  148. if (home_node >= 0 && starpu_node_get_kind(home_node) == STARPU_CPU_RAM)
  149. {
  150. STARPU_ASSERT_ACCESSIBLE(ptr);
  151. STARPU_ASSERT_ACCESSIBLE(ptr + (nz-1)*ldz*elemsize + (ny-1)*ldy*elemsize + nx*elemsize - 1);
  152. }
  153. #endif
  154. starpu_data_register(handleptr, home_node, &block_interface, &starpu_interface_block_ops);
  155. }
  156. void starpu_block_ptr_register(starpu_data_handle_t handle, unsigned node,
  157. uintptr_t ptr, uintptr_t dev_handle, size_t offset, uint32_t ldy, uint32_t ldz)
  158. {
  159. struct starpu_block_interface *block_interface = starpu_data_get_interface_on_node(handle, node);
  160. starpu_data_ptr_register(handle, node);
  161. block_interface->ptr = ptr;
  162. block_interface->dev_handle = dev_handle;
  163. block_interface->offset = offset;
  164. block_interface->ldy = ldy;
  165. block_interface->ldz = ldz;
  166. }
  167. static uint32_t footprint_block_interface_crc32(starpu_data_handle_t handle)
  168. {
  169. uint32_t hash;
  170. hash = starpu_hash_crc32c_be(starpu_block_get_nx(handle), 0);
  171. hash = starpu_hash_crc32c_be(starpu_block_get_ny(handle), hash);
  172. hash = starpu_hash_crc32c_be(starpu_block_get_nz(handle), hash);
  173. return hash;
  174. }
  175. static int block_compare(void *data_interface_a, void *data_interface_b)
  176. {
  177. struct starpu_block_interface *block_a = (struct starpu_block_interface *) data_interface_a;
  178. struct starpu_block_interface *block_b = (struct starpu_block_interface *) data_interface_b;
  179. /* Two matricess are considered compatible if they have the same size */
  180. return (block_a->nx == block_b->nx)
  181. && (block_a->ny == block_b->ny)
  182. && (block_a->nz == block_b->nz)
  183. && (block_a->elemsize == block_b->elemsize);
  184. }
  185. static void display_block_interface(starpu_data_handle_t handle, FILE *f)
  186. {
  187. struct starpu_block_interface *block_interface;
  188. block_interface = (struct starpu_block_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  189. fprintf(f, "%u\t%u\t%u\t", block_interface->nx, block_interface->ny, block_interface->nz);
  190. }
  191. static int pack_block_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count)
  192. {
  193. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  194. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  195. starpu_data_get_interface_on_node(handle, node);
  196. *count = block_interface->nx*block_interface->ny*block_interface->nz*block_interface->elemsize;
  197. if (ptr != NULL)
  198. {
  199. uint32_t z, y;
  200. char *block = (void *)block_interface->ptr;
  201. *ptr = (void *)starpu_malloc_on_node_flags(node, *count, 0);
  202. char *cur = *ptr;
  203. for(z=0 ; z<block_interface->nz ; z++)
  204. {
  205. char *block_z = block;
  206. for(y=0 ; y<block_interface->ny ; y++)
  207. {
  208. memcpy(cur, block, block_interface->nx*block_interface->elemsize);
  209. cur += block_interface->nx*block_interface->elemsize;
  210. block += block_interface->ldy * block_interface->elemsize;
  211. }
  212. block = block_z + block_interface->ldz * block_interface->elemsize;
  213. }
  214. }
  215. return 0;
  216. }
  217. static int unpack_block_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  218. {
  219. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  220. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  221. starpu_data_get_interface_on_node(handle, node);
  222. STARPU_ASSERT(count == block_interface->elemsize * block_interface->nx * block_interface->ny * block_interface->nz);
  223. uint32_t z, y;
  224. char *cur = ptr;
  225. char *block = (void *)block_interface->ptr;
  226. for(z=0 ; z<block_interface->nz ; z++)
  227. {
  228. char *block_z = block;
  229. for(y=0 ; y<block_interface->ny ; y++)
  230. {
  231. memcpy(block, cur, block_interface->nx*block_interface->elemsize);
  232. cur += block_interface->nx*block_interface->elemsize;
  233. block += block_interface->ldy * block_interface->elemsize;
  234. }
  235. block = block_z + block_interface->ldz * block_interface->elemsize;
  236. }
  237. starpu_free_on_node_flags(node, (uintptr_t)ptr, count, 0);
  238. return 0;
  239. }
  240. static size_t block_interface_get_size(starpu_data_handle_t handle)
  241. {
  242. size_t size;
  243. struct starpu_block_interface *block_interface;
  244. block_interface = (struct starpu_block_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  245. #ifdef STARPU_DEBUG
  246. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  247. #endif
  248. size = block_interface->nx*block_interface->ny*block_interface->nz*block_interface->elemsize;
  249. return size;
  250. }
  251. /* offer an access to the data parameters */
  252. uint32_t starpu_block_get_nx(starpu_data_handle_t handle)
  253. {
  254. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  255. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  256. #ifdef STARPU_DEBUG
  257. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  258. #endif
  259. return block_interface->nx;
  260. }
  261. uint32_t starpu_block_get_ny(starpu_data_handle_t handle)
  262. {
  263. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  264. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  265. #ifdef STARPU_DEBUG
  266. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  267. #endif
  268. return block_interface->ny;
  269. }
  270. uint32_t starpu_block_get_nz(starpu_data_handle_t handle)
  271. {
  272. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  273. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  274. #ifdef STARPU_DEBUG
  275. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  276. #endif
  277. return block_interface->nz;
  278. }
  279. uint32_t starpu_block_get_local_ldy(starpu_data_handle_t handle)
  280. {
  281. unsigned node;
  282. node = starpu_worker_get_local_memory_node();
  283. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  284. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  285. starpu_data_get_interface_on_node(handle, node);
  286. #ifdef STARPU_DEBUG
  287. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  288. #endif
  289. return block_interface->ldy;
  290. }
  291. uint32_t starpu_block_get_local_ldz(starpu_data_handle_t handle)
  292. {
  293. unsigned node;
  294. node = starpu_worker_get_local_memory_node();
  295. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  296. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  297. starpu_data_get_interface_on_node(handle, node);
  298. #ifdef STARPU_DEBUG
  299. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  300. #endif
  301. return block_interface->ldz;
  302. }
  303. uintptr_t starpu_block_get_local_ptr(starpu_data_handle_t handle)
  304. {
  305. unsigned node;
  306. node = starpu_worker_get_local_memory_node();
  307. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  308. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  309. starpu_data_get_interface_on_node(handle, node);
  310. #ifdef STARPU_DEBUG
  311. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  312. #endif
  313. return block_interface->ptr;
  314. }
  315. size_t starpu_block_get_elemsize(starpu_data_handle_t handle)
  316. {
  317. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  318. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  319. #ifdef STARPU_DEBUG
  320. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  321. #endif
  322. return block_interface->elemsize;
  323. }
  324. /* memory allocation/deallocation primitives for the BLOCK interface */
  325. /* returns the size of the allocated area */
  326. static starpu_ssize_t allocate_block_buffer_on_node(void *data_interface_, unsigned dst_node)
  327. {
  328. uintptr_t addr = 0, handle;
  329. struct starpu_block_interface *dst_block = (struct starpu_block_interface *) data_interface_;
  330. uint32_t nx = dst_block->nx;
  331. uint32_t ny = dst_block->ny;
  332. uint32_t nz = dst_block->nz;
  333. size_t elemsize = dst_block->elemsize;
  334. starpu_ssize_t allocated_memory;
  335. handle = starpu_malloc_on_node(dst_node, nx*ny*nz*elemsize);
  336. if (!handle)
  337. return -ENOMEM;
  338. if (starpu_node_get_kind(dst_node) != STARPU_OPENCL_RAM)
  339. addr = handle;
  340. allocated_memory = nx*ny*nz*elemsize;
  341. /* update the data properly in consequence */
  342. dst_block->ptr = addr;
  343. dst_block->dev_handle = handle;
  344. dst_block->offset = 0;
  345. dst_block->ldy = nx;
  346. dst_block->ldz = nx*ny;
  347. return allocated_memory;
  348. }
  349. static void free_block_buffer_on_node(void *data_interface, unsigned node)
  350. {
  351. struct starpu_block_interface *block_interface = (struct starpu_block_interface *) data_interface;
  352. uint32_t nx = block_interface->nx;
  353. uint32_t ny = block_interface->ny;
  354. uint32_t nz = block_interface->nz;
  355. size_t elemsize = block_interface->elemsize;
  356. starpu_free_on_node(node, block_interface->dev_handle, nx*ny*nz*elemsize);
  357. }
  358. #ifdef STARPU_USE_CUDA
  359. 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)
  360. {
  361. struct starpu_block_interface *src_block = src_interface;
  362. struct starpu_block_interface *dst_block = dst_interface;
  363. uint32_t nx = src_block->nx;
  364. uint32_t ny = src_block->ny;
  365. uint32_t nz = src_block->nz;
  366. size_t elemsize = src_block->elemsize;
  367. cudaError_t cures;
  368. if ((nx == src_block->ldy) && (src_block->ldy == dst_block->ldy))
  369. {
  370. /* Is that a single contiguous buffer ? */
  371. if (((nx*ny) == src_block->ldz) && (src_block->ldz == dst_block->ldz))
  372. {
  373. starpu_cuda_copy_async_sync((void *)src_block->ptr, src_node, (void *)dst_block->ptr, dst_node, nx*ny*nz*elemsize, NULL, kind);
  374. }
  375. else
  376. {
  377. /* Are all plans contiguous */
  378. cures = cudaMemcpy2D((char *)dst_block->ptr, dst_block->ldz*elemsize,
  379. (char *)src_block->ptr, src_block->ldz*elemsize,
  380. nx*ny*elemsize, nz, kind);
  381. if (!cures)
  382. cures = cudaThreadSynchronize();
  383. if (STARPU_UNLIKELY(cures))
  384. STARPU_CUDA_REPORT_ERROR(cures);
  385. }
  386. }
  387. else
  388. {
  389. /* Default case: we transfer all blocks one by one: nz transfers */
  390. /* TODO: use cudaMemcpy3D now that it works (except on cuda 4.2) */
  391. unsigned layer;
  392. for (layer = 0; layer < src_block->nz; layer++)
  393. {
  394. uint8_t *src_ptr = ((uint8_t *)src_block->ptr) + layer*src_block->ldz*src_block->elemsize;
  395. uint8_t *dst_ptr = ((uint8_t *)dst_block->ptr) + layer*dst_block->ldz*dst_block->elemsize;
  396. cures = cudaMemcpy2D((char *)dst_ptr, dst_block->ldy*elemsize,
  397. (char *)src_ptr, src_block->ldy*elemsize,
  398. nx*elemsize, ny, kind);
  399. if (!cures)
  400. cures = cudaThreadSynchronize();
  401. if (STARPU_UNLIKELY(cures))
  402. STARPU_CUDA_REPORT_ERROR(cures);
  403. }
  404. }
  405. starpu_interface_data_copy(src_node, dst_node, src_block->nx*src_block->ny*src_block->elemsize*src_block->elemsize);
  406. return 0;
  407. }
  408. 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)
  409. {
  410. struct starpu_block_interface *src_block = src_interface;
  411. struct starpu_block_interface *dst_block = dst_interface;
  412. uint32_t nx = src_block->nx;
  413. uint32_t ny = src_block->ny;
  414. uint32_t nz = src_block->nz;
  415. size_t elemsize = src_block->elemsize;
  416. cudaError_t cures;
  417. int ret;
  418. /* We may have a contiguous buffer for the entire block, or contiguous
  419. * plans within the block, we can avoid many small transfers that way */
  420. if ((nx == src_block->ldy) && (src_block->ldy == dst_block->ldy))
  421. {
  422. /* Is that a single contiguous buffer ? */
  423. if (((nx*ny) == src_block->ldz) && (src_block->ldz == dst_block->ldz))
  424. {
  425. ret = starpu_cuda_copy_async_sync((void *)src_block->ptr, src_node, (void *)dst_block->ptr, dst_node, nx*ny*nz*elemsize, stream, kind);
  426. }
  427. else
  428. {
  429. double start;
  430. /* Are all plans contiguous */
  431. starpu_interface_start_driver_copy_async(src_node, dst_node, &start);
  432. cures = cudaMemcpy2DAsync((char *)dst_block->ptr, dst_block->ldz*elemsize,
  433. (char *)src_block->ptr, src_block->ldz*elemsize,
  434. nx*ny*elemsize, nz, kind, stream);
  435. starpu_interface_end_driver_copy_async(src_node, dst_node, start);
  436. if (STARPU_UNLIKELY(cures))
  437. {
  438. cures = cudaMemcpy2D((char *)dst_block->ptr, dst_block->ldz*elemsize,
  439. (char *)src_block->ptr, src_block->ldz*elemsize,
  440. nx*ny*elemsize, nz, kind);
  441. if (!cures)
  442. cures = cudaThreadSynchronize();
  443. if (STARPU_UNLIKELY(cures))
  444. STARPU_CUDA_REPORT_ERROR(cures);
  445. ret = 0;
  446. }
  447. else
  448. {
  449. ret = -EAGAIN;
  450. }
  451. }
  452. }
  453. else
  454. {
  455. /* Default case: we transfer all blocks one by one: nz 2D transfers */
  456. /* TODO: use cudaMemcpy3D now that it works (except on cuda 4.2) */
  457. unsigned layer;
  458. for (layer = 0; layer < src_block->nz; layer++)
  459. {
  460. uint8_t *src_ptr = ((uint8_t *)src_block->ptr) + layer*src_block->ldz*src_block->elemsize;
  461. uint8_t *dst_ptr = ((uint8_t *)dst_block->ptr) + layer*dst_block->ldz*dst_block->elemsize;
  462. double start;
  463. starpu_interface_start_driver_copy_async(src_node, dst_node, &start);
  464. cures = cudaMemcpy2DAsync((char *)dst_ptr, dst_block->ldy*elemsize,
  465. (char *)src_ptr, src_block->ldy*elemsize,
  466. nx*elemsize, ny, kind, stream);
  467. starpu_interface_end_driver_copy_async(src_node, dst_node, start);
  468. if (STARPU_UNLIKELY(cures))
  469. {
  470. /* I don't know how to do that "better" */
  471. goto no_async_default;
  472. }
  473. }
  474. ret = -EAGAIN;
  475. }
  476. starpu_interface_data_copy(src_node, dst_node, src_block->nx*src_block->ny*src_block->nz*src_block->elemsize);
  477. return ret;
  478. no_async_default:
  479. {
  480. unsigned layer;
  481. for (layer = 0; layer < src_block->nz; layer++)
  482. {
  483. uint8_t *src_ptr = ((uint8_t *)src_block->ptr) + layer*src_block->ldz*src_block->elemsize;
  484. uint8_t *dst_ptr = ((uint8_t *)dst_block->ptr) + layer*dst_block->ldz*dst_block->elemsize;
  485. cures = cudaMemcpy2D((char *)dst_ptr, dst_block->ldy*elemsize,
  486. (char *)src_ptr, src_block->ldy*elemsize,
  487. nx*elemsize, ny, kind);
  488. if (!cures)
  489. cures = cudaThreadSynchronize();
  490. if (STARPU_UNLIKELY(cures))
  491. STARPU_CUDA_REPORT_ERROR(cures);
  492. }
  493. starpu_interface_data_copy(src_node, dst_node, src_block->nx*src_block->ny*src_block->nz*src_block->elemsize);
  494. return 0;
  495. }
  496. }
  497. static int copy_cuda_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node)
  498. {
  499. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost);
  500. }
  501. static int copy_ram_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  502. {
  503. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice);
  504. }
  505. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  506. {
  507. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToDevice);
  508. }
  509. 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)
  510. {
  511. return copy_cuda_async_common(src_interface, src_node, dst_interface, dst_node, stream, cudaMemcpyDeviceToHost);
  512. }
  513. 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)
  514. {
  515. return copy_cuda_async_common(src_interface, src_node, dst_interface, dst_node, stream, cudaMemcpyHostToDevice);
  516. }
  517. #endif // STARPU_USE_CUDA
  518. #ifdef STARPU_USE_OPENCL
  519. static int copy_opencl_common(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  520. {
  521. struct starpu_block_interface *src_block = src_interface;
  522. struct starpu_block_interface *dst_block = dst_interface;
  523. int ret = 0;
  524. uint32_t nx = src_block->nx;
  525. uint32_t ny = src_block->ny;
  526. /* We may have a contiguous buffer for the entire block, or contiguous
  527. * plans within the block, we can avoid many small transfers that way */
  528. if ((nx == src_block->ldy) && (src_block->ldy == dst_block->ldy))
  529. {
  530. /* Is that a single contiguous buffer ? */
  531. if (((nx*ny) == src_block->ldz) && (src_block->ldz == dst_block->ldz))
  532. {
  533. ret = starpu_opencl_copy_async_sync(src_block->dev_handle, src_block->offset, src_node,
  534. dst_block->dev_handle, dst_block->offset, dst_node,
  535. src_block->nx*src_block->ny*src_block->nz*src_block->elemsize,
  536. event);
  537. }
  538. else
  539. {
  540. /* Are all plans contiguous */
  541. STARPU_ASSERT_MSG(0, "XXX non contiguous buffers are not properly supported in OpenCL yet. (TODO)");
  542. }
  543. }
  544. else
  545. {
  546. /* Default case: we transfer all lines one by one: ny*nz transfers */
  547. /* TODO: rect support */
  548. unsigned layer;
  549. for (layer = 0; layer < src_block->nz; layer++)
  550. {
  551. unsigned j;
  552. for(j=0 ; j<src_block->ny ; j++)
  553. {
  554. ret = starpu_opencl_copy_async_sync(src_block->dev_handle,
  555. src_block->offset + layer*src_block->ldz*src_block->elemsize + j*src_block->ldy*src_block->elemsize,
  556. src_node,
  557. dst_block->dev_handle,
  558. dst_block->offset + layer*dst_block->ldz*dst_block->elemsize + j*dst_block->ldy*dst_block->elemsize,
  559. dst_node,
  560. src_block->nx*src_block->elemsize,
  561. event);
  562. }
  563. }
  564. }
  565. starpu_interface_data_copy(src_node, dst_node, src_block->nx*src_block->ny*src_block->nz*src_block->elemsize);
  566. return ret;
  567. }
  568. static int copy_ram_to_opencl_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  569. {
  570. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  571. }
  572. static int copy_opencl_to_ram_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  573. {
  574. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  575. }
  576. static int copy_opencl_to_opencl_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  577. {
  578. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  579. }
  580. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  581. {
  582. return copy_ram_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  583. }
  584. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  585. {
  586. return copy_opencl_to_ram_async(src_interface, src_node, dst_interface, dst_node, NULL);
  587. }
  588. static int copy_opencl_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  589. {
  590. return copy_opencl_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  591. }
  592. #endif
  593. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data)
  594. {
  595. struct starpu_block_interface *src_block = (struct starpu_block_interface *) src_interface;
  596. struct starpu_block_interface *dst_block = (struct starpu_block_interface *) dst_interface;
  597. int ret = 0;
  598. uint32_t nx = dst_block->nx;
  599. uint32_t ny = dst_block->ny;
  600. uint32_t nz = dst_block->nz;
  601. size_t elemsize = dst_block->elemsize;
  602. uint32_t ldy_src = src_block->ldy;
  603. uint32_t ldz_src = src_block->ldz;
  604. uint32_t ldy_dst = dst_block->ldy;
  605. uint32_t ldz_dst = dst_block->ldz;
  606. if (ldy_src == nx && ldy_dst == nx && ldz_src == ny && ldz_dst == ny)
  607. {
  608. /* Optimise non-partitioned and z-partitioned case */
  609. if (starpu_interface_copy(src_block->dev_handle, src_block->offset, src_node,
  610. dst_block->dev_handle, dst_block->offset, dst_node,
  611. nx*ny*nz*elemsize, async_data))
  612. ret = -EAGAIN;
  613. }
  614. else
  615. {
  616. unsigned z;
  617. for (z = 0; z < nz; z++)
  618. {
  619. if (ldy_src == nx && ldy_dst == nx)
  620. {
  621. /* Optimise y-partitioned case */
  622. uint32_t src_offset = z*ldz_src*elemsize;
  623. uint32_t dst_offset = z*ldz_dst*elemsize;
  624. if (starpu_interface_copy(src_block->dev_handle, src_block->offset + src_offset, src_node,
  625. dst_block->dev_handle, dst_block->offset + dst_offset, dst_node,
  626. nx*ny*elemsize, async_data))
  627. ret = -EAGAIN;
  628. }
  629. else
  630. {
  631. unsigned y;
  632. for (y = 0; y < ny; y++)
  633. {
  634. /* Eerf, x-partitioned case */
  635. uint32_t src_offset = (y*ldy_src + z*ldz_src)*elemsize;
  636. uint32_t dst_offset = (y*ldy_dst + z*ldz_dst)*elemsize;
  637. if (starpu_interface_copy(src_block->dev_handle, src_block->offset + src_offset, src_node,
  638. dst_block->dev_handle, dst_block->offset + dst_offset, dst_node,
  639. nx*elemsize, async_data))
  640. ret = -EAGAIN;
  641. }
  642. }
  643. }
  644. }
  645. starpu_interface_data_copy(src_node, dst_node, nx*ny*nz*elemsize);
  646. return ret;
  647. }
  648. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size)
  649. {
  650. struct starpu_block_interface *block = (struct starpu_block_interface *) data_interface;
  651. return snprintf(buf, size, "B%ux%ux%ux%u",
  652. (unsigned) block->nx,
  653. (unsigned) block->ny,
  654. (unsigned) block->nz,
  655. (unsigned) block->elemsize);
  656. }