spmv.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. /* partition the CSR matrix along a block distribution */
  38. static struct starpu_data_filter csr_f = {
  39. .filter_func = starpu_vertical_block_filter_func_csr,
  40. /* This value is defined later on */
  41. .nchildren = -1,
  42. .get_nchildren = NULL,
  43. /* the children also use a csr interface */
  44. .get_child_ops = NULL
  45. };
  46. static struct starpu_data_filter vector_f = {
  47. .filter_func = starpu_block_filter_func_vector,
  48. /* This value is defined later on */
  49. .nchildren = -1,
  50. .get_nchildren = NULL,
  51. .get_child_ops = NULL
  52. };
  53. static starpu_codelet spmv_cl = {
  54. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  55. .cpu_func = spmv_kernel_cpu,
  56. #ifdef STARPU_USE_CUDA
  57. .cuda_func = spmv_kernel_cuda,
  58. #endif
  59. #ifdef STARPU_USE_OPENCL
  60. .opencl_func = spmv_kernel_opencl,
  61. #endif
  62. .nbuffers = 3,
  63. .model = NULL
  64. };
  65. int main(int argc, char **argv)
  66. {
  67. int ret;
  68. unsigned part;
  69. double timing;
  70. struct timeval start, end;
  71. unsigned row, pos;
  72. unsigned ind;
  73. /* CSR matrix description */
  74. float *nzval;
  75. uint32_t nnz;
  76. uint32_t *colind;
  77. uint32_t *rowptr;
  78. /* Input and Output vectors */
  79. float *vector_in_ptr;
  80. float *vector_out_ptr;
  81. /*
  82. * Parse command-line arguments
  83. */
  84. parse_args(argc, argv);
  85. /*
  86. * Launch StarPU
  87. */
  88. starpu_init(NULL);
  89. /*
  90. * Create a 3-band sparse matrix as input example
  91. */
  92. nnz = 3*size-2;
  93. nzval = malloc(nnz*sizeof(float));
  94. colind = malloc(nnz*sizeof(uint32_t));
  95. rowptr = malloc((size+1)*sizeof(uint32_t));
  96. assert(nzval && colind && rowptr);
  97. /* fill the matrix */
  98. for (row = 0, pos = 0; row < size; row++)
  99. {
  100. rowptr[row] = pos;
  101. if (row > 0) {
  102. nzval[pos] = 1.0f;
  103. colind[pos] = row-1;
  104. pos++;
  105. }
  106. nzval[pos] = 5.0f;
  107. colind[pos] = row;
  108. pos++;
  109. if (row < size - 1) {
  110. nzval[pos] = 1.0f;
  111. colind[pos] = row+1;
  112. pos++;
  113. }
  114. }
  115. STARPU_ASSERT(pos == nnz);
  116. rowptr[size] = nnz;
  117. /* initiate the 2 vectors */
  118. vector_in_ptr = malloc(size*sizeof(float));
  119. vector_out_ptr = malloc(size*sizeof(float));
  120. assert(vector_in_ptr && vector_out_ptr);
  121. /* fill them */
  122. for (ind = 0; ind < size; ind++)
  123. {
  124. vector_in_ptr[ind] = 2.0f;
  125. vector_out_ptr[ind] = 0.0f;
  126. }
  127. /*
  128. * Register the CSR matrix and the 2 vectors
  129. */
  130. starpu_csr_data_register(&sparse_matrix, 0, nnz, size, (uintptr_t)nzval, colind, rowptr, 0, sizeof(float));
  131. starpu_vector_data_register(&vector_in, 0, (uintptr_t)vector_in_ptr, size, sizeof(float));
  132. starpu_vector_data_register(&vector_out, 0, (uintptr_t)vector_out_ptr, size, sizeof(float));
  133. /*
  134. * Partition the CSR matrix and the output vector
  135. */
  136. csr_f.nchildren = nblocks;
  137. vector_f.nchildren = nblocks;
  138. starpu_data_partition(sparse_matrix, &csr_f);
  139. starpu_data_partition(vector_out, &vector_f);
  140. /*
  141. * If we use OpenCL, we need to compile the SpMV kernel
  142. */
  143. #ifdef STARPU_USE_OPENCL
  144. compile_spmv_opencl_kernel();
  145. #endif
  146. gettimeofday(&start, NULL);
  147. /*
  148. * Create and submit StarPU tasks
  149. */
  150. for (part = 0; part < nblocks; part++)
  151. {
  152. struct starpu_task *task = starpu_task_create();
  153. task->cl = &spmv_cl;
  154. task->buffers[0].handle = starpu_data_get_sub_data(sparse_matrix, 1, part);
  155. task->buffers[0].mode = STARPU_R;
  156. task->buffers[1].handle = vector_in;
  157. task->buffers[1].mode = STARPU_R;
  158. task->buffers[2].handle = starpu_data_get_sub_data(vector_out, 1, part);
  159. task->buffers[2].mode = STARPU_W;
  160. ret = starpu_task_submit(task);
  161. if (STARPU_UNLIKELY(ret == -ENODEV))
  162. {
  163. FPRINTF(stderr, "No worker may execute this task\n");
  164. exit(0);
  165. }
  166. }
  167. starpu_task_wait_for_all();
  168. gettimeofday(&end, NULL);
  169. /*
  170. * Unregister the CSR matrix and the output vector
  171. */
  172. starpu_data_unpartition(sparse_matrix, 0);
  173. starpu_data_unpartition(vector_out, 0);
  174. /*
  175. * Unregister data
  176. */
  177. starpu_data_unregister(sparse_matrix);
  178. starpu_data_unregister(vector_in);
  179. starpu_data_unregister(vector_out);
  180. /*
  181. * Display the result
  182. */
  183. for (row = 0; row < STARPU_MIN(size, 16); row++)
  184. {
  185. FPRINTF(stdout, "%2.2f\t%2.2f\n", vector_in_ptr[row], vector_out_ptr[row]);
  186. }
  187. /*
  188. * Stop StarPU
  189. */
  190. starpu_shutdown();
  191. free(nzval);
  192. free(colind);
  193. free(rowptr);
  194. free(vector_in_ptr);
  195. free(vector_out_ptr);
  196. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  197. FPRINTF(stderr, "Computation took (in ms)\n");
  198. FPRINTF(stdout, "%2.2f\n", timing/1000);
  199. return 0;
  200. }