dw_sparse_cg.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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, 2017 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. {
  107. .cpu_funcs = { cpu_codelet_func_1 },
  108. .cpu_funcs_name = { "cpu_codelet_func_1" },
  109. .nbuffers = 4,
  110. .modes = { STARPU_R, STARPU_R, STARPU_W, STARPU_R },
  111. };
  112. static struct starpu_codelet cl2 =
  113. {
  114. .cpu_funcs = { cpu_codelet_func_2 },
  115. .cpu_funcs_name = { "cpu_codelet_func_2" },
  116. .nbuffers = 2,
  117. .modes = { STARPU_W, STARPU_R },
  118. };
  119. static struct starpu_codelet cl3 =
  120. {
  121. .cpu_funcs = { cpu_codelet_func_3 },
  122. .cpu_funcs_name = { "cpu_codelet_func_3" },
  123. #ifdef STARPU_USE_CUDA
  124. .cuda_funcs = { cublas_codelet_func_3 },
  125. #endif
  126. .nbuffers = 1,
  127. .modes = { STARPU_R },
  128. };
  129. void init_cg(struct cg_problem *problem)
  130. {
  131. int ret;
  132. problem->i = 0;
  133. /* r = b - A x */
  134. struct starpu_task *task1 = create_task(1UL);
  135. task1->cl = &cl1;
  136. task1->handles[0] = problem->ds_matrixA;
  137. task1->handles[1] = problem->ds_vecx;
  138. task1->handles[2] = problem->ds_vecr;
  139. task1->handles[3] = problem->ds_vecb;
  140. /* d = r */
  141. struct starpu_task *task2 = create_task(2UL);
  142. task2->cl = &cl2;
  143. task2->handles[0] = problem->ds_vecd;
  144. task2->handles[1] = problem->ds_vecr;
  145. starpu_tag_declare_deps((starpu_tag_t)2UL, 1, (starpu_tag_t)1UL);
  146. /* delta_new = trans(r) r */
  147. struct starpu_task *task3 = create_task(3UL);
  148. task3->cl = &cl3;
  149. task3->cl_arg = problem;
  150. task3->cl_arg_size = sizeof(*problem);
  151. task3->handles[0] = problem->ds_vecr;
  152. task3->callback_func = iteration_cg;
  153. task3->callback_arg = problem;
  154. /* XXX 3 should only depend on 1 ... */
  155. starpu_tag_declare_deps((starpu_tag_t)3UL, 1, (starpu_tag_t)2UL);
  156. /* launch the computation now */
  157. ret = starpu_task_submit(task1);
  158. if (STARPU_UNLIKELY(ret == -ENODEV))
  159. {
  160. FPRINTF(stderr, "No worker may execute this task\n");
  161. exit(0);
  162. }
  163. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  164. ret = starpu_task_submit(task2);
  165. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  166. ret = starpu_task_submit(task3);
  167. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  168. }
  169. /*
  170. * the inner iteration of the cg algorithm
  171. * the codelet code launcher is its own callback !
  172. */
  173. static struct starpu_codelet cl4 =
  174. {
  175. .cpu_funcs = { cpu_codelet_func_4 },
  176. .cpu_funcs_name = { "cpu_codelet_func_4" },
  177. .nbuffers = 3,
  178. .modes = { STARPU_R, STARPU_R, STARPU_W },
  179. };
  180. static struct starpu_codelet cl5 =
  181. {
  182. .cpu_funcs = { cpu_codelet_func_5 },
  183. .cpu_funcs_name = { "cpu_codelet_func_5" },
  184. #ifdef STARPU_USE_CUDA
  185. .cuda_funcs = { cublas_codelet_func_5 },
  186. #endif
  187. .nbuffers = 2,
  188. .modes = { STARPU_R, STARPU_R },
  189. };
  190. static struct starpu_codelet cl6 =
  191. {
  192. .cpu_funcs = { cpu_codelet_func_6 },
  193. .cpu_funcs_name = { "cpu_codelet_func_6" },
  194. #ifdef STARPU_USE_CUDA
  195. .cuda_funcs = { cublas_codelet_func_6 },
  196. .cuda_flags = { STARPU_CUDA_ASYNC },
  197. #endif
  198. .nbuffers = 2,
  199. .modes = { STARPU_RW, STARPU_R },
  200. };
  201. static struct starpu_codelet cl7 =
  202. {
  203. .cpu_funcs = { cpu_codelet_func_7 },
  204. .cpu_funcs_name = { "cpu_codelet_func_7" },
  205. #ifdef STARPU_USE_CUDA
  206. .cuda_funcs = { cublas_codelet_func_7 },
  207. .cuda_flags = { STARPU_CUDA_ASYNC },
  208. #endif
  209. .nbuffers = 2,
  210. .modes = { STARPU_RW, STARPU_R },
  211. };
  212. static struct starpu_codelet cl8 =
  213. {
  214. .cpu_funcs = { cpu_codelet_func_8 },
  215. .cpu_funcs_name = { "cpu_codelet_func_8" },
  216. #ifdef STARPU_USE_CUDA
  217. .cuda_funcs = { cublas_codelet_func_8 },
  218. #endif
  219. .nbuffers = 1,
  220. .modes = { STARPU_R },
  221. };
  222. static struct starpu_codelet cl9 =
  223. {
  224. .cpu_funcs = { cpu_codelet_func_9 },
  225. .cpu_funcs_name = { "cpu_codelet_func_9" },
  226. #ifdef STARPU_USE_CUDA
  227. .cuda_funcs = { cublas_codelet_func_9 },
  228. .cuda_flags = { STARPU_CUDA_ASYNC },
  229. #endif
  230. .nbuffers = 2,
  231. .modes = { STARPU_RW, STARPU_R },
  232. };
  233. void launch_new_cg_iteration(struct cg_problem *problem)
  234. {
  235. int ret;
  236. unsigned iter = problem->i;
  237. unsigned long long maskiter = ((unsigned long long)iter*1024);
  238. /* q = A d */
  239. struct starpu_task *task4 = create_task(maskiter | 4UL);
  240. task4->cl = &cl4;
  241. task4->handles[0] = problem->ds_matrixA;
  242. task4->handles[1] = problem->ds_vecd;
  243. task4->handles[2] = problem->ds_vecq;
  244. /* alpha = delta_new / ( trans(d) q )*/
  245. struct starpu_task *task5 = create_task(maskiter | 5UL);
  246. task5->cl = &cl5;
  247. task5->cl_arg = problem;
  248. task5->cl_arg_size = sizeof(*problem);
  249. task5->handles[0] = problem->ds_vecd;
  250. task5->handles[1] = problem->ds_vecq;
  251. starpu_tag_declare_deps((starpu_tag_t)(maskiter | 5UL), 1, (starpu_tag_t)(maskiter | 4UL));
  252. /* x = x + alpha d */
  253. struct starpu_task *task6 = create_task(maskiter | 6UL);
  254. task6->cl = &cl6;
  255. task6->cl_arg = problem;
  256. task6->cl_arg_size = sizeof(*problem);
  257. task6->handles[0] = problem->ds_vecx;
  258. task6->handles[1] = problem->ds_vecd;
  259. starpu_tag_declare_deps((starpu_tag_t)(maskiter | 6UL), 1, (starpu_tag_t)(maskiter | 5UL));
  260. /* r = r - alpha q */
  261. struct starpu_task *task7 = create_task(maskiter | 7UL);
  262. task7->cl = &cl7;
  263. task7->cl_arg = problem;
  264. task7->cl_arg_size = sizeof(*problem);
  265. task7->handles[0] = problem->ds_vecr;
  266. task7->handles[1] = problem->ds_vecq;
  267. starpu_tag_declare_deps((starpu_tag_t)(maskiter | 7UL), 1, (starpu_tag_t)(maskiter | 6UL));
  268. /* update delta_* and compute beta */
  269. struct starpu_task *task8 = create_task(maskiter | 8UL);
  270. task8->cl = &cl8;
  271. task8->cl_arg = problem;
  272. task8->cl_arg_size = sizeof(*problem);
  273. task8->handles[0] = problem->ds_vecr;
  274. starpu_tag_declare_deps((starpu_tag_t)(maskiter | 8UL), 1, (starpu_tag_t)(maskiter | 7UL));
  275. /* d = r + beta d */
  276. struct starpu_task *task9 = create_task(maskiter | 9UL);
  277. task9->cl = &cl9;
  278. task9->cl_arg = problem;
  279. task9->cl_arg_size = sizeof(*problem);
  280. task9->handles[0] = problem->ds_vecd;
  281. task9->handles[1] = problem->ds_vecr;
  282. starpu_tag_declare_deps((starpu_tag_t)(maskiter | 9UL), 1, (starpu_tag_t)(maskiter | 8UL));
  283. task9->callback_func = iteration_cg;
  284. task9->callback_arg = problem;
  285. /* launch the computation now */
  286. ret = starpu_task_submit(task4);
  287. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  288. ret = starpu_task_submit(task5);
  289. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  290. ret = starpu_task_submit(task6);
  291. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  292. ret = starpu_task_submit(task7);
  293. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  294. ret = starpu_task_submit(task8);
  295. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  296. ret = starpu_task_submit(task9);
  297. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  298. }
  299. void iteration_cg(void *problem)
  300. {
  301. struct cg_problem *pb = problem;
  302. FPRINTF(stdout, "i : %d (MAX %d)\n\tdelta_new %f (%f)\n", pb->i, MAXITER, pb->delta_new, sqrt(pb->delta_new / pb->size));
  303. if ((pb->i < MAXITER) &&
  304. (pb->delta_new > pb->epsilon) )
  305. {
  306. if (pb->i % 1000 == 0)
  307. FPRINTF(stdout, "i : %d\n\tdelta_new %f (%f)\n", pb->i, pb->delta_new, sqrt(pb->delta_new / pb->size));
  308. pb->i++;
  309. /* we did not reach the stop condition yet */
  310. launch_new_cg_iteration(problem);
  311. }
  312. else
  313. {
  314. /* we may stop */
  315. FPRINTF(stdout, "We are done ... after %d iterations \n", pb->i - 1);
  316. FPRINTF(stdout, "i : %d\n\tdelta_new %2.5f\n", pb->i, pb->delta_new);
  317. sem_post(pb->sem);
  318. }
  319. }
  320. /*
  321. * initializing the problem
  322. */
  323. void conjugate_gradient(float *nzvalA, float *vecb, float *vecx, uint32_t nnz,
  324. unsigned nrow, uint32_t *colind, uint32_t *rowptr)
  325. {
  326. /* first register all the data structures to StarPU */
  327. starpu_data_handle_t ds_matrixA;
  328. starpu_data_handle_t ds_vecx, ds_vecb;
  329. starpu_data_handle_t ds_vecr, ds_vecd, ds_vecq;
  330. /* first the user-allocated data */
  331. starpu_csr_data_register(&ds_matrixA, STARPU_MAIN_RAM, nnz, nrow,
  332. (uintptr_t)nzvalA, colind, rowptr, 0, sizeof(float));
  333. starpu_vector_data_register(&ds_vecx, STARPU_MAIN_RAM, (uintptr_t)vecx, nrow, sizeof(float));
  334. starpu_vector_data_register(&ds_vecb, STARPU_MAIN_RAM, (uintptr_t)vecb, nrow, sizeof(float));
  335. /* then allocate the algorithm intern data */
  336. float *ptr_vecr, *ptr_vecd, *ptr_vecq;
  337. unsigned i;
  338. ptr_vecr = malloc(nrow*sizeof(float));
  339. ptr_vecd = malloc(nrow*sizeof(float));
  340. ptr_vecq = malloc(nrow*sizeof(float));
  341. for (i = 0; i < nrow; i++)
  342. {
  343. ptr_vecr[i] = 0.0f;
  344. ptr_vecd[i] = 0.0f;
  345. ptr_vecq[i] = 0.0f;
  346. }
  347. FPRINTF(stdout, "nrow = %u \n", nrow);
  348. /* and register them as well */
  349. starpu_vector_data_register(&ds_vecr, STARPU_MAIN_RAM, (uintptr_t)ptr_vecr, nrow, sizeof(float));
  350. starpu_vector_data_register(&ds_vecd, STARPU_MAIN_RAM, (uintptr_t)ptr_vecd, nrow, sizeof(float));
  351. starpu_vector_data_register(&ds_vecq, STARPU_MAIN_RAM, (uintptr_t)ptr_vecq, nrow, sizeof(float));
  352. /* we now have the complete problem */
  353. struct cg_problem problem;
  354. problem.ds_matrixA = ds_matrixA;
  355. problem.ds_vecx = ds_vecx;
  356. problem.ds_vecb = ds_vecb;
  357. problem.ds_vecr = ds_vecr;
  358. problem.ds_vecd = ds_vecd;
  359. problem.ds_vecq = ds_vecq;
  360. problem.epsilon = EPSILON;
  361. problem.size = nrow;
  362. problem.delta_old = 1.0;
  363. problem.delta_new = 1.0; /* just to make sure we do at least one iteration */
  364. /* we need a semaphore to synchronize with callbacks */
  365. sem_t sem;
  366. sem_init(&sem, 0, 0U);
  367. problem.sem = &sem;
  368. init_cg(&problem);
  369. sem_wait(&sem);
  370. sem_destroy(&sem);
  371. starpu_task_wait_for_all();
  372. print_results(vecx, nrow);
  373. starpu_data_unregister(ds_matrixA);
  374. starpu_data_unregister(ds_vecx);
  375. starpu_data_unregister(ds_vecb);
  376. starpu_data_unregister(ds_vecr);
  377. starpu_data_unregister(ds_vecd);
  378. starpu_data_unregister(ds_vecq);
  379. free(ptr_vecr);
  380. free(ptr_vecd);
  381. free(ptr_vecq);
  382. }
  383. void do_conjugate_gradient(float *nzvalA, float *vecb, float *vecx, uint32_t nnz,
  384. unsigned nrow, uint32_t *colind, uint32_t *rowptr)
  385. {
  386. /* start the runtime */
  387. int ret;
  388. ret = starpu_init(NULL);
  389. if (ret == -ENODEV)
  390. exit(77);
  391. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  392. starpu_cublas_init();
  393. conjugate_gradient(nzvalA, vecb, vecx, nnz, nrow, colind, rowptr);
  394. starpu_shutdown();
  395. }