block_interface.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include <common/config.h>
  19. #include <datawizard/coherency.h>
  20. #include <datawizard/copy_driver.h>
  21. #include <datawizard/filters.h>
  22. #include <starpu_hash.h>
  23. #include <starpu_cuda.h>
  24. #include <starpu_opencl.h>
  25. #include <drivers/opencl/driver_opencl.h>
  26. static int copy_ram_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  27. #ifdef STARPU_USE_CUDA
  28. static int copy_ram_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  29. static int copy_cuda_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  30. 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);
  31. 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);
  32. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  33. #endif
  34. #ifdef STARPU_USE_OPENCL
  35. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  36. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  37. 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);
  38. 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);
  39. #endif
  40. static struct starpu_data_copy_methods block_copy_data_methods_s =
  41. {
  42. .ram_to_ram = copy_ram_to_ram,
  43. .ram_to_spu = NULL,
  44. #ifdef STARPU_USE_CUDA
  45. .ram_to_cuda = copy_ram_to_cuda,
  46. .cuda_to_ram = copy_cuda_to_ram,
  47. .ram_to_cuda_async = copy_ram_to_cuda_async,
  48. .cuda_to_ram_async = copy_cuda_to_ram_async,
  49. .cuda_to_cuda = copy_cuda_to_cuda,
  50. #endif
  51. #ifdef STARPU_USE_OPENCL
  52. .ram_to_opencl = copy_ram_to_opencl,
  53. .opencl_to_ram = copy_opencl_to_ram,
  54. .ram_to_opencl_async = copy_ram_to_opencl_async,
  55. .opencl_to_ram_async = copy_opencl_to_ram_async,
  56. #endif
  57. .cuda_to_spu = NULL,
  58. .spu_to_ram = NULL,
  59. .spu_to_cuda = NULL,
  60. .spu_to_spu = NULL
  61. };
  62. static void register_block_handle(starpu_data_handle_t handle, uint32_t home_node, void *data_interface);
  63. static void *block_handle_to_pointer(starpu_data_handle_t data_handle, uint32_t node);
  64. static ssize_t allocate_block_buffer_on_node(void *data_interface_, uint32_t dst_node);
  65. static void free_block_buffer_on_node(void *data_interface, uint32_t node);
  66. static size_t block_interface_get_size(starpu_data_handle_t handle);
  67. static uint32_t footprint_block_interface_crc32(starpu_data_handle_t handle);
  68. static int block_compare(void *data_interface_a, void *data_interface_b);
  69. static void display_block_interface(starpu_data_handle_t handle, FILE *f);
  70. #ifdef STARPU_USE_GORDON
  71. static int convert_block_to_gordon(void *data_interface, uint64_t *ptr, gordon_strideSize_t *ss);
  72. #endif
  73. static struct starpu_data_interface_ops interface_block_ops =
  74. {
  75. .register_data_handle = register_block_handle,
  76. .allocate_data_on_node = allocate_block_buffer_on_node,
  77. .handle_to_pointer = block_handle_to_pointer,
  78. .free_data_on_node = free_block_buffer_on_node,
  79. .copy_methods = &block_copy_data_methods_s,
  80. .get_size = block_interface_get_size,
  81. .footprint = footprint_block_interface_crc32,
  82. .compare = block_compare,
  83. #ifdef STARPU_USE_GORDON
  84. .convert_to_gordon = convert_block_to_gordon,
  85. #endif
  86. .interfaceid = STARPU_BLOCK_INTERFACE_ID,
  87. .interface_size = sizeof(struct starpu_block_interface),
  88. .display = display_block_interface,
  89. };
  90. #ifdef STARPU_USE_GORDON
  91. int convert_block_to_gordon(void *data_interface, uint64_t *ptr, gordon_strideSize_t *ss)
  92. {
  93. /* TODO */
  94. STARPU_ABORT();
  95. return 0;
  96. }
  97. #endif
  98. static void *block_handle_to_pointer(starpu_data_handle_t handle, uint32_t node)
  99. {
  100. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  101. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  102. starpu_data_get_interface_on_node(handle, node);
  103. return (void*) block_interface->ptr;
  104. }
  105. static void register_block_handle(starpu_data_handle_t handle, uint32_t home_node, void *data_interface)
  106. {
  107. struct starpu_block_interface *block_interface = (struct starpu_block_interface *) data_interface;
  108. unsigned node;
  109. for (node = 0; node < STARPU_MAXNODES; node++)
  110. {
  111. struct starpu_block_interface *local_interface = (struct starpu_block_interface *)
  112. starpu_data_get_interface_on_node(handle, node);
  113. if (node == home_node)
  114. {
  115. local_interface->ptr = block_interface->ptr;
  116. local_interface->dev_handle = block_interface->dev_handle;
  117. local_interface->offset = block_interface->offset;
  118. local_interface->ldy = block_interface->ldy;
  119. local_interface->ldz = block_interface->ldz;
  120. }
  121. else
  122. {
  123. local_interface->ptr = 0;
  124. local_interface->dev_handle = 0;
  125. local_interface->offset = 0;
  126. local_interface->ldy = 0;
  127. local_interface->ldz = 0;
  128. }
  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, uint32_t 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. .ptr = ptr,
  143. .dev_handle = ptr,
  144. .offset = 0,
  145. .ldy = ldy,
  146. .ldz = ldz,
  147. .nx = nx,
  148. .ny = ny,
  149. .nz = nz,
  150. .elemsize = elemsize
  151. };
  152. starpu_data_register(handleptr, home_node, &block_interface, &interface_block_ops);
  153. }
  154. static uint32_t footprint_block_interface_crc32(starpu_data_handle_t handle)
  155. {
  156. uint32_t hash;
  157. hash = starpu_crc32_be(starpu_block_get_nx(handle), 0);
  158. hash = starpu_crc32_be(starpu_block_get_ny(handle), hash);
  159. hash = starpu_crc32_be(starpu_block_get_nz(handle), hash);
  160. return hash;
  161. }
  162. static int block_compare(void *data_interface_a, void *data_interface_b)
  163. {
  164. struct starpu_block_interface *block_a = (struct starpu_block_interface *) data_interface_a;
  165. struct starpu_block_interface *block_b = (struct starpu_block_interface *) data_interface_b;
  166. /* Two matricess are considered compatible if they have the same size */
  167. return ((block_a->nx == block_b->nx)
  168. && (block_a->ny == block_b->ny)
  169. && (block_a->nz == block_b->nz)
  170. && (block_a->elemsize == block_b->elemsize));
  171. }
  172. static void display_block_interface(starpu_data_handle_t handle, FILE *f)
  173. {
  174. struct starpu_block_interface *block_interface;
  175. block_interface = (struct starpu_block_interface *) starpu_data_get_interface_on_node(handle, 0);
  176. fprintf(f, "%u\t%u\t%u\t", block_interface->nx, block_interface->ny, block_interface->nz);
  177. }
  178. static size_t block_interface_get_size(starpu_data_handle_t handle)
  179. {
  180. size_t size;
  181. struct starpu_block_interface *block_interface;
  182. block_interface = (struct starpu_block_interface *) starpu_data_get_interface_on_node(handle, 0);
  183. size = block_interface->nx*block_interface->ny*block_interface->nz*block_interface->elemsize;
  184. return size;
  185. }
  186. /* offer an access to the data parameters */
  187. uint32_t starpu_block_get_nx(starpu_data_handle_t handle)
  188. {
  189. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  190. starpu_data_get_interface_on_node(handle, 0);
  191. return block_interface->nx;
  192. }
  193. uint32_t starpu_block_get_ny(starpu_data_handle_t handle)
  194. {
  195. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  196. starpu_data_get_interface_on_node(handle, 0);
  197. return block_interface->ny;
  198. }
  199. uint32_t starpu_block_get_nz(starpu_data_handle_t handle)
  200. {
  201. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  202. starpu_data_get_interface_on_node(handle, 0);
  203. return block_interface->nz;
  204. }
  205. uint32_t starpu_block_get_local_ldy(starpu_data_handle_t handle)
  206. {
  207. unsigned node;
  208. node = _starpu_get_local_memory_node();
  209. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  210. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  211. starpu_data_get_interface_on_node(handle, node);
  212. return block_interface->ldy;
  213. }
  214. uint32_t starpu_block_get_local_ldz(starpu_data_handle_t handle)
  215. {
  216. unsigned node;
  217. node = _starpu_get_local_memory_node();
  218. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  219. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  220. starpu_data_get_interface_on_node(handle, node);
  221. return block_interface->ldz;
  222. }
  223. uintptr_t starpu_block_get_local_ptr(starpu_data_handle_t handle)
  224. {
  225. unsigned node;
  226. node = _starpu_get_local_memory_node();
  227. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  228. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  229. starpu_data_get_interface_on_node(handle, node);
  230. return block_interface->ptr;
  231. }
  232. size_t starpu_block_get_elemsize(starpu_data_handle_t handle)
  233. {
  234. struct starpu_block_interface *block_interface = (struct starpu_block_interface *)
  235. starpu_data_get_interface_on_node(handle, 0);
  236. return block_interface->elemsize;
  237. }
  238. /* memory allocation/deallocation primitives for the BLOCK interface */
  239. /* returns the size of the allocated area */
  240. static ssize_t allocate_block_buffer_on_node(void *data_interface_, uint32_t dst_node)
  241. {
  242. uintptr_t addr = 0, handle;
  243. struct starpu_block_interface *dst_block = (struct starpu_block_interface *) data_interface_;
  244. uint32_t nx = dst_block->nx;
  245. uint32_t ny = dst_block->ny;
  246. uint32_t nz = dst_block->nz;
  247. size_t elemsize = dst_block->elemsize;
  248. ssize_t allocated_memory;
  249. handle = starpu_allocate_buffer_on_node(dst_node, nx*ny*nz*elemsize);
  250. if (!handle)
  251. return -ENOMEM;
  252. if (starpu_node_get_kind(dst_node) != STARPU_OPENCL_RAM)
  253. addr = handle;
  254. allocated_memory = nx*ny*nz*elemsize;
  255. /* update the data properly in consequence */
  256. dst_block->ptr = addr;
  257. dst_block->dev_handle = handle;
  258. dst_block->offset = 0;
  259. dst_block->ldy = nx;
  260. dst_block->ldz = nx*ny;
  261. return allocated_memory;
  262. }
  263. static void free_block_buffer_on_node(void *data_interface, uint32_t node)
  264. {
  265. struct starpu_block_interface *block_interface = (struct starpu_block_interface *) data_interface;
  266. uint32_t nx = block_interface->nx;
  267. uint32_t ny = block_interface->ny;
  268. uint32_t nz = block_interface->nz;
  269. size_t elemsize = block_interface->elemsize;
  270. starpu_free_buffer_on_node(node, block_interface->ptr, nx*ny*nz*elemsize);
  271. }
  272. #ifdef STARPU_USE_CUDA
  273. 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)
  274. {
  275. struct starpu_block_interface *src_block = src_interface;
  276. struct starpu_block_interface *dst_block = dst_interface;
  277. uint32_t nx = src_block->nx;
  278. uint32_t ny = src_block->ny;
  279. uint32_t nz = src_block->nz;
  280. size_t elemsize = src_block->elemsize;
  281. cudaError_t cures;
  282. if ((nx == src_block->ldy) && (src_block->ldy == dst_block->ldy))
  283. {
  284. /* Is that a single contiguous buffer ? */
  285. if (((nx*ny) == src_block->ldz) && (src_block->ldz == dst_block->ldz))
  286. {
  287. starpu_cuda_copy_async_sync((void *)src_block->ptr, src_node, (void *)dst_block->ptr, dst_node, nx*ny*nz*elemsize, NULL, kind);
  288. }
  289. else
  290. {
  291. /* Are all plans contiguous */
  292. cures = cudaMemcpy2D((char *)dst_block->ptr, dst_block->ldz*elemsize,
  293. (char *)src_block->ptr, src_block->ldz*elemsize,
  294. nx*ny*elemsize, nz, kind);
  295. if (STARPU_UNLIKELY(cures))
  296. STARPU_CUDA_REPORT_ERROR(cures);
  297. }
  298. }
  299. else
  300. {
  301. /* Default case: we transfer all lines one by one: ny*nz transfers */
  302. unsigned layer;
  303. for (layer = 0; layer < src_block->nz; layer++)
  304. {
  305. uint8_t *src_ptr = ((uint8_t *)src_block->ptr) + layer*src_block->ldz*src_block->elemsize;
  306. uint8_t *dst_ptr = ((uint8_t *)dst_block->ptr) + layer*dst_block->ldz*dst_block->elemsize;
  307. cures = cudaMemcpy2D((char *)dst_ptr, dst_block->ldy*elemsize,
  308. (char *)src_ptr, src_block->ldy*elemsize,
  309. nx*elemsize, ny, kind);
  310. if (STARPU_UNLIKELY(cures))
  311. STARPU_CUDA_REPORT_ERROR(cures);
  312. }
  313. }
  314. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_block->nx*src_block->ny*src_block->elemsize*src_block->elemsize);
  315. return 0;
  316. }
  317. 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)
  318. {
  319. struct starpu_block_interface *src_block = src_interface;
  320. struct starpu_block_interface *dst_block = dst_interface;
  321. uint32_t nx = src_block->nx;
  322. uint32_t ny = src_block->ny;
  323. uint32_t nz = src_block->nz;
  324. size_t elemsize = src_block->elemsize;
  325. cudaError_t cures;
  326. int ret;
  327. /* We may have a contiguous buffer for the entire block, or contiguous
  328. * plans within the block, we can avoid many small transfers that way */
  329. if ((nx == src_block->ldy) && (src_block->ldy == dst_block->ldy))
  330. {
  331. /* Is that a single contiguous buffer ? */
  332. if (((nx*ny) == src_block->ldz) && (src_block->ldz == dst_block->ldz))
  333. {
  334. ret = starpu_cuda_copy_async_sync((void *)src_block->ptr, src_node, (void *)dst_block->ptr, dst_node, nx*ny*nz*elemsize, stream, kind);
  335. }
  336. else
  337. {
  338. /* Are all plans contiguous */
  339. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  340. cures = cudaMemcpy2DAsync((char *)dst_block->ptr, dst_block->ldz*elemsize,
  341. (char *)src_block->ptr, src_block->ldz*elemsize,
  342. nx*ny*elemsize, nz, kind, stream);
  343. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  344. if (STARPU_UNLIKELY(cures))
  345. {
  346. cures = cudaMemcpy2D((char *)dst_block->ptr, dst_block->ldz*elemsize,
  347. (char *)src_block->ptr, src_block->ldz*elemsize,
  348. nx*ny*elemsize, nz, kind);
  349. if (STARPU_UNLIKELY(cures))
  350. STARPU_CUDA_REPORT_ERROR(cures);
  351. ret = 0;
  352. }
  353. else
  354. {
  355. ret = -EAGAIN;
  356. }
  357. }
  358. }
  359. else
  360. {
  361. /* Default case: we transfer all lines one by one: ny*nz transfers */
  362. unsigned layer;
  363. for (layer = 0; layer < src_block->nz; layer++)
  364. {
  365. uint8_t *src_ptr = ((uint8_t *)src_block->ptr) + layer*src_block->ldz*src_block->elemsize;
  366. uint8_t *dst_ptr = ((uint8_t *)dst_block->ptr) + layer*dst_block->ldz*dst_block->elemsize;
  367. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  368. cures = cudaMemcpy2DAsync((char *)dst_ptr, dst_block->ldy*elemsize,
  369. (char *)src_ptr, src_block->ldy*elemsize,
  370. nx*elemsize, ny, kind, stream);
  371. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  372. if (STARPU_UNLIKELY(cures))
  373. {
  374. /* I don't know how to do that "better" */
  375. goto no_async_default;
  376. }
  377. }
  378. ret = -EAGAIN;
  379. }
  380. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_block->nx*src_block->ny*src_block->nz*src_block->elemsize);
  381. return ret;
  382. no_async_default:
  383. {
  384. unsigned layer;
  385. for (layer = 0; layer < src_block->nz; layer++)
  386. {
  387. uint8_t *src_ptr = ((uint8_t *)src_block->ptr) + layer*src_block->ldz*src_block->elemsize;
  388. uint8_t *dst_ptr = ((uint8_t *)dst_block->ptr) + layer*dst_block->ldz*dst_block->elemsize;
  389. cures = cudaMemcpy2D((char *)dst_ptr, dst_block->ldy*elemsize,
  390. (char *)src_ptr, src_block->ldy*elemsize,
  391. nx*elemsize, ny, kind);
  392. if (STARPU_UNLIKELY(cures))
  393. STARPU_CUDA_REPORT_ERROR(cures);
  394. }
  395. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_block->nx*src_block->ny*src_block->nz*src_block->elemsize);
  396. return 0;
  397. }
  398. }
  399. static int copy_cuda_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node)
  400. {
  401. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost);
  402. }
  403. static int copy_ram_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  404. {
  405. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice);
  406. }
  407. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  408. {
  409. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToDevice);
  410. }
  411. 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)
  412. {
  413. return copy_cuda_async_common(src_interface, src_node, dst_interface, dst_node, stream, cudaMemcpyDeviceToHost);
  414. }
  415. 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)
  416. {
  417. return copy_cuda_async_common(src_interface, src_node, dst_interface, dst_node, stream, cudaMemcpyHostToDevice);
  418. }
  419. #endif // STARPU_USE_CUDA
  420. #ifdef STARPU_USE_OPENCL
  421. 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)
  422. {
  423. struct starpu_block_interface *src_block = src_interface;
  424. struct starpu_block_interface *dst_block = dst_interface;
  425. int err, ret = 0;
  426. uint32_t nx = src_block->nx;
  427. uint32_t ny = src_block->ny;
  428. /* We may have a contiguous buffer for the entire block, or contiguous
  429. * plans within the block, we can avoid many small transfers that way */
  430. if ((nx == src_block->ldy) && (src_block->ldy == dst_block->ldy))
  431. {
  432. /* Is that a single contiguous buffer ? */
  433. if (((nx*ny) == src_block->ldz) && (src_block->ldz == dst_block->ldz))
  434. {
  435. err = starpu_opencl_copy_ram_to_opencl((void*)src_block->ptr, src_node, (cl_mem)dst_block->dev_handle, dst_node,
  436. src_block->nx*src_block->ny*src_block->nz*src_block->elemsize,
  437. dst_block->offset, event, &ret);
  438. if (STARPU_UNLIKELY(err))
  439. STARPU_OPENCL_REPORT_ERROR(err);
  440. }
  441. else
  442. {
  443. /* Are all plans contiguous */
  444. STARPU_ASSERT_MSG(0, "XXX non contiguous buffers are not properly supported in OpenCL yet. (TODO)");
  445. }
  446. }
  447. else
  448. {
  449. /* Default case: we transfer all lines one by one: ny*nz transfers */
  450. unsigned layer;
  451. for (layer = 0; layer < src_block->nz; layer++)
  452. {
  453. unsigned j;
  454. for(j=0 ; j<src_block->ny ; j++)
  455. {
  456. void *ptr = (void*)src_block->ptr+(layer*src_block->ldz*src_block->elemsize)+(j*src_block->ldy*src_block->elemsize);
  457. err = starpu_opencl_copy_ram_to_opencl(ptr, src_node, (cl_mem)dst_block->dev_handle, dst_node,
  458. src_block->nx*src_block->elemsize,
  459. layer*dst_block->ldz*dst_block->elemsize + j*dst_block->ldy*dst_block->elemsize
  460. + dst_block->offset, NULL, NULL);
  461. if (STARPU_UNLIKELY(err))
  462. STARPU_OPENCL_REPORT_ERROR(err);
  463. }
  464. // int *foo = (int *)(src_block->ptr+(layer*src_block->ldz*src_block->elemsize));
  465. // fprintf(stderr, "layer %d --> value %d\n", layer, foo[1]);
  466. // const size_t buffer_origin[3] = {layer*src_block->ldz*src_block->elemsize, 0, 0};
  467. // //const size_t buffer_origin[3] = {0, 0, 0};
  468. // const size_t host_origin[3] = {layer*dst_block->ldz*dst_block->elemsize+dst_block->offset, 0, 0};
  469. // size_t region[3] = {src_block->nx*src_block->elemsize,src_block->ny, 1};
  470. // size_t buffer_row_pitch=region[0];
  471. // size_t buffer_slice_pitch=region[1] * buffer_row_pitch;
  472. // size_t host_row_pitch=region[0];
  473. // size_t host_slice_pitch=region[1] * host_row_pitch;
  474. //
  475. // _starpu_opencl_copy_rect_ram_to_opencl((void *)src_block->ptr, src_node, (cl_mem)dst_block->dev_handle, dst_node,
  476. // buffer_origin, host_origin, region,
  477. // buffer_row_pitch, buffer_slice_pitch,
  478. // host_row_pitch, host_slice_pitch, NULL);
  479. }
  480. }
  481. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_block->nx*src_block->ny*src_block->nz*src_block->elemsize);
  482. return ret;
  483. }
  484. 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)
  485. {
  486. struct starpu_block_interface *src_block = src_interface;
  487. struct starpu_block_interface *dst_block = dst_interface;
  488. int err, ret = 0;
  489. /* We may have a contiguous buffer for the entire block, or contiguous
  490. * plans within the block, we can avoid many small transfers that way */
  491. if ((src_block->nx == src_block->ldy) && (src_block->ldy == dst_block->ldy))
  492. {
  493. /* Is that a single contiguous buffer ? */
  494. if (((src_block->nx*src_block->ny) == src_block->ldz) && (src_block->ldz == dst_block->ldz))
  495. {
  496. err = starpu_opencl_copy_opencl_to_ram((cl_mem)src_block->dev_handle, src_node, (void*)dst_block->ptr, dst_node,
  497. src_block->nx*src_block->ny*src_block->nz*src_block->elemsize,
  498. src_block->offset, event, &ret);
  499. if (STARPU_UNLIKELY(err))
  500. STARPU_OPENCL_REPORT_ERROR(err);
  501. }
  502. else
  503. {
  504. /* Are all plans contiguous */
  505. STARPU_ASSERT_MSG(0, "XXX non contiguous buffers are not properly supported in OpenCL yet. (TODO)");
  506. }
  507. }
  508. else
  509. {
  510. /* Default case: we transfer all lines one by one: ny*nz transfers */
  511. /* XXX non contiguous buffers are not properly supported yet. (TODO) */
  512. unsigned layer;
  513. for (layer = 0; layer < src_block->nz; layer++)
  514. {
  515. unsigned j;
  516. for(j=0 ; j<src_block->ny ; j++)
  517. {
  518. void *ptr = (void *)dst_block->ptr+(layer*dst_block->ldz*dst_block->elemsize)+(j*dst_block->ldy*dst_block->elemsize);
  519. err = starpu_opencl_copy_opencl_to_ram((void*)src_block->dev_handle, src_node, ptr, dst_node,
  520. src_block->nx*src_block->elemsize,
  521. layer*src_block->ldz*src_block->elemsize+j*src_block->ldy*src_block->elemsize+
  522. src_block->offset, NULL, NULL);
  523. if (STARPU_UNLIKELY(err))
  524. STARPU_OPENCL_REPORT_ERROR(err);
  525. }
  526. // const size_t buffer_origin[3] = {src_block->offset, 0, 0};
  527. // const size_t host_origin[3] = {layer*src_block->ldz*src_block->elemsize, 0, 0};
  528. // size_t region[3] = {src_block->nx*src_block->elemsize,src_block->ny, 1};
  529. // size_t buffer_row_pitch=region[0];
  530. // size_t buffer_slice_pitch=region[1] * buffer_row_pitch;
  531. // size_t host_row_pitch=region[0];
  532. // size_t host_slice_pitch=region[1] * host_row_pitch;
  533. //
  534. // _starpu_opencl_copy_rect_opencl_to_ram((cl_mem)src_block->dev_handle, src_node, (void *)dst_block->ptr, dst_node,
  535. // buffer_origin, host_origin, region,
  536. // buffer_row_pitch, buffer_slice_pitch,
  537. // host_row_pitch, host_slice_pitch, NULL);
  538. }
  539. }
  540. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_block->nx*src_block->ny*src_block->nz*src_block->elemsize);
  541. return ret;
  542. }
  543. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  544. {
  545. return copy_ram_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  546. }
  547. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  548. {
  549. return copy_opencl_to_ram_async(src_interface, src_node, dst_interface, dst_node, NULL);
  550. }
  551. #endif
  552. /* as not all platform easily have a BLAS lib installed ... */
  553. static int copy_ram_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  554. {
  555. struct starpu_block_interface *src_block = (struct starpu_block_interface *) src_interface;
  556. struct starpu_block_interface *dst_block = (struct starpu_block_interface *) dst_interface;
  557. uint32_t nx = dst_block->nx;
  558. uint32_t ny = dst_block->ny;
  559. uint32_t nz = dst_block->nz;
  560. size_t elemsize = dst_block->elemsize;
  561. uint32_t ldy_src = src_block->ldy;
  562. uint32_t ldz_src = src_block->ldz;
  563. uint32_t ldy_dst = dst_block->ldy;
  564. uint32_t ldz_dst = dst_block->ldz;
  565. uintptr_t ptr_src = src_block->ptr;
  566. uintptr_t ptr_dst = dst_block->ptr;
  567. unsigned y, z;
  568. for (z = 0; z < nz; z++)
  569. {
  570. for (y = 0; y < ny; y++)
  571. {
  572. uint32_t src_offset = (y*ldy_src + z*ldz_src)*elemsize;
  573. uint32_t dst_offset = (y*ldy_dst + z*ldz_dst)*elemsize;
  574. memcpy((void *)(ptr_dst + dst_offset),
  575. (void *)(ptr_src + src_offset), nx*elemsize);
  576. }
  577. }
  578. _STARPU_TRACE_DATA_COPY(src_node, dst_node, nx*ny*nz*elemsize);
  579. return 0;
  580. }