csr_interface.c 10 KB

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