spmv.c 7.0 KB

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