spmv.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2010 Mehdi Juhoor
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. /*
  18. * This computes an SPMV with a CSR sparse matrix, by splitting it in
  19. * horizontal stripes and processing them in parallel.
  20. */
  21. #include "spmv.h"
  22. unsigned nblocks = 4;
  23. uint32_t size = 4*1024*1024;
  24. starpu_data_handle_t sparse_matrix;
  25. starpu_data_handle_t vector_in, vector_out;
  26. static void parse_args(int argc, char **argv)
  27. {
  28. int i;
  29. for (i = 1; i < argc; i++)
  30. {
  31. if (strcmp(argv[i], "-size") == 0)
  32. {
  33. char *argptr;
  34. size = strtol(argv[++i], &argptr, 10);
  35. }
  36. if (strcmp(argv[i], "-nblocks") == 0)
  37. {
  38. char *argptr;
  39. nblocks = strtol(argv[++i], &argptr, 10);
  40. }
  41. }
  42. }
  43. /* partition the CSR matrix along a block distribution */
  44. static struct starpu_data_filter csr_f =
  45. {
  46. .filter_func = starpu_csr_filter_vertical_block,
  47. /* This value is defined later on */
  48. .nchildren = -1,
  49. /* the children also use a csr interface */
  50. };
  51. static struct starpu_data_filter vector_f =
  52. {
  53. .filter_func = starpu_vector_filter_block,
  54. /* This value is defined later on */
  55. .nchildren = -1,
  56. };
  57. static struct starpu_codelet spmv_cl =
  58. {
  59. .cpu_funcs = {spmv_kernel_cpu},
  60. .cpu_funcs_name = {"spmv_kernel_cpu"},
  61. #ifdef STARPU_USE_CUDA
  62. .cuda_funcs = {spmv_kernel_cuda},
  63. .cuda_flags = {STARPU_CUDA_ASYNC},
  64. #endif
  65. #ifdef STARPU_USE_OPENCL
  66. .opencl_funcs = {spmv_kernel_opencl},
  67. .opencl_flags = {STARPU_OPENCL_ASYNC},
  68. #endif
  69. .nbuffers = 3,
  70. .modes = {STARPU_R, STARPU_R, STARPU_W},
  71. .model = NULL,
  72. .name = "spmv"
  73. };
  74. int main(int argc, char **argv)
  75. {
  76. int ret;
  77. unsigned part;
  78. double timing;
  79. double start, end;
  80. unsigned row, pos;
  81. unsigned ind;
  82. /* CSR matrix description */
  83. float *nzval;
  84. uint32_t nnz;
  85. uint32_t *colind;
  86. uint32_t *rowptr;
  87. /* Input and Output vectors */
  88. float *vector_in_ptr;
  89. float *vector_out_ptr;
  90. float *vector_exp_out_ptr;
  91. /*
  92. * Parse command-line arguments
  93. */
  94. parse_args(argc, argv);
  95. /*
  96. * Launch StarPU
  97. */
  98. ret = starpu_init(NULL);
  99. if (ret == -ENODEV)
  100. return 77;
  101. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  102. /*
  103. * Create a 3-band sparse matrix as input example
  104. */
  105. nnz = 3*size-2;
  106. starpu_malloc((void **)&nzval, nnz*sizeof(float));
  107. starpu_malloc((void **)&colind, nnz*sizeof(uint32_t));
  108. starpu_malloc((void **)&rowptr, (size+1)*sizeof(uint32_t));
  109. assert(nzval && colind && rowptr);
  110. #define UPPER_BAND 1.
  111. #define MIDDLE_BAND 5.
  112. #define LOWER_BAND 1.
  113. /* fill the matrix */
  114. for (row = 0, pos = 0; row < size; row++)
  115. {
  116. rowptr[row] = pos;
  117. if (row > 0)
  118. {
  119. nzval[pos] = LOWER_BAND;
  120. colind[pos] = row-1;
  121. pos++;
  122. }
  123. nzval[pos] = MIDDLE_BAND;
  124. colind[pos] = row;
  125. pos++;
  126. if (row < size - 1)
  127. {
  128. nzval[pos] = UPPER_BAND;
  129. colind[pos] = row+1;
  130. pos++;
  131. }
  132. }
  133. STARPU_ASSERT(pos == nnz);
  134. rowptr[size] = nnz;
  135. /* initiate the 2 vectors */
  136. starpu_malloc((void **)&vector_in_ptr, size*sizeof(float));
  137. starpu_malloc((void **)&vector_out_ptr, size*sizeof(float));
  138. starpu_malloc((void **)&vector_exp_out_ptr, size*sizeof(float));
  139. assert(vector_in_ptr && vector_out_ptr && vector_exp_out_ptr);
  140. /* fill them */
  141. for (ind = 0; ind < size; ind++)
  142. {
  143. vector_in_ptr[ind] = ind % 100;
  144. vector_out_ptr[ind] = 0.0f;
  145. }
  146. /*
  147. * Register the CSR matrix and the 2 vectors
  148. */
  149. starpu_csr_data_register(&sparse_matrix, STARPU_MAIN_RAM, nnz, size, (uintptr_t)nzval, colind, rowptr, 0, sizeof(float));
  150. starpu_vector_data_register(&vector_in, STARPU_MAIN_RAM, (uintptr_t)vector_in_ptr, size, sizeof(float));
  151. starpu_vector_data_register(&vector_out, STARPU_MAIN_RAM, (uintptr_t)vector_out_ptr, size, sizeof(float));
  152. /*
  153. * Partition the CSR matrix and the output vector
  154. */
  155. csr_f.nchildren = nblocks;
  156. vector_f.nchildren = nblocks;
  157. starpu_data_partition(sparse_matrix, &csr_f);
  158. starpu_data_partition(vector_out, &vector_f);
  159. /*
  160. * If we use OpenCL, we need to compile the SpMV kernel
  161. */
  162. #ifdef STARPU_USE_OPENCL
  163. compile_spmv_opencl_kernel();
  164. #endif
  165. start = starpu_timing_now();
  166. /*
  167. * Create and submit StarPU tasks
  168. */
  169. for (part = 0; part < nblocks; part++)
  170. {
  171. struct starpu_task *task = starpu_task_create();
  172. task->cl = &spmv_cl;
  173. task->handles[0] = starpu_data_get_sub_data(sparse_matrix, 1, part);
  174. task->handles[1] = vector_in;
  175. task->handles[2] = starpu_data_get_sub_data(vector_out, 1, part);
  176. ret = starpu_task_submit(task);
  177. if (STARPU_UNLIKELY(ret == -ENODEV))
  178. {
  179. FPRINTF(stderr, "No worker may execute this task\n");
  180. exit(0);
  181. }
  182. }
  183. starpu_task_wait_for_all();
  184. end = starpu_timing_now();
  185. /*
  186. * Unregister the CSR matrix and the output vector
  187. */
  188. starpu_data_unpartition(sparse_matrix, STARPU_MAIN_RAM);
  189. starpu_data_unpartition(vector_out, STARPU_MAIN_RAM);
  190. /*
  191. * Unregister data
  192. */
  193. starpu_data_unregister(sparse_matrix);
  194. starpu_data_unregister(vector_in);
  195. starpu_data_unregister(vector_out);
  196. /*
  197. * Display the result
  198. */
  199. for (row = 0; row < STARPU_MIN(size, 16); row++)
  200. {
  201. FPRINTF(stdout, "%2.2f\t%2.2f\n", vector_in_ptr[row], vector_out_ptr[row]);
  202. }
  203. /* Check the result */
  204. memset(vector_exp_out_ptr, 0, sizeof(vector_exp_out_ptr[0])*size);
  205. for (row = 0; row < size; row++)
  206. {
  207. if (row > 0)
  208. vector_exp_out_ptr[row] += LOWER_BAND * vector_in_ptr[row-1];
  209. vector_exp_out_ptr[row] += MIDDLE_BAND * vector_in_ptr[row];
  210. if (row < size-1)
  211. vector_exp_out_ptr[row] += UPPER_BAND * vector_in_ptr[row+1];
  212. }
  213. for (row = 0; row < size; row++)
  214. {
  215. if (vector_out_ptr[row] != vector_exp_out_ptr[row])
  216. {
  217. FPRINTF(stderr, "check failed at %u: %f vs expected %f\n", row, vector_out_ptr[row], vector_exp_out_ptr[row]);
  218. exit(EXIT_FAILURE);
  219. }
  220. }
  221. starpu_free_noflag(nzval, nnz*sizeof(float));
  222. starpu_free_noflag(colind, nnz*sizeof(uint32_t));
  223. starpu_free_noflag(rowptr, (size+1)*sizeof(uint32_t));
  224. starpu_free_noflag(vector_in_ptr, size*sizeof(float));
  225. starpu_free_noflag(vector_out_ptr, size*sizeof(float));
  226. starpu_free_noflag(vector_exp_out_ptr, size*sizeof(float));
  227. /*
  228. * Stop StarPU
  229. */
  230. starpu_shutdown();
  231. timing = end - start;
  232. FPRINTF(stderr, "Computation took (in ms)\n");
  233. FPRINTF(stdout, "%2.2f\n", timing/1000);
  234. return 0;
  235. }