csr_interface.c 13 KB

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