block_interface.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. uint32_t ldy = block_interface->ldy;
  96. uint32_t ldz = block_interface->ldz;
  97. uint32_t nx = block_interface->nx;
  98. uint32_t ny = block_interface->ny;
  99. uint32_t nz = block_interface->nz;
  100. size_t elemsize = block_interface->elemsize;
  101. return (char*) ptr >= (char*) block_interface->ptr &&
  102. (char*) ptr < (char*) block_interface->ptr + (nz-1)*ldz*elemsize + (ny-1)*ldy*elemsize + nx*elemsize;
  103. }
  104. static void register_block_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  105. {
  106. struct starpu_block_interface *block_interface = (struct starpu_block_interface *) data_interface;
  107. unsigned node;
  108. for (node = 0; node < STARPU_MAXNODES; node++)
  109. {
  110. struct starpu_block_interface *local_interface = (struct starpu_block_interface *)
  111. starpu_data_get_interface_on_node(handle, node);
  112. if (node == home_node)
  113. {
  114. local_interface->ptr = block_interface->ptr;
  115. local_interface->dev_handle = block_interface->dev_handle;
  116. local_interface->offset = block_interface->offset;
  117. local_interface->ldy = block_interface->ldy;
  118. local_interface->ldz = block_interface->ldz;
  119. }
  120. else
  121. {
  122. local_interface->ptr = 0;
  123. local_interface->dev_handle = 0;
  124. local_interface->offset = 0;
  125. local_interface->ldy = 0;
  126. local_interface->ldz = 0;
  127. }
  128. local_interface->id = block_interface->id;
  129. local_interface->nx = block_interface->nx;
  130. local_interface->ny = block_interface->ny;
  131. local_interface->nz = block_interface->nz;
  132. local_interface->elemsize = block_interface->elemsize;
  133. }
  134. }
  135. /* declare a new data with the BLAS interface */
  136. void starpu_block_data_register(starpu_data_handle_t *handleptr, int home_node,
  137. uintptr_t ptr, uint32_t ldy, uint32_t ldz, uint32_t nx,
  138. uint32_t ny, uint32_t nz, size_t elemsize)
  139. {
  140. struct starpu_block_interface block_interface =
  141. {
  142. .id = STARPU_BLOCK_INTERFACE_ID,
  143. .ptr = ptr,
  144. .dev_handle = ptr,
  145. .offset = 0,
  146. .ldy = ldy,
  147. .ldz = ldz,
  148. .nx = nx,
  149. .ny = ny,
  150. .nz = nz,
  151. .elemsize = elemsize
  152. };
  153. #ifndef STARPU_SIMGRID
  154. if (home_node >= 0 && starpu_node_get_kind(home_node) == STARPU_CPU_RAM)
  155. {
  156. STARPU_ASSERT_ACCESSIBLE(ptr);
  157. STARPU_ASSERT_ACCESSIBLE(ptr + (nz-1)*ldz*elemsize + (ny-1)*ldy*elemsize + nx*elemsize - 1);
  158. }
  159. #endif
  160. starpu_data_register(handleptr, home_node, &block_interface, &starpu_interface_block_ops);
  161. }
  162. void starpu_block_ptr_register(starpu_data_handle_t handle, unsigned node,
  163. uintptr_t ptr, uintptr_t dev_handle, size_t offset, uint32_t ldy, uint32_t ldz)
  164. {
  165. struct starpu_block_interface *block_interface = starpu_data_get_interface_on_node(handle, node);
  166. starpu_data_ptr_register(handle, node);
  167. block_interface->ptr = ptr;
  168. block_interface->dev_handle = dev_handle;
  169. block_interface->offset = offset;
  170. block_interface->ldy = ldy;
  171. block_interface->ldz = ldz;
  172. }
  173. static uint32_t footprint_block_interface_crc32(starpu_data_handle_t handle)
  174. {
  175. uint32_t hash;
  176. hash = starpu_hash_crc32c_be(starpu_block_get_nx(handle), 0);
  177. hash = starpu_hash_crc32c_be(starpu_block_get_ny(handle), hash);
  178. hash = starpu_hash_crc32c_be(starpu_block_get_nz(handle), hash);
  179. return hash;
  180. }
  181. static int block_compare(void *data_interface_a, void *data_interface_b)
  182. {
  183. struct starpu_block_interface *block_a = (struct starpu_block_interface *) data_interface_a;
  184. struct starpu_block_interface *block_b = (struct starpu_block_interface *) data_interface_b;
  185. /* Two matricess are considered compatible if they have the same size */
  186. return (block_a->nx == block_b->nx)
  187. && (block_a->ny == block_b->ny)
  188. && (block_a->nz == block_b->nz)
  189. && (block_a->elemsize == block_b->elemsize);
  190. }
  191. static void display_block_interface(starpu_data_handle_t handle, FILE *f)
  192. {
  193. struct starpu_block_interface *block_interface;
  194. block_interface = (struct starpu_block_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  195. fprintf(f, "%u\t%u\t%u\t", block_interface->nx, block_interface->ny, block_interface->nz);
  196. }
  197. static int pack_block_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count)
  198. {
  199. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  200. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  201. starpu_data_get_interface_on_node(handle, node);
  202. *count = block_interface->nx*block_interface->ny*block_interface->nz*block_interface->elemsize;
  203. if (ptr != NULL)
  204. {
  205. uint32_t z, y;
  206. char *block = (void *)block_interface->ptr;
  207. *ptr = (void *)starpu_malloc_on_node_flags(node, *count, 0);
  208. char *cur = *ptr;
  209. for(z=0 ; z<block_interface->nz ; z++)
  210. {
  211. char *block_z = block;
  212. for(y=0 ; y<block_interface->ny ; y++)
  213. {
  214. memcpy(cur, block, block_interface->nx*block_interface->elemsize);
  215. cur += block_interface->nx*block_interface->elemsize;
  216. block += block_interface->ldy * block_interface->elemsize;
  217. }
  218. block = block_z + block_interface->ldz * block_interface->elemsize;
  219. }
  220. }
  221. return 0;
  222. }
  223. static int unpack_block_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  224. {
  225. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  226. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  227. starpu_data_get_interface_on_node(handle, node);
  228. STARPU_ASSERT(count == block_interface->elemsize * block_interface->nx * block_interface->ny * block_interface->nz);
  229. uint32_t z, y;
  230. char *cur = ptr;
  231. char *block = (void *)block_interface->ptr;
  232. for(z=0 ; z<block_interface->nz ; z++)
  233. {
  234. char *block_z = block;
  235. for(y=0 ; y<block_interface->ny ; y++)
  236. {
  237. memcpy(block, cur, block_interface->nx*block_interface->elemsize);
  238. cur += block_interface->nx*block_interface->elemsize;
  239. block += block_interface->ldy * block_interface->elemsize;
  240. }
  241. block = block_z + block_interface->ldz * block_interface->elemsize;
  242. }
  243. starpu_free_on_node_flags(node, (uintptr_t)ptr, count, 0);
  244. return 0;
  245. }
  246. static size_t block_interface_get_size(starpu_data_handle_t handle)
  247. {
  248. size_t size;
  249. struct starpu_block_interface *block_interface;
  250. block_interface = (struct starpu_block_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  251. #ifdef STARPU_DEBUG
  252. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  253. #endif
  254. size = block_interface->nx*block_interface->ny*block_interface->nz*block_interface->elemsize;
  255. return size;
  256. }
  257. /* offer an access to the data parameters */
  258. uint32_t starpu_block_get_nx(starpu_data_handle_t handle)
  259. {
  260. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  261. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  262. #ifdef STARPU_DEBUG
  263. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  264. #endif
  265. return block_interface->nx;
  266. }
  267. uint32_t starpu_block_get_ny(starpu_data_handle_t handle)
  268. {
  269. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  270. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  271. #ifdef STARPU_DEBUG
  272. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  273. #endif
  274. return block_interface->ny;
  275. }
  276. uint32_t starpu_block_get_nz(starpu_data_handle_t handle)
  277. {
  278. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  279. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  280. #ifdef STARPU_DEBUG
  281. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  282. #endif
  283. return block_interface->nz;
  284. }
  285. uint32_t starpu_block_get_local_ldy(starpu_data_handle_t handle)
  286. {
  287. unsigned node;
  288. node = starpu_worker_get_local_memory_node();
  289. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  290. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  291. starpu_data_get_interface_on_node(handle, node);
  292. #ifdef STARPU_DEBUG
  293. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  294. #endif
  295. return block_interface->ldy;
  296. }
  297. uint32_t starpu_block_get_local_ldz(starpu_data_handle_t handle)
  298. {
  299. unsigned node;
  300. node = starpu_worker_get_local_memory_node();
  301. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  302. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  303. starpu_data_get_interface_on_node(handle, node);
  304. #ifdef STARPU_DEBUG
  305. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  306. #endif
  307. return block_interface->ldz;
  308. }
  309. uintptr_t starpu_block_get_local_ptr(starpu_data_handle_t handle)
  310. {
  311. unsigned node;
  312. node = starpu_worker_get_local_memory_node();
  313. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  314. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  315. starpu_data_get_interface_on_node(handle, node);
  316. #ifdef STARPU_DEBUG
  317. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  318. #endif
  319. return block_interface->ptr;
  320. }
  321. size_t starpu_block_get_elemsize(starpu_data_handle_t handle)
  322. {
  323. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  324. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  325. #ifdef STARPU_DEBUG
  326. STARPU_ASSERT_MSG(block_interface->id == STARPU_BLOCK_INTERFACE_ID, "Error. The given data is not a block.");
  327. #endif
  328. return block_interface->elemsize;
  329. }
  330. /* memory allocation/deallocation primitives for the BLOCK interface */
  331. /* returns the size of the allocated area */
  332. static starpu_ssize_t allocate_block_buffer_on_node(void *data_interface_, unsigned dst_node)
  333. {
  334. uintptr_t addr = 0, handle;
  335. struct starpu_block_interface *dst_block = (struct starpu_block_interface *) data_interface_;
  336. uint32_t nx = dst_block->nx;
  337. uint32_t ny = dst_block->ny;
  338. uint32_t nz = dst_block->nz;
  339. size_t elemsize = dst_block->elemsize;
  340. starpu_ssize_t allocated_memory;
  341. handle = starpu_malloc_on_node(dst_node, nx*ny*nz*elemsize);
  342. if (!handle)
  343. return -ENOMEM;
  344. if (starpu_node_get_kind(dst_node) != STARPU_OPENCL_RAM)
  345. addr = handle;
  346. allocated_memory = nx*ny*nz*elemsize;
  347. /* update the data properly in consequence */
  348. dst_block->ptr = addr;
  349. dst_block->dev_handle = handle;
  350. dst_block->offset = 0;
  351. dst_block->ldy = nx;
  352. dst_block->ldz = nx*ny;
  353. return allocated_memory;
  354. }
  355. static void free_block_buffer_on_node(void *data_interface, unsigned node)
  356. {
  357. struct starpu_block_interface *block_interface = (struct starpu_block_interface *) data_interface;
  358. uint32_t nx = block_interface->nx;
  359. uint32_t ny = block_interface->ny;
  360. uint32_t nz = block_interface->nz;
  361. size_t elemsize = block_interface->elemsize;
  362. starpu_free_on_node(node, block_interface->dev_handle, nx*ny*nz*elemsize);
  363. }
  364. #ifdef STARPU_USE_CUDA
  365. 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)
  366. {
  367. struct starpu_block_interface *src_block = src_interface;
  368. struct starpu_block_interface *dst_block = dst_interface;
  369. uint32_t nx = src_block->nx;
  370. uint32_t ny = src_block->ny;
  371. uint32_t nz = src_block->nz;
  372. size_t elemsize = src_block->elemsize;
  373. cudaError_t cures;
  374. if ((nx == src_block->ldy) && (src_block->ldy == dst_block->ldy))
  375. {
  376. /* Is that a single contiguous buffer ? */
  377. if (((nx*ny) == src_block->ldz) && (src_block->ldz == dst_block->ldz))
  378. {
  379. starpu_cuda_copy_async_sync((void *)src_block->ptr, src_node, (void *)dst_block->ptr, dst_node, nx*ny*nz*elemsize, NULL, kind);
  380. }
  381. else
  382. {
  383. /* Are all plans contiguous */
  384. cures = cudaMemcpy2D((char *)dst_block->ptr, dst_block->ldz*elemsize,
  385. (char *)src_block->ptr, src_block->ldz*elemsize,
  386. nx*ny*elemsize, nz, kind);
  387. if (!cures)
  388. cures = cudaThreadSynchronize();
  389. if (STARPU_UNLIKELY(cures))
  390. STARPU_CUDA_REPORT_ERROR(cures);
  391. }
  392. }
  393. else
  394. {
  395. /* Default case: we transfer all blocks one by one: nz transfers */
  396. /* TODO: use cudaMemcpy3D now that it works (except on cuda 4.2) */
  397. unsigned layer;
  398. for (layer = 0; layer < src_block->nz; layer++)
  399. {
  400. uint8_t *src_ptr = ((uint8_t *)src_block->ptr) + layer*src_block->ldz*src_block->elemsize;
  401. uint8_t *dst_ptr = ((uint8_t *)dst_block->ptr) + layer*dst_block->ldz*dst_block->elemsize;
  402. cures = cudaMemcpy2D((char *)dst_ptr, dst_block->ldy*elemsize,
  403. (char *)src_ptr, src_block->ldy*elemsize,
  404. nx*elemsize, ny, kind);
  405. if (!cures)
  406. cures = cudaThreadSynchronize();
  407. if (STARPU_UNLIKELY(cures))
  408. STARPU_CUDA_REPORT_ERROR(cures);
  409. }
  410. }
  411. starpu_interface_data_copy(src_node, dst_node, src_block->nx*src_block->ny*src_block->elemsize*src_block->elemsize);
  412. return 0;
  413. }
  414. 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)
  415. {
  416. struct starpu_block_interface *src_block = src_interface;
  417. struct starpu_block_interface *dst_block = dst_interface;
  418. uint32_t nx = src_block->nx;
  419. uint32_t ny = src_block->ny;
  420. uint32_t nz = src_block->nz;
  421. size_t elemsize = src_block->elemsize;
  422. cudaError_t cures;
  423. int ret;
  424. /* We may have a contiguous buffer for the entire block, or contiguous
  425. * plans within the block, we can avoid many small transfers that way */
  426. if ((nx == src_block->ldy) && (src_block->ldy == dst_block->ldy))
  427. {
  428. /* Is that a single contiguous buffer ? */
  429. if (((nx*ny) == src_block->ldz) && (src_block->ldz == dst_block->ldz))
  430. {
  431. ret = starpu_cuda_copy_async_sync((void *)src_block->ptr, src_node, (void *)dst_block->ptr, dst_node, nx*ny*nz*elemsize, stream, kind);
  432. }
  433. else
  434. {
  435. double start;
  436. /* Are all plans contiguous */
  437. starpu_interface_start_driver_copy_async(src_node, dst_node, &start);
  438. cures = cudaMemcpy2DAsync((char *)dst_block->ptr, dst_block->ldz*elemsize,
  439. (char *)src_block->ptr, src_block->ldz*elemsize,
  440. nx*ny*elemsize, nz, kind, stream);
  441. starpu_interface_end_driver_copy_async(src_node, dst_node, start);
  442. if (STARPU_UNLIKELY(cures))
  443. {
  444. cures = cudaMemcpy2D((char *)dst_block->ptr, dst_block->ldz*elemsize,
  445. (char *)src_block->ptr, src_block->ldz*elemsize,
  446. nx*ny*elemsize, nz, kind);
  447. if (!cures)
  448. cures = cudaThreadSynchronize();
  449. if (STARPU_UNLIKELY(cures))
  450. STARPU_CUDA_REPORT_ERROR(cures);
  451. ret = 0;
  452. }
  453. else
  454. {
  455. ret = -EAGAIN;
  456. }
  457. }
  458. }
  459. else
  460. {
  461. /* Default case: we transfer all blocks one by one: nz 2D transfers */
  462. /* TODO: use cudaMemcpy3D now that it works (except on cuda 4.2) */
  463. unsigned layer;
  464. for (layer = 0; layer < src_block->nz; layer++)
  465. {
  466. uint8_t *src_ptr = ((uint8_t *)src_block->ptr) + layer*src_block->ldz*src_block->elemsize;
  467. uint8_t *dst_ptr = ((uint8_t *)dst_block->ptr) + layer*dst_block->ldz*dst_block->elemsize;
  468. double start;
  469. starpu_interface_start_driver_copy_async(src_node, dst_node, &start);
  470. cures = cudaMemcpy2DAsync((char *)dst_ptr, dst_block->ldy*elemsize,
  471. (char *)src_ptr, src_block->ldy*elemsize,
  472. nx*elemsize, ny, kind, stream);
  473. starpu_interface_end_driver_copy_async(src_node, dst_node, start);
  474. if (STARPU_UNLIKELY(cures))
  475. {
  476. /* I don't know how to do that "better" */
  477. goto no_async_default;
  478. }
  479. }
  480. ret = -EAGAIN;
  481. }
  482. starpu_interface_data_copy(src_node, dst_node, src_block->nx*src_block->ny*src_block->nz*src_block->elemsize);
  483. return ret;
  484. no_async_default:
  485. {
  486. unsigned layer;
  487. for (layer = 0; layer < src_block->nz; layer++)
  488. {
  489. uint8_t *src_ptr = ((uint8_t *)src_block->ptr) + layer*src_block->ldz*src_block->elemsize;
  490. uint8_t *dst_ptr = ((uint8_t *)dst_block->ptr) + layer*dst_block->ldz*dst_block->elemsize;
  491. cures = cudaMemcpy2D((char *)dst_ptr, dst_block->ldy*elemsize,
  492. (char *)src_ptr, src_block->ldy*elemsize,
  493. nx*elemsize, ny, kind);
  494. if (!cures)
  495. cures = cudaThreadSynchronize();
  496. if (STARPU_UNLIKELY(cures))
  497. STARPU_CUDA_REPORT_ERROR(cures);
  498. }
  499. starpu_interface_data_copy(src_node, dst_node, src_block->nx*src_block->ny*src_block->nz*src_block->elemsize);
  500. return 0;
  501. }
  502. }
  503. static int copy_cuda_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node)
  504. {
  505. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost);
  506. }
  507. static int copy_ram_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  508. {
  509. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice);
  510. }
  511. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  512. {
  513. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToDevice);
  514. }
  515. 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)
  516. {
  517. return copy_cuda_async_common(src_interface, src_node, dst_interface, dst_node, stream, cudaMemcpyDeviceToHost);
  518. }
  519. 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)
  520. {
  521. return copy_cuda_async_common(src_interface, src_node, dst_interface, dst_node, stream, cudaMemcpyHostToDevice);
  522. }
  523. #endif // STARPU_USE_CUDA
  524. #ifdef STARPU_USE_OPENCL
  525. static int copy_opencl_common(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  526. {
  527. struct starpu_block_interface *src_block = src_interface;
  528. struct starpu_block_interface *dst_block = dst_interface;
  529. int ret = 0;
  530. uint32_t nx = src_block->nx;
  531. uint32_t ny = src_block->ny;
  532. /* We may have a contiguous buffer for the entire block, or contiguous
  533. * plans within the block, we can avoid many small transfers that way */
  534. if ((nx == src_block->ldy) && (src_block->ldy == dst_block->ldy))
  535. {
  536. /* Is that a single contiguous buffer ? */
  537. if (((nx*ny) == src_block->ldz) && (src_block->ldz == dst_block->ldz))
  538. {
  539. ret = starpu_opencl_copy_async_sync(src_block->dev_handle, src_block->offset, src_node,
  540. dst_block->dev_handle, dst_block->offset, dst_node,
  541. src_block->nx*src_block->ny*src_block->nz*src_block->elemsize,
  542. event);
  543. }
  544. else
  545. {
  546. /* Are all plans contiguous */
  547. STARPU_ASSERT_MSG(0, "XXX non contiguous buffers are not properly supported in OpenCL yet. (TODO)");
  548. }
  549. }
  550. else
  551. {
  552. /* Default case: we transfer all lines one by one: ny*nz transfers */
  553. /* TODO: rect support */
  554. unsigned layer;
  555. for (layer = 0; layer < src_block->nz; layer++)
  556. {
  557. unsigned j;
  558. for(j=0 ; j<src_block->ny ; j++)
  559. {
  560. ret = starpu_opencl_copy_async_sync(src_block->dev_handle,
  561. src_block->offset + layer*src_block->ldz*src_block->elemsize + j*src_block->ldy*src_block->elemsize,
  562. src_node,
  563. dst_block->dev_handle,
  564. dst_block->offset + layer*dst_block->ldz*dst_block->elemsize + j*dst_block->ldy*dst_block->elemsize,
  565. dst_node,
  566. src_block->nx*src_block->elemsize,
  567. event);
  568. }
  569. }
  570. }
  571. starpu_interface_data_copy(src_node, dst_node, src_block->nx*src_block->ny*src_block->nz*src_block->elemsize);
  572. return ret;
  573. }
  574. static int copy_ram_to_opencl_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  575. {
  576. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  577. }
  578. static int copy_opencl_to_ram_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  579. {
  580. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  581. }
  582. static int copy_opencl_to_opencl_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  583. {
  584. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  585. }
  586. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  587. {
  588. return copy_ram_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  589. }
  590. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  591. {
  592. return copy_opencl_to_ram_async(src_interface, src_node, dst_interface, dst_node, NULL);
  593. }
  594. static int copy_opencl_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  595. {
  596. return copy_opencl_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  597. }
  598. #endif
  599. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data)
  600. {
  601. struct starpu_block_interface *src_block = (struct starpu_block_interface *) src_interface;
  602. struct starpu_block_interface *dst_block = (struct starpu_block_interface *) dst_interface;
  603. int ret = 0;
  604. uint32_t nx = dst_block->nx;
  605. uint32_t ny = dst_block->ny;
  606. uint32_t nz = dst_block->nz;
  607. size_t elemsize = dst_block->elemsize;
  608. uint32_t ldy_src = src_block->ldy;
  609. uint32_t ldz_src = src_block->ldz;
  610. uint32_t ldy_dst = dst_block->ldy;
  611. uint32_t ldz_dst = dst_block->ldz;
  612. if (ldy_src == nx && ldy_dst == nx && ldz_src == ny && ldz_dst == ny)
  613. {
  614. /* Optimise non-partitioned and z-partitioned case */
  615. if (starpu_interface_copy(src_block->dev_handle, src_block->offset, src_node,
  616. dst_block->dev_handle, dst_block->offset, dst_node,
  617. nx*ny*nz*elemsize, async_data))
  618. ret = -EAGAIN;
  619. }
  620. else
  621. {
  622. unsigned z;
  623. for (z = 0; z < nz; z++)
  624. {
  625. if (ldy_src == nx && ldy_dst == nx)
  626. {
  627. /* Optimise y-partitioned case */
  628. uint32_t src_offset = z*ldz_src*elemsize;
  629. uint32_t dst_offset = z*ldz_dst*elemsize;
  630. if (starpu_interface_copy(src_block->dev_handle, src_block->offset + src_offset, src_node,
  631. dst_block->dev_handle, dst_block->offset + dst_offset, dst_node,
  632. nx*ny*elemsize, async_data))
  633. ret = -EAGAIN;
  634. }
  635. else
  636. {
  637. unsigned y;
  638. for (y = 0; y < ny; y++)
  639. {
  640. /* Eerf, x-partitioned case */
  641. uint32_t src_offset = (y*ldy_src + z*ldz_src)*elemsize;
  642. uint32_t dst_offset = (y*ldy_dst + z*ldz_dst)*elemsize;
  643. if (starpu_interface_copy(src_block->dev_handle, src_block->offset + src_offset, src_node,
  644. dst_block->dev_handle, dst_block->offset + dst_offset, dst_node,
  645. nx*elemsize, async_data))
  646. ret = -EAGAIN;
  647. }
  648. }
  649. }
  650. }
  651. starpu_interface_data_copy(src_node, dst_node, nx*ny*nz*elemsize);
  652. return ret;
  653. }
  654. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size)
  655. {
  656. struct starpu_block_interface *block = (struct starpu_block_interface *) data_interface;
  657. return snprintf(buf, size, "B%ux%ux%ux%u",
  658. (unsigned) block->nx,
  659. (unsigned) block->ny,
  660. (unsigned) block->nz,
  661. (unsigned) block->elemsize);
  662. }