dw_block_spmv.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include "dw_block_spmv.h"
  17. #include "matrix_market/mm_to_bcsr.h"
  18. struct timeval start;
  19. struct timeval end;
  20. sem_t sem;
  21. unsigned c = 256;
  22. unsigned r = 256;
  23. unsigned remainingtasks = -1;
  24. starpu_data_handle sparse_matrix;
  25. starpu_data_handle vector_in, vector_out;
  26. uint32_t size;
  27. char *inputfile;
  28. bcsr_t *bcsr_matrix;
  29. float *vector_in_ptr;
  30. float *vector_out_ptr;
  31. void create_data(void)
  32. {
  33. /* read the input file */
  34. bcsr_matrix = mm_file_to_bcsr(inputfile, c, r);
  35. /* declare the corresponding block CSR to the runtime */
  36. starpu_bcsr_data_register(&sparse_matrix, 0, bcsr_matrix->nnz_blocks, bcsr_matrix->nrows_blocks,
  37. (uintptr_t)bcsr_matrix->val, bcsr_matrix->colind, bcsr_matrix->rowptr,
  38. 0, bcsr_matrix->r, bcsr_matrix->c, sizeof(float));
  39. size = c*r*starpu_bcsr_get_nnz(sparse_matrix);
  40. // printf("size = %d \n ", size);
  41. /* initiate the 2 vectors */
  42. vector_in_ptr = malloc(size*sizeof(float));
  43. assert(vector_in_ptr);
  44. vector_out_ptr = malloc(size*sizeof(float));
  45. assert(vector_out_ptr);
  46. /* fill those */
  47. unsigned ind;
  48. for (ind = 0; ind < size; ind++)
  49. {
  50. vector_in_ptr[ind] = 2.0f;
  51. vector_out_ptr[ind] = 0.0f;
  52. }
  53. starpu_vector_data_register(&vector_in, 0, (uintptr_t)vector_in_ptr, size, sizeof(float));
  54. starpu_vector_data_register(&vector_out, 0, (uintptr_t)vector_out_ptr, size, sizeof(float));
  55. }
  56. void init_problem_callback(void *arg)
  57. {
  58. unsigned *remaining = arg;
  59. unsigned val = STARPU_ATOMIC_ADD(remaining, -1);
  60. // if (val < 10)
  61. // printf("callback %d remaining \n", val);
  62. if ( val == 0 )
  63. {
  64. printf("DONE ...\n");
  65. gettimeofday(&end, NULL);
  66. // starpu_data_unpartition(sparse_matrix, 0);
  67. starpu_data_unpartition(vector_out, 0);
  68. sem_post(&sem);
  69. }
  70. }
  71. unsigned get_bcsr_nchildren(__attribute__((unused)) struct starpu_data_filter *f, starpu_data_handle handle)
  72. {
  73. return (unsigned)starpu_bcsr_get_nnz(handle);
  74. }
  75. struct starpu_data_interface_ops_t *get_bcsr_child_ops(__attribute__((unused)) struct starpu_data_filter *f, __attribute__((unused)) unsigned child)
  76. {
  77. return &_starpu_interface_matrix_ops;
  78. }
  79. void call_filters(void)
  80. {
  81. struct starpu_data_filter bcsr_f;
  82. struct starpu_data_filter vector_in_f, vector_out_f;
  83. bcsr_f.filter_func = starpu_canonical_block_filter_bcsr;
  84. bcsr_f.get_nchildren = get_bcsr_nchildren;
  85. /* the children use a matrix interface ! */
  86. bcsr_f.get_child_ops = get_bcsr_child_ops;
  87. vector_in_f.filter_func = starpu_block_filter_func_vector;
  88. vector_in_f.filter_arg = size/c;
  89. vector_in_f.get_nchildren = NULL;
  90. vector_in_f.get_child_ops = NULL;
  91. vector_out_f.filter_func = starpu_block_filter_func_vector;
  92. vector_out_f.filter_arg = size/r;
  93. vector_out_f.get_nchildren = NULL;
  94. vector_out_f.get_child_ops = NULL;
  95. starpu_data_partition(sparse_matrix, &bcsr_f);
  96. starpu_data_partition(vector_in, &vector_in_f);
  97. starpu_data_partition(vector_out, &vector_out_f);
  98. }
  99. #define NSPMV 32
  100. unsigned totaltasks;
  101. starpu_codelet cl = {
  102. .where = STARPU_CPU|STARPU_CUDA,
  103. .cpu_func = cpu_block_spmv,
  104. #ifdef STARPU_USE_CUDA
  105. .cuda_func = cublas_block_spmv,
  106. #endif
  107. .nbuffers = 3
  108. };
  109. void launch_spmv_codelets(void)
  110. {
  111. struct starpu_task *task_tab;
  112. uint8_t *is_entry_tab;
  113. /* we call one codelet per block */
  114. unsigned nblocks = starpu_bcsr_get_nnz(sparse_matrix);
  115. unsigned nrows = starpu_bcsr_get_nrow(sparse_matrix);
  116. remainingtasks = NSPMV*nblocks;
  117. totaltasks = remainingtasks;
  118. unsigned taskid = 0;
  119. task_tab = malloc(totaltasks*sizeof(struct starpu_task));
  120. STARPU_ASSERT(task_tab);
  121. is_entry_tab = calloc(totaltasks, sizeof(uint8_t));
  122. STARPU_ASSERT(is_entry_tab);
  123. printf("there will be %d codelets\n", remainingtasks);
  124. uint32_t *rowptr = starpu_bcsr_get_local_rowptr(sparse_matrix);
  125. uint32_t *colind = starpu_bcsr_get_local_colind(sparse_matrix);
  126. gettimeofday(&start, NULL);
  127. unsigned loop;
  128. for (loop = 0; loop < NSPMV; loop++)
  129. {
  130. unsigned row;
  131. unsigned part = 0;
  132. for (row = 0; row < nrows; row++)
  133. {
  134. unsigned index;
  135. if (rowptr[row] == rowptr[row+1])
  136. {
  137. continue;
  138. }
  139. for (index = rowptr[row]; index < rowptr[row+1]; index++, part++)
  140. {
  141. struct starpu_task *task = &task_tab[taskid];
  142. task->use_tag = 1;
  143. task->tag_id = taskid;
  144. task->callback_func = init_problem_callback;
  145. task->callback_arg = &remainingtasks;
  146. task->cl = &cl;
  147. task->cl_arg = NULL;
  148. unsigned i = colind[index];
  149. unsigned j = row;
  150. task->buffers[0].handle = starpu_data_get_sub_data(sparse_matrix, 1, part);
  151. task->buffers[0].mode = STARPU_R;
  152. task->buffers[1].handle = starpu_data_get_sub_data(vector_in, 1, i);
  153. task->buffers[1].mode = STARPU_R;
  154. task->buffers[2].handle = starpu_data_get_sub_data(vector_out, 1, j);
  155. task->buffers[2].mode = STARPU_RW;
  156. /* all tasks in the same row are dependant so that we don't wait too much for data
  157. * we need to wait on the previous task if we are not the first task of a row */
  158. if (index != rowptr[row & ~0x3])
  159. {
  160. /* this is not the first task in the row */
  161. starpu_tag_declare_deps((starpu_tag_t)taskid, 1, (starpu_tag_t)(taskid-1));
  162. is_entry_tab[taskid] = 0;
  163. }
  164. else {
  165. /* this is an entry task */
  166. is_entry_tab[taskid] = 1;
  167. }
  168. taskid++;
  169. }
  170. }
  171. }
  172. printf("start submitting tasks !\n");
  173. /* submit ALL tasks now */
  174. unsigned nchains = 0;
  175. unsigned task;
  176. for (task = 0; task < totaltasks; task++)
  177. {
  178. if (is_entry_tab[task]) {
  179. nchains++;
  180. }
  181. starpu_task_submit(&task_tab[task]);
  182. }
  183. printf("end of task submission (there was %d chains for %d tasks : ratio %d tasks per chain) !\n", nchains, totaltasks, totaltasks/nchains);
  184. }
  185. void init_problem(void)
  186. {
  187. /* create the sparse input matrix */
  188. create_data();
  189. /* create a new codelet that will perform a SpMV on it */
  190. call_filters();
  191. }
  192. void print_results(void)
  193. {
  194. unsigned row;
  195. for (row = 0; row < STARPU_MIN(size, 16); row++)
  196. {
  197. printf("%2.2f\t%2.2f\n", vector_in_ptr[row], vector_out_ptr[row]);
  198. }
  199. }
  200. int main(__attribute__ ((unused)) int argc,
  201. __attribute__ ((unused)) char **argv)
  202. {
  203. if (argc < 2)
  204. {
  205. fprintf(stderr, "usage : %s filename [tile size]\n", argv[0]);
  206. exit(-1);
  207. }
  208. if (argc == 3)
  209. {
  210. /* third argument is the tile size */
  211. char *argptr;
  212. r = strtol(argv[2], &argptr, 10);
  213. c = r;
  214. }
  215. inputfile = argv[1];
  216. /* start the runtime */
  217. starpu_init(NULL);
  218. sem_init(&sem, 0, 0U);
  219. init_problem();
  220. launch_spmv_codelets();
  221. sem_wait(&sem);
  222. sem_destroy(&sem);
  223. print_results();
  224. double totalflop = 2.0*c*r*totaltasks;
  225. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  226. fprintf(stderr, "Computation took (in ms)\n");
  227. printf("%2.2f\n", timing/1000);
  228. fprintf(stderr, "Flop %e\n", totalflop);
  229. fprintf(stderr, "GFlops : %2.2f\n", totalflop/timing/1000);
  230. return 0;
  231. }