spmv.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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,2020 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. /* partition the CSR matrix along a block distribution */
  46. static struct starpu_data_filter csr_f =
  47. {
  48. .filter_func = starpu_csr_filter_vertical_block,
  49. /* This value is defined later on */
  50. .nchildren = -1,
  51. /* the children also use a csr interface */
  52. };
  53. static struct starpu_data_filter vector_f =
  54. {
  55. .filter_func = starpu_vector_filter_block,
  56. /* This value is defined later on */
  57. .nchildren = -1,
  58. };
  59. static struct starpu_codelet spmv_cl =
  60. {
  61. .cpu_funcs = {spmv_kernel_cpu},
  62. .cpu_funcs_name = {"spmv_kernel_cpu"},
  63. #ifdef STARPU_USE_CUDA
  64. .cuda_funcs = {spmv_kernel_cuda},
  65. .cuda_flags = {STARPU_CUDA_ASYNC},
  66. #endif
  67. #ifdef STARPU_USE_OPENCL
  68. .opencl_funcs = {spmv_kernel_opencl},
  69. .opencl_flags = {STARPU_OPENCL_ASYNC},
  70. #endif
  71. .nbuffers = 3,
  72. .modes = {STARPU_R, STARPU_R, STARPU_W},
  73. .model = NULL,
  74. .name = "spmv"
  75. };
  76. int main(int argc, char **argv)
  77. {
  78. int ret;
  79. unsigned part;
  80. double timing;
  81. double start, end;
  82. unsigned row, pos;
  83. unsigned ind;
  84. /* CSR matrix description */
  85. float *nzval;
  86. uint32_t nnz;
  87. uint32_t *colind;
  88. uint32_t *rowptr;
  89. /* Input and Output vectors */
  90. float *vector_in_ptr;
  91. float *vector_out_ptr;
  92. float *vector_exp_out_ptr;
  93. /*
  94. * Parse command-line arguments
  95. */
  96. parse_args(argc, argv);
  97. /*
  98. * Launch StarPU
  99. */
  100. ret = starpu_init(NULL);
  101. if (ret == -ENODEV)
  102. return 77;
  103. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  104. /*
  105. * Create a 3-band sparse matrix as input example
  106. */
  107. nnz = 3*size-2;
  108. starpu_malloc((void **)&nzval, nnz*sizeof(float));
  109. starpu_malloc((void **)&colind, nnz*sizeof(uint32_t));
  110. starpu_malloc((void **)&rowptr, (size+1)*sizeof(uint32_t));
  111. assert(nzval && colind && rowptr);
  112. #define UPPER_BAND 1.
  113. #define MIDDLE_BAND 5.
  114. #define LOWER_BAND 1.
  115. /* fill the matrix */
  116. for (row = 0, pos = 0; row < size; row++)
  117. {
  118. rowptr[row] = pos;
  119. if (row > 0)
  120. {
  121. nzval[pos] = LOWER_BAND;
  122. colind[pos] = row-1;
  123. pos++;
  124. }
  125. nzval[pos] = MIDDLE_BAND;
  126. colind[pos] = row;
  127. pos++;
  128. if (row < size - 1)
  129. {
  130. nzval[pos] = UPPER_BAND;
  131. colind[pos] = row+1;
  132. pos++;
  133. }
  134. }
  135. STARPU_ASSERT(pos == nnz);
  136. rowptr[size] = nnz;
  137. /* initiate the 2 vectors */
  138. starpu_malloc((void **)&vector_in_ptr, size*sizeof(float));
  139. starpu_malloc((void **)&vector_out_ptr, size*sizeof(float));
  140. starpu_malloc((void **)&vector_exp_out_ptr, size*sizeof(float));
  141. assert(vector_in_ptr && vector_out_ptr && vector_exp_out_ptr);
  142. /* fill them */
  143. for (ind = 0; ind < size; ind++)
  144. {
  145. vector_in_ptr[ind] = ind % 100;
  146. vector_out_ptr[ind] = 0.0f;
  147. }
  148. /*
  149. * Register the CSR matrix and the 2 vectors
  150. */
  151. starpu_csr_data_register(&sparse_matrix, STARPU_MAIN_RAM, nnz, size, (uintptr_t)nzval, colind, rowptr, 0, sizeof(float));
  152. starpu_vector_data_register(&vector_in, STARPU_MAIN_RAM, (uintptr_t)vector_in_ptr, size, sizeof(float));
  153. starpu_vector_data_register(&vector_out, STARPU_MAIN_RAM, (uintptr_t)vector_out_ptr, size, sizeof(float));
  154. /*
  155. * Partition the CSR matrix and the output vector
  156. */
  157. csr_f.nchildren = nblocks;
  158. vector_f.nchildren = nblocks;
  159. starpu_data_partition(sparse_matrix, &csr_f);
  160. starpu_data_partition(vector_out, &vector_f);
  161. /*
  162. * If we use OpenCL, we need to compile the SpMV kernel
  163. */
  164. #ifdef STARPU_USE_OPENCL
  165. compile_spmv_opencl_kernel();
  166. #endif
  167. start = starpu_timing_now();
  168. /*
  169. * Create and submit StarPU tasks
  170. */
  171. for (part = 0; part < nblocks; part++)
  172. {
  173. struct starpu_task *task = starpu_task_create();
  174. task->cl = &spmv_cl;
  175. task->handles[0] = starpu_data_get_sub_data(sparse_matrix, 1, part);
  176. task->handles[1] = vector_in;
  177. task->handles[2] = starpu_data_get_sub_data(vector_out, 1, part);
  178. ret = starpu_task_submit(task);
  179. if (STARPU_UNLIKELY(ret == -ENODEV))
  180. {
  181. FPRINTF(stderr, "No worker may execute this task\n");
  182. exit(0);
  183. }
  184. }
  185. starpu_task_wait_for_all();
  186. end = starpu_timing_now();
  187. /*
  188. * Unregister the CSR matrix and the output vector
  189. */
  190. starpu_data_unpartition(sparse_matrix, STARPU_MAIN_RAM);
  191. starpu_data_unpartition(vector_out, STARPU_MAIN_RAM);
  192. /*
  193. * Unregister data
  194. */
  195. starpu_data_unregister(sparse_matrix);
  196. starpu_data_unregister(vector_in);
  197. starpu_data_unregister(vector_out);
  198. /*
  199. * Display the result
  200. */
  201. for (row = 0; row < STARPU_MIN(size, 16); row++)
  202. {
  203. FPRINTF(stdout, "%2.2f\t%2.2f\n", vector_in_ptr[row], vector_out_ptr[row]);
  204. }
  205. /* Check the result */
  206. memset(vector_exp_out_ptr, 0, sizeof(vector_exp_out_ptr[0])*size);
  207. for (row = 0; row < size; row++)
  208. {
  209. if (row > 0)
  210. vector_exp_out_ptr[row] += LOWER_BAND * vector_in_ptr[row-1];
  211. vector_exp_out_ptr[row] += MIDDLE_BAND * vector_in_ptr[row];
  212. if (row < size-1)
  213. vector_exp_out_ptr[row] += UPPER_BAND * vector_in_ptr[row+1];
  214. }
  215. for (row = 0; row < size; row++)
  216. if (vector_out_ptr[row] != vector_exp_out_ptr[row]) {
  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. starpu_free(nzval);
  221. starpu_free(colind);
  222. starpu_free(rowptr);
  223. starpu_free(vector_in_ptr);
  224. starpu_free(vector_out_ptr);
  225. starpu_free(vector_exp_out_ptr);
  226. /*
  227. * Stop StarPU
  228. */
  229. starpu_shutdown();
  230. timing = end - start;
  231. FPRINTF(stderr, "Computation took (in ms)\n");
  232. FPRINTF(stdout, "%2.2f\n", timing/1000);
  233. return 0;
  234. }