csr_interface.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include <common/config.h>
  20. #include <datawizard/coherency.h>
  21. #include <datawizard/copy_driver.h>
  22. #include <datawizard/filters.h>
  23. #include <common/hash.h>
  24. #include <starpu_cuda.h>
  25. #include <starpu_opencl.h>
  26. #include <drivers/opencl/driver_opencl.h>
  27. static int copy_ram_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  28. #ifdef STARPU_USE_CUDA
  29. static int copy_ram_to_cuda(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  30. static int copy_cuda_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  31. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  32. static int copy_cuda_to_ram_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cudaStream_t stream);
  33. static int copy_ram_to_cuda_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cudaStream_t stream);
  34. static int copy_cuda_to_cuda_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cudaStream_t stream);
  35. #endif
  36. #ifdef STARPU_USE_OPENCL
  37. static int copy_ram_to_opencl(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  38. static int copy_opencl_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node);
  39. #endif
  40. static const struct starpu_data_copy_methods csr_copy_data_methods_s =
  41. {
  42. .ram_to_ram = copy_ram_to_ram,
  43. .ram_to_spu = NULL,
  44. #ifdef STARPU_USE_CUDA
  45. .ram_to_cuda = copy_ram_to_cuda,
  46. .cuda_to_ram = copy_cuda_to_ram,
  47. .cuda_to_cuda = copy_cuda_to_cuda,
  48. .ram_to_cuda_async = copy_ram_to_cuda_async,
  49. .cuda_to_ram_async = copy_cuda_to_ram_async,
  50. .cuda_to_cuda_async = copy_cuda_to_cuda_async,
  51. #endif
  52. #ifdef STARPU_USE_OPENCL
  53. .ram_to_opencl = copy_ram_to_opencl,
  54. .opencl_to_ram = copy_opencl_to_ram,
  55. #endif
  56. .cuda_to_spu = NULL,
  57. .spu_to_ram = NULL,
  58. .spu_to_cuda = NULL,
  59. .spu_to_spu = NULL
  60. };
  61. static void register_csr_handle(starpu_data_handle_t handle, uint32_t home_node, void *data_interface);
  62. static ssize_t allocate_csr_buffer_on_node(void *data_interface_, uint32_t dst_node);
  63. static void free_csr_buffer_on_node(void *data_interface, uint32_t node);
  64. static size_t csr_interface_get_size(starpu_data_handle_t handle);
  65. static int csr_compare(void *data_interface_a, void *data_interface_b);
  66. static uint32_t footprint_csr_interface_crc32(starpu_data_handle_t handle);
  67. static struct starpu_data_interface_ops interface_csr_ops =
  68. {
  69. .register_data_handle = register_csr_handle,
  70. .allocate_data_on_node = allocate_csr_buffer_on_node,
  71. .free_data_on_node = free_csr_buffer_on_node,
  72. .copy_methods = &csr_copy_data_methods_s,
  73. .get_size = csr_interface_get_size,
  74. .interfaceid = STARPU_CSR_INTERFACE_ID,
  75. .interface_size = sizeof(struct starpu_csr_interface),
  76. .footprint = footprint_csr_interface_crc32,
  77. .compare = csr_compare
  78. };
  79. static void register_csr_handle(starpu_data_handle_t handle, uint32_t home_node, void *data_interface)
  80. {
  81. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *) data_interface;
  82. unsigned node;
  83. for (node = 0; node < STARPU_MAXNODES; node++)
  84. {
  85. struct starpu_csr_interface *local_interface = (struct starpu_csr_interface *)
  86. starpu_data_get_interface_on_node(handle, node);
  87. if (node == home_node)
  88. {
  89. local_interface->nzval = csr_interface->nzval;
  90. local_interface->colind = csr_interface->colind;
  91. }
  92. else
  93. {
  94. local_interface->nzval = 0;
  95. local_interface->colind = NULL;
  96. }
  97. local_interface->rowptr = csr_interface->rowptr;
  98. local_interface->nnz = csr_interface->nnz;
  99. local_interface->nrow = csr_interface->nrow;
  100. local_interface->firstentry = csr_interface->firstentry;
  101. local_interface->elemsize = csr_interface->elemsize;
  102. }
  103. }
  104. /* declare a new data with the BLAS interface */
  105. void starpu_csr_data_register(starpu_data_handle_t *handleptr, uint32_t home_node,
  106. uint32_t nnz, uint32_t nrow, uintptr_t nzval, uint32_t *colind, uint32_t *rowptr, uint32_t firstentry, size_t elemsize)
  107. {
  108. struct starpu_csr_interface csr_interface =
  109. {
  110. .nnz = nnz,
  111. .nrow = nrow,
  112. .nzval = nzval,
  113. .colind = colind,
  114. .rowptr = rowptr,
  115. .firstentry = firstentry,
  116. .elemsize = elemsize
  117. };
  118. starpu_data_register(handleptr, home_node, &csr_interface, &interface_csr_ops);
  119. }
  120. static uint32_t footprint_csr_interface_crc32(starpu_data_handle_t handle)
  121. {
  122. return _starpu_crc32_be(starpu_csr_get_nnz(handle), 0);
  123. }
  124. static int csr_compare(void *data_interface_a, void *data_interface_b)
  125. {
  126. struct starpu_csr_interface *csr_a = (struct starpu_csr_interface *) data_interface_a;
  127. struct starpu_csr_interface *csr_b = (struct starpu_csr_interface *) data_interface_b;
  128. /* Two matricess are considered compatible if they have the same size */
  129. return ((csr_a->nnz == csr_b->nnz)
  130. && (csr_a->nrow == csr_b->nrow)
  131. && (csr_a->elemsize == csr_b->elemsize));
  132. }
  133. /* offer an access to the data parameters */
  134. uint32_t starpu_csr_get_nnz(starpu_data_handle_t handle)
  135. {
  136. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  137. starpu_data_get_interface_on_node(handle, 0);
  138. return csr_interface->nnz;
  139. }
  140. uint32_t starpu_csr_get_nrow(starpu_data_handle_t handle)
  141. {
  142. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  143. starpu_data_get_interface_on_node(handle, 0);
  144. return csr_interface->nrow;
  145. }
  146. uint32_t starpu_csr_get_firstentry(starpu_data_handle_t handle)
  147. {
  148. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  149. starpu_data_get_interface_on_node(handle, 0);
  150. return csr_interface->firstentry;
  151. }
  152. size_t starpu_csr_get_elemsize(starpu_data_handle_t handle)
  153. {
  154. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  155. starpu_data_get_interface_on_node(handle, 0);
  156. return csr_interface->elemsize;
  157. }
  158. uintptr_t starpu_csr_get_local_nzval(starpu_data_handle_t handle)
  159. {
  160. unsigned node;
  161. node = _starpu_get_local_memory_node();
  162. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  163. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  164. starpu_data_get_interface_on_node(handle, node);
  165. return csr_interface->nzval;
  166. }
  167. uint32_t *starpu_csr_get_local_colind(starpu_data_handle_t handle)
  168. {
  169. unsigned node;
  170. node = _starpu_get_local_memory_node();
  171. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  172. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  173. starpu_data_get_interface_on_node(handle, node);
  174. return csr_interface->colind;
  175. }
  176. uint32_t *starpu_csr_get_local_rowptr(starpu_data_handle_t handle)
  177. {
  178. unsigned node;
  179. node = _starpu_get_local_memory_node();
  180. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  181. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  182. starpu_data_get_interface_on_node(handle, node);
  183. return csr_interface->rowptr;
  184. }
  185. static size_t csr_interface_get_size(starpu_data_handle_t handle)
  186. {
  187. size_t size;
  188. uint32_t nnz = starpu_csr_get_nnz(handle);
  189. uint32_t nrow = starpu_csr_get_nrow(handle);
  190. size_t elemsize = starpu_csr_get_elemsize(handle);
  191. size = nnz*elemsize + nnz*sizeof(uint32_t) + (nrow+1)*sizeof(uint32_t);
  192. return size;
  193. }
  194. /* memory allocation/deallocation primitives for the BLAS interface */
  195. /* returns the size of the allocated area */
  196. static ssize_t allocate_csr_buffer_on_node(void *data_interface_, uint32_t dst_node)
  197. {
  198. uintptr_t addr_nzval;
  199. uint32_t *addr_colind, *addr_rowptr;
  200. ssize_t allocated_memory;
  201. /* we need the 3 arrays to be allocated */
  202. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *) data_interface_;
  203. uint32_t nnz = csr_interface->nnz;
  204. uint32_t nrow = csr_interface->nrow;
  205. size_t elemsize = csr_interface->elemsize;
  206. enum _starpu_node_kind kind = _starpu_get_node_kind(dst_node);
  207. switch(kind)
  208. {
  209. case STARPU_CPU_RAM:
  210. addr_nzval = (uintptr_t)malloc(nnz*elemsize);
  211. if (!addr_nzval)
  212. goto fail_nzval;
  213. addr_colind = (uint32_t *) malloc(nnz*sizeof(uint32_t));
  214. if (!addr_colind)
  215. goto fail_colind;
  216. addr_rowptr = (uint32_t *) malloc((nrow+1)*sizeof(uint32_t));
  217. if (!addr_rowptr)
  218. goto fail_rowptr;
  219. break;
  220. #ifdef STARPU_USE_CUDA
  221. case STARPU_CUDA_RAM:
  222. cudaMalloc((void **)&addr_nzval, nnz*elemsize);
  223. if (!addr_nzval)
  224. goto fail_nzval;
  225. cudaMalloc((void **)&addr_colind, nnz*sizeof(uint32_t));
  226. if (!addr_colind)
  227. goto fail_colind;
  228. cudaMalloc((void **)&addr_rowptr, (nrow+1)*sizeof(uint32_t));
  229. if (!addr_rowptr)
  230. goto fail_rowptr;
  231. break;
  232. #endif
  233. #ifdef STARPU_USE_OPENCL
  234. case STARPU_OPENCL_RAM:
  235. {
  236. int ret;
  237. void *ptr;
  238. ret = _starpu_opencl_allocate_memory(&ptr, nnz*elemsize, CL_MEM_READ_WRITE);
  239. addr_nzval = (uintptr_t)ptr;
  240. if (ret) goto fail_nzval;
  241. ret = _starpu_opencl_allocate_memory(&ptr, nnz*sizeof(uint32_t), CL_MEM_READ_WRITE);
  242. addr_colind = ptr;
  243. if (ret) goto fail_colind;
  244. ret = _starpu_opencl_allocate_memory(&ptr, (nrow+1)*sizeof(uint32_t), CL_MEM_READ_WRITE);
  245. addr_rowptr = ptr;
  246. if (ret) goto fail_rowptr;
  247. break;
  248. }
  249. #endif
  250. default:
  251. assert(0);
  252. }
  253. /* allocation succeeded */
  254. allocated_memory =
  255. nnz*elemsize + nnz*sizeof(uint32_t) + (nrow+1)*sizeof(uint32_t);
  256. /* update the data properly in consequence */
  257. csr_interface->nzval = addr_nzval;
  258. csr_interface->colind = addr_colind;
  259. csr_interface->rowptr = addr_rowptr;
  260. return allocated_memory;
  261. fail_rowptr:
  262. switch(kind)
  263. {
  264. case STARPU_CPU_RAM:
  265. free((void *)addr_colind);
  266. #ifdef STARPU_USE_CUDA
  267. case STARPU_CUDA_RAM:
  268. cudaFree((void*)addr_colind);
  269. break;
  270. #endif
  271. #ifdef STARPU_USE_OPENCL
  272. case STARPU_OPENCL_RAM:
  273. clReleaseMemObject((void*)addr_colind);
  274. break;
  275. #endif
  276. default:
  277. assert(0);
  278. }
  279. fail_colind:
  280. switch(kind)
  281. {
  282. case STARPU_CPU_RAM:
  283. free((void *)addr_nzval);
  284. #ifdef STARPU_USE_CUDA
  285. case STARPU_CUDA_RAM:
  286. cudaFree((void*)addr_nzval);
  287. break;
  288. #endif
  289. #ifdef STARPU_USE_OPENCL
  290. case STARPU_OPENCL_RAM:
  291. clReleaseMemObject((void*)addr_nzval);
  292. break;
  293. #endif
  294. default:
  295. assert(0);
  296. }
  297. fail_nzval:
  298. /* allocation failed */
  299. return -ENOMEM;
  300. }
  301. static void free_csr_buffer_on_node(void *data_interface, uint32_t node)
  302. {
  303. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *) data_interface;
  304. enum _starpu_node_kind kind = _starpu_get_node_kind(node);
  305. switch(kind)
  306. {
  307. case STARPU_CPU_RAM:
  308. free((void*)csr_interface->nzval);
  309. free((void*)csr_interface->colind);
  310. free((void*)csr_interface->rowptr);
  311. break;
  312. #ifdef STARPU_USE_CUDA
  313. case STARPU_CUDA_RAM:
  314. cudaFree((void*)csr_interface->nzval);
  315. cudaFree((void*)csr_interface->colind);
  316. cudaFree((void*)csr_interface->rowptr);
  317. break;
  318. #endif
  319. #ifdef STARPU_USE_OPENCL
  320. case STARPU_OPENCL_RAM:
  321. clReleaseMemObject((void*)csr_interface->nzval);
  322. clReleaseMemObject((void*)csr_interface->colind);
  323. clReleaseMemObject((void*)csr_interface->rowptr);
  324. break;
  325. #endif
  326. default:
  327. assert(0);
  328. }
  329. }
  330. #ifdef STARPU_USE_CUDA
  331. 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)
  332. {
  333. struct starpu_csr_interface *src_csr = src_interface;
  334. struct starpu_csr_interface *dst_csr = dst_interface;
  335. uint32_t nnz = src_csr->nnz;
  336. uint32_t nrow = src_csr->nrow;
  337. size_t elemsize = src_csr->elemsize;
  338. cudaError_t cures;
  339. cures = cudaMemcpy((char *)dst_csr->nzval, (char *)src_csr->nzval, nnz*elemsize, kind);
  340. if (STARPU_UNLIKELY(cures))
  341. STARPU_CUDA_REPORT_ERROR(cures);
  342. cures = cudaMemcpy((char *)dst_csr->colind, (char *)src_csr->colind, nnz*sizeof(uint32_t), kind);
  343. if (STARPU_UNLIKELY(cures))
  344. STARPU_CUDA_REPORT_ERROR(cures);
  345. cures = cudaMemcpy((char *)dst_csr->rowptr, (char *)src_csr->rowptr, (nrow+1)*sizeof(uint32_t), kind);
  346. if (STARPU_UNLIKELY(cures))
  347. STARPU_CUDA_REPORT_ERROR(cures);
  348. _STARPU_TRACE_DATA_COPY(src_node, dst_node, nnz*elemsize + (nnz+nrow+1)*sizeof(uint32_t));
  349. return 0;
  350. }
  351. static int copy_cuda_common_async(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, enum cudaMemcpyKind kind, cudaStream_t stream)
  352. {
  353. struct starpu_csr_interface *src_csr = src_interface;
  354. struct starpu_csr_interface *dst_csr = dst_interface;
  355. uint32_t nnz = src_csr->nnz;
  356. uint32_t nrow = src_csr->nrow;
  357. size_t elemsize = src_csr->elemsize;
  358. cudaError_t cures;
  359. int synchronous_fallback = 0;
  360. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  361. cures = cudaMemcpyAsync((char *)dst_csr->nzval, (char *)src_csr->nzval, nnz*elemsize, kind, stream);
  362. if (cures)
  363. {
  364. synchronous_fallback = 1;
  365. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  366. cures = cudaMemcpy((char *)dst_csr->nzval, (char *)src_csr->nzval, nnz*elemsize, kind);
  367. if (STARPU_UNLIKELY(cures))
  368. STARPU_CUDA_REPORT_ERROR(cures);
  369. }
  370. if (!synchronous_fallback)
  371. {
  372. cures = cudaMemcpyAsync((char *)dst_csr->colind, (char *)src_csr->colind, nnz*sizeof(uint32_t), kind, stream);
  373. }
  374. if (synchronous_fallback || cures != cudaSuccess)
  375. {
  376. synchronous_fallback = 1;
  377. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  378. cures = cudaMemcpy((char *)dst_csr->colind, (char *)src_csr->colind, nnz*sizeof(uint32_t), kind);
  379. if (STARPU_UNLIKELY(cures))
  380. STARPU_CUDA_REPORT_ERROR(cures);
  381. }
  382. if (!synchronous_fallback)
  383. {
  384. cures = cudaMemcpyAsync((char *)dst_csr->rowptr, (char *)src_csr->rowptr, (nrow+1)*sizeof(uint32_t), kind, stream);
  385. }
  386. if (synchronous_fallback || cures != cudaSuccess)
  387. {
  388. synchronous_fallback = 1;
  389. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  390. cures = cudaMemcpy((char *)dst_csr->rowptr, (char *)src_csr->rowptr, (nrow+1)*sizeof(uint32_t), kind);
  391. if (STARPU_UNLIKELY(cures))
  392. STARPU_CUDA_REPORT_ERROR(cures);
  393. }
  394. if (synchronous_fallback)
  395. {
  396. _STARPU_TRACE_DATA_COPY(src_node, dst_node, nnz*elemsize + (nnz+nrow+1)*sizeof(uint32_t));
  397. return 0;
  398. }
  399. else
  400. {
  401. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  402. return -EAGAIN;
  403. }
  404. }
  405. static int copy_cuda_peer(void *src_interface STARPU_ATTRIBUTE_UNUSED, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface STARPU_ATTRIBUTE_UNUSED, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  406. {
  407. #ifdef HAVE_CUDA_MEMCPY_PEER
  408. struct starpu_csr_interface *src_csr = src_interface;
  409. struct starpu_csr_interface *dst_csr = dst_interface;
  410. uint32_t nnz = src_csr->nnz;
  411. uint32_t nrow = src_csr->nrow;
  412. size_t elemsize = src_csr->elemsize;
  413. int src_dev = _starpu_memory_node_to_devid(src_node);
  414. int dst_dev = _starpu_memory_node_to_devid(dst_node);
  415. cudaError_t cures;
  416. cures = cudaMemcpyPeer((char *)dst_csr->nzval, dst_dev, (char *)src_csr->nzval, src_dev, nnz*elemsize);
  417. if (STARPU_UNLIKELY(cures))
  418. STARPU_CUDA_REPORT_ERROR(cures);
  419. cures = cudaMemcpyPeer((char *)dst_csr->colind, dst_dev, (char *)src_csr->colind, src_dev, nnz*sizeof(uint32_t));
  420. if (STARPU_UNLIKELY(cures))
  421. STARPU_CUDA_REPORT_ERROR(cures);
  422. cures = cudaMemcpyPeer((char *)dst_csr->rowptr, dst_dev, (char *)src_csr->rowptr, src_dev, (nrow+1)*sizeof(uint32_t));
  423. if (STARPU_UNLIKELY(cures))
  424. STARPU_CUDA_REPORT_ERROR(cures);
  425. _STARPU_TRACE_DATA_COPY(src_node, dst_node, nnz*elemsize + (nnz+nrow+1)*sizeof(uint32_t));
  426. return 0;
  427. #else
  428. STARPU_ABORT();
  429. return 0;
  430. #endif
  431. }
  432. static int copy_cuda_peer_async(void *src_interface STARPU_ATTRIBUTE_UNUSED, unsigned src_node STARPU_ATTRIBUTE_UNUSED,
  433. void *dst_interface STARPU_ATTRIBUTE_UNUSED, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, cudaStream_t stream STARPU_ATTRIBUTE_UNUSED)
  434. {
  435. #ifdef HAVE_CUDA_MEMCPY_PEER
  436. struct starpu_csr_interface *src_csr = src_interface;
  437. struct starpu_csr_interface *dst_csr = dst_interface;
  438. uint32_t nnz = src_csr->nnz;
  439. uint32_t nrow = src_csr->nrow;
  440. size_t elemsize = src_csr->elemsize;
  441. cudaError_t cures;
  442. int src_dev = _starpu_memory_node_to_devid(src_node);
  443. int dst_dev = _starpu_memory_node_to_devid(dst_node);
  444. int synchronous_fallback = 0;
  445. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  446. cures = cudaMemcpyPeerAsync((char *)dst_csr->nzval, dst_dev, (char *)src_csr->nzval, src_dev, nnz*elemsize, stream);
  447. if (cures)
  448. {
  449. synchronous_fallback = 1;
  450. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  451. cures = cudaMemcpyPeer((char *)dst_csr->nzval, dst_dev, (char *)src_csr->nzval, src_dev, nnz*elemsize);
  452. if (STARPU_UNLIKELY(cures))
  453. STARPU_CUDA_REPORT_ERROR(cures);
  454. }
  455. if (!synchronous_fallback)
  456. {
  457. cures = cudaMemcpyPeerAsync((char *)dst_csr->colind, dst_dev, (char *)src_csr->colind, src_dev, nnz*sizeof(uint32_t), stream);
  458. }
  459. if (synchronous_fallback || cures != cudaSuccess)
  460. {
  461. synchronous_fallback = 1;
  462. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  463. cures = cudaMemcpyPeer((char *)dst_csr->colind, dst_dev, (char *)src_csr->colind, src_dev, nnz*sizeof(uint32_t));
  464. if (STARPU_UNLIKELY(cures))
  465. STARPU_CUDA_REPORT_ERROR(cures);
  466. }
  467. if (!synchronous_fallback)
  468. {
  469. cures = cudaMemcpyPeerAsync((char *)dst_csr->rowptr, dst_dev, (char *)src_csr->rowptr, src_dev, (nrow+1)*sizeof(uint32_t), stream);
  470. }
  471. if (synchronous_fallback || cures != cudaSuccess)
  472. {
  473. synchronous_fallback = 1;
  474. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  475. cures = cudaMemcpyPeer((char *)dst_csr->rowptr, dst_dev, (char *)src_csr->rowptr, src_dev, (nrow+1)*sizeof(uint32_t));
  476. if (STARPU_UNLIKELY(cures))
  477. STARPU_CUDA_REPORT_ERROR(cures);
  478. }
  479. if (synchronous_fallback)
  480. {
  481. _STARPU_TRACE_DATA_COPY(src_node, dst_node, nnz*elemsize + (nnz+nrow+1)*sizeof(uint32_t));
  482. return 0;
  483. }
  484. else
  485. {
  486. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  487. return -EAGAIN;
  488. }
  489. #else
  490. /* Illegal without Peer tranfers */
  491. STARPU_ABORT();
  492. return 0;
  493. #endif
  494. }
  495. static int copy_cuda_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node)
  496. {
  497. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost);
  498. }
  499. static int copy_ram_to_cuda(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node)
  500. {
  501. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice);
  502. }
  503. static int copy_cuda_to_cuda(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node)
  504. {
  505. if (src_node == dst_node)
  506. return copy_cuda_common(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToDevice);
  507. else
  508. return copy_cuda_peer(src_interface, src_node, dst_interface, dst_node);
  509. }
  510. static int copy_cuda_to_ram_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cudaStream_t stream)
  511. {
  512. return copy_cuda_common_async(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToHost, stream);
  513. }
  514. static int copy_ram_to_cuda_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cudaStream_t stream)
  515. {
  516. return copy_cuda_common_async(src_interface, src_node, dst_interface, dst_node, cudaMemcpyHostToDevice, stream);
  517. }
  518. static int copy_cuda_to_cuda_async(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cudaStream_t stream)
  519. {
  520. if (src_node == dst_node)
  521. return copy_cuda_common_async(src_interface, src_node, dst_interface, dst_node, cudaMemcpyDeviceToDevice, stream);
  522. else
  523. return copy_cuda_peer_async(src_interface, src_node, dst_interface, dst_node, stream);
  524. }
  525. #endif // STARPU_USE_CUDA
  526. #ifdef STARPU_USE_OPENCL
  527. static int copy_opencl_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  528. {
  529. struct starpu_csr_interface *src_csr = src_interface;
  530. struct starpu_csr_interface *dst_csr = dst_interface;
  531. uint32_t nnz = src_csr->nnz;
  532. uint32_t nrow = src_csr->nrow;
  533. size_t elemsize = src_csr->elemsize;
  534. int err;
  535. err = _starpu_opencl_copy_opencl_to_ram((cl_mem)src_csr->nzval, src_node, (void *)dst_csr->nzval, dst_node, nnz*elemsize, 0, NULL);
  536. if (STARPU_UNLIKELY(err))
  537. STARPU_OPENCL_REPORT_ERROR(err);
  538. err = _starpu_opencl_copy_opencl_to_ram((cl_mem)src_csr->colind, src_node, (void *)dst_csr->colind, dst_node, nnz*sizeof(uint32_t), 0, NULL);
  539. if (STARPU_UNLIKELY(err))
  540. STARPU_OPENCL_REPORT_ERROR(err);
  541. err = _starpu_opencl_copy_opencl_to_ram((cl_mem)src_csr->rowptr, src_node, (void *)dst_csr->rowptr, dst_node, (nrow+1)*sizeof(uint32_t), 0, NULL);
  542. if (STARPU_UNLIKELY(err))
  543. STARPU_OPENCL_REPORT_ERROR(err);
  544. _STARPU_TRACE_DATA_COPY(src_node, dst_node, nnz*elemsize + (nnz+nrow+1)*sizeof(uint32_t));
  545. return 0;
  546. }
  547. static int copy_ram_to_opencl(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  548. {
  549. struct starpu_csr_interface *src_csr = src_interface;
  550. struct starpu_csr_interface *dst_csr = dst_interface;
  551. uint32_t nnz = src_csr->nnz;
  552. uint32_t nrow = src_csr->nrow;
  553. size_t elemsize = src_csr->elemsize;
  554. int err;
  555. err = _starpu_opencl_copy_ram_to_opencl((void *)src_csr->nzval, src_node, (cl_mem)dst_csr->nzval, dst_node, nnz*elemsize, 0, NULL);
  556. if (STARPU_UNLIKELY(err))
  557. STARPU_OPENCL_REPORT_ERROR(err);
  558. err = _starpu_opencl_copy_ram_to_opencl((void *)src_csr->colind, src_node, (cl_mem)dst_csr->colind, dst_node, nnz*sizeof(uint32_t), 0, NULL);
  559. if (STARPU_UNLIKELY(err))
  560. STARPU_OPENCL_REPORT_ERROR(err);
  561. err = _starpu_opencl_copy_ram_to_opencl((void *)src_csr->rowptr, src_node, (cl_mem)dst_csr->rowptr, dst_node, (nrow+1)*sizeof(uint32_t), 0, NULL);
  562. if (STARPU_UNLIKELY(err))
  563. STARPU_OPENCL_REPORT_ERROR(err);
  564. _STARPU_TRACE_DATA_COPY(src_node, dst_node, nnz*elemsize + (nnz+nrow+1)*sizeof(uint32_t));
  565. return 0;
  566. }
  567. #endif // STARPU_USE_OPENCL
  568. /* as not all platform easily have a BLAS lib installed ... */
  569. static int copy_ram_to_ram(void *src_interface, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst_interface, unsigned dst_node STARPU_ATTRIBUTE_UNUSED)
  570. {
  571. struct starpu_csr_interface *src_csr = (struct starpu_csr_interface *) src_interface;
  572. struct starpu_csr_interface *dst_csr = (struct starpu_csr_interface *) dst_interface;
  573. uint32_t nnz = src_csr->nnz;
  574. uint32_t nrow = src_csr->nrow;
  575. size_t elemsize = src_csr->elemsize;
  576. memcpy((void *)dst_csr->nzval, (void *)src_csr->nzval, nnz*elemsize);
  577. memcpy((void *)dst_csr->colind, (void *)src_csr->colind, nnz*sizeof(uint32_t));
  578. memcpy((void *)dst_csr->rowptr, (void *)src_csr->rowptr, (nrow+1)*sizeof(uint32_t));
  579. _STARPU_TRACE_DATA_COPY(src_node, dst_node, nnz*elemsize + (nnz+nrow+1)*sizeof(uint32_t));
  580. return 0;
  581. }