matrix_interface.c 22 KB

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