block_interface.c 29 KB

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