block_interface.c 30 KB

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