matrix_interface.c 23 KB

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