dw_block_spmv.c 7.9 KB

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