csr_interface.c 9.4 KB

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