spmv.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2013 Inria
  4. * Copyright (C) 2010-2013,2015,2017 CNRS
  5. * Copyright (C) 2009-2011,2013-2015 Université de Bordeaux
  6. * Copyright (C) 2010 Mehdi Juhoor
  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. /*
  20. * This computes an SPMV with a CSR sparse matrix, by splitting it in
  21. * horizontal stripes and processing them in parallel.
  22. */
  23. #include "spmv.h"
  24. unsigned nblocks = 4;
  25. uint32_t size = 4*1024*1024;
  26. starpu_data_handle_t sparse_matrix;
  27. starpu_data_handle_t vector_in, vector_out;
  28. static void parse_args(int argc, char **argv)
  29. {
  30. int i;
  31. for (i = 1; i < argc; i++)
  32. {
  33. if (strcmp(argv[i], "-size") == 0)
  34. {
  35. char *argptr;
  36. size = strtol(argv[++i], &argptr, 10);
  37. }
  38. if (strcmp(argv[i], "-nblocks") == 0)
  39. {
  40. char *argptr;
  41. nblocks = strtol(argv[++i], &argptr, 10);
  42. }
  43. }
  44. }
  45. /* This filter function takes a CSR matrix, and divides it into nparts with the
  46. * same number of rows. */
  47. static void csr_filter_func(void *father_interface, void *child_interface, struct starpu_data_filter *f, unsigned id, unsigned nparts)
  48. {
  49. (void)f;
  50. struct starpu_csr_interface *csr_father = (struct starpu_csr_interface *) father_interface;
  51. struct starpu_csr_interface *csr_child = (struct starpu_csr_interface *) child_interface;
  52. uint32_t nrow = csr_father->nrow;
  53. size_t elemsize = csr_father->elemsize;
  54. uint32_t firstentry = csr_father->firstentry;
  55. /* Every sub-parts should contain the same number of non-zero entries */
  56. uint32_t chunk_size = (nrow + nparts - 1)/nparts;
  57. uint32_t *rowptr = csr_father->rowptr;
  58. uint32_t first_index = id*chunk_size - firstentry;
  59. uint32_t local_firstentry = rowptr[first_index];
  60. uint32_t child_nrow = STARPU_MIN(chunk_size, nrow - id*chunk_size);
  61. uint32_t local_nnz = rowptr[first_index + child_nrow] - rowptr[first_index];
  62. csr_child->id = csr_father->id;
  63. csr_child->nnz = local_nnz;
  64. csr_child->nrow = child_nrow;
  65. csr_child->firstentry = local_firstentry;
  66. csr_child->elemsize = elemsize;
  67. if (csr_father->nzval)
  68. {
  69. csr_child->rowptr = &csr_father->rowptr[first_index];
  70. csr_child->colind = &csr_father->colind[local_firstentry];
  71. csr_child->nzval = csr_father->nzval + local_firstentry * elemsize;
  72. }
  73. }
  74. /* partition the CSR matrix along a block distribution */
  75. static struct starpu_data_filter csr_f =
  76. {
  77. .filter_func = csr_filter_func,
  78. /* This value is defined later on */
  79. .nchildren = -1,
  80. /* the children also use a csr interface */
  81. };
  82. static struct starpu_data_filter vector_f =
  83. {
  84. .filter_func = starpu_vector_filter_block,
  85. /* This value is defined later on */
  86. .nchildren = -1,
  87. };
  88. static struct starpu_codelet spmv_cl =
  89. {
  90. .cpu_funcs = {spmv_kernel_cpu},
  91. .cpu_funcs_name = {"spmv_kernel_cpu"},
  92. #ifdef STARPU_USE_CUDA
  93. .cuda_funcs = {spmv_kernel_cuda},
  94. .cuda_flags = {STARPU_CUDA_ASYNC},
  95. #endif
  96. #ifdef STARPU_USE_OPENCL
  97. .opencl_funcs = {spmv_kernel_opencl},
  98. .opencl_flags = {STARPU_OPENCL_ASYNC},
  99. #endif
  100. .nbuffers = 3,
  101. .modes = {STARPU_R, STARPU_R, STARPU_W},
  102. .model = NULL,
  103. .name = "spmv"
  104. };
  105. int main(int argc, char **argv)
  106. {
  107. int ret;
  108. unsigned part;
  109. double timing;
  110. double start, end;
  111. unsigned row, pos;
  112. unsigned ind;
  113. /* CSR matrix description */
  114. float *nzval;
  115. uint32_t nnz;
  116. uint32_t *colind;
  117. uint32_t *rowptr;
  118. /* Input and Output vectors */
  119. float *vector_in_ptr;
  120. float *vector_out_ptr;
  121. /*
  122. * Parse command-line arguments
  123. */
  124. parse_args(argc, argv);
  125. /*
  126. * Launch StarPU
  127. */
  128. ret = starpu_init(NULL);
  129. if (ret == -ENODEV)
  130. return 77;
  131. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  132. /*
  133. * Create a 3-band sparse matrix as input example
  134. */
  135. nnz = 3*size-2;
  136. starpu_malloc((void **)&nzval, nnz*sizeof(float));
  137. starpu_malloc((void **)&colind, nnz*sizeof(uint32_t));
  138. starpu_malloc((void **)&rowptr, (size+1)*sizeof(uint32_t));
  139. assert(nzval && colind && rowptr);
  140. /* fill the matrix */
  141. for (row = 0, pos = 0; row < size; row++)
  142. {
  143. rowptr[row] = pos;
  144. if (row > 0)
  145. {
  146. nzval[pos] = 1.0f;
  147. colind[pos] = row-1;
  148. pos++;
  149. }
  150. nzval[pos] = 5.0f;
  151. colind[pos] = row;
  152. pos++;
  153. if (row < size - 1)
  154. {
  155. nzval[pos] = 1.0f;
  156. colind[pos] = row+1;
  157. pos++;
  158. }
  159. }
  160. STARPU_ASSERT(pos == nnz);
  161. rowptr[size] = nnz;
  162. /* initiate the 2 vectors */
  163. starpu_malloc((void **)&vector_in_ptr, size*sizeof(float));
  164. starpu_malloc((void **)&vector_out_ptr, size*sizeof(float));
  165. assert(vector_in_ptr && vector_out_ptr);
  166. /* fill them */
  167. for (ind = 0; ind < size; ind++)
  168. {
  169. vector_in_ptr[ind] = 2.0f;
  170. vector_out_ptr[ind] = 0.0f;
  171. }
  172. /*
  173. * Register the CSR matrix and the 2 vectors
  174. */
  175. starpu_csr_data_register(&sparse_matrix, STARPU_MAIN_RAM, nnz, size, (uintptr_t)nzval, colind, rowptr, 0, sizeof(float));
  176. starpu_vector_data_register(&vector_in, STARPU_MAIN_RAM, (uintptr_t)vector_in_ptr, size, sizeof(float));
  177. starpu_vector_data_register(&vector_out, STARPU_MAIN_RAM, (uintptr_t)vector_out_ptr, size, sizeof(float));
  178. /*
  179. * Partition the CSR matrix and the output vector
  180. */
  181. csr_f.nchildren = nblocks;
  182. vector_f.nchildren = nblocks;
  183. starpu_data_partition(sparse_matrix, &csr_f);
  184. starpu_data_partition(vector_out, &vector_f);
  185. /*
  186. * If we use OpenCL, we need to compile the SpMV kernel
  187. */
  188. #ifdef STARPU_USE_OPENCL
  189. compile_spmv_opencl_kernel();
  190. #endif
  191. start = starpu_timing_now();
  192. /*
  193. * Create and submit StarPU tasks
  194. */
  195. for (part = 0; part < nblocks; part++)
  196. {
  197. struct starpu_task *task = starpu_task_create();
  198. task->cl = &spmv_cl;
  199. task->handles[0] = starpu_data_get_sub_data(sparse_matrix, 1, part);
  200. task->handles[1] = vector_in;
  201. task->handles[2] = starpu_data_get_sub_data(vector_out, 1, part);
  202. ret = starpu_task_submit(task);
  203. if (STARPU_UNLIKELY(ret == -ENODEV))
  204. {
  205. FPRINTF(stderr, "No worker may execute this task\n");
  206. exit(0);
  207. }
  208. }
  209. starpu_task_wait_for_all();
  210. end = starpu_timing_now();
  211. /*
  212. * Unregister the CSR matrix and the output vector
  213. */
  214. starpu_data_unpartition(sparse_matrix, STARPU_MAIN_RAM);
  215. starpu_data_unpartition(vector_out, STARPU_MAIN_RAM);
  216. /*
  217. * Unregister data
  218. */
  219. starpu_data_unregister(sparse_matrix);
  220. starpu_data_unregister(vector_in);
  221. starpu_data_unregister(vector_out);
  222. /*
  223. * Display the result
  224. */
  225. for (row = 0; row < STARPU_MIN(size, 16); row++)
  226. {
  227. FPRINTF(stdout, "%2.2f\t%2.2f\n", vector_in_ptr[row], vector_out_ptr[row]);
  228. }
  229. starpu_free(nzval);
  230. starpu_free(colind);
  231. starpu_free(rowptr);
  232. starpu_free(vector_in_ptr);
  233. starpu_free(vector_out_ptr);
  234. /*
  235. * Stop StarPU
  236. */
  237. starpu_shutdown();
  238. timing = end - start;
  239. FPRINTF(stderr, "Computation took (in ms)\n");
  240. FPRINTF(stdout, "%2.2f\n", timing/1000);
  241. return 0;
  242. }