dw_spmv.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. /*
  17. * Conjugate gradients for Sparse matrices
  18. */
  19. #include "dw_spmv.h"
  20. struct timeval start;
  21. struct timeval end;
  22. unsigned nblocks = 1;
  23. unsigned remainingtasks = -1;
  24. #ifdef USE_CUDA
  25. extern void spmv_kernel_cpu_wrapper(uint32_t nnz, uint32_t nrow, float *nzval,
  26. uint32_t *colind, uint32_t *rowptr, uint32_t firstentry,
  27. float *vecin, uint32_t nx_in,
  28. float * vecout, uint32_t nx_out);
  29. void spmv_kernel_cuda(void *descr[], void *args)
  30. {
  31. uint32_t nnz = STARPU_GET_CSR_NNZ(descr[0]);
  32. uint32_t nrow = STARPU_GET_CSR_NROW(descr[0]);
  33. float *nzval = (float *)STARPU_GET_CSR_NZVAL(descr[0]);
  34. uint32_t *colind = STARPU_GET_CSR_COLIND(descr[0]);
  35. uint32_t *rowptr = STARPU_GET_CSR_ROWPTR(descr[0]);
  36. uint32_t firstentry = STARPU_GET_CSR_FIRSTENTRY(descr[0]);
  37. float *vecin = (float *)STARPU_GET_VECTOR_PTR(descr[1]);
  38. uint32_t nx_in = STARPU_GET_VECTOR_NX(descr[1]);
  39. float *vecout = (float *)STARPU_GET_VECTOR_PTR(descr[2]);
  40. uint32_t nx_out = STARPU_GET_VECTOR_NX(descr[2]);
  41. spmv_kernel_cpu_wrapper(nnz, nrow, nzval, colind, rowptr, firstentry, vecin, nx_in, vecout, nx_out);
  42. }
  43. #endif // USE_CUDA
  44. sem_t sem;
  45. uint32_t size = 4194304;
  46. starpu_data_handle sparse_matrix;
  47. starpu_data_handle vector_in, vector_out;
  48. float *sparse_matrix_nzval;
  49. uint32_t *sparse_matrix_colind;
  50. uint32_t *sparse_matrix_rowptr;
  51. float *vector_in_ptr;
  52. float *vector_out_ptr;
  53. unsigned usecpu = 0;
  54. void parse_args(int argc, char **argv)
  55. {
  56. int i;
  57. for (i = 1; i < argc; i++) {
  58. if (strcmp(argv[i], "-size") == 0) {
  59. char *argptr;
  60. size = strtol(argv[++i], &argptr, 10);
  61. }
  62. if (strcmp(argv[i], "-nblocks") == 0) {
  63. char *argptr;
  64. nblocks = strtol(argv[++i], &argptr, 10);
  65. }
  66. if (strcmp(argv[i], "-cpu") == 0) {
  67. usecpu = 1;
  68. }
  69. }
  70. }
  71. void cpu_spmv(void *descr[], __attribute__((unused)) void *arg)
  72. {
  73. float *nzval = (float *)STARPU_GET_CSR_NZVAL(descr[0]);
  74. uint32_t *colind = STARPU_GET_CSR_COLIND(descr[0]);
  75. uint32_t *rowptr = STARPU_GET_CSR_ROWPTR(descr[0]);
  76. float *vecin = (float *)STARPU_GET_VECTOR_PTR(descr[1]);
  77. float *vecout = (float *)STARPU_GET_VECTOR_PTR(descr[2]);
  78. uint32_t firstelem = STARPU_GET_CSR_FIRSTENTRY(descr[0]);
  79. uint32_t nnz;
  80. uint32_t nrow;
  81. nnz = STARPU_GET_CSR_NNZ(descr[0]);
  82. nrow = STARPU_GET_CSR_NROW(descr[0]);
  83. //STARPU_ASSERT(nrow == STARPU_GET_VECTOR_NX(descr[1]));
  84. STARPU_ASSERT(nrow == STARPU_GET_VECTOR_NX(descr[2]));
  85. unsigned row;
  86. for (row = 0; row < nrow; row++)
  87. {
  88. float tmp = 0.0f;
  89. unsigned index;
  90. unsigned firstindex = rowptr[row] - firstelem;
  91. unsigned lastindex = rowptr[row+1] - firstelem;
  92. for (index = firstindex; index < lastindex; index++)
  93. {
  94. unsigned col;
  95. col = colind[index];
  96. tmp += nzval[index]*vecin[col];
  97. }
  98. vecout[row] = tmp;
  99. }
  100. }
  101. void create_data(void)
  102. {
  103. /* we need a sparse symetric (definite positive ?) matrix and a "dense" vector */
  104. /* example of 3-band matrix */
  105. float *nzval;
  106. uint32_t nnz;
  107. uint32_t *colind;
  108. uint32_t *rowptr;
  109. nnz = 3*size-2;
  110. nzval = malloc(nnz*sizeof(float));
  111. colind = malloc(nnz*sizeof(uint32_t));
  112. rowptr = malloc((size+1)*sizeof(uint32_t));
  113. assert(nzval);
  114. assert(colind);
  115. assert(rowptr);
  116. /* fill the matrix */
  117. unsigned row;
  118. unsigned pos = 0;
  119. for (row = 0; row < size; row++)
  120. {
  121. rowptr[row] = pos;
  122. if (row > 0) {
  123. nzval[pos] = 1.0f;
  124. colind[pos] = row-1;
  125. pos++;
  126. }
  127. nzval[pos] = 5.0f;
  128. colind[pos] = row;
  129. pos++;
  130. if (row < size - 1) {
  131. nzval[pos] = 1.0f;
  132. colind[pos] = row+1;
  133. pos++;
  134. }
  135. }
  136. STARPU_ASSERT(pos == nnz);
  137. rowptr[size] = nnz;
  138. starpu_register_csr_data(&sparse_matrix, 0, nnz, size, (uintptr_t)nzval, colind, rowptr, 0, sizeof(float));
  139. sparse_matrix_nzval = nzval;
  140. sparse_matrix_colind = colind;
  141. sparse_matrix_rowptr = rowptr;
  142. /* initiate the 2 vectors */
  143. float *invec, *outvec;
  144. invec = malloc(size*sizeof(float));
  145. assert(invec);
  146. outvec = malloc(size*sizeof(float));
  147. assert(outvec);
  148. /* fill those */
  149. unsigned ind;
  150. for (ind = 0; ind < size; ind++)
  151. {
  152. invec[ind] = 2.0f;
  153. outvec[ind] = 0.0f;
  154. }
  155. starpu_register_vector_data(&vector_in, 0, (uintptr_t)invec, size, sizeof(float));
  156. starpu_register_vector_data(&vector_out, 0, (uintptr_t)outvec, size, sizeof(float));
  157. vector_in_ptr = invec;
  158. vector_out_ptr = outvec;
  159. }
  160. void init_problem_callback(void *arg)
  161. {
  162. unsigned *remaining = arg;
  163. unsigned val = STARPU_ATOMIC_ADD(remaining, -1);
  164. printf("callback %d remaining \n", val);
  165. if ( val == 0 )
  166. {
  167. printf("DONE ...\n");
  168. gettimeofday(&end, NULL);
  169. starpu_unpartition_data(sparse_matrix, 0);
  170. starpu_unpartition_data(vector_out, 0);
  171. sem_post(&sem);
  172. }
  173. }
  174. void call_spmv_codelet_filters(void)
  175. {
  176. remainingtasks = nblocks;
  177. starpu_codelet *cl = malloc(sizeof(starpu_codelet));
  178. /* partition the data along a block distribution */
  179. starpu_filter csr_f, vector_f;
  180. csr_f.filter_func = starpu_vertical_block_filter_func_csr;
  181. csr_f.filter_arg = nblocks;
  182. vector_f.filter_func = starpu_block_filter_func_vector;
  183. vector_f.filter_arg = nblocks;
  184. starpu_partition_data(sparse_matrix, &csr_f);
  185. starpu_partition_data(vector_out, &vector_f);
  186. cl->where = STARPU_CPU|STARPU_CUDA;
  187. cl->cpu_func = cpu_spmv;
  188. #ifdef USE_CUDA
  189. cl->cuda_func = spmv_kernel_cuda;
  190. #endif
  191. cl->nbuffers = 3;
  192. gettimeofday(&start, NULL);
  193. unsigned part;
  194. for (part = 0; part < nblocks; part++)
  195. {
  196. struct starpu_task *task = starpu_task_create();
  197. task->callback_func = init_problem_callback;
  198. task->callback_arg = &remainingtasks;
  199. task->cl = cl;
  200. task->cl_arg = NULL;
  201. task->buffers[0].handle = get_sub_data(sparse_matrix, 1, part);
  202. task->buffers[0].mode = STARPU_R;
  203. task->buffers[1].handle = vector_in;
  204. task->buffers[1].mode = STARPU_R;
  205. task->buffers[2].handle = get_sub_data(vector_out, 1, part);
  206. task->buffers[2].mode = STARPU_W;
  207. starpu_submit_task(task);
  208. }
  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_spmv_codelet_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(__attribute__ ((unused)) int argc,
  226. __attribute__ ((unused)) char **argv)
  227. {
  228. parse_args(argc, argv);
  229. /* start the runtime */
  230. starpu_init(NULL);
  231. sem_init(&sem, 0, 0U);
  232. init_problem();
  233. sem_wait(&sem);
  234. sem_destroy(&sem);
  235. print_results();
  236. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  237. fprintf(stderr, "Computation took (in ms)\n");
  238. printf("%2.2f\n", timing/1000);
  239. return 0;
  240. }