dw_block_spmv.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (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_monitor_bcsr_data(&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_get_bcsr_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_monitor_vector_data(&vector_in, 0, (uintptr_t)vector_in_ptr, size, sizeof(float));
  54. starpu_monitor_vector_data(&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_unpartition_data(sparse_matrix, 0);
  67. starpu_unpartition_data(vector_out, 0);
  68. sem_post(&sem);
  69. }
  70. }
  71. void call_filters(void)
  72. {
  73. starpu_filter bcsr_f;
  74. starpu_filter vector_in_f, vector_out_f;
  75. bcsr_f.filter_func = starpu_canonical_block_filter_bcsr;
  76. vector_in_f.filter_func = starpu_block_filter_func_vector;
  77. vector_in_f.filter_arg = size/c;
  78. vector_out_f.filter_func = starpu_block_filter_func_vector;
  79. vector_out_f.filter_arg = size/r;
  80. starpu_partition_data(sparse_matrix, &bcsr_f);
  81. starpu_partition_data(vector_in, &vector_in_f);
  82. starpu_partition_data(vector_out, &vector_out_f);
  83. }
  84. #define NSPMV 32
  85. unsigned totaltasks;
  86. starpu_codelet cl = {
  87. .where = CORE|CUBLAS,
  88. .core_func = core_block_spmv,
  89. #ifdef USE_CUDA
  90. .cublas_func = cublas_block_spmv,
  91. #endif
  92. .nbuffers = 3
  93. };
  94. void launch_spmv_codelets(void)
  95. {
  96. struct starpu_task *task_tab;
  97. uint8_t *is_entry_tab;
  98. /* we call one codelet per block */
  99. unsigned nblocks = starpu_get_bcsr_nnz(sparse_matrix);
  100. unsigned nrows = starpu_get_bcsr_nrow(sparse_matrix);
  101. remainingtasks = NSPMV*nblocks;
  102. totaltasks = remainingtasks;
  103. unsigned taskid = 0;
  104. task_tab = malloc(totaltasks*sizeof(struct starpu_task));
  105. STARPU_ASSERT(task_tab);
  106. is_entry_tab = calloc(totaltasks, sizeof(uint8_t));
  107. STARPU_ASSERT(is_entry_tab);
  108. printf("there will be %d codelets\n", remainingtasks);
  109. uint32_t *rowptr = starpu_get_bcsr_local_rowptr(sparse_matrix);
  110. uint32_t *colind = starpu_get_bcsr_local_colind(sparse_matrix);
  111. gettimeofday(&start, NULL);
  112. unsigned loop;
  113. for (loop = 0; loop < NSPMV; loop++)
  114. {
  115. unsigned row;
  116. unsigned part = 0;
  117. for (row = 0; row < nrows; row++)
  118. {
  119. unsigned index;
  120. if (rowptr[row] == rowptr[row+1])
  121. {
  122. continue;
  123. }
  124. for (index = rowptr[row]; index < rowptr[row+1]; index++, part++)
  125. {
  126. struct starpu_task *task = &task_tab[taskid];
  127. task->use_tag = 1;
  128. task->tag_id = taskid;
  129. task->callback_func = init_problem_callback;
  130. task->callback_arg = &remainingtasks;
  131. task->cl = &cl;
  132. task->cl_arg = NULL;
  133. unsigned i = colind[index];
  134. unsigned j = row;
  135. task->buffers[0].state = get_sub_data(sparse_matrix, 1, part);
  136. task->buffers[0].mode = R;
  137. task->buffers[1].state = get_sub_data(vector_in, 1, i);
  138. task->buffers[1].mode = R;
  139. task->buffers[2].state = get_sub_data(vector_out, 1, j);
  140. task->buffers[2].mode = RW;
  141. /* all tasks in the same row are dependant so that we don't wait too much for data
  142. * we need to wait on the previous task if we are not the first task of a row */
  143. if (index != rowptr[row & ~0x3])
  144. {
  145. /* this is not the first task in the row */
  146. starpu_tag_declare_deps(taskid, 1, taskid-1);
  147. is_entry_tab[taskid] = 0;
  148. }
  149. else {
  150. /* this is an entry task */
  151. is_entry_tab[taskid] = 1;
  152. }
  153. taskid++;
  154. }
  155. }
  156. }
  157. printf("start submitting tasks !\n");
  158. /* submit ALL tasks now */
  159. unsigned nchains = 0;
  160. unsigned task;
  161. for (task = 0; task < totaltasks; task++)
  162. {
  163. if (is_entry_tab[task]) {
  164. nchains++;
  165. }
  166. starpu_submit_task(&task_tab[task]);
  167. }
  168. printf("end of task submission (there was %d chains for %d tasks : ratio %d tasks per chain) !\n", nchains, totaltasks, totaltasks/nchains);
  169. }
  170. void init_problem(void)
  171. {
  172. /* create the sparse input matrix */
  173. create_data();
  174. /* create a new codelet that will perform a SpMV on it */
  175. call_filters();
  176. }
  177. void print_results(void)
  178. {
  179. unsigned row;
  180. for (row = 0; row < STARPU_MIN(size, 16); row++)
  181. {
  182. printf("%2.2f\t%2.2f\n", vector_in_ptr[row], vector_out_ptr[row]);
  183. }
  184. }
  185. int main(__attribute__ ((unused)) int argc,
  186. __attribute__ ((unused)) char **argv)
  187. {
  188. if (argc < 2)
  189. {
  190. fprintf(stderr, "usage : %s filename [tile size]\n", argv[0]);
  191. exit(-1);
  192. }
  193. if (argc == 3)
  194. {
  195. /* third argument is the tile size */
  196. char *argptr;
  197. r = strtol(argv[2], &argptr, 10);
  198. c = r;
  199. }
  200. inputfile = argv[1];
  201. /* start the runtime */
  202. starpu_init(NULL);
  203. sem_init(&sem, 0, 0U);
  204. init_problem();
  205. launch_spmv_codelets();
  206. sem_wait(&sem);
  207. sem_destroy(&sem);
  208. print_results();
  209. double totalflop = 2.0*c*r*totaltasks;
  210. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  211. fprintf(stderr, "Computation took (in ms)\n");
  212. printf("%2.2f\n", timing/1000);
  213. fprintf(stderr, "Flop %e\n", totalflop);
  214. fprintf(stderr, "GFlops : %2.2f\n", totalflop/timing/1000);
  215. return 0;
  216. }