csr_interface.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include <common/config.h>
  18. #include <datawizard/coherency.h>
  19. #include <datawizard/copy_driver.h>
  20. #include <datawizard/filters.h>
  21. #include <common/hash.h>
  22. #include <starpu_cuda.h>
  23. #include <starpu_opencl.h>
  24. #include <drivers/opencl/driver_opencl.h>
  25. static int copy_ram_to_ram(void *src_interface, unsigned src_node __attribute__((unused)), void *dst_interface, unsigned dst_node __attribute__((unused)));
  26. #ifdef STARPU_USE_CUDA
  27. static int copy_ram_to_cuda(void *src_interface, unsigned src_node __attribute__((unused)), void *dst_interface, unsigned dst_node __attribute__((unused)));
  28. static int copy_cuda_to_ram(void *src_interface, unsigned src_node __attribute__((unused)), void *dst_interface, unsigned dst_node __attribute__((unused)));
  29. #endif
  30. #ifdef STARPU_USE_OPENCL
  31. static int copy_ram_to_opencl(void *src_interface, unsigned src_node __attribute__((unused)), void *dst_interface, unsigned dst_node __attribute__((unused)));
  32. static int copy_opencl_to_ram(void *src_interface, unsigned src_node __attribute__((unused)), void *dst_interface, unsigned dst_node __attribute__((unused)));
  33. #endif
  34. static const struct starpu_data_copy_methods csr_copy_data_methods_s = {
  35. .ram_to_ram = copy_ram_to_ram,
  36. .ram_to_spu = NULL,
  37. #ifdef STARPU_USE_CUDA
  38. .ram_to_cuda = copy_ram_to_cuda,
  39. .cuda_to_ram = copy_cuda_to_ram,
  40. #endif
  41. #ifdef STARPU_USE_OPENCL
  42. .ram_to_opencl = copy_ram_to_opencl,
  43. .opencl_to_ram = copy_opencl_to_ram,
  44. #endif
  45. .cuda_to_cuda = NULL,
  46. .cuda_to_spu = NULL,
  47. .spu_to_ram = NULL,
  48. .spu_to_cuda = NULL,
  49. .spu_to_spu = NULL
  50. };
  51. static void register_csr_handle(starpu_data_handle handle, uint32_t home_node, void *interface);
  52. static ssize_t allocate_csr_buffer_on_node(void *interface_, uint32_t dst_node);
  53. static void free_csr_buffer_on_node(void *interface, uint32_t node);
  54. static size_t csr_interface_get_size(starpu_data_handle handle);
  55. static int csr_compare(void *interface_a, void *interface_b);
  56. static uint32_t footprint_csr_interface_crc32(starpu_data_handle handle);
  57. static struct starpu_data_interface_ops_t interface_csr_ops = {
  58. .register_data_handle = register_csr_handle,
  59. .allocate_data_on_node = allocate_csr_buffer_on_node,
  60. .free_data_on_node = free_csr_buffer_on_node,
  61. .copy_methods = &csr_copy_data_methods_s,
  62. .get_size = csr_interface_get_size,
  63. .interfaceid = STARPU_CSR_INTERFACE_ID,
  64. .interface_size = sizeof(starpu_csr_interface_t),
  65. .footprint = footprint_csr_interface_crc32,
  66. .compare = csr_compare
  67. };
  68. static void register_csr_handle(starpu_data_handle handle, uint32_t home_node, void *interface)
  69. {
  70. starpu_csr_interface_t *csr_interface = interface;
  71. unsigned node;
  72. for (node = 0; node < STARPU_MAXNODES; node++)
  73. {
  74. starpu_csr_interface_t *local_interface =
  75. starpu_data_get_interface_on_node(handle, node);
  76. if (node == home_node) {
  77. local_interface->nzval = csr_interface->nzval;
  78. local_interface->colind = csr_interface->colind;
  79. }
  80. else {
  81. local_interface->nzval = 0;
  82. local_interface->colind = NULL;
  83. }
  84. local_interface->rowptr = csr_interface->rowptr;
  85. local_interface->nnz = csr_interface->nnz;
  86. local_interface->nrow = csr_interface->nrow;
  87. local_interface->firstentry = csr_interface->firstentry;
  88. local_interface->elemsize = csr_interface->elemsize;
  89. }
  90. }
  91. /* declare a new data with the BLAS interface */
  92. void starpu_csr_data_register(starpu_data_handle *handleptr, uint32_t home_node,
  93. uint32_t nnz, uint32_t nrow, uintptr_t nzval, uint32_t *colind, uint32_t *rowptr, uint32_t firstentry, size_t elemsize)
  94. {
  95. starpu_csr_interface_t interface = {
  96. .nnz = nnz,
  97. .nrow = nrow,
  98. .nzval = nzval,
  99. .colind = colind,
  100. .rowptr = rowptr,
  101. .firstentry = firstentry,
  102. .elemsize = elemsize
  103. };
  104. starpu_data_register(handleptr, home_node, &interface, &interface_csr_ops);
  105. }
  106. static uint32_t footprint_csr_interface_crc32(starpu_data_handle handle)
  107. {
  108. return _starpu_crc32_be(starpu_csr_get_nnz(handle), 0);
  109. }
  110. static int csr_compare(void *interface_a, void *interface_b)
  111. {
  112. starpu_csr_interface_t *csr_a = interface_a;
  113. starpu_csr_interface_t *csr_b = interface_b;
  114. /* Two matricess are considered compatible if they have the same size */
  115. return ((csr_a->nnz == csr_b->nnz)
  116. && (csr_a->nrow == csr_b->nrow)
  117. && (csr_a->elemsize == csr_b->elemsize));
  118. }
  119. /* offer an access to the data parameters */
  120. uint32_t starpu_csr_get_nnz(starpu_data_handle handle)
  121. {
  122. starpu_csr_interface_t *interface =
  123. starpu_data_get_interface_on_node(handle, 0);
  124. return interface->nnz;
  125. }
  126. uint32_t starpu_csr_get_nrow(starpu_data_handle handle)
  127. {
  128. starpu_csr_interface_t *interface =
  129. starpu_data_get_interface_on_node(handle, 0);
  130. return interface->nrow;
  131. }
  132. uint32_t starpu_csr_get_firstentry(starpu_data_handle handle)
  133. {
  134. starpu_csr_interface_t *interface =
  135. starpu_data_get_interface_on_node(handle, 0);
  136. return interface->firstentry;
  137. }
  138. size_t starpu_csr_get_elemsize(starpu_data_handle handle)
  139. {
  140. starpu_csr_interface_t *interface =
  141. starpu_data_get_interface_on_node(handle, 0);
  142. return interface->elemsize;
  143. }
  144. uintptr_t starpu_csr_get_local_nzval(starpu_data_handle handle)
  145. {
  146. unsigned node;
  147. node = _starpu_get_local_memory_node();
  148. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  149. starpu_csr_interface_t *interface =
  150. starpu_data_get_interface_on_node(handle, node);
  151. return interface->nzval;
  152. }
  153. uint32_t *starpu_csr_get_local_colind(starpu_data_handle handle)
  154. {
  155. unsigned node;
  156. node = _starpu_get_local_memory_node();
  157. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  158. starpu_csr_interface_t *interface =
  159. starpu_data_get_interface_on_node(handle, node);
  160. return interface->colind;
  161. }
  162. uint32_t *starpu_csr_get_local_rowptr(starpu_data_handle handle)
  163. {
  164. unsigned node;
  165. node = _starpu_get_local_memory_node();
  166. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  167. starpu_csr_interface_t *interface =
  168. starpu_data_get_interface_on_node(handle, node);
  169. return interface->rowptr;
  170. }
  171. static size_t csr_interface_get_size(starpu_data_handle handle)
  172. {
  173. size_t size;
  174. uint32_t nnz = starpu_csr_get_nnz(handle);
  175. uint32_t nrow = starpu_csr_get_nrow(handle);
  176. size_t elemsize = starpu_csr_get_elemsize(handle);
  177. size = nnz*elemsize + nnz*sizeof(uint32_t) + (nrow+1)*sizeof(uint32_t);
  178. return size;
  179. }
  180. /* memory allocation/deallocation primitives for the BLAS interface */
  181. /* returns the size of the allocated area */
  182. static ssize_t allocate_csr_buffer_on_node(void *interface_, uint32_t dst_node)
  183. {
  184. uintptr_t addr_nzval;
  185. uint32_t *addr_colind, *addr_rowptr;
  186. ssize_t allocated_memory;
  187. /* we need the 3 arrays to be allocated */
  188. starpu_csr_interface_t *interface = interface_;
  189. uint32_t nnz = interface->nnz;
  190. uint32_t nrow = interface->nrow;
  191. size_t elemsize = interface->elemsize;
  192. starpu_node_kind kind = _starpu_get_node_kind(dst_node);
  193. switch(kind) {
  194. case STARPU_CPU_RAM:
  195. addr_nzval = (uintptr_t)malloc(nnz*elemsize);
  196. if (!addr_nzval)
  197. goto fail_nzval;
  198. addr_colind = malloc(nnz*sizeof(uint32_t));
  199. if (!addr_colind)
  200. goto fail_colind;
  201. addr_rowptr = malloc((nrow+1)*sizeof(uint32_t));
  202. if (!addr_rowptr)
  203. goto fail_rowptr;
  204. break;
  205. #ifdef STARPU_USE_CUDA
  206. case STARPU_CUDA_RAM:
  207. cudaMalloc((void **)&addr_nzval, nnz*elemsize);
  208. if (!addr_nzval)
  209. goto fail_nzval;
  210. cudaMalloc((void **)&addr_colind, nnz*sizeof(uint32_t));
  211. if (!addr_colind)
  212. goto fail_colind;
  213. cudaMalloc((void **)&addr_rowptr, (nrow+1)*sizeof(uint32_t));
  214. if (!addr_rowptr)
  215. goto fail_rowptr;
  216. break;
  217. #endif
  218. #ifdef STARPU_USE_OPENCL
  219. case STARPU_OPENCL_RAM:
  220. {
  221. int ret;
  222. void *ptr;
  223. ret = _starpu_opencl_allocate_memory(&ptr, nnz*elemsize, CL_MEM_READ_WRITE);
  224. addr_nzval = (uintptr_t)ptr;
  225. if (ret) goto fail_nzval;
  226. ret = _starpu_opencl_allocate_memory(&ptr, nnz*sizeof(uint32_t), CL_MEM_READ_WRITE);
  227. addr_colind = ptr;
  228. if (ret) goto fail_colind;
  229. ret = _starpu_opencl_allocate_memory(&ptr, (nrow+1)*sizeof(uint32_t), CL_MEM_READ_WRITE);
  230. addr_rowptr = ptr;
  231. if (ret) goto fail_rowptr;
  232. break;
  233. }
  234. #endif
  235. default:
  236. assert(0);
  237. }
  238. /* allocation succeeded */
  239. allocated_memory =
  240. nnz*elemsize + nnz*sizeof(uint32_t) + (nrow+1)*sizeof(uint32_t);
  241. /* update the data properly in consequence */
  242. interface->nzval = addr_nzval;
  243. interface->colind = addr_colind;
  244. interface->rowptr = addr_rowptr;
  245. return allocated_memory;
  246. fail_rowptr:
  247. switch(kind) {
  248. case STARPU_CPU_RAM:
  249. free((void *)addr_colind);
  250. #ifdef STARPU_USE_CUDA
  251. case STARPU_CUDA_RAM:
  252. cudaFree((void*)addr_colind);
  253. break;
  254. #endif
  255. #ifdef STARPU_USE_OPENCL
  256. case STARPU_OPENCL_RAM:
  257. clReleaseMemObject((void*)addr_colind);
  258. break;
  259. #endif
  260. default:
  261. assert(0);
  262. }
  263. fail_colind:
  264. switch(kind) {
  265. case STARPU_CPU_RAM:
  266. free((void *)addr_nzval);
  267. #ifdef STARPU_USE_CUDA
  268. case STARPU_CUDA_RAM:
  269. cudaFree((void*)addr_nzval);
  270. break;
  271. #endif
  272. #ifdef STARPU_USE_OPENCL
  273. case STARPU_OPENCL_RAM:
  274. clReleaseMemObject((void*)addr_nzval);
  275. break;
  276. #endif
  277. default:
  278. assert(0);
  279. }
  280. fail_nzval:
  281. /* allocation failed */
  282. return -ENOMEM;
  283. }
  284. static void free_csr_buffer_on_node(void *interface, uint32_t node)
  285. {
  286. starpu_csr_interface_t *csr_interface = interface;
  287. starpu_node_kind kind = _starpu_get_node_kind(node);
  288. switch(kind) {
  289. case STARPU_CPU_RAM:
  290. free((void*)csr_interface->nzval);
  291. free((void*)csr_interface->colind);
  292. free((void*)csr_interface->rowptr);
  293. break;
  294. #ifdef STARPU_USE_CUDA
  295. case STARPU_CUDA_RAM:
  296. cudaFree((void*)csr_interface->nzval);
  297. cudaFree((void*)csr_interface->colind);
  298. cudaFree((void*)csr_interface->rowptr);
  299. break;
  300. #endif
  301. #ifdef STARPU_USE_OPENCL
  302. case STARPU_OPENCL_RAM:
  303. clReleaseMemObject((void*)csr_interface->nzval);
  304. clReleaseMemObject((void*)csr_interface->colind);
  305. clReleaseMemObject((void*)csr_interface->rowptr);
  306. break;
  307. #endif
  308. default:
  309. assert(0);
  310. }
  311. }
  312. #ifdef STARPU_USE_CUDA
  313. static int copy_cuda_to_ram(void *src_interface, unsigned src_node __attribute__((unused)), void *dst_interface, unsigned dst_node __attribute__((unused)))
  314. {
  315. starpu_csr_interface_t *src_csr = src_interface;
  316. starpu_csr_interface_t *dst_csr = dst_interface;
  317. uint32_t nnz = src_csr->nnz;
  318. uint32_t nrow = src_csr->nrow;
  319. size_t elemsize = src_csr->elemsize;
  320. cudaError_t cures;
  321. cures = cudaMemcpy((char *)dst_csr->nzval, (char *)src_csr->nzval, nnz*elemsize, cudaMemcpyDeviceToHost);
  322. if (STARPU_UNLIKELY(cures))
  323. STARPU_CUDA_REPORT_ERROR(cures);
  324. cures = cudaMemcpy((char *)dst_csr->colind, (char *)src_csr->colind, nnz*sizeof(uint32_t), cudaMemcpyDeviceToHost);
  325. if (STARPU_UNLIKELY(cures))
  326. STARPU_CUDA_REPORT_ERROR(cures);
  327. cures = cudaMemcpy((char *)dst_csr->rowptr, (char *)src_csr->rowptr, (nrow+1)*sizeof(uint32_t), cudaMemcpyDeviceToHost);
  328. if (STARPU_UNLIKELY(cures))
  329. STARPU_CUDA_REPORT_ERROR(cures);
  330. cudaThreadSynchronize();
  331. STARPU_TRACE_DATA_COPY(src_node, dst_node, nnz*elemsize + (nnz+nrow+1)*sizeof(uint32_t));
  332. return 0;
  333. }
  334. static int copy_ram_to_cuda(void *src_interface, unsigned src_node __attribute__((unused)), void *dst_interface, unsigned dst_node __attribute__((unused)))
  335. {
  336. starpu_csr_interface_t *src_csr = src_interface;
  337. starpu_csr_interface_t *dst_csr = dst_interface;
  338. uint32_t nnz = src_csr->nnz;
  339. uint32_t nrow = src_csr->nrow;
  340. size_t elemsize = src_csr->elemsize;
  341. cudaError_t cures;
  342. cures = cudaMemcpy((char *)dst_csr->nzval, (char *)src_csr->nzval, nnz*elemsize, cudaMemcpyHostToDevice);
  343. if (STARPU_UNLIKELY(cures))
  344. STARPU_CUDA_REPORT_ERROR(cures);
  345. cures = cudaMemcpy((char *)dst_csr->colind, (char *)src_csr->colind, nnz*sizeof(uint32_t), cudaMemcpyHostToDevice);
  346. if (STARPU_UNLIKELY(cures))
  347. STARPU_CUDA_REPORT_ERROR(cures);
  348. cures = cudaMemcpy((char *)dst_csr->rowptr, (char *)src_csr->rowptr, (nrow+1)*sizeof(uint32_t), cudaMemcpyHostToDevice);
  349. if (STARPU_UNLIKELY(cures))
  350. STARPU_CUDA_REPORT_ERROR(cures);
  351. cudaThreadSynchronize();
  352. STARPU_TRACE_DATA_COPY(src_node, dst_node, nnz*elemsize + (nnz+nrow+1)*sizeof(uint32_t));
  353. return 0;
  354. }
  355. #endif // STARPU_USE_CUDA
  356. #ifdef STARPU_USE_OPENCL
  357. static int copy_opencl_to_ram(void *src_interface, unsigned src_node __attribute__((unused)), void *dst_interface, unsigned dst_node __attribute__((unused)))
  358. {
  359. starpu_csr_interface_t *src_csr = src_interface;
  360. starpu_csr_interface_t *dst_csr = dst_interface;
  361. uint32_t nnz = src_csr->nnz;
  362. uint32_t nrow = src_csr->nrow;
  363. size_t elemsize = src_csr->elemsize;
  364. int err;
  365. err = _starpu_opencl_copy_opencl_to_ram((cl_mem)src_csr->nzval, (void *)dst_csr->nzval, nnz*elemsize, 0, NULL);
  366. if (STARPU_UNLIKELY(err))
  367. STARPU_OPENCL_REPORT_ERROR(err);
  368. err = _starpu_opencl_copy_opencl_to_ram((cl_mem)src_csr->colind, (void *)dst_csr->colind, nnz*sizeof(uint32_t), 0, NULL);
  369. if (STARPU_UNLIKELY(err))
  370. STARPU_OPENCL_REPORT_ERROR(err);
  371. err = _starpu_opencl_copy_opencl_to_ram((cl_mem)src_csr->rowptr, (void *)dst_csr->rowptr, (nrow+1)*sizeof(uint32_t), 0, NULL);
  372. if (STARPU_UNLIKELY(err))
  373. STARPU_OPENCL_REPORT_ERROR(err);
  374. STARPU_TRACE_DATA_COPY(src_node, dst_node, nnz*elemsize + (nnz+nrow+1)*sizeof(uint32_t));
  375. return 0;
  376. }
  377. static int copy_ram_to_opencl(void *src_interface, unsigned src_node __attribute__((unused)), void *dst_interface, unsigned dst_node __attribute__((unused)))
  378. {
  379. starpu_csr_interface_t *src_csr = src_interface;
  380. starpu_csr_interface_t *dst_csr = dst_interface;
  381. uint32_t nnz = src_csr->nnz;
  382. uint32_t nrow = src_csr->nrow;
  383. size_t elemsize = src_csr->elemsize;
  384. int err;
  385. err = _starpu_opencl_copy_ram_to_opencl((void *)src_csr->nzval, (cl_mem)dst_csr->nzval, nnz*elemsize, 0, NULL);
  386. if (STARPU_UNLIKELY(err))
  387. STARPU_OPENCL_REPORT_ERROR(err);
  388. err = _starpu_opencl_copy_ram_to_opencl((void *)src_csr->colind, (cl_mem)dst_csr->colind, nnz*sizeof(uint32_t), 0, NULL);
  389. if (STARPU_UNLIKELY(err))
  390. STARPU_OPENCL_REPORT_ERROR(err);
  391. err = _starpu_opencl_copy_ram_to_opencl((void *)src_csr->rowptr, (cl_mem)dst_csr->rowptr, (nrow+1)*sizeof(uint32_t), 0, NULL);
  392. if (STARPU_UNLIKELY(err))
  393. STARPU_OPENCL_REPORT_ERROR(err);
  394. STARPU_TRACE_DATA_COPY(src_node, dst_node, nnz*elemsize + (nnz+nrow+1)*sizeof(uint32_t));
  395. return 0;
  396. }
  397. #endif // STARPU_USE_OPENCL
  398. /* as not all platform easily have a BLAS lib installed ... */
  399. static int copy_ram_to_ram(void *src_interface, unsigned src_node __attribute__((unused)), void *dst_interface, unsigned dst_node __attribute__((unused)))
  400. {
  401. starpu_csr_interface_t *src_csr = src_interface;
  402. starpu_csr_interface_t *dst_csr = dst_interface;
  403. uint32_t nnz = src_csr->nnz;
  404. uint32_t nrow = src_csr->nrow;
  405. size_t elemsize = src_csr->elemsize;
  406. memcpy((void *)dst_csr->nzval, (void *)src_csr->nzval, nnz*elemsize);
  407. memcpy((void *)dst_csr->colind, (void *)src_csr->colind, nnz*sizeof(uint32_t));
  408. memcpy((void *)dst_csr->rowptr, (void *)src_csr->rowptr, (nrow+1)*sizeof(uint32_t));
  409. STARPU_TRACE_DATA_COPY(src_node, dst_node, nnz*elemsize + (nnz+nrow+1)*sizeof(uint32_t));
  410. return 0;
  411. }