matrix_interface.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2016 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, int 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. #ifndef STARPU_SIMGRID
  161. if (home_node == STARPU_MAIN_RAM)
  162. {
  163. STARPU_ASSERT_ACCESSIBLE(ptr);
  164. STARPU_ASSERT_ACCESSIBLE(ptr + (ny-1)*ld*elemsize + nx*elemsize - 1);
  165. }
  166. #endif
  167. #ifdef STARPU_USE_SCC
  168. _starpu_scc_set_offset_in_shared_memory((void*)matrix_interface.ptr,
  169. (void**)&(matrix_interface.dev_handle), &(matrix_interface.offset));
  170. #endif
  171. starpu_data_register(handleptr, home_node, &matrix_interface, &starpu_interface_matrix_ops);
  172. }
  173. void starpu_matrix_ptr_register(starpu_data_handle_t handle, unsigned node,
  174. uintptr_t ptr, uintptr_t dev_handle, size_t offset, uint32_t ld)
  175. {
  176. struct starpu_matrix_interface *matrix_interface = starpu_data_get_interface_on_node(handle, node);
  177. starpu_data_ptr_register(handle, node);
  178. matrix_interface->ptr = ptr;
  179. matrix_interface->dev_handle = dev_handle;
  180. matrix_interface->offset = offset;
  181. matrix_interface->ld = ld;
  182. }
  183. static uint32_t footprint_matrix_interface_crc32(starpu_data_handle_t handle)
  184. {
  185. return starpu_hash_crc32c_be(starpu_matrix_get_nx(handle), starpu_matrix_get_ny(handle));
  186. }
  187. static int matrix_compare(void *data_interface_a, void *data_interface_b)
  188. {
  189. struct starpu_matrix_interface *matrix_a = (struct starpu_matrix_interface *) data_interface_a;
  190. struct starpu_matrix_interface *matrix_b = (struct starpu_matrix_interface *) data_interface_b;
  191. /* Two matricess are considered compatible if they have the same size */
  192. return ((matrix_a->nx == matrix_b->nx)
  193. && (matrix_a->ny == matrix_b->ny)
  194. && (matrix_a->elemsize == matrix_b->elemsize));
  195. }
  196. static void display_matrix_interface(starpu_data_handle_t handle, FILE *f)
  197. {
  198. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  199. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  200. fprintf(f, "%u\t%u\t", matrix_interface->nx, matrix_interface->ny);
  201. }
  202. static int pack_matrix_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count)
  203. {
  204. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  205. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  206. starpu_data_get_interface_on_node(handle, node);
  207. *count = matrix_interface->nx*matrix_interface->ny*matrix_interface->elemsize;
  208. if (ptr != NULL)
  209. {
  210. uint32_t y;
  211. char *matrix = (void *)matrix_interface->ptr;
  212. starpu_malloc_flags(ptr, *count, 0);
  213. char *cur = *ptr;
  214. for(y=0 ; y<matrix_interface->ny ; y++)
  215. {
  216. memcpy(cur, matrix, matrix_interface->nx*matrix_interface->elemsize);
  217. cur += matrix_interface->nx*matrix_interface->elemsize;
  218. matrix += matrix_interface->ld * matrix_interface->elemsize;
  219. }
  220. }
  221. return 0;
  222. }
  223. static int unpack_matrix_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  224. {
  225. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  226. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  227. starpu_data_get_interface_on_node(handle, node);
  228. STARPU_ASSERT(count == matrix_interface->elemsize * matrix_interface->nx * matrix_interface->ny);
  229. uint32_t y;
  230. char *cur = ptr;
  231. char *matrix = (void *)matrix_interface->ptr;
  232. for(y=0 ; y<matrix_interface->ny ; y++)
  233. {
  234. memcpy(matrix, cur, matrix_interface->nx*matrix_interface->elemsize);
  235. cur += matrix_interface->nx*matrix_interface->elemsize;
  236. matrix += matrix_interface->ld * matrix_interface->elemsize;
  237. }
  238. return 0;
  239. }
  240. static size_t matrix_interface_get_size(starpu_data_handle_t handle)
  241. {
  242. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  243. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  244. size_t size;
  245. size = (size_t)matrix_interface->nx*matrix_interface->ny*matrix_interface->elemsize;
  246. return size;
  247. }
  248. /* offer an access to the data parameters */
  249. uint32_t starpu_matrix_get_nx(starpu_data_handle_t handle)
  250. {
  251. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  252. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  253. return matrix_interface->nx;
  254. }
  255. uint32_t starpu_matrix_get_ny(starpu_data_handle_t handle)
  256. {
  257. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  258. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  259. return matrix_interface->ny;
  260. }
  261. uint32_t starpu_matrix_get_local_ld(starpu_data_handle_t handle)
  262. {
  263. unsigned node;
  264. node = _starpu_memory_node_get_local_key();
  265. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  266. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  267. starpu_data_get_interface_on_node(handle, node);
  268. return matrix_interface->ld;
  269. }
  270. uintptr_t starpu_matrix_get_local_ptr(starpu_data_handle_t handle)
  271. {
  272. unsigned node;
  273. node = _starpu_memory_node_get_local_key();
  274. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  275. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  276. starpu_data_get_interface_on_node(handle, node);
  277. return matrix_interface->ptr;
  278. }
  279. size_t starpu_matrix_get_elemsize(starpu_data_handle_t handle)
  280. {
  281. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *)
  282. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  283. return matrix_interface->elemsize;
  284. }
  285. /* memory allocation/deallocation primitives for the matrix interface */
  286. /* returns the size of the allocated area */
  287. static starpu_ssize_t allocate_matrix_buffer_on_node(void *data_interface_, unsigned dst_node)
  288. {
  289. uintptr_t addr = 0, handle;
  290. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *) data_interface_;
  291. uint32_t nx = matrix_interface->nx;
  292. uint32_t ny = matrix_interface->ny;
  293. uint32_t ld = nx; // by default
  294. size_t elemsize = matrix_interface->elemsize;
  295. starpu_ssize_t allocated_memory;
  296. handle = starpu_malloc_on_node(dst_node, nx*ny*elemsize);
  297. if (!handle)
  298. return -ENOMEM;
  299. if (starpu_node_get_kind(dst_node) != STARPU_OPENCL_RAM)
  300. addr = handle;
  301. allocated_memory = (size_t)nx*ny*elemsize;
  302. /* update the data properly in consequence */
  303. matrix_interface->ptr = addr;
  304. matrix_interface->dev_handle = handle;
  305. matrix_interface->offset = 0;
  306. matrix_interface->ld = ld;
  307. return allocated_memory;
  308. }
  309. static void free_matrix_buffer_on_node(void *data_interface, unsigned node)
  310. {
  311. struct starpu_matrix_interface *matrix_interface = (struct starpu_matrix_interface *) data_interface;
  312. uint32_t nx = matrix_interface->nx;
  313. uint32_t ny = matrix_interface->ny;
  314. size_t elemsize = matrix_interface->elemsize;
  315. starpu_free_on_node(node, matrix_interface->dev_handle, nx*ny*elemsize);
  316. }
  317. #ifdef STARPU_USE_CUDA
  318. 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)
  319. {
  320. struct starpu_matrix_interface *src_matrix = src_interface;
  321. struct starpu_matrix_interface *dst_matrix = dst_interface;
  322. size_t elemsize = src_matrix->elemsize;
  323. cudaError_t cures;
  324. if (is_async)
  325. {
  326. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  327. cures = cudaMemcpy2DAsync((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, stream);
  330. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  331. if (!cures)
  332. return -EAGAIN;
  333. }
  334. cures = cudaMemcpy2D((char *)dst_matrix->ptr, dst_matrix->ld*elemsize,
  335. (char *)src_matrix->ptr, src_matrix->ld*elemsize,
  336. src_matrix->nx*elemsize, src_matrix->ny, kind);
  337. if (STARPU_UNLIKELY(cures))
  338. {
  339. int ret = copy_any_to_any(src_interface, src_node, dst_interface, dst_node, (void*)(uintptr_t)is_async);
  340. if (ret == -EAGAIN) return ret;
  341. if (ret) STARPU_CUDA_REPORT_ERROR(cures);
  342. }
  343. _STARPU_TRACE_DATA_COPY(src_node, dst_node, (size_t)src_matrix->nx*src_matrix->ny*src_matrix->elemsize);
  344. return 0;
  345. }
  346. #ifndef BUGGED_MEMCPY3D
  347. 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)
  348. {
  349. #ifdef HAVE_CUDA_MEMCPY_PEER
  350. struct starpu_matrix_interface *src_matrix = src_interface;
  351. struct starpu_matrix_interface *dst_matrix = dst_interface;
  352. size_t elemsize = src_matrix->elemsize;
  353. cudaError_t cures;
  354. int src_dev = _starpu_memory_node_get_devid(src_node);
  355. int dst_dev = _starpu_memory_node_get_devid(dst_node);
  356. struct cudaMemcpy3DPeerParms p;
  357. memset(&p, 0, sizeof(p));
  358. p.srcDevice = src_dev;
  359. p.dstDevice = dst_dev;
  360. p.srcPtr = make_cudaPitchedPtr((char *)src_matrix->ptr, src_matrix->ld * elemsize, src_matrix->nx, src_matrix->ny);
  361. p.dstPtr = make_cudaPitchedPtr((char *)dst_matrix->ptr, dst_matrix->ld * elemsize, dst_matrix->nx, dst_matrix->ny);
  362. p.extent = make_cudaExtent(src_matrix->nx * elemsize, src_matrix->ny, 1);
  363. if (is_async)
  364. {
  365. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  366. cures = cudaMemcpy3DPeerAsync(&p, stream);
  367. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  368. if (!cures)
  369. return -EAGAIN;
  370. }
  371. cures = cudaMemcpy3DPeer(&p);
  372. if (STARPU_UNLIKELY(cures))
  373. STARPU_CUDA_REPORT_ERROR(cures);
  374. _STARPU_TRACE_DATA_COPY(src_node, dst_node, (size_t)src_matrix->nx*src_matrix->ny*src_matrix->elemsize);
  375. return 0;
  376. #else
  377. STARPU_ABORT_MSG("CUDA memcpy 3D peer not available, but core triggered one ?!");
  378. #endif
  379. }
  380. #endif
  381. static int copy_cuda_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  382. {
  383. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost, 0, 0);
  384. }
  385. static int copy_ram_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  386. {
  387. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice, 0, 0);
  388. }
  389. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  390. {
  391. if (src_node == dst_node)
  392. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToDevice, 0, 0);
  393. else
  394. #ifdef BUGGED_MEMCPY3D
  395. STARPU_ABORT_MSG("CUDA memcpy 3D peer not available, but core triggered one?!");
  396. #else
  397. return copy_cuda_peer(src_interface, src_node, dst_interface, dst_node, 0, 0);
  398. #endif
  399. }
  400. 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)
  401. {
  402. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost, 1, stream);
  403. }
  404. 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)
  405. {
  406. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice, 1, stream);
  407. }
  408. #ifndef BUGGED_MEMCPY3D
  409. 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)
  410. {
  411. if (src_node == dst_node)
  412. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToDevice, 1, stream);
  413. else
  414. return copy_cuda_peer(src_interface, src_node, dst_interface, dst_node, 1, stream);
  415. }
  416. #endif
  417. #endif // STARPU_USE_CUDA
  418. #ifdef STARPU_USE_OPENCL
  419. static int copy_opencl_common(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  420. {
  421. struct starpu_matrix_interface *src_matrix = src_interface;
  422. struct starpu_matrix_interface *dst_matrix = dst_interface;
  423. int ret;
  424. 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)");
  425. ret = starpu_opencl_copy_async_sync(src_matrix->dev_handle, src_matrix->offset, src_node,
  426. dst_matrix->dev_handle, dst_matrix->offset, dst_node,
  427. src_matrix->nx*src_matrix->ny*src_matrix->elemsize,
  428. event);
  429. _STARPU_TRACE_DATA_COPY(src_node, dst_node, src_matrix->nx*src_matrix->ny*src_matrix->elemsize);
  430. return ret;
  431. }
  432. static int copy_ram_to_opencl_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  433. {
  434. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  435. }
  436. static int copy_opencl_to_ram_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  437. {
  438. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  439. }
  440. static int copy_opencl_to_opencl_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cl_event *event)
  441. {
  442. return copy_opencl_common(src_interface, src_node, dst_interface, dst_node, event);
  443. }
  444. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  445. {
  446. return copy_ram_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  447. }
  448. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  449. {
  450. return copy_opencl_to_ram_async(src_interface, src_node, dst_interface, dst_node, NULL);
  451. }
  452. static int copy_opencl_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  453. {
  454. return copy_opencl_to_opencl_async(src_interface, src_node, dst_interface, dst_node, NULL);
  455. }
  456. #endif
  457. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data)
  458. {
  459. struct starpu_matrix_interface *src_matrix = (struct starpu_matrix_interface *) src_interface;
  460. struct starpu_matrix_interface *dst_matrix = (struct starpu_matrix_interface *) dst_interface;
  461. int ret = 0;
  462. uint32_t nx = dst_matrix->nx;
  463. uint32_t ny = dst_matrix->ny;
  464. size_t elemsize = dst_matrix->elemsize;
  465. uint32_t ld_src = src_matrix->ld;
  466. uint32_t ld_dst = dst_matrix->ld;
  467. if (ld_src == nx && ld_dst == nx)
  468. {
  469. /* Optimize unpartitioned and y-partitioned cases */
  470. if (starpu_interface_copy(src_matrix->dev_handle, src_matrix->offset, src_node,
  471. dst_matrix->dev_handle, dst_matrix->offset, dst_node,
  472. nx*ny*elemsize, async_data))
  473. ret = -EAGAIN;
  474. }
  475. else
  476. {
  477. unsigned y;
  478. for (y = 0; y < ny; y++)
  479. {
  480. uint32_t src_offset = y*ld_src*elemsize;
  481. uint32_t dst_offset = y*ld_dst*elemsize;
  482. if (starpu_interface_copy(src_matrix->dev_handle, src_matrix->offset + src_offset, src_node,
  483. dst_matrix->dev_handle, dst_matrix->offset + dst_offset, dst_node,
  484. nx*elemsize, async_data))
  485. ret = -EAGAIN;
  486. }
  487. }
  488. _STARPU_TRACE_DATA_COPY(src_node, dst_node, (size_t)nx*ny*elemsize);
  489. return ret;
  490. }
  491. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size)
  492. {
  493. struct starpu_matrix_interface *matrix = (struct starpu_matrix_interface *) data_interface;
  494. return snprintf(buf, size, "M%ux%ux%u",
  495. (unsigned) matrix->nx,
  496. (unsigned) matrix->ny,
  497. (unsigned) matrix->elemsize);
  498. }