csr_interface.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2015 Université de Bordeaux
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011, 2012, 2013, 2014 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 <starpu_hash.h>
  25. #include <starpu_cuda.h>
  26. #include <starpu_opencl.h>
  27. #include <drivers/opencl/driver_opencl.h>
  28. #include <drivers/scc/driver_scc_source.h>
  29. #include <drivers/mic/driver_mic_source.h>
  30. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data);
  31. static const struct starpu_data_copy_methods csr_copy_data_methods_s =
  32. {
  33. .any_to_any = copy_any_to_any,
  34. };
  35. static void register_csr_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  36. static starpu_ssize_t allocate_csr_buffer_on_node(void *data_interface_, unsigned dst_node);
  37. static void free_csr_buffer_on_node(void *data_interface, unsigned node);
  38. static size_t csr_interface_get_size(starpu_data_handle_t handle);
  39. static int csr_compare(void *data_interface_a, void *data_interface_b);
  40. static uint32_t footprint_csr_interface_crc32(starpu_data_handle_t handle);
  41. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size);
  42. struct starpu_data_interface_ops starpu_interface_csr_ops =
  43. {
  44. .register_data_handle = register_csr_handle,
  45. .allocate_data_on_node = allocate_csr_buffer_on_node,
  46. .free_data_on_node = free_csr_buffer_on_node,
  47. .copy_methods = &csr_copy_data_methods_s,
  48. .get_size = csr_interface_get_size,
  49. .interfaceid = STARPU_CSR_INTERFACE_ID,
  50. .interface_size = sizeof(struct starpu_csr_interface),
  51. .footprint = footprint_csr_interface_crc32,
  52. .compare = csr_compare,
  53. .describe = describe
  54. };
  55. static void register_csr_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  56. {
  57. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *) data_interface;
  58. unsigned node;
  59. for (node = 0; node < STARPU_MAXNODES; node++)
  60. {
  61. struct starpu_csr_interface *local_interface = (struct starpu_csr_interface *)
  62. starpu_data_get_interface_on_node(handle, node);
  63. if (node == home_node)
  64. {
  65. local_interface->nzval = csr_interface->nzval;
  66. local_interface->colind = csr_interface->colind;
  67. }
  68. else
  69. {
  70. local_interface->nzval = 0;
  71. local_interface->colind = NULL;
  72. }
  73. local_interface->id = csr_interface->id;
  74. local_interface->rowptr = csr_interface->rowptr;
  75. local_interface->nnz = csr_interface->nnz;
  76. local_interface->nrow = csr_interface->nrow;
  77. local_interface->firstentry = csr_interface->firstentry;
  78. local_interface->elemsize = csr_interface->elemsize;
  79. }
  80. }
  81. /* declare a new data with the BLAS interface */
  82. void starpu_csr_data_register(starpu_data_handle_t *handleptr, unsigned home_node,
  83. uint32_t nnz, uint32_t nrow, uintptr_t nzval, uint32_t *colind, uint32_t *rowptr, uint32_t firstentry, size_t elemsize)
  84. {
  85. struct starpu_csr_interface csr_interface =
  86. {
  87. .id = STARPU_CSR_INTERFACE_ID,
  88. .nnz = nnz,
  89. .nrow = nrow,
  90. .nzval = nzval,
  91. .colind = colind,
  92. .rowptr = rowptr,
  93. .firstentry = firstentry,
  94. .elemsize = elemsize
  95. };
  96. starpu_data_register(handleptr, home_node, &csr_interface, &starpu_interface_csr_ops);
  97. }
  98. static uint32_t footprint_csr_interface_crc32(starpu_data_handle_t handle)
  99. {
  100. return starpu_hash_crc32c_be(starpu_csr_get_nnz(handle), 0);
  101. }
  102. static int csr_compare(void *data_interface_a, void *data_interface_b)
  103. {
  104. struct starpu_csr_interface *csr_a = (struct starpu_csr_interface *) data_interface_a;
  105. struct starpu_csr_interface *csr_b = (struct starpu_csr_interface *) data_interface_b;
  106. /* Two matricess are considered compatible if they have the same size */
  107. return ((csr_a->nnz == csr_b->nnz)
  108. && (csr_a->nrow == csr_b->nrow)
  109. && (csr_a->elemsize == csr_b->elemsize));
  110. }
  111. /* offer an access to the data parameters */
  112. uint32_t starpu_csr_get_nnz(starpu_data_handle_t handle)
  113. {
  114. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  115. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  116. return csr_interface->nnz;
  117. }
  118. uint32_t starpu_csr_get_nrow(starpu_data_handle_t handle)
  119. {
  120. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  121. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  122. return csr_interface->nrow;
  123. }
  124. uint32_t starpu_csr_get_firstentry(starpu_data_handle_t handle)
  125. {
  126. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  127. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  128. return csr_interface->firstentry;
  129. }
  130. size_t starpu_csr_get_elemsize(starpu_data_handle_t handle)
  131. {
  132. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  133. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  134. return csr_interface->elemsize;
  135. }
  136. uintptr_t starpu_csr_get_local_nzval(starpu_data_handle_t handle)
  137. {
  138. unsigned node;
  139. node = _starpu_memory_node_get_local_key();
  140. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  141. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  142. starpu_data_get_interface_on_node(handle, node);
  143. return csr_interface->nzval;
  144. }
  145. uint32_t *starpu_csr_get_local_colind(starpu_data_handle_t handle)
  146. {
  147. unsigned node;
  148. node = _starpu_memory_node_get_local_key();
  149. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  150. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  151. starpu_data_get_interface_on_node(handle, node);
  152. return csr_interface->colind;
  153. }
  154. uint32_t *starpu_csr_get_local_rowptr(starpu_data_handle_t handle)
  155. {
  156. unsigned node;
  157. node = _starpu_memory_node_get_local_key();
  158. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  159. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *)
  160. starpu_data_get_interface_on_node(handle, node);
  161. return csr_interface->rowptr;
  162. }
  163. static size_t csr_interface_get_size(starpu_data_handle_t handle)
  164. {
  165. size_t size;
  166. uint32_t nnz = starpu_csr_get_nnz(handle);
  167. uint32_t nrow = starpu_csr_get_nrow(handle);
  168. size_t elemsize = starpu_csr_get_elemsize(handle);
  169. size = nnz*elemsize + nnz*sizeof(uint32_t) + (nrow+1)*sizeof(uint32_t);
  170. return size;
  171. }
  172. /* memory allocation/deallocation primitives for the BLAS interface */
  173. /* returns the size of the allocated area */
  174. static starpu_ssize_t allocate_csr_buffer_on_node(void *data_interface_, unsigned dst_node)
  175. {
  176. uintptr_t addr_nzval = 0;
  177. uint32_t *addr_colind = NULL, *addr_rowptr = NULL;
  178. starpu_ssize_t allocated_memory;
  179. /* we need the 3 arrays to be allocated */
  180. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *) data_interface_;
  181. uint32_t nnz = csr_interface->nnz;
  182. uint32_t nrow = csr_interface->nrow;
  183. size_t elemsize = csr_interface->elemsize;
  184. addr_nzval = starpu_malloc_on_node(dst_node, nnz*elemsize);
  185. if (!addr_nzval)
  186. goto fail_nzval;
  187. addr_colind = (uint32_t*) starpu_malloc_on_node(dst_node, nnz*sizeof(uint32_t));
  188. if (!addr_colind)
  189. goto fail_colind;
  190. addr_rowptr = (uint32_t*) starpu_malloc_on_node(dst_node, (nrow+1)*sizeof(uint32_t));
  191. if (!addr_rowptr)
  192. goto fail_rowptr;
  193. /* allocation succeeded */
  194. allocated_memory =
  195. nnz*elemsize + nnz*sizeof(uint32_t) + (nrow+1)*sizeof(uint32_t);
  196. /* update the data properly in consequence */
  197. csr_interface->nzval = addr_nzval;
  198. csr_interface->colind = addr_colind;
  199. csr_interface->rowptr = addr_rowptr;
  200. return allocated_memory;
  201. fail_rowptr:
  202. starpu_free_on_node(dst_node, (uintptr_t) addr_colind, nnz*sizeof(uint32_t));
  203. fail_colind:
  204. starpu_free_on_node(dst_node, addr_nzval, nnz*elemsize);
  205. fail_nzval:
  206. /* allocation failed */
  207. return -ENOMEM;
  208. }
  209. static void free_csr_buffer_on_node(void *data_interface, unsigned node)
  210. {
  211. struct starpu_csr_interface *csr_interface = (struct starpu_csr_interface *) data_interface;
  212. uint32_t nnz = csr_interface->nnz;
  213. uint32_t nrow = csr_interface->nrow;
  214. size_t elemsize = csr_interface->elemsize;
  215. starpu_free_on_node(node, csr_interface->nzval, nnz*elemsize);
  216. starpu_free_on_node(node, (uintptr_t) csr_interface->colind, nnz*sizeof(uint32_t));
  217. starpu_free_on_node(node, (uintptr_t) csr_interface->rowptr, (nrow+1)*sizeof(uint32_t));
  218. }
  219. /* as not all platform easily have a BLAS lib installed ... */
  220. static int copy_any_to_any(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data)
  221. {
  222. struct starpu_csr_interface *src_csr = (struct starpu_csr_interface *) src_interface;
  223. struct starpu_csr_interface *dst_csr = (struct starpu_csr_interface *) dst_interface;
  224. uint32_t nnz = src_csr->nnz;
  225. uint32_t nrow = src_csr->nrow;
  226. size_t elemsize = src_csr->elemsize;
  227. int ret = 0;
  228. if (starpu_interface_copy(src_csr->nzval, 0, src_node, dst_csr->nzval, 0, dst_node, nnz*elemsize, async_data))
  229. ret = -EAGAIN;
  230. 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))
  231. ret = -EAGAIN;
  232. 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))
  233. ret = -EAGAIN;
  234. _STARPU_TRACE_DATA_COPY(src_node, dst_node, nnz*elemsize + (nnz+nrow+1)*sizeof(uint32_t));
  235. return ret;
  236. }
  237. static starpu_ssize_t describe(void *data_interface, char *buf, size_t size)
  238. {
  239. struct starpu_csr_interface *csr = (struct starpu_csr_interface *) data_interface;
  240. return snprintf(buf, size, "C%ux%ux%u",
  241. (unsigned) csr->nnz,
  242. (unsigned) csr->nrow,
  243. (unsigned) csr->elemsize);
  244. }