csr_interface.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2008-2018 Université de Bordeaux
  4. * Copyright (C) 2011,2012,2017 Inria
  5. * Copyright (C) 2010 Mehdi Juhoor
  6. * Copyright (C) 2010-2015,2017 CNRS
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <starpu.h>
  20. #include <common/config.h>
  21. #include <datawizard/coherency.h>
  22. #include <datawizard/copy_driver.h>
  23. #include <datawizard/filters.h>
  24. #include <datawizard/memory_nodes.h>
  25. #include <datawizard/malloc.h>
  26. #include <starpu_hash.h>
  27. #include <starpu_cuda.h>
  28. #include <starpu_opencl.h>
  29. #include <drivers/opencl/driver_opencl.h>
  30. #include <drivers/scc/driver_scc_source.h>
  31. #include <drivers/mic/driver_mic_source.h>
  32. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data);
  33. static const struct starpu_data_copy_methods csr_copy_data_methods_s =
  34. {
  35. .any_to_any = copy_any_to_any,
  36. };
  37. static void register_csr_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  38. static int csr_pointer_is_inside(void *data_interface, unsigned node, void *ptr);
  39. static starpu_ssize_t allocate_csr_buffer_on_node(void *data_interface_, unsigned dst_node);
  40. static void free_csr_buffer_on_node(void *data_interface, unsigned node);
  41. static size_t csr_interface_get_size(starpu_data_handle_t handle);
  42. static int csr_compare(void *data_interface_a, void *data_interface_b);
  43. static uint32_t footprint_csr_interface_crc32(starpu_data_handle_t handle);
  44. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size);
  45. static int pack_data(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count);
  46. static int unpack_data(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count);
  47. struct starpu_data_interface_ops starpu_interface_csr_ops =
  48. {
  49. .register_data_handle = register_csr_handle,
  50. .allocate_data_on_node = allocate_csr_buffer_on_node,
  51. .free_data_on_node = free_csr_buffer_on_node,
  52. .copy_methods = &csr_copy_data_methods_s,
  53. .get_size = csr_interface_get_size,
  54. .interfaceid = STARPU_CSR_INTERFACE_ID,
  55. .interface_size = sizeof(struct starpu_csr_interface),
  56. .footprint = footprint_csr_interface_crc32,
  57. .compare = csr_compare,
  58. .describe = describe,
  59. .pointer_is_inside = csr_pointer_is_inside,
  60. .name = "STARPU_CSR_INTERFACE",
  61. .pack_data = pack_data,
  62. .unpack_data = unpack_data
  63. };
  64. static int csr_pointer_is_inside(void *data_interface, unsigned node, void *ptr)
  65. {
  66. (void) node;
  67. struct starpu_csr_interface *csr_interface = data_interface;
  68. return ((char*) ptr >= (char*) csr_interface->nzval &&
  69. (char*) ptr < (char*) csr_interface->nzval + csr_interface->nnz*csr_interface->elemsize)
  70. || ((char*) ptr >= (char*) csr_interface->colind &&
  71. (char*) ptr < (char*) csr_interface->colind + csr_interface->nnz*sizeof(uint32_t))
  72. || ((char*) ptr >= (char*) csr_interface->rowptr &&
  73. (char*) ptr < (char*) csr_interface->rowptr + (csr_interface->nrow+1)*sizeof(uint32_t));
  74. }
  75. static void register_csr_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  76. {
  77. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *) data_interface;
  78. unsigned node;
  79. for (node = 0; node < STARPU_MAXNODES; node++)
  80. {
  81. struct starpu_csr_interface *local_interface = (struct starpu_csr_interface *)
  82. starpu_data_get_interface_on_node(handle, node);
  83. if (node == home_node)
  84. {
  85. local_interface->nzval = csr_interface->nzval;
  86. local_interface->colind = csr_interface->colind;
  87. }
  88. else
  89. {
  90. local_interface->nzval = 0;
  91. local_interface->colind = NULL;
  92. }
  93. local_interface->id = csr_interface->id;
  94. local_interface->rowptr = csr_interface->rowptr;
  95. local_interface->nnz = csr_interface->nnz;
  96. local_interface->nrow = csr_interface->nrow;
  97. local_interface->firstentry = csr_interface->firstentry;
  98. local_interface->elemsize = csr_interface->elemsize;
  99. }
  100. }
  101. /* declare a new data with the BLAS interface */
  102. void starpu_csr_data_register(starpu_data_handle_t *handleptr, int home_node,
  103. uint32_t nnz, uint32_t nrow, uintptr_t nzval, uint32_t *colind, uint32_t *rowptr, uint32_t firstentry, size_t elemsize)
  104. {
  105. struct starpu_csr_interface csr_interface =
  106. {
  107. .id = STARPU_CSR_INTERFACE_ID,
  108. .nnz = nnz,
  109. .nrow = nrow,
  110. .nzval = nzval,
  111. .colind = colind,
  112. .rowptr = rowptr,
  113. .firstentry = firstentry,
  114. .elemsize = elemsize
  115. };
  116. #ifndef STARPU_SIMGRID
  117. if (home_node >= 0 && starpu_node_get_kind(home_node) == STARPU_CPU_RAM)
  118. {
  119. STARPU_ASSERT_ACCESSIBLE(nzval);
  120. STARPU_ASSERT_ACCESSIBLE(nzval + nnz*elemsize - 1);
  121. STARPU_ASSERT_ACCESSIBLE(colind);
  122. STARPU_ASSERT_ACCESSIBLE((uintptr_t) colind + nnz*sizeof(uint32_t) - 1);
  123. STARPU_ASSERT_ACCESSIBLE(rowptr);
  124. STARPU_ASSERT_ACCESSIBLE((uintptr_t) rowptr + (nrow+1)*sizeof(uint32_t) - 1);
  125. }
  126. #endif
  127. starpu_data_register(handleptr, home_node, &csr_interface, &starpu_interface_csr_ops);
  128. }
  129. static uint32_t footprint_csr_interface_crc32(starpu_data_handle_t handle)
  130. {
  131. return starpu_hash_crc32c_be(starpu_csr_get_nnz(handle), 0);
  132. }
  133. static int csr_compare(void *data_interface_a, void *data_interface_b)
  134. {
  135. struct starpu_csr_interface *csr_a = (struct starpu_csr_interface *) data_interface_a;
  136. struct starpu_csr_interface *csr_b = (struct starpu_csr_interface *) data_interface_b;
  137. /* Two matricess are considered compatible if they have the same size */
  138. return (csr_a->nnz == csr_b->nnz)
  139. && (csr_a->nrow == csr_b->nrow)
  140. && (csr_a->elemsize == csr_b->elemsize);
  141. }
  142. /* offer an access to the data parameters */
  143. uint32_t starpu_csr_get_nnz(starpu_data_handle_t handle)
  144. {
  145. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  146. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  147. #ifdef STARPU_DEBUG
  148. STARPU_ASSERT_MSG(csr_interface->id == STARPU_CSR_INTERFACE_ID, "Error. The given data is not a csr.");
  149. #endif
  150. return csr_interface->nnz;
  151. }
  152. uint32_t starpu_csr_get_nrow(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, STARPU_MAIN_RAM);
  156. #ifdef STARPU_DEBUG
  157. STARPU_ASSERT_MSG(csr_interface->id == STARPU_CSR_INTERFACE_ID, "Error. The given data is not a csr.");
  158. #endif
  159. return csr_interface->nrow;
  160. }
  161. uint32_t starpu_csr_get_firstentry(starpu_data_handle_t handle)
  162. {
  163. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  164. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  165. #ifdef STARPU_DEBUG
  166. STARPU_ASSERT_MSG(csr_interface->id == STARPU_CSR_INTERFACE_ID, "Error. The given data is not a csr.");
  167. #endif
  168. return csr_interface->firstentry;
  169. }
  170. size_t starpu_csr_get_elemsize(starpu_data_handle_t handle)
  171. {
  172. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  173. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  174. #ifdef STARPU_DEBUG
  175. STARPU_ASSERT_MSG(csr_interface->id == STARPU_CSR_INTERFACE_ID, "Error. The given data is not a csr.");
  176. #endif
  177. return csr_interface->elemsize;
  178. }
  179. uintptr_t starpu_csr_get_local_nzval(starpu_data_handle_t handle)
  180. {
  181. unsigned node;
  182. node = _starpu_memory_node_get_local_key();
  183. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  184. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  185. starpu_data_get_interface_on_node(handle, node);
  186. #ifdef STARPU_DEBUG
  187. STARPU_ASSERT_MSG(csr_interface->id == STARPU_CSR_INTERFACE_ID, "Error. The given data is not a csr.");
  188. #endif
  189. return csr_interface->nzval;
  190. }
  191. uint32_t *starpu_csr_get_local_colind(starpu_data_handle_t handle)
  192. {
  193. unsigned node;
  194. node = _starpu_memory_node_get_local_key();
  195. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  196. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  197. starpu_data_get_interface_on_node(handle, node);
  198. #ifdef STARPU_DEBUG
  199. STARPU_ASSERT_MSG(csr_interface->id == STARPU_CSR_INTERFACE_ID, "Error. The given data is not a csr.");
  200. #endif
  201. return csr_interface->colind;
  202. }
  203. uint32_t *starpu_csr_get_local_rowptr(starpu_data_handle_t handle)
  204. {
  205. unsigned node;
  206. node = _starpu_memory_node_get_local_key();
  207. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  208. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  209. starpu_data_get_interface_on_node(handle, node);
  210. #ifdef STARPU_DEBUG
  211. STARPU_ASSERT_MSG(csr_interface->id == STARPU_CSR_INTERFACE_ID, "Error. The given data is not a csr.");
  212. #endif
  213. return csr_interface->rowptr;
  214. }
  215. static size_t csr_interface_get_size(starpu_data_handle_t handle)
  216. {
  217. size_t size;
  218. uint32_t nnz = starpu_csr_get_nnz(handle);
  219. uint32_t nrow = starpu_csr_get_nrow(handle);
  220. size_t elemsize = starpu_csr_get_elemsize(handle);
  221. size = nnz*elemsize + nnz*sizeof(uint32_t) + (nrow+1)*sizeof(uint32_t);
  222. return size;
  223. }
  224. /* memory allocation/deallocation primitives for the BLAS interface */
  225. /* returns the size of the allocated area */
  226. static starpu_ssize_t allocate_csr_buffer_on_node(void *data_interface_, unsigned dst_node)
  227. {
  228. uintptr_t addr_nzval = 0;
  229. uint32_t *addr_colind = NULL, *addr_rowptr = NULL;
  230. starpu_ssize_t allocated_memory;
  231. /* we need the 3 arrays to be allocated */
  232. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *) data_interface_;
  233. uint32_t nnz = csr_interface->nnz;
  234. uint32_t nrow = csr_interface->nrow;
  235. size_t elemsize = csr_interface->elemsize;
  236. addr_nzval = starpu_malloc_on_node(dst_node, nnz*elemsize);
  237. if (!addr_nzval)
  238. goto fail_nzval;
  239. addr_colind = (uint32_t*) starpu_malloc_on_node(dst_node, nnz*sizeof(uint32_t));
  240. if (!addr_colind)
  241. goto fail_colind;
  242. addr_rowptr = (uint32_t*) starpu_malloc_on_node(dst_node, (nrow+1)*sizeof(uint32_t));
  243. if (!addr_rowptr)
  244. goto fail_rowptr;
  245. /* allocation succeeded */
  246. allocated_memory =
  247. nnz*elemsize + nnz*sizeof(uint32_t) + (nrow+1)*sizeof(uint32_t);
  248. /* update the data properly in consequence */
  249. csr_interface->nzval = addr_nzval;
  250. csr_interface->colind = addr_colind;
  251. csr_interface->rowptr = addr_rowptr;
  252. return allocated_memory;
  253. fail_rowptr:
  254. starpu_free_on_node(dst_node, (uintptr_t) addr_colind, nnz*sizeof(uint32_t));
  255. fail_colind:
  256. starpu_free_on_node(dst_node, addr_nzval, nnz*elemsize);
  257. fail_nzval:
  258. /* allocation failed */
  259. return -ENOMEM;
  260. }
  261. static void free_csr_buffer_on_node(void *data_interface, unsigned node)
  262. {
  263. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *) data_interface;
  264. uint32_t nnz = csr_interface->nnz;
  265. uint32_t nrow = csr_interface->nrow;
  266. size_t elemsize = csr_interface->elemsize;
  267. starpu_free_on_node(node, csr_interface->nzval, nnz*elemsize);
  268. starpu_free_on_node(node, (uintptr_t) csr_interface->colind, nnz*sizeof(uint32_t));
  269. starpu_free_on_node(node, (uintptr_t) csr_interface->rowptr, (nrow+1)*sizeof(uint32_t));
  270. }
  271. /* as not all platform easily have a BLAS lib installed ... */
  272. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data)
  273. {
  274. struct starpu_csr_interface *src_csr = (struct starpu_csr_interface *) src_interface;
  275. struct starpu_csr_interface *dst_csr = (struct starpu_csr_interface *) dst_interface;
  276. uint32_t nnz = src_csr->nnz;
  277. uint32_t nrow = src_csr->nrow;
  278. size_t elemsize = src_csr->elemsize;
  279. int ret = 0;
  280. if (starpu_interface_copy(src_csr->nzval, 0, src_node, dst_csr->nzval, 0, dst_node, nnz*elemsize, async_data))
  281. ret = -EAGAIN;
  282. if (starpu_interface_copy((uintptr_t)src_csr->colind, 0, src_node, (uintptr_t)dst_csr->colind, 0, dst_node, nnz*sizeof(uint32_t), async_data))
  283. ret = -EAGAIN;
  284. if (starpu_interface_copy((uintptr_t)src_csr->rowptr, 0, src_node, (uintptr_t)dst_csr->rowptr, 0, dst_node, (nrow+1)*sizeof(uint32_t), async_data))
  285. ret = -EAGAIN;
  286. _STARPU_TRACE_DATA_COPY(src_node, dst_node, nnz*elemsize + (nnz+nrow+1)*sizeof(uint32_t));
  287. return ret;
  288. }
  289. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size)
  290. {
  291. struct starpu_csr_interface *csr = (struct starpu_csr_interface *) data_interface;
  292. return snprintf(buf, size, "C%ux%ux%u",
  293. (unsigned) csr->nnz,
  294. (unsigned) csr->nrow,
  295. (unsigned) csr->elemsize);
  296. }
  297. static int pack_data(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count)
  298. {
  299. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  300. struct starpu_csr_interface *csr = (struct starpu_csr_interface *) starpu_data_get_interface_on_node(handle, node);
  301. // We first pack colind
  302. *count = csr->nnz * sizeof(csr->colind[0]);
  303. // Then rowptr
  304. *count += (csr->nrow + 1) * sizeof(csr->rowptr[0]);
  305. // Then nnzval
  306. *count += csr->nnz * csr->elemsize;
  307. if (ptr != NULL)
  308. {
  309. _starpu_malloc_flags_on_node(node, ptr, *count, 0);
  310. char *tmp = *ptr;
  311. memcpy(tmp, (void*)csr->colind, csr->nnz * sizeof(csr->colind[0]));
  312. tmp += csr->nnz * sizeof(csr->colind[0]);
  313. memcpy(tmp, (void*)csr->rowptr, (csr->nrow + 1) * sizeof(csr->rowptr[0]));
  314. tmp += (csr->nrow + 1) * sizeof(csr->rowptr[0]);
  315. memcpy(tmp, (void*)csr->nzval, csr->nnz * csr->elemsize);
  316. }
  317. return 0;
  318. }
  319. static int unpack_data(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  320. {
  321. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  322. struct starpu_csr_interface *csr = (struct starpu_csr_interface *) starpu_data_get_interface_on_node(handle, node);
  323. STARPU_ASSERT(count == (csr->nnz * sizeof(csr->colind[0]))+((csr->nrow + 1) * sizeof(csr->rowptr[0]))+(csr->nnz * csr->elemsize));
  324. char *tmp = ptr;
  325. memcpy((void*)csr->colind, tmp, csr->nnz * sizeof(csr->colind[0]));
  326. tmp += csr->nnz * sizeof(csr->colind[0]);
  327. memcpy((void*)csr->rowptr, tmp, (csr->nrow + 1) * sizeof(csr->rowptr[0]));
  328. tmp += (csr->nrow + 1) * sizeof(csr->rowptr[0]);
  329. memcpy((void*)csr->nzval, tmp, csr->nnz * csr->elemsize);
  330. return 0;
  331. }