dw_sparse_cg.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2011, 2015, 2017 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2016 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. /*
  18. * Conjugate gradients for Sparse matrices
  19. * The task graph is declared through tag dependencies
  20. */
  21. #include "dw_sparse_cg.h"
  22. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  23. static struct starpu_task *create_task(starpu_tag_t id)
  24. {
  25. struct starpu_task *task = starpu_task_create();
  26. task->use_tag = 1;
  27. task->tag_id = id;
  28. return task;
  29. }
  30. static void create_data(float **_nzvalA, float **_vecb, float **_vecx, uint32_t *_nnz, uint32_t *_nrow, uint32_t **_colind, uint32_t **_rowptr)
  31. {
  32. /* we need a sparse symetric (definite positive ?) matrix and a "dense" vector */
  33. /* example of 3-band matrix */
  34. float *nzval;
  35. uint32_t nnz;
  36. uint32_t *colind;
  37. uint32_t *rowptr;
  38. nnz = 3*_size-2;
  39. nzval = malloc(nnz*sizeof(float));
  40. colind = malloc(nnz*sizeof(uint32_t));
  41. rowptr = malloc(_size*sizeof(uint32_t));
  42. assert(nzval);
  43. assert(colind);
  44. assert(rowptr);
  45. /* fill the matrix */
  46. unsigned row;
  47. unsigned pos = 0;
  48. for (row = 0; row < _size; row++)
  49. {
  50. rowptr[row] = pos;
  51. if (row > 0)
  52. {
  53. nzval[pos] = 1.0f;
  54. colind[pos] = row-1;
  55. pos++;
  56. }
  57. nzval[pos] = 5.0f;
  58. colind[pos] = row;
  59. pos++;
  60. if (row < _size - 1)
  61. {
  62. nzval[pos] = 1.0f;
  63. colind[pos] = row+1;
  64. pos++;
  65. }
  66. }
  67. *_nnz = nnz;
  68. *_nrow = _size;
  69. *_nzvalA = nzval;
  70. *_colind = colind;
  71. *_rowptr = rowptr;
  72. STARPU_ASSERT(pos == nnz);
  73. /* initiate the 2 vectors */
  74. float *invec, *outvec;
  75. invec = malloc(_size*sizeof(float));
  76. assert(invec);
  77. outvec = malloc(_size*sizeof(float));
  78. assert(outvec);
  79. /* fill those */
  80. unsigned ind;
  81. for (ind = 0; ind < _size; ind++)
  82. {
  83. invec[ind] = 2.0f;
  84. outvec[ind] = 0.0f;
  85. }
  86. *_vecb = invec;
  87. *_vecx = outvec;
  88. }
  89. void init_problem(void)
  90. {
  91. /* create the sparse input matrix */
  92. float *nzval;
  93. float *vecb;
  94. float *vecx;
  95. uint32_t nnz;
  96. uint32_t nrow;
  97. uint32_t *colind;
  98. uint32_t *rowptr;
  99. create_data(&nzval, &vecb, &vecx, &nnz, &nrow, &colind, &rowptr);
  100. conjugate_gradient(nzval, vecb, vecx, nnz, nrow, colind, rowptr);
  101. }
  102. /*
  103. * cg initialization phase
  104. */
  105. static struct starpu_codelet cl1 = {
  106. .cpu_funcs = { cpu_codelet_func_1 },
  107. .cpu_funcs_name = { "cpu_codelet_func_1" },
  108. .nbuffers = 4,
  109. .modes = { STARPU_R, STARPU_R, STARPU_W, STARPU_R },
  110. };
  111. static struct starpu_codelet cl2 = {
  112. .cpu_funcs = { cpu_codelet_func_2 },
  113. .cpu_funcs_name = { "cpu_codelet_func_2" },
  114. .nbuffers = 2,
  115. .modes = { STARPU_W, STARPU_R },
  116. };
  117. static struct starpu_codelet cl3 = {
  118. .cpu_funcs = { cpu_codelet_func_3 },
  119. .cpu_funcs_name = { "cpu_codelet_func_3" },
  120. #ifdef STARPU_USE_CUDA
  121. .cuda_funcs = { cublas_codelet_func_3 },
  122. #endif
  123. .nbuffers = 1,
  124. .modes = { STARPU_R },
  125. };
  126. void init_cg(struct cg_problem *problem)
  127. {
  128. int ret;
  129. problem->i = 0;
  130. /* r = b - A x */
  131. struct starpu_task *task1 = create_task(1UL);
  132. task1->cl = &cl1;
  133. task1->handles[0] = problem->ds_matrixA;
  134. task1->handles[1] = problem->ds_vecx;
  135. task1->handles[2] = problem->ds_vecr;
  136. task1->handles[3] = problem->ds_vecb;
  137. /* d = r */
  138. struct starpu_task *task2 = create_task(2UL);
  139. task2->cl = &cl2;
  140. task2->handles[0] = problem->ds_vecd;
  141. task2->handles[1] = problem->ds_vecr;
  142. starpu_tag_declare_deps((starpu_tag_t)2UL, 1, (starpu_tag_t)1UL);
  143. /* delta_new = trans(r) r */
  144. struct starpu_task *task3 = create_task(3UL);
  145. task3->cl = &cl3;
  146. task3->cl_arg = problem;
  147. task3->cl_arg_size = sizeof(*problem);
  148. task3->handles[0] = problem->ds_vecr;
  149. task3->callback_func = iteration_cg;
  150. task3->callback_arg = problem;
  151. /* XXX 3 should only depend on 1 ... */
  152. starpu_tag_declare_deps((starpu_tag_t)3UL, 1, (starpu_tag_t)2UL);
  153. /* launch the computation now */
  154. ret = starpu_task_submit(task1);
  155. if (STARPU_UNLIKELY(ret == -ENODEV))
  156. {
  157. FPRINTF(stderr, "No worker may execute this task\n");
  158. exit(0);
  159. }
  160. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  161. ret = starpu_task_submit(task2);
  162. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  163. ret = starpu_task_submit(task3);
  164. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  165. }
  166. /*
  167. * the inner iteration of the cg algorithm
  168. * the codelet code launcher is its own callback !
  169. */
  170. static struct starpu_codelet cl4 = {
  171. .cpu_funcs = { cpu_codelet_func_4 },
  172. .cpu_funcs_name = { "cpu_codelet_func_4" },
  173. .nbuffers = 3,
  174. .modes = { STARPU_R, STARPU_R, STARPU_W },
  175. };
  176. static struct starpu_codelet cl5 = {
  177. .cpu_funcs = { cpu_codelet_func_5 },
  178. .cpu_funcs_name = { "cpu_codelet_func_5" },
  179. #ifdef STARPU_USE_CUDA
  180. .cuda_funcs = { cublas_codelet_func_5 },
  181. #endif
  182. .nbuffers = 2,
  183. .modes = { STARPU_R, STARPU_R },
  184. };
  185. static struct starpu_codelet cl6 = {
  186. .cpu_funcs = { cpu_codelet_func_6 },
  187. .cpu_funcs_name = { "cpu_codelet_func_6" },
  188. #ifdef STARPU_USE_CUDA
  189. .cuda_funcs = { cublas_codelet_func_6 },
  190. .cuda_flags = { STARPU_CUDA_ASYNC },
  191. #endif
  192. .nbuffers = 2,
  193. .modes = { STARPU_RW, STARPU_R },
  194. };
  195. static struct starpu_codelet cl7 = {
  196. .cpu_funcs = { cpu_codelet_func_7 },
  197. .cpu_funcs_name = { "cpu_codelet_func_7" },
  198. #ifdef STARPU_USE_CUDA
  199. .cuda_funcs = { cublas_codelet_func_7 },
  200. .cuda_flags = { STARPU_CUDA_ASYNC },
  201. #endif
  202. .nbuffers = 2,
  203. .modes = { STARPU_RW, STARPU_R },
  204. };
  205. static struct starpu_codelet cl8 = {
  206. .cpu_funcs = { cpu_codelet_func_8 },
  207. .cpu_funcs_name = { "cpu_codelet_func_8" },
  208. #ifdef STARPU_USE_CUDA
  209. .cuda_funcs = { cublas_codelet_func_8 },
  210. #endif
  211. .nbuffers = 1,
  212. .modes = { STARPU_R },
  213. };
  214. static struct starpu_codelet cl9 = {
  215. .cpu_funcs = { cpu_codelet_func_9 },
  216. .cpu_funcs_name = { "cpu_codelet_func_9" },
  217. #ifdef STARPU_USE_CUDA
  218. .cuda_funcs = { cublas_codelet_func_9 },
  219. .cuda_flags = { STARPU_CUDA_ASYNC },
  220. #endif
  221. .nbuffers = 2,
  222. .modes = { STARPU_RW, STARPU_R },
  223. };
  224. void launch_new_cg_iteration(struct cg_problem *problem)
  225. {
  226. int ret;
  227. unsigned iter = problem->i;
  228. unsigned long long maskiter = ((unsigned long long)iter*1024);
  229. /* q = A d */
  230. struct starpu_task *task4 = create_task(maskiter | 4UL);
  231. task4->cl = &cl4;
  232. task4->handles[0] = problem->ds_matrixA;
  233. task4->handles[1] = problem->ds_vecd;
  234. task4->handles[2] = problem->ds_vecq;
  235. /* alpha = delta_new / ( trans(d) q )*/
  236. struct starpu_task *task5 = create_task(maskiter | 5UL);
  237. task5->cl = &cl5;
  238. task5->cl_arg = problem;
  239. task5->cl_arg_size = sizeof(*problem);
  240. task5->handles[0] = problem->ds_vecd;
  241. task5->handles[1] = problem->ds_vecq;
  242. starpu_tag_declare_deps((starpu_tag_t)(maskiter | 5UL), 1, (starpu_tag_t)(maskiter | 4UL));
  243. /* x = x + alpha d */
  244. struct starpu_task *task6 = create_task(maskiter | 6UL);
  245. task6->cl = &cl6;
  246. task6->cl_arg = problem;
  247. task6->cl_arg_size = sizeof(*problem);
  248. task6->handles[0] = problem->ds_vecx;
  249. task6->handles[1] = problem->ds_vecd;
  250. starpu_tag_declare_deps((starpu_tag_t)(maskiter | 6UL), 1, (starpu_tag_t)(maskiter | 5UL));
  251. /* r = r - alpha q */
  252. struct starpu_task *task7 = create_task(maskiter | 7UL);
  253. task7->cl = &cl7;
  254. task7->cl_arg = problem;
  255. task7->cl_arg_size = sizeof(*problem);
  256. task7->handles[0] = problem->ds_vecr;
  257. task7->handles[1] = problem->ds_vecq;
  258. starpu_tag_declare_deps((starpu_tag_t)(maskiter | 7UL), 1, (starpu_tag_t)(maskiter | 6UL));
  259. /* update delta_* and compute beta */
  260. struct starpu_task *task8 = create_task(maskiter | 8UL);
  261. task8->cl = &cl8;
  262. task8->cl_arg = problem;
  263. task8->cl_arg_size = sizeof(*problem);
  264. task8->handles[0] = problem->ds_vecr;
  265. starpu_tag_declare_deps((starpu_tag_t)(maskiter | 8UL), 1, (starpu_tag_t)(maskiter | 7UL));
  266. /* d = r + beta d */
  267. struct starpu_task *task9 = create_task(maskiter | 9UL);
  268. task9->cl = &cl9;
  269. task9->cl_arg = problem;
  270. task9->cl_arg_size = sizeof(*problem);
  271. task9->handles[0] = problem->ds_vecd;
  272. task9->handles[1] = problem->ds_vecr;
  273. starpu_tag_declare_deps((starpu_tag_t)(maskiter | 9UL), 1, (starpu_tag_t)(maskiter | 8UL));
  274. task9->callback_func = iteration_cg;
  275. task9->callback_arg = problem;
  276. /* launch the computation now */
  277. ret = starpu_task_submit(task4);
  278. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  279. ret = starpu_task_submit(task5);
  280. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  281. ret = starpu_task_submit(task6);
  282. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  283. ret = starpu_task_submit(task7);
  284. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  285. ret = starpu_task_submit(task8);
  286. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  287. ret = starpu_task_submit(task9);
  288. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  289. }
  290. void iteration_cg(void *problem)
  291. {
  292. struct cg_problem *pb = problem;
  293. FPRINTF(stdout, "i : %d (MAX %d)\n\tdelta_new %f (%f)\n", pb->i, MAXITER, pb->delta_new, sqrt(pb->delta_new / pb->size));
  294. if ((pb->i < MAXITER) &&
  295. (pb->delta_new > pb->epsilon) )
  296. {
  297. if (pb->i % 1000 == 0)
  298. FPRINTF(stdout, "i : %d\n\tdelta_new %f (%f)\n", pb->i, pb->delta_new, sqrt(pb->delta_new / pb->size));
  299. pb->i++;
  300. /* we did not reach the stop condition yet */
  301. launch_new_cg_iteration(problem);
  302. }
  303. else
  304. {
  305. /* we may stop */
  306. FPRINTF(stdout, "We are done ... after %d iterations \n", pb->i - 1);
  307. FPRINTF(stdout, "i : %d\n\tdelta_new %2.5f\n", pb->i, pb->delta_new);
  308. sem_post(pb->sem);
  309. }
  310. }
  311. /*
  312. * initializing the problem
  313. */
  314. void conjugate_gradient(float *nzvalA, float *vecb, float *vecx, uint32_t nnz,
  315. unsigned nrow, uint32_t *colind, uint32_t *rowptr)
  316. {
  317. /* first register all the data structures to StarPU */
  318. starpu_data_handle_t ds_matrixA;
  319. starpu_data_handle_t ds_vecx, ds_vecb;
  320. starpu_data_handle_t ds_vecr, ds_vecd, ds_vecq;
  321. /* first the user-allocated data */
  322. starpu_csr_data_register(&ds_matrixA, STARPU_MAIN_RAM, nnz, nrow,
  323. (uintptr_t)nzvalA, colind, rowptr, 0, sizeof(float));
  324. starpu_vector_data_register(&ds_vecx, STARPU_MAIN_RAM, (uintptr_t)vecx, nrow, sizeof(float));
  325. starpu_vector_data_register(&ds_vecb, STARPU_MAIN_RAM, (uintptr_t)vecb, nrow, sizeof(float));
  326. /* then allocate the algorithm intern data */
  327. float *ptr_vecr, *ptr_vecd, *ptr_vecq;
  328. unsigned i;
  329. ptr_vecr = malloc(nrow*sizeof(float));
  330. ptr_vecd = malloc(nrow*sizeof(float));
  331. ptr_vecq = malloc(nrow*sizeof(float));
  332. for (i = 0; i < nrow; i++)
  333. {
  334. ptr_vecr[i] = 0.0f;
  335. ptr_vecd[i] = 0.0f;
  336. ptr_vecq[i] = 0.0f;
  337. }
  338. FPRINTF(stdout, "nrow = %u \n", nrow);
  339. /* and register them as well */
  340. starpu_vector_data_register(&ds_vecr, STARPU_MAIN_RAM, (uintptr_t)ptr_vecr, nrow, sizeof(float));
  341. starpu_vector_data_register(&ds_vecd, STARPU_MAIN_RAM, (uintptr_t)ptr_vecd, nrow, sizeof(float));
  342. starpu_vector_data_register(&ds_vecq, STARPU_MAIN_RAM, (uintptr_t)ptr_vecq, nrow, sizeof(float));
  343. /* we now have the complete problem */
  344. struct cg_problem problem;
  345. problem.ds_matrixA = ds_matrixA;
  346. problem.ds_vecx = ds_vecx;
  347. problem.ds_vecb = ds_vecb;
  348. problem.ds_vecr = ds_vecr;
  349. problem.ds_vecd = ds_vecd;
  350. problem.ds_vecq = ds_vecq;
  351. problem.epsilon = EPSILON;
  352. problem.size = nrow;
  353. problem.delta_old = 1.0;
  354. problem.delta_new = 1.0; /* just to make sure we do at least one iteration */
  355. /* we need a semaphore to synchronize with callbacks */
  356. sem_t sem;
  357. sem_init(&sem, 0, 0U);
  358. problem.sem = &sem;
  359. init_cg(&problem);
  360. sem_wait(&sem);
  361. sem_destroy(&sem);
  362. print_results(vecx, nrow);
  363. starpu_data_unregister(ds_matrixA);
  364. starpu_data_unregister(ds_vecx);
  365. starpu_data_unregister(ds_vecb);
  366. starpu_data_unregister(ds_vecr);
  367. starpu_data_unregister(ds_vecd);
  368. starpu_data_unregister(ds_vecq);
  369. free(ptr_vecr);
  370. free(ptr_vecd);
  371. free(ptr_vecq);
  372. }
  373. void do_conjugate_gradient(float *nzvalA, float *vecb, float *vecx, uint32_t nnz,
  374. unsigned nrow, uint32_t *colind, uint32_t *rowptr)
  375. {
  376. /* start the runtime */
  377. int ret;
  378. ret = starpu_init(NULL);
  379. if (ret == -ENODEV)
  380. exit(77);
  381. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  382. starpu_cublas_init();
  383. conjugate_gradient(nzvalA, vecb, vecx, nnz, nrow, colind, rowptr);
  384. starpu_shutdown();
  385. }