dw_sparse_cg.c 12 KB

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