dw_spmv.c 5.8 KB

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