matrix_interface.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2017 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2016, 2017 CNRS
  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 <datawizard/memory_nodes.h>
  23. #include <datawizard/malloc.h>
  24. #include <starpu_hash.h>
  25. #include <starpu_cuda.h>
  26. #include <starpu_opencl.h>
  27. #include <drivers/opencl/driver_opencl.h>
  28. #include <drivers/scc/driver_scc_source.h>
  29. #include <drivers/mic/driver_mic_source.h>
  30. #ifdef STARPU_USE_CUDA
  31. /* At least CUDA 4.2 still didn't have working memcpy3D */
  32. #if CUDART_VERSION < 5000
  33. #define BUGGED_MEMCPY3D
  34. #endif
  35. static int copy_ram_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  36. static int copy_cuda_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  37. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  38. 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);
  39. 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);
  40. #ifndef BUGGED_MEMCPY3D
  41. static int copy_cuda_to_cuda_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cudaStream_t stream);
  42. #endif
  43. #endif
  44. #ifdef STARPU_USE_OPENCL
  45. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  46. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  47. static int copy_opencl_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED);
  48. 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);
  49. 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);
  50. 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);
  51. #endif
  52. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data);
  53. static const struct starpu_data_copy_methods matrix_copy_data_methods_s =
  54. {
  55. #ifdef STARPU_USE_CUDA
  56. .ram_to_cuda = copy_ram_to_cuda,
  57. .cuda_to_ram = copy_cuda_to_ram,
  58. .ram_to_cuda_async = copy_ram_to_cuda_async,
  59. .cuda_to_ram_async = copy_cuda_to_ram_async,
  60. .cuda_to_cuda = copy_cuda_to_cuda,
  61. #ifndef BUGGED_MEMCPY3D
  62. .cuda_to_cuda_async = copy_cuda_to_cuda_async,
  63. #endif
  64. #else
  65. #ifdef STARPU_SIMGRID
  66. #ifndef BUGGED_MEMCPY3D
  67. /* Enable GPU-GPU transfers in simgrid */
  68. .cuda_to_cuda_async = (void *)1,
  69. #endif
  70. #endif
  71. #endif
  72. #ifdef STARPU_USE_OPENCL
  73. .ram_to_opencl = copy_ram_to_opencl,
  74. .opencl_to_ram = copy_opencl_to_ram,
  75. .opencl_to_opencl = copy_opencl_to_opencl,
  76. .ram_to_opencl_async = copy_ram_to_opencl_async,
  77. .opencl_to_ram_async = copy_opencl_to_ram_async,
  78. .opencl_to_opencl_async = copy_opencl_to_opencl_async,
  79. #endif
  80. .any_to_any = copy_any_to_any,
  81. };
  82. static void register_matrix_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  83. static void *matrix_handle_to_pointer(starpu_data_handle_t data_handle, unsigned node);
  84. static starpu_ssize_t allocate_matrix_buffer_on_node(void *data_interface_, unsigned dst_node);
  85. static void free_matrix_buffer_on_node(void *data_interface, unsigned node);
  86. static size_t matrix_interface_get_size(starpu_data_handle_t handle);
  87. static uint32_t footprint_matrix_interface_crc32(starpu_data_handle_t handle);
  88. static int matrix_compare(void *data_interface_a, void *data_interface_b);
  89. static void display_matrix_interface(starpu_data_handle_t handle, FILE *f);
  90. static int pack_matrix_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count);
  91. static int unpack_matrix_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count);
  92. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size);
  93. struct starpu_data_interface_ops starpu_interface_matrix_ops =
  94. {
  95. .register_data_handle = register_matrix_handle,
  96. .allocate_data_on_node = allocate_matrix_buffer_on_node,
  97. .handle_to_pointer = matrix_handle_to_pointer,
  98. .free_data_on_node = free_matrix_buffer_on_node,
  99. .copy_methods = &matrix_copy_data_methods_s,
  100. .get_size = matrix_interface_get_size,
  101. .footprint = footprint_matrix_interface_crc32,
  102. .compare = matrix_compare,
  103. .interfaceid = STARPU_MATRIX_INTERFACE_ID,
  104. .interface_size = sizeof(struct starpu_matrix_interface),
  105. .display = display_matrix_interface,
  106. .pack_data = pack_matrix_handle,
  107. .unpack_data = unpack_matrix_handle,
  108. .describe = describe,
  109. .name = "STARPU_MATRIX_INTERFACE"
  110. };
  111. static void register_matrix_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  112. {
  113. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *) data_interface;
  114. unsigned node;
  115. for (node = 0; node < STARPU_MAXNODES; node++)
  116. {
  117. struct starpu_matrix_interface *local_interface = (struct starpu_matrix_interface *)
  118. starpu_data_get_interface_on_node(handle, node);
  119. if (node == home_node)
  120. {
  121. local_interface->ptr = matrix_interface->ptr;
  122. local_interface->dev_handle = matrix_interface->dev_handle;
  123. local_interface->offset = matrix_interface->offset;
  124. local_interface->ld = matrix_interface->ld;
  125. }
  126. else
  127. {
  128. local_interface->ptr = 0;
  129. local_interface->dev_handle = 0;
  130. local_interface->offset = 0;
  131. local_interface->ld = 0;
  132. }
  133. local_interface->id = matrix_interface->id;
  134. local_interface->nx = matrix_interface->nx;
  135. local_interface->ny = matrix_interface->ny;
  136. local_interface->elemsize = matrix_interface->elemsize;
  137. }
  138. }
  139. static void *matrix_handle_to_pointer(starpu_data_handle_t handle, unsigned node)
  140. {
  141. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  142. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  143. starpu_data_get_interface_on_node(handle, node);
  144. return (void*) matrix_interface->ptr;
  145. }
  146. /* declare a new data with the matrix interface */
  147. void starpu_matrix_data_register(starpu_data_handle_t *handleptr, int home_node,
  148. uintptr_t ptr, uint32_t ld, uint32_t nx,
  149. uint32_t ny, size_t elemsize)
  150. {
  151. struct starpu_matrix_interface matrix_interface =
  152. {
  153. .id = STARPU_MATRIX_INTERFACE_ID,
  154. .ptr = ptr,
  155. .ld = ld,
  156. .nx = nx,
  157. .ny = ny,
  158. .elemsize = elemsize,
  159. .dev_handle = ptr,
  160. .offset = 0
  161. };
  162. #ifndef STARPU_SIMGRID
  163. if (home_node >= 0 && starpu_node_get_kind(home_node) == STARPU_CPU_RAM)
  164. {
  165. STARPU_ASSERT_ACCESSIBLE(ptr);
  166. STARPU_ASSERT_ACCESSIBLE(ptr + (ny-1)*ld*elemsize + nx*elemsize - 1);
  167. }
  168. #endif
  169. #ifdef STARPU_USE_SCC
  170. _starpu_scc_set_offset_in_shared_memory((void*)matrix_interface.ptr,
  171. (void**)&(matrix_interface.dev_handle), &(matrix_interface.offset));
  172. #endif
  173. starpu_data_register(handleptr, home_node, &matrix_interface, &starpu_interface_matrix_ops);
  174. }
  175. void starpu_matrix_ptr_register(starpu_data_handle_t handle, unsigned node,
  176. uintptr_t ptr, uintptr_t dev_handle, size_t offset, uint32_t ld)
  177. {
  178. struct starpu_matrix_interface *matrix_interface = starpu_data_get_interface_on_node(handle, node);
  179. starpu_data_ptr_register(handle, node);
  180. matrix_interface->ptr = ptr;
  181. matrix_interface->dev_handle = dev_handle;
  182. matrix_interface->offset = offset;
  183. matrix_interface->ld = ld;
  184. }
  185. static uint32_t footprint_matrix_interface_crc32(starpu_data_handle_t handle)
  186. {
  187. return starpu_hash_crc32c_be(starpu_matrix_get_nx(handle), starpu_matrix_get_ny(handle));
  188. }
  189. static int matrix_compare(void *data_interface_a, void *data_interface_b)
  190. {
  191. struct starpu_matrix_interface *matrix_a = (struct starpu_matrix_interface *) data_interface_a;
  192. struct starpu_matrix_interface *matrix_b = (struct starpu_matrix_interface *) data_interface_b;
  193. /* Two matricess are considered compatible if they have the same size */
  194. return (matrix_a->nx == matrix_b->nx)
  195. && (matrix_a->ny == matrix_b->ny)
  196. && (matrix_a->elemsize == matrix_b->elemsize);
  197. }
  198. static void display_matrix_interface(starpu_data_handle_t handle, FILE *f)
  199. {
  200. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  201. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  202. fprintf(f, "%u\t%u\t", matrix_interface->nx, matrix_interface->ny);
  203. }
  204. static int pack_matrix_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count)
  205. {
  206. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  207. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  208. starpu_data_get_interface_on_node(handle, node);
  209. *count = matrix_interface->nx*matrix_interface->ny*matrix_interface->elemsize;
  210. if (ptr != NULL)
  211. {
  212. uint32_t y;
  213. char *matrix = (void *)matrix_interface->ptr;
  214. _starpu_malloc_flags_on_node(node, ptr, *count, 0);
  215. char *cur = *ptr;
  216. for(y=0 ; y<matrix_interface->ny ; y++)
  217. {
  218. memcpy(cur, matrix, matrix_interface->nx*matrix_interface->elemsize);
  219. cur += matrix_interface->nx*matrix_interface->elemsize;
  220. matrix += matrix_interface->ld * matrix_interface->elemsize;
  221. }
  222. }
  223. return 0;
  224. }
  225. static int unpack_matrix_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  226. {
  227. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  228. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  229. starpu_data_get_interface_on_node(handle, node);
  230. STARPU_ASSERT(count == matrix_interface->elemsize * matrix_interface->nx * matrix_interface->ny);
  231. uint32_t y;
  232. char *cur = ptr;
  233. char *matrix = (void *)matrix_interface->ptr;
  234. for(y=0 ; y<matrix_interface->ny ; y++)
  235. {
  236. memcpy(matrix, cur, matrix_interface->nx*matrix_interface->elemsize);
  237. cur += matrix_interface->nx*matrix_interface->elemsize;
  238. matrix += matrix_interface->ld * matrix_interface->elemsize;
  239. }
  240. return 0;
  241. }
  242. static size_t matrix_interface_get_size(starpu_data_handle_t handle)
  243. {
  244. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  245. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  246. #ifdef STARPU_DEBUG
  247. STARPU_ASSERT_MSG(matrix_interface->id == STARPU_MATRIX_INTERFACE_ID, "Error. The given data is not a matrix.");
  248. #endif
  249. size_t size;
  250. size = (size_t)matrix_interface->nx*matrix_interface->ny*matrix_interface->elemsize;
  251. return size;
  252. }
  253. /* offer an access to the data parameters */
  254. uint32_t starpu_matrix_get_nx(starpu_data_handle_t handle)
  255. {
  256. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  257. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  258. #ifdef STARPU_DEBUG
  259. STARPU_ASSERT_MSG(matrix_interface->id == STARPU_MATRIX_INTERFACE_ID, "Error. The given data is not a matrix.");
  260. #endif
  261. return matrix_interface->nx;
  262. }
  263. uint32_t starpu_matrix_get_ny(starpu_data_handle_t handle)
  264. {
  265. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  266. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  267. #ifdef STARPU_DEBUG
  268. STARPU_ASSERT_MSG(matrix_interface->id == STARPU_MATRIX_INTERFACE_ID, "Error. The given data is not a matrix.");
  269. #endif
  270. return matrix_interface->ny;
  271. }
  272. uint32_t starpu_matrix_get_local_ld(starpu_data_handle_t handle)
  273. {
  274. unsigned node;
  275. node = _starpu_memory_node_get_local_key();
  276. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  277. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  278. starpu_data_get_interface_on_node(handle, node);
  279. #ifdef STARPU_DEBUG
  280. STARPU_ASSERT_MSG(matrix_interface->id == STARPU_MATRIX_INTERFACE_ID, "Error. The given data is not a matrix.");
  281. #endif
  282. return matrix_interface->ld;
  283. }
  284. uintptr_t starpu_matrix_get_local_ptr(starpu_data_handle_t handle)
  285. {
  286. unsigned node;
  287. node = _starpu_memory_node_get_local_key();
  288. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  289. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  290. starpu_data_get_interface_on_node(handle, node);
  291. #ifdef STARPU_DEBUG
  292. STARPU_ASSERT_MSG(matrix_interface->id == STARPU_MATRIX_INTERFACE_ID, "Error. The given data is not a matrix.");
  293. #endif
  294. return matrix_interface->ptr;
  295. }
  296. size_t starpu_matrix_get_elemsize(starpu_data_handle_t handle)
  297. {
  298. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  299. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  300. #ifdef STARPU_DEBUG
  301. STARPU_ASSERT_MSG(matrix_interface->id == STARPU_MATRIX_INTERFACE_ID, "Error. The given data is not a matrix.");
  302. #endif
  303. return matrix_interface->elemsize;
  304. }
  305. /* memory allocation/deallocation primitives for the matrix interface */
  306. /* returns the size of the allocated area */
  307. static starpu_ssize_t allocate_matrix_buffer_on_node(void *data_interface_, unsigned dst_node)
  308. {
  309. uintptr_t addr = 0, handle;
  310. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *) data_interface_;
  311. uint32_t nx = matrix_interface->nx;
  312. uint32_t ny = matrix_interface->ny;
  313. uint32_t ld = nx; // by default
  314. size_t elemsize = matrix_interface->elemsize;
  315. starpu_ssize_t allocated_memory;
  316. handle = starpu_malloc_on_node(dst_node, nx*ny*elemsize);
  317. if (!handle)
  318. return -ENOMEM;
  319. if (starpu_node_get_kind(dst_node) != STARPU_OPENCL_RAM)
  320. addr = handle;
  321. allocated_memory = (size_t)nx*ny*elemsize;
  322. /* update the data properly in consequence */
  323. matrix_interface->ptr = addr;
  324. matrix_interface->dev_handle = handle;
  325. matrix_interface->offset = 0;
  326. matrix_interface->ld = ld;
  327. return allocated_memory;
  328. }
  329. static void free_matrix_buffer_on_node(void *data_interface, unsigned node)
  330. {
  331. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *) data_interface;
  332. uint32_t nx = matrix_interface->nx;
  333. uint32_t ny = matrix_interface->ny;
  334. size_t elemsize = matrix_interface->elemsize;
  335. starpu_free_on_node(node, matrix_interface->dev_handle, nx*ny*elemsize);
  336. }
  337. #ifdef STARPU_USE_CUDA
  338. 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, int is_async, cudaStream_t stream)
  339. {
  340. struct starpu_matrix_interface *src_matrix = src_interface;
  341. struct starpu_matrix_interface *dst_matrix = dst_interface;
  342. size_t elemsize = src_matrix->elemsize;
  343. cudaError_t cures;
  344. if (is_async)
  345. {
  346. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  347. cures = cudaMemcpy2DAsync((char *)dst_matrix->ptr, dst_matrix->ld*elemsize,
  348. (char *)src_matrix->ptr, src_matrix->ld*elemsize,
  349. src_matrix->nx*elemsize, src_matrix->ny, kind, stream);
  350. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  351. if (!cures)
  352. return -EAGAIN;
  353. }
  354. cures = cudaMemcpy2D((char *)dst_matrix->ptr, dst_matrix->ld*elemsize,
  355. (char *)src_matrix->ptr, src_matrix->ld*elemsize,
  356. src_matrix->nx*elemsize, src_matrix->ny, kind);
  357. if (STARPU_UNLIKELY(cures))
  358. {
  359. int ret = copy_any_to_any(src_interface, src_node, dst_interface, dst_node, (void*)(uintptr_t)is_async);
  360. if (ret == -EAGAIN) return ret;
  361. if (ret) STARPU_CUDA_REPORT_ERROR(cures);
  362. }
  363. _STARPU_TRACE_DATA_COPY(src_node, dst_node, (size_t)src_matrix->nx*src_matrix->ny*src_matrix->elemsize);
  364. return 0;
  365. }
  366. #ifndef BUGGED_MEMCPY3D
  367. static int copy_cuda_peer(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, int is_async, cudaStream_t stream)
  368. {
  369. #ifdef HAVE_CUDA_MEMCPY_PEER
  370. struct starpu_matrix_interface *src_matrix = src_interface;
  371. struct starpu_matrix_interface *dst_matrix = dst_interface;
  372. size_t elemsize = src_matrix->elemsize;
  373. cudaError_t cures;
  374. int src_dev = _starpu_memory_node_get_devid(src_node);
  375. int dst_dev = _starpu_memory_node_get_devid(dst_node);
  376. struct cudaMemcpy3DPeerParms p;
  377. memset(&p, 0, sizeof(p));
  378. p.srcDevice = src_dev;
  379. p.dstDevice = dst_dev;
  380. p.srcPtr = make_cudaPitchedPtr((char *)src_matrix->ptr, src_matrix->ld * elemsize, src_matrix->nx, src_matrix->ny);
  381. p.dstPtr = make_cudaPitchedPtr((char *)dst_matrix->ptr, dst_matrix->ld * elemsize, dst_matrix->nx, dst_matrix->ny);
  382. p.extent = make_cudaExtent(src_matrix->nx * elemsize, src_matrix->ny, 1);
  383. if (is_async)
  384. {
  385. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  386. cures = cudaMemcpy3DPeerAsync(&p, stream);
  387. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  388. if (!cures)
  389. return -EAGAIN;
  390. }
  391. cures = cudaMemcpy3DPeer(&p);
  392. if (STARPU_UNLIKELY(cures))
  393. STARPU_CUDA_REPORT_ERROR(cures);
  394. _STARPU_TRACE_DATA_COPY(src_node, dst_node, (size_t)src_matrix->nx*src_matrix->ny*src_matrix->elemsize);
  395. return 0;
  396. #else
  397. STARPU_ABORT_MSG("CUDA memcpy 3D peer not available, but core triggered one ?!");
  398. #endif
  399. }
  400. #endif
  401. static int copy_cuda_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  402. {
  403. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost, 0, 0);
  404. }
  405. static int copy_ram_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  406. {
  407. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice, 0, 0);
  408. }
  409. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  410. {
  411. if (src_node == dst_node)
  412. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToDevice, 0, 0);
  413. else
  414. #ifdef BUGGED_MEMCPY3D
  415. STARPU_ABORT_MSG("CUDA memcpy 3D peer not available, but core triggered one?!");
  416. #else
  417. return copy_cuda_peer(src_interface, src_node, dst_interface, dst_node, 0, 0);
  418. #endif
  419. }
  420. 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)
  421. {
  422. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost, 1, stream);
  423. }
  424. 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)
  425. {
  426. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice, 1, stream);
  427. }
  428. #ifndef BUGGED_MEMCPY3D
  429. static int copy_cuda_to_cuda_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cudaStream_t stream)
  430. {
  431. if (src_node == dst_node)
  432. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToDevice, 1, stream);
  433. else
  434. return copy_cuda_peer(src_interface, src_node, dst_interface, dst_node, 1, stream);
  435. }
  436. #endif
  437. #endif // STARPU_USE_CUDA
  438. #ifdef STARPU_USE_OPENCL
  439. static int copy_opencl_common(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  440. {
  441. struct starpu_matrix_interface *src_matrix = src_interface;
  442. struct starpu_matrix_interface *dst_matrix = dst_interface;
  443. int ret;
  444. STARPU_ASSERT_MSG((src_matrix->ld == src_matrix->nx) && (dst_matrix->ld == dst_matrix->nx), "XXX non contiguous buffers are not properly supported in OpenCL yet. (TODO)");
  445. ret = starpu_opencl_copy_async_sync(src_matrix->dev_handle, src_matrix->offset, src_node,
  446. dst_matrix->dev_handle, dst_matrix->offset, dst_node,
  447. src_matrix->nx*src_matrix->ny*src_matrix->elemsize,
  448. event);
  449. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_matrix->nx*src_matrix->ny*src_matrix->elemsize);
  450. return ret;
  451. }
  452. static int copy_ram_to_opencl_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  453. {
  454. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  455. }
  456. static int copy_opencl_to_ram_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  457. {
  458. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  459. }
  460. static int copy_opencl_to_opencl_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  461. {
  462. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  463. }
  464. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  465. {
  466. return copy_ram_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  467. }
  468. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  469. {
  470. return copy_opencl_to_ram_async(src_interface, src_node, dst_interface, dst_node, NULL);
  471. }
  472. static int copy_opencl_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  473. {
  474. return copy_opencl_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  475. }
  476. #endif
  477. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data)
  478. {
  479. struct starpu_matrix_interface *src_matrix = (struct starpu_matrix_interface *) src_interface;
  480. struct starpu_matrix_interface *dst_matrix = (struct starpu_matrix_interface *) dst_interface;
  481. int ret = 0;
  482. uint32_t nx = dst_matrix->nx;
  483. uint32_t ny = dst_matrix->ny;
  484. size_t elemsize = dst_matrix->elemsize;
  485. uint32_t ld_src = src_matrix->ld;
  486. uint32_t ld_dst = dst_matrix->ld;
  487. if (ld_src == nx && ld_dst == nx)
  488. {
  489. /* Optimize unpartitioned and y-partitioned cases */
  490. if (starpu_interface_copy(src_matrix->dev_handle, src_matrix->offset, src_node,
  491. dst_matrix->dev_handle, dst_matrix->offset, dst_node,
  492. nx*ny*elemsize, async_data))
  493. ret = -EAGAIN;
  494. }
  495. else
  496. {
  497. unsigned y;
  498. for (y = 0; y < ny; y++)
  499. {
  500. uint32_t src_offset = y*ld_src*elemsize;
  501. uint32_t dst_offset = y*ld_dst*elemsize;
  502. if (starpu_interface_copy(src_matrix->dev_handle, src_matrix->offset + src_offset, src_node,
  503. dst_matrix->dev_handle, dst_matrix->offset + dst_offset, dst_node,
  504. nx*elemsize, async_data))
  505. ret = -EAGAIN;
  506. }
  507. }
  508. _STARPU_TRACE_DATA_COPY(src_node, dst_node, (size_t)nx*ny*elemsize);
  509. return ret;
  510. }
  511. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size)
  512. {
  513. struct starpu_matrix_interface *matrix = (struct starpu_matrix_interface *) data_interface;
  514. return snprintf(buf, size, "M%ux%ux%u",
  515. (unsigned) matrix->nx,
  516. (unsigned) matrix->ny,
  517. (unsigned) matrix->elemsize);
  518. }