spmv.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2011 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011 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 "spmv.h"
  19. unsigned nblocks = 4;
  20. uint32_t size = 4*1024*1024;
  21. starpu_data_handle sparse_matrix;
  22. starpu_data_handle vector_in, vector_out;
  23. static void parse_args(int argc, char **argv)
  24. {
  25. int i;
  26. for (i = 1; i < argc; i++) {
  27. if (strcmp(argv[i], "-size") == 0) {
  28. char *argptr;
  29. size = strtol(argv[++i], &argptr, 10);
  30. }
  31. if (strcmp(argv[i], "-nblocks") == 0) {
  32. char *argptr;
  33. nblocks = strtol(argv[++i], &argptr, 10);
  34. }
  35. }
  36. }
  37. /* This filter function takes a CSR matrix, and divides it into nparts with the
  38. * same number of non-zero entries. */
  39. static void csr_filter_func(void *father_interface, void *child_interface, struct starpu_data_filter *f, unsigned id, unsigned nparts)
  40. {
  41. starpu_csr_interface_t *csr_father = father_interface;
  42. starpu_csr_interface_t *csr_child = child_interface;
  43. uint32_t nrow = csr_father->nrow;
  44. size_t elemsize = csr_father->elemsize;
  45. uint32_t firstentry = csr_father->firstentry;
  46. /* Every sub-parts should contain the same number of non-zero entries */
  47. uint32_t chunk_size = (nrow + nparts - 1)/nparts;
  48. uint32_t *rowptr = csr_father->rowptr;
  49. uint32_t first_index = id*chunk_size - firstentry;
  50. uint32_t local_firstentry = rowptr[first_index];
  51. uint32_t child_nrow = STARPU_MIN(chunk_size, nrow - id*chunk_size);
  52. uint32_t local_nnz = rowptr[first_index + child_nrow] - rowptr[first_index];
  53. csr_child->nnz = local_nnz;
  54. csr_child->nrow = child_nrow;
  55. csr_child->firstentry = local_firstentry;
  56. csr_child->elemsize = elemsize;
  57. if (csr_father->nzval) {
  58. csr_child->rowptr = &csr_father->rowptr[first_index];
  59. csr_child->colind = &csr_father->colind[local_firstentry];
  60. csr_child->nzval = csr_father->nzval + local_firstentry * elemsize;
  61. }
  62. }
  63. /* partition the CSR matrix along a block distribution */
  64. static struct starpu_data_filter csr_f = {
  65. .filter_func = csr_filter_func,
  66. /* This value is defined later on */
  67. .nchildren = -1,
  68. /* the children also use a csr interface */
  69. };
  70. static struct starpu_data_filter vector_f = {
  71. .filter_func = starpu_block_filter_func_vector,
  72. /* This value is defined later on */
  73. .nchildren = -1,
  74. };
  75. static starpu_codelet spmv_cl = {
  76. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  77. .cpu_func = spmv_kernel_cpu,
  78. #ifdef STARPU_USE_CUDA
  79. .cuda_func = spmv_kernel_cuda,
  80. #endif
  81. #ifdef STARPU_USE_OPENCL
  82. .opencl_func = spmv_kernel_opencl,
  83. #endif
  84. .nbuffers = 3,
  85. .model = NULL
  86. };
  87. int main(int argc, char **argv)
  88. {
  89. int ret;
  90. unsigned part;
  91. double timing;
  92. struct timeval start, end;
  93. unsigned row, pos;
  94. unsigned ind;
  95. /* CSR matrix description */
  96. float *nzval;
  97. uint32_t nnz;
  98. uint32_t *colind;
  99. uint32_t *rowptr;
  100. /* Input and Output vectors */
  101. float *vector_in_ptr;
  102. float *vector_out_ptr;
  103. /*
  104. * Parse command-line arguments
  105. */
  106. parse_args(argc, argv);
  107. /*
  108. * Launch StarPU
  109. */
  110. starpu_init(NULL);
  111. /*
  112. * Create a 3-band sparse matrix as input example
  113. */
  114. nnz = 3*size-2;
  115. starpu_malloc((void **)&nzval, nnz*sizeof(float));
  116. starpu_malloc((void **)&colind, nnz*sizeof(uint32_t));
  117. starpu_malloc((void **)&rowptr, (size+1)*sizeof(uint32_t));
  118. assert(nzval && colind && rowptr);
  119. /* fill the matrix */
  120. for (row = 0, pos = 0; row < size; row++)
  121. {
  122. rowptr[row] = pos;
  123. if (row > 0) {
  124. nzval[pos] = 1.0f;
  125. colind[pos] = row-1;
  126. pos++;
  127. }
  128. nzval[pos] = 5.0f;
  129. colind[pos] = row;
  130. pos++;
  131. if (row < size - 1) {
  132. nzval[pos] = 1.0f;
  133. colind[pos] = row+1;
  134. pos++;
  135. }
  136. }
  137. STARPU_ASSERT(pos == nnz);
  138. rowptr[size] = nnz;
  139. /* initiate the 2 vectors */
  140. starpu_malloc((void **)&vector_in_ptr, size*sizeof(float));
  141. starpu_malloc((void **)&vector_out_ptr, size*sizeof(float));
  142. assert(vector_in_ptr && vector_out_ptr);
  143. /* fill them */
  144. for (ind = 0; ind < size; ind++)
  145. {
  146. vector_in_ptr[ind] = 2.0f;
  147. vector_out_ptr[ind] = 0.0f;
  148. }
  149. /*
  150. * Register the CSR matrix and the 2 vectors
  151. */
  152. starpu_csr_data_register(&sparse_matrix, 0, nnz, size, (uintptr_t)nzval, colind, rowptr, 0, sizeof(float));
  153. starpu_vector_data_register(&vector_in, 0, (uintptr_t)vector_in_ptr, size, sizeof(float));
  154. starpu_vector_data_register(&vector_out, 0, (uintptr_t)vector_out_ptr, size, sizeof(float));
  155. /*
  156. * Partition the CSR matrix and the output vector
  157. */
  158. csr_f.nchildren = nblocks;
  159. vector_f.nchildren = nblocks;
  160. starpu_data_partition(sparse_matrix, &csr_f);
  161. starpu_data_partition(vector_out, &vector_f);
  162. /*
  163. * If we use OpenCL, we need to compile the SpMV kernel
  164. */
  165. #ifdef STARPU_USE_OPENCL
  166. compile_spmv_opencl_kernel();
  167. #endif
  168. gettimeofday(&start, NULL);
  169. /*
  170. * Create and submit StarPU tasks
  171. */
  172. for (part = 0; part < nblocks; part++)
  173. {
  174. struct starpu_task *task = starpu_task_create();
  175. task->cl = &spmv_cl;
  176. task->buffers[0].handle = starpu_data_get_sub_data(sparse_matrix, 1, part);
  177. task->buffers[0].mode = STARPU_R;
  178. task->buffers[1].handle = vector_in;
  179. task->buffers[1].mode = STARPU_R;
  180. task->buffers[2].handle = starpu_data_get_sub_data(vector_out, 1, part);
  181. task->buffers[2].mode = STARPU_W;
  182. ret = starpu_task_submit(task);
  183. if (STARPU_UNLIKELY(ret == -ENODEV))
  184. {
  185. FPRINTF(stderr, "No worker may execute this task\n");
  186. exit(0);
  187. }
  188. }
  189. starpu_task_wait_for_all();
  190. gettimeofday(&end, NULL);
  191. /*
  192. * Unregister the CSR matrix and the output vector
  193. */
  194. starpu_data_unpartition(sparse_matrix, 0);
  195. starpu_data_unpartition(vector_out, 0);
  196. /*
  197. * Unregister data
  198. */
  199. starpu_data_unregister(sparse_matrix);
  200. starpu_data_unregister(vector_in);
  201. starpu_data_unregister(vector_out);
  202. /*
  203. * Display the result
  204. */
  205. for (row = 0; row < STARPU_MIN(size, 16); row++)
  206. {
  207. FPRINTF(stdout, "%2.2f\t%2.2f\n", vector_in_ptr[row], vector_out_ptr[row]);
  208. }
  209. starpu_free(nzval);
  210. starpu_free(colind);
  211. starpu_free(rowptr);
  212. starpu_free(vector_in_ptr);
  213. starpu_free(vector_out_ptr);
  214. /*
  215. * Stop StarPU
  216. */
  217. starpu_shutdown();
  218. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  219. FPRINTF(stderr, "Computation took (in ms)\n");
  220. FPRINTF(stdout, "%2.2f\n", timing/1000);
  221. return 0;
  222. }