csr_interface.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (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/hierarchy.h>
  21. #include <common/hash.h>
  22. static int dummy_copy_ram_to_ram(starpu_data_handle handle, uint32_t src_node, uint32_t dst_node);
  23. #ifdef STARPU_USE_CUDA
  24. static int copy_ram_to_cuda(starpu_data_handle handle, uint32_t src_node, uint32_t dst_node);
  25. static int copy_cuda_to_ram(starpu_data_handle handle, uint32_t src_node, uint32_t dst_node);
  26. #endif
  27. static const struct starpu_copy_data_methods_s csr_copy_data_methods_s = {
  28. .ram_to_ram = dummy_copy_ram_to_ram,
  29. .ram_to_spu = NULL,
  30. #ifdef STARPU_USE_CUDA
  31. .ram_to_cuda = copy_ram_to_cuda,
  32. .cuda_to_ram = copy_cuda_to_ram,
  33. #endif
  34. .cuda_to_cuda = NULL,
  35. .cuda_to_spu = NULL,
  36. .spu_to_ram = NULL,
  37. .spu_to_cuda = NULL,
  38. .spu_to_spu = NULL
  39. };
  40. static void register_csr_handle(starpu_data_handle handle, uint32_t home_node, void *interface);
  41. static size_t allocate_csr_buffer_on_node(starpu_data_handle handle, uint32_t dst_node);
  42. static void liberate_csr_buffer_on_node(void *interface, uint32_t node);
  43. static size_t csr_interface_get_size(starpu_data_handle handle);
  44. static uint32_t footprint_csr_interface_crc32(starpu_data_handle handle);
  45. struct starpu_data_interface_ops_t interface_csr_ops = {
  46. .register_data_handle = register_csr_handle,
  47. .allocate_data_on_node = allocate_csr_buffer_on_node,
  48. .liberate_data_on_node = liberate_csr_buffer_on_node,
  49. .copy_methods = &csr_copy_data_methods_s,
  50. .get_size = csr_interface_get_size,
  51. .interfaceid = STARPU_CSR_INTERFACE_ID,
  52. .interface_size = sizeof(starpu_csr_interface_t),
  53. .footprint = footprint_csr_interface_crc32
  54. };
  55. static void register_csr_handle(starpu_data_handle handle, uint32_t home_node, void *interface)
  56. {
  57. starpu_csr_interface_t *csr_interface = interface;
  58. unsigned node;
  59. for (node = 0; node < STARPU_MAXNODES; node++)
  60. {
  61. starpu_csr_interface_t *local_interface =
  62. starpu_data_get_interface_on_node(handle, node);
  63. if (node == home_node) {
  64. local_interface->nzval = csr_interface->nzval;
  65. local_interface->colind = csr_interface->colind;
  66. local_interface->rowptr = csr_interface->rowptr;
  67. }
  68. else {
  69. local_interface->nzval = 0;
  70. local_interface->colind = NULL;
  71. local_interface->rowptr = NULL;
  72. }
  73. local_interface->nnz = csr_interface->nnz;
  74. local_interface->nrow = csr_interface->nrow;
  75. local_interface->firstentry = csr_interface->firstentry;
  76. local_interface->elemsize = csr_interface->elemsize;
  77. }
  78. }
  79. /* declare a new data with the BLAS interface */
  80. void starpu_register_csr_data(starpu_data_handle *handleptr, uint32_t home_node,
  81. uint32_t nnz, uint32_t nrow, uintptr_t nzval, uint32_t *colind, uint32_t *rowptr, uint32_t firstentry, size_t elemsize)
  82. {
  83. starpu_csr_interface_t interface = {
  84. .nnz = nnz,
  85. .nrow = nrow,
  86. .nzval = nzval,
  87. .colind = colind,
  88. .rowptr = rowptr,
  89. .firstentry = firstentry,
  90. .elemsize = elemsize
  91. };
  92. register_data_handle(handleptr, home_node, &interface, &interface_csr_ops);
  93. }
  94. static uint32_t footprint_csr_interface_crc32(starpu_data_handle handle)
  95. {
  96. return crc32_be(starpu_get_csr_nnz(handle), 0);
  97. }
  98. /* offer an access to the data parameters */
  99. uint32_t starpu_get_csr_nnz(starpu_data_handle handle)
  100. {
  101. starpu_csr_interface_t *interface =
  102. starpu_data_get_interface_on_node(handle, 0);
  103. return interface->nnz;
  104. }
  105. uint32_t starpu_get_csr_nrow(starpu_data_handle handle)
  106. {
  107. starpu_csr_interface_t *interface =
  108. starpu_data_get_interface_on_node(handle, 0);
  109. return interface->nrow;
  110. }
  111. uint32_t starpu_get_csr_firstentry(starpu_data_handle handle)
  112. {
  113. starpu_csr_interface_t *interface =
  114. starpu_data_get_interface_on_node(handle, 0);
  115. return interface->firstentry;
  116. }
  117. size_t starpu_get_csr_elemsize(starpu_data_handle handle)
  118. {
  119. starpu_csr_interface_t *interface =
  120. starpu_data_get_interface_on_node(handle, 0);
  121. return interface->elemsize;
  122. }
  123. uintptr_t starpu_get_csr_local_nzval(starpu_data_handle handle)
  124. {
  125. unsigned node;
  126. node = get_local_memory_node();
  127. STARPU_ASSERT(starpu_test_if_data_is_allocated_on_node(handle, node));
  128. starpu_csr_interface_t *interface =
  129. starpu_data_get_interface_on_node(handle, node);
  130. return interface->nzval;
  131. }
  132. uint32_t *starpu_get_csr_local_colind(starpu_data_handle handle)
  133. {
  134. unsigned node;
  135. node = get_local_memory_node();
  136. STARPU_ASSERT(starpu_test_if_data_is_allocated_on_node(handle, node));
  137. starpu_csr_interface_t *interface =
  138. starpu_data_get_interface_on_node(handle, node);
  139. return interface->colind;
  140. }
  141. uint32_t *starpu_get_csr_local_rowptr(starpu_data_handle handle)
  142. {
  143. unsigned node;
  144. node = get_local_memory_node();
  145. STARPU_ASSERT(starpu_test_if_data_is_allocated_on_node(handle, node));
  146. starpu_csr_interface_t *interface =
  147. starpu_data_get_interface_on_node(handle, node);
  148. return interface->rowptr;
  149. }
  150. static size_t csr_interface_get_size(starpu_data_handle handle)
  151. {
  152. size_t size;
  153. uint32_t nnz = starpu_get_csr_nnz(handle);
  154. uint32_t nrow = starpu_get_csr_nrow(handle);
  155. size_t elemsize = starpu_get_csr_elemsize(handle);
  156. size = nnz*elemsize + nnz*sizeof(uint32_t) + (nrow+1)*sizeof(uint32_t);
  157. return size;
  158. }
  159. /* memory allocation/deallocation primitives for the BLAS interface */
  160. /* returns the size of the allocated area */
  161. static size_t allocate_csr_buffer_on_node(starpu_data_handle handle, uint32_t dst_node)
  162. {
  163. uintptr_t addr_nzval;
  164. uint32_t *addr_colind, *addr_rowptr;
  165. size_t allocated_memory;
  166. /* we need the 3 arrays to be allocated */
  167. starpu_csr_interface_t *interface =
  168. starpu_data_get_interface_on_node(handle, dst_node);
  169. uint32_t nnz = interface->nnz;
  170. uint32_t nrow = interface->nrow;
  171. size_t elemsize = interface->elemsize;
  172. node_kind kind = get_node_kind(dst_node);
  173. switch(kind) {
  174. case RAM:
  175. addr_nzval = (uintptr_t)malloc(nnz*elemsize);
  176. if (!addr_nzval)
  177. goto fail_nzval;
  178. addr_colind = malloc(nnz*sizeof(uint32_t));
  179. if (!addr_colind)
  180. goto fail_colind;
  181. addr_rowptr = malloc((nrow+1)*sizeof(uint32_t));
  182. if (!addr_rowptr)
  183. goto fail_rowptr;
  184. break;
  185. #ifdef STARPU_USE_CUDA
  186. case CUDA_RAM:
  187. cudaMalloc((void **)&addr_nzval, nnz*elemsize);
  188. if (!addr_nzval)
  189. goto fail_nzval;
  190. cudaMalloc((void **)&addr_colind, nnz*sizeof(uint32_t));
  191. if (!addr_colind)
  192. goto fail_colind;
  193. cudaMalloc((void **)&addr_rowptr, (nrow+1)*sizeof(uint32_t));
  194. if (!addr_rowptr)
  195. goto fail_rowptr;
  196. break;
  197. #endif
  198. default:
  199. assert(0);
  200. }
  201. /* allocation succeeded */
  202. allocated_memory =
  203. nnz*elemsize + nnz*sizeof(uint32_t) + (nrow+1)*sizeof(uint32_t);
  204. /* update the data properly in consequence */
  205. interface->nzval = addr_nzval;
  206. interface->colind = addr_colind;
  207. interface->rowptr = addr_rowptr;
  208. return allocated_memory;
  209. fail_rowptr:
  210. switch(kind) {
  211. case RAM:
  212. free((void *)addr_colind);
  213. #ifdef STARPU_USE_CUDA
  214. case CUDA_RAM:
  215. cudaFree((void*)addr_colind);
  216. break;
  217. #endif
  218. default:
  219. assert(0);
  220. }
  221. fail_colind:
  222. switch(kind) {
  223. case RAM:
  224. free((void *)addr_nzval);
  225. #ifdef STARPU_USE_CUDA
  226. case CUDA_RAM:
  227. cudaFree((void*)addr_nzval);
  228. break;
  229. #endif
  230. default:
  231. assert(0);
  232. }
  233. fail_nzval:
  234. /* allocation failed */
  235. allocated_memory = 0;
  236. return allocated_memory;
  237. }
  238. static void liberate_csr_buffer_on_node(void *interface, uint32_t node)
  239. {
  240. starpu_csr_interface_t *csr_interface = interface;
  241. node_kind kind = get_node_kind(node);
  242. switch(kind) {
  243. case RAM:
  244. free((void*)csr_interface->nzval);
  245. free((void*)csr_interface->colind);
  246. free((void*)csr_interface->rowptr);
  247. break;
  248. #ifdef STARPU_USE_CUDA
  249. case CUDA_RAM:
  250. cudaFree((void*)csr_interface->nzval);
  251. cudaFree((void*)csr_interface->colind);
  252. cudaFree((void*)csr_interface->rowptr);
  253. break;
  254. #endif
  255. default:
  256. assert(0);
  257. }
  258. }
  259. #ifdef STARPU_USE_CUDA
  260. static int copy_cuda_to_ram(starpu_data_handle handle, uint32_t src_node, uint32_t dst_node)
  261. {
  262. starpu_csr_interface_t *src_csr;
  263. starpu_csr_interface_t *dst_csr;
  264. src_csr = starpu_data_get_interface_on_node(handle, src_node);
  265. dst_csr = starpu_data_get_interface_on_node(handle, dst_node);
  266. uint32_t nnz = src_csr->nnz;
  267. uint32_t nrow = src_csr->nrow;
  268. size_t elemsize = src_csr->elemsize;
  269. cudaError_t cures;
  270. cures = cudaMemcpy((char *)dst_csr->nzval, (char *)src_csr->nzval, nnz*elemsize, cudaMemcpyDeviceToHost);
  271. if (STARPU_UNLIKELY(cures))
  272. STARPU_CUDA_REPORT_ERROR(cures);
  273. cures = cudaMemcpy((char *)dst_csr->colind, (char *)src_csr->colind, nnz*sizeof(uint32_t), cudaMemcpyDeviceToHost);
  274. if (STARPU_UNLIKELY(cures))
  275. STARPU_CUDA_REPORT_ERROR(cures);
  276. cures = cudaMemcpy((char *)dst_csr->rowptr, (char *)src_csr->rowptr, (nrow+1)*sizeof(uint32_t), cudaMemcpyDeviceToHost);
  277. if (STARPU_UNLIKELY(cures))
  278. STARPU_CUDA_REPORT_ERROR(cures);
  279. cudaThreadSynchronize();
  280. TRACE_DATA_COPY(src_node, dst_node, nnz*elemsize + (nnz+nrow+1)*sizeof(uint32_t));
  281. return 0;
  282. }
  283. static int copy_ram_to_cuda(starpu_data_handle handle, uint32_t src_node, uint32_t dst_node)
  284. {
  285. starpu_csr_interface_t *src_csr;
  286. starpu_csr_interface_t *dst_csr;
  287. src_csr = starpu_data_get_interface_on_node(handle, src_node);
  288. dst_csr = starpu_data_get_interface_on_node(handle, dst_node);
  289. uint32_t nnz = src_csr->nnz;
  290. uint32_t nrow = src_csr->nrow;
  291. size_t elemsize = src_csr->elemsize;
  292. cudaError_t cures;
  293. cures = cudaMemcpy((char *)dst_csr->nzval, (char *)src_csr->nzval, nnz*elemsize, cudaMemcpyHostToDevice);
  294. if (STARPU_UNLIKELY(cures))
  295. STARPU_CUDA_REPORT_ERROR(cures);
  296. cures = cudaMemcpy((char *)dst_csr->colind, (char *)src_csr->colind, nnz*sizeof(uint32_t), cudaMemcpyHostToDevice);
  297. if (STARPU_UNLIKELY(cures))
  298. STARPU_CUDA_REPORT_ERROR(cures);
  299. cures = cudaMemcpy((char *)dst_csr->rowptr, (char *)src_csr->rowptr, (nrow+1)*sizeof(uint32_t), cudaMemcpyHostToDevice);
  300. if (STARPU_UNLIKELY(cures))
  301. STARPU_CUDA_REPORT_ERROR(cures);
  302. cudaThreadSynchronize();
  303. TRACE_DATA_COPY(src_node, dst_node, nnz*elemsize + (nnz+nrow+1)*sizeof(uint32_t));
  304. return 0;
  305. }
  306. #endif // STARPU_USE_CUDA
  307. /* as not all platform easily have a BLAS lib installed ... */
  308. static int dummy_copy_ram_to_ram(starpu_data_handle handle, uint32_t src_node, uint32_t dst_node)
  309. {
  310. starpu_csr_interface_t *src_csr;
  311. starpu_csr_interface_t *dst_csr;
  312. src_csr = starpu_data_get_interface_on_node(handle, src_node);
  313. dst_csr = starpu_data_get_interface_on_node(handle, dst_node);
  314. uint32_t nnz = src_csr->nnz;
  315. uint32_t nrow = src_csr->nrow;
  316. size_t elemsize = src_csr->elemsize;
  317. memcpy((void *)dst_csr->nzval, (void *)src_csr->nzval, nnz*elemsize);
  318. memcpy((void *)dst_csr->colind, (void *)src_csr->colind, nnz*sizeof(uint32_t));
  319. memcpy((void *)dst_csr->rowptr, (void *)src_csr->rowptr, (nrow+1)*sizeof(uint32_t));
  320. TRACE_DATA_COPY(src_node, dst_node, nnz*elemsize + (nnz+nrow+1)*sizeof(uint32_t));
  321. return 0;
  322. }