matrix_interface.c 22 KB

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