dw_block_spmv.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2008-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 on a BCSR sparse matrix. It simply splits the matrix
  19. * into its blocks, thus turning the problem into mere matrix-vector products
  20. * (GEMV) which can be run in parallel.
  21. */
  22. #include "dw_block_spmv.h"
  23. #include "matrix_market/mm_to_bcsr.h"
  24. #ifdef STARPU_HAVE_HELGRIND_H
  25. #include <valgrind/helgrind.h>
  26. #endif
  27. #ifndef ANNOTATE_HAPPENS_BEFORE
  28. #define ANNOTATE_HAPPENS_BEFORE(obj) ((void)0)
  29. #endif
  30. #ifndef ANNOTATE_HAPPENS_AFTER
  31. #define ANNOTATE_HAPPENS_AFTER(obj) ((void)0)
  32. #endif
  33. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  34. static double start;
  35. static double end;
  36. static sem_t sem;
  37. static unsigned c = 256;
  38. static unsigned r = 256;
  39. static int remainingtasks = -1;
  40. static starpu_data_handle_t sparse_matrix;
  41. static starpu_data_handle_t vector_in, vector_out;
  42. static uint32_t size;
  43. static char *inputfile;
  44. static bcsr_t *bcsr_matrix;
  45. static float *vector_in_ptr;
  46. static float *vector_out_ptr;
  47. void create_data(void)
  48. {
  49. /* read the input file */
  50. bcsr_matrix = mm_file_to_bcsr(inputfile, c, r);
  51. /* declare the corresponding block CSR to the runtime */
  52. starpu_bcsr_data_register(&sparse_matrix, STARPU_MAIN_RAM, bcsr_matrix->nnz_blocks, bcsr_matrix->nrows_blocks,
  53. (uintptr_t)bcsr_matrix->val, bcsr_matrix->colind, bcsr_matrix->rowptr,
  54. 0, bcsr_matrix->r, bcsr_matrix->c, sizeof(float));
  55. size = c*r*starpu_bcsr_get_nnz(sparse_matrix);
  56. /* printf("size = %d \n ", size); */
  57. /* initiate the 2 vectors */
  58. vector_in_ptr = malloc(size*sizeof(float));
  59. assert(vector_in_ptr);
  60. vector_out_ptr = malloc(size*sizeof(float));
  61. assert(vector_out_ptr);
  62. /* fill those */
  63. unsigned ind;
  64. for (ind = 0; ind < size; ind++)
  65. {
  66. vector_in_ptr[ind] = 2.0f;
  67. vector_out_ptr[ind] = 0.0f;
  68. }
  69. starpu_vector_data_register(&vector_in, STARPU_MAIN_RAM, (uintptr_t)vector_in_ptr, size, sizeof(float));
  70. starpu_vector_data_register(&vector_out, STARPU_MAIN_RAM, (uintptr_t)vector_out_ptr, size, sizeof(float));
  71. }
  72. void unregister_data(void)
  73. {
  74. starpu_data_unpartition(sparse_matrix, STARPU_MAIN_RAM);
  75. starpu_data_unregister(sparse_matrix);
  76. starpu_data_unpartition(vector_in, STARPU_MAIN_RAM);
  77. starpu_data_unregister(vector_in);
  78. starpu_data_unpartition(vector_out, STARPU_MAIN_RAM);
  79. starpu_data_unregister(vector_out);
  80. }
  81. void init_problem_callback(void *arg)
  82. {
  83. unsigned *remaining = arg;
  84. unsigned val = STARPU_ATOMIC_ADD(remaining, -1);
  85. ANNOTATE_HAPPENS_BEFORE(&remaining);
  86. /* if (val < 10)
  87. printf("callback %d remaining \n", val); */
  88. if ( val == 0 )
  89. {
  90. ANNOTATE_HAPPENS_AFTER(&remaining);
  91. printf("DONE ...\n");
  92. end = starpu_timing_now();
  93. sem_post(&sem);
  94. }
  95. }
  96. void call_filters(void)
  97. {
  98. struct starpu_data_filter bcsr_f;
  99. struct starpu_data_filter vector_in_f, vector_out_f;
  100. bcsr_f.filter_func = starpu_bcsr_filter_canonical_block;
  101. bcsr_f.get_nchildren = starpu_bcsr_filter_canonical_block_get_nchildren;
  102. /* the children use a matrix interface ! */
  103. bcsr_f.get_child_ops = starpu_bcsr_filter_canonical_block_child_ops;
  104. vector_in_f.filter_func = starpu_vector_filter_block;
  105. vector_in_f.nchildren = size/c;
  106. vector_in_f.get_nchildren = NULL;
  107. vector_in_f.get_child_ops = NULL;
  108. vector_out_f.filter_func = starpu_vector_filter_block;
  109. vector_out_f.nchildren = size/r;
  110. vector_out_f.get_nchildren = NULL;
  111. vector_out_f.get_child_ops = NULL;
  112. starpu_data_partition(sparse_matrix, &bcsr_f);
  113. starpu_data_partition(vector_in, &vector_in_f);
  114. starpu_data_partition(vector_out, &vector_out_f);
  115. }
  116. #define NSPMV 32
  117. unsigned totaltasks;
  118. struct starpu_codelet cl =
  119. {
  120. .cpu_funcs = { cpu_block_spmv},
  121. .cpu_funcs_name = { "cpu_block_spmv" },
  122. #ifdef STARPU_USE_CUDA
  123. .cuda_funcs = {cublas_block_spmv},
  124. #endif
  125. .cuda_flags = {STARPU_CUDA_ASYNC},
  126. .nbuffers = 3,
  127. .modes = {STARPU_R, STARPU_R, STARPU_RW}
  128. };
  129. void launch_spmv_codelets(void)
  130. {
  131. struct starpu_task *task_tab;
  132. uint8_t *is_entry_tab;
  133. /* we call one codelet per block */
  134. unsigned nblocks = starpu_bcsr_get_nnz(sparse_matrix);
  135. unsigned nrows = starpu_bcsr_get_nrow(sparse_matrix);
  136. remainingtasks = NSPMV*nblocks;
  137. totaltasks = remainingtasks;
  138. unsigned taskid = 0;
  139. task_tab = calloc(totaltasks, sizeof(struct starpu_task));
  140. STARPU_ASSERT(task_tab);
  141. is_entry_tab = calloc(totaltasks, sizeof(uint8_t));
  142. STARPU_ASSERT(is_entry_tab);
  143. printf("there will be %d codelets\n", remainingtasks);
  144. uint32_t *rowptr = starpu_bcsr_get_local_rowptr(sparse_matrix);
  145. uint32_t *colind = starpu_bcsr_get_local_colind(sparse_matrix);
  146. start = starpu_timing_now();
  147. unsigned loop;
  148. for (loop = 0; loop < NSPMV; loop++)
  149. {
  150. unsigned row;
  151. unsigned part = 0;
  152. for (row = 0; row < nrows; row++)
  153. {
  154. unsigned index;
  155. if (rowptr[row] == rowptr[row+1])
  156. {
  157. continue;
  158. }
  159. for (index = rowptr[row]; index < rowptr[row+1]; index++, part++)
  160. {
  161. struct starpu_task *task = &task_tab[taskid];
  162. starpu_task_init(task);
  163. task->use_tag = 1;
  164. task->tag_id = taskid;
  165. task->callback_func = init_problem_callback;
  166. task->callback_arg = &remainingtasks;
  167. task->cl = &cl;
  168. task->cl_arg = NULL;
  169. unsigned i = colind[index];
  170. unsigned j = row;
  171. task->handles[0] = starpu_data_get_sub_data(sparse_matrix, 1, part);
  172. task->handles[1] = starpu_data_get_sub_data(vector_in, 1, i);
  173. task->handles[2] = starpu_data_get_sub_data(vector_out, 1, j);
  174. /* all tasks in the same row are dependant so that we don't wait too much for data
  175. * we need to wait on the previous task if we are not the first task of a row */
  176. if (index != rowptr[row & ~0x3])
  177. {
  178. /* this is not the first task in the row */
  179. starpu_tag_declare_deps((starpu_tag_t)taskid, 1, (starpu_tag_t)(taskid-1));
  180. is_entry_tab[taskid] = 0;
  181. }
  182. else
  183. {
  184. /* this is an entry task */
  185. is_entry_tab[taskid] = 1;
  186. }
  187. taskid++;
  188. }
  189. }
  190. }
  191. printf("start submitting tasks !\n");
  192. /* submit ALL tasks now */
  193. unsigned nchains = 0;
  194. unsigned task;
  195. for (task = 0; task < totaltasks; task++)
  196. {
  197. int ret;
  198. if (is_entry_tab[task])
  199. {
  200. nchains++;
  201. }
  202. ret = starpu_task_submit(&task_tab[task]);
  203. if (ret == -ENODEV)
  204. exit(77);
  205. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  206. }
  207. printf("end of task submission (there was %u chains for %u tasks : ratio %u tasks per chain) !\n", nchains, totaltasks, totaltasks/nchains);
  208. free(is_entry_tab);
  209. }
  210. void init_problem(void)
  211. {
  212. /* create the sparse input matrix */
  213. create_data();
  214. /* create a new codelet that will perform a SpMV on it */
  215. call_filters();
  216. }
  217. void print_results(void)
  218. {
  219. unsigned row;
  220. for (row = 0; row < STARPU_MIN(size, 16); row++)
  221. {
  222. printf("%2.2f\t%2.2f\n", vector_in_ptr[row], vector_out_ptr[row]);
  223. }
  224. }
  225. int main(int argc, char *argv[])
  226. {
  227. int ret;
  228. if (argc < 2)
  229. {
  230. FPRINTF(stderr, "usage : %s filename [tile size]\n", argv[0]);
  231. exit(-1);
  232. }
  233. if (argc == 3)
  234. {
  235. /* third argument is the tile size */
  236. char *argptr;
  237. r = strtol(argv[2], &argptr, 10);
  238. c = r;
  239. }
  240. inputfile = argv[1];
  241. /* start the runtime */
  242. ret = starpu_init(NULL);
  243. if (ret == -ENODEV)
  244. return 77;
  245. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  246. starpu_cublas_init();
  247. sem_init(&sem, 0, 0U);
  248. init_problem();
  249. launch_spmv_codelets();
  250. sem_wait(&sem);
  251. sem_destroy(&sem);
  252. unregister_data();
  253. print_results();
  254. double totalflop = 2.0*c*r*totaltasks;
  255. double timing = end - start;
  256. FPRINTF(stderr, "Computation took (in ms)\n");
  257. FPRINTF(stdout, "%2.2f\n", timing/1000);
  258. FPRINTF(stderr, "Flop %e\n", totalflop);
  259. FPRINTF(stderr, "GFlops : %2.2f\n", totalflop/timing/1000);
  260. starpu_shutdown();
  261. return 0;
  262. }