cg.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 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. #include <math.h>
  17. #include <assert.h>
  18. #include <starpu.h>
  19. #include <common/blas.h>
  20. #ifdef STARPU_USE_CUDA
  21. #include <cuda.h>
  22. #endif
  23. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  24. /*
  25. * Conjugate Gradient
  26. *
  27. * Input:
  28. * - matrix A
  29. * - vector b
  30. * - vector x (starting value)
  31. * - int i_max, error tolerance eps < 1.
  32. * Ouput:
  33. * - vector x
  34. *
  35. * Pseudo code:
  36. *
  37. * i <- 0
  38. * r <- b - Ax
  39. * d <- r
  40. * delta_new <- dot(r,r)
  41. * delta_0 <- delta_new
  42. *
  43. * while (i < i_max && delta_new > eps^2 delta_0)
  44. * {
  45. * q <- Ad
  46. * alpha <- delta_new/dot(d, q)
  47. * x <- x + alpha d
  48. *
  49. * If (i is divisible by 50)
  50. * r <- b - Ax
  51. * else
  52. * r <- r - alpha q
  53. *
  54. * delta_old <- delta_new
  55. * delta_new <- dot(r,r)
  56. * beta <- delta_new/delta_old
  57. * d <- r + beta d
  58. * i <- i + 1
  59. * }
  60. *
  61. * The dot() operations makes use of reduction to optimize parallelism.
  62. *
  63. */
  64. #include "cg.h"
  65. static int long long n = 4096;
  66. static int nblocks = 8;
  67. static int use_reduction = 1;
  68. static int display_result = 0;
  69. static starpu_data_handle_t A_handle, b_handle, x_handle;
  70. static TYPE *A, *b, *x;
  71. #ifdef STARPU_QUICK_CHECK
  72. static int i_max = 5;
  73. #elif !defined(STARPU_LONG_CHECK)
  74. static int i_max = 100;
  75. #else
  76. static int i_max = 1000;
  77. #endif
  78. static double eps = (10e-14);
  79. static starpu_data_handle_t r_handle, d_handle, q_handle;
  80. static TYPE *r, *d, *q;
  81. static starpu_data_handle_t dtq_handle, rtr_handle;
  82. static TYPE dtq, rtr;
  83. extern struct starpu_codelet accumulate_variable_cl;
  84. extern struct starpu_codelet accumulate_vector_cl;
  85. extern struct starpu_codelet bzero_variable_cl;
  86. extern struct starpu_codelet bzero_vector_cl;
  87. /*
  88. * Generate Input data
  89. */
  90. static void generate_random_problem(void)
  91. {
  92. int i, j;
  93. starpu_malloc((void **)&A, n*n*sizeof(TYPE));
  94. starpu_malloc((void **)&b, n*sizeof(TYPE));
  95. starpu_malloc((void **)&x, n*sizeof(TYPE));
  96. assert(A && b && x);
  97. for (j = 0; j < n; j++)
  98. {
  99. b[j] = (TYPE)1.0;
  100. x[j] = (TYPE)0.0;
  101. /* We take Hilbert matrix that is not well conditionned but definite positive: H(i,j) = 1/(1+i+j) */
  102. for (i = 0; i < n; i++)
  103. {
  104. A[n*j + i] = (TYPE)(1.0/(1.0+i+j));
  105. }
  106. }
  107. /* Internal vectors */
  108. starpu_malloc((void **)&r, n*sizeof(TYPE));
  109. starpu_malloc((void **)&d, n*sizeof(TYPE));
  110. starpu_malloc((void **)&q, n*sizeof(TYPE));
  111. assert(r && d && q);
  112. memset(r, 0, n*sizeof(TYPE));
  113. memset(d, 0, n*sizeof(TYPE));
  114. memset(q, 0, n*sizeof(TYPE));
  115. }
  116. static void free_data(void)
  117. {
  118. starpu_free(A);
  119. starpu_free(b);
  120. starpu_free(x);
  121. starpu_free(r);
  122. starpu_free(d);
  123. starpu_free(q);
  124. }
  125. static void register_data(void)
  126. {
  127. starpu_matrix_data_register(&A_handle, STARPU_MAIN_RAM, (uintptr_t)A, n, n, n, sizeof(TYPE));
  128. starpu_vector_data_register(&b_handle, STARPU_MAIN_RAM, (uintptr_t)b, n, sizeof(TYPE));
  129. starpu_vector_data_register(&x_handle, STARPU_MAIN_RAM, (uintptr_t)x, n, sizeof(TYPE));
  130. starpu_vector_data_register(&r_handle, STARPU_MAIN_RAM, (uintptr_t)r, n, sizeof(TYPE));
  131. starpu_vector_data_register(&d_handle, STARPU_MAIN_RAM, (uintptr_t)d, n, sizeof(TYPE));
  132. starpu_vector_data_register(&q_handle, STARPU_MAIN_RAM, (uintptr_t)q, n, sizeof(TYPE));
  133. starpu_variable_data_register(&dtq_handle, STARPU_MAIN_RAM, (uintptr_t)&dtq, sizeof(TYPE));
  134. starpu_variable_data_register(&rtr_handle, STARPU_MAIN_RAM, (uintptr_t)&rtr, sizeof(TYPE));
  135. if (use_reduction)
  136. {
  137. starpu_data_set_reduction_methods(q_handle, &accumulate_vector_cl, &bzero_vector_cl);
  138. starpu_data_set_reduction_methods(r_handle, &accumulate_vector_cl, &bzero_vector_cl);
  139. starpu_data_set_reduction_methods(dtq_handle, &accumulate_variable_cl, &bzero_variable_cl);
  140. starpu_data_set_reduction_methods(rtr_handle, &accumulate_variable_cl, &bzero_variable_cl);
  141. }
  142. }
  143. static void unregister_data(void)
  144. {
  145. starpu_data_unpartition(A_handle, STARPU_MAIN_RAM);
  146. starpu_data_unpartition(b_handle, STARPU_MAIN_RAM);
  147. starpu_data_unpartition(x_handle, STARPU_MAIN_RAM);
  148. starpu_data_unpartition(r_handle, STARPU_MAIN_RAM);
  149. starpu_data_unpartition(d_handle, STARPU_MAIN_RAM);
  150. starpu_data_unpartition(q_handle, STARPU_MAIN_RAM);
  151. starpu_data_unregister(A_handle);
  152. starpu_data_unregister(b_handle);
  153. starpu_data_unregister(x_handle);
  154. starpu_data_unregister(r_handle);
  155. starpu_data_unregister(d_handle);
  156. starpu_data_unregister(q_handle);
  157. starpu_data_unregister(dtq_handle);
  158. starpu_data_unregister(rtr_handle);
  159. }
  160. /*
  161. * Data partitioning filters
  162. */
  163. struct starpu_data_filter vector_filter;
  164. struct starpu_data_filter matrix_filter_1;
  165. struct starpu_data_filter matrix_filter_2;
  166. static void partition_data(void)
  167. {
  168. assert(n % nblocks == 0);
  169. /*
  170. * Partition the A matrix
  171. */
  172. /* Partition into contiguous parts */
  173. matrix_filter_1.filter_func = starpu_matrix_filter_block;
  174. matrix_filter_1.nchildren = nblocks;
  175. /* Partition into non-contiguous parts */
  176. matrix_filter_2.filter_func = starpu_matrix_filter_vertical_block;
  177. matrix_filter_2.nchildren = nblocks;
  178. /* A is in FORTRAN ordering, starpu_data_get_sub_data(A_handle, 2, i,
  179. * j) designates the block in column i and row j. */
  180. starpu_data_map_filters(A_handle, 2, &matrix_filter_1, &matrix_filter_2);
  181. /*
  182. * Partition the vectors
  183. */
  184. vector_filter.filter_func = starpu_vector_filter_block;
  185. vector_filter.nchildren = nblocks;
  186. starpu_data_partition(b_handle, &vector_filter);
  187. starpu_data_partition(x_handle, &vector_filter);
  188. starpu_data_partition(r_handle, &vector_filter);
  189. starpu_data_partition(d_handle, &vector_filter);
  190. starpu_data_partition(q_handle, &vector_filter);
  191. }
  192. /*
  193. * Debug
  194. */
  195. #if 0
  196. static void display_vector(starpu_data_handle_t handle, TYPE *ptr)
  197. {
  198. unsigned block_size = n / nblocks;
  199. unsigned b, ind;
  200. for (b = 0; b < nblocks; b++)
  201. {
  202. starpu_data_acquire(starpu_data_get_sub_data(handle, 1, b), STARPU_R);
  203. for (ind = 0; ind < block_size; ind++)
  204. {
  205. FPRINTF(stderr, "%2.2e ", ptr[b*block_size + ind]);
  206. }
  207. FPRINTF(stderr, "| ");
  208. starpu_data_release(starpu_data_get_sub_data(handle, 1, b));
  209. }
  210. FPRINTF(stderr, "\n");
  211. }
  212. static void display_matrix(void)
  213. {
  214. unsigned i, j;
  215. for (i = 0; i < n; i++)
  216. {
  217. for (j = 0; j < n; j++)
  218. {
  219. FPRINTF(stderr, "%2.2e ", A[j*n + i]);
  220. }
  221. FPRINTF(stderr, "\n");
  222. }
  223. }
  224. #endif
  225. static void display_x_result(void)
  226. {
  227. int j, i;
  228. starpu_data_handle_t sub;
  229. FPRINTF(stderr, "Computed X vector:\n");
  230. int block_size = n / nblocks;
  231. for (j = 0; j < nblocks; j++)
  232. {
  233. sub = starpu_data_get_sub_data(x_handle, 1, j);
  234. starpu_data_acquire(sub, STARPU_R);
  235. for (i = 0; i < block_size; i++)
  236. {
  237. FPRINTF(stderr, "% 02.2e\n", x[j*block_size + i]);
  238. }
  239. starpu_data_release(sub);
  240. }
  241. }
  242. /*
  243. * Main loop
  244. */
  245. static int cg(void)
  246. {
  247. double delta_new, delta_0;
  248. int i = 0;
  249. int ret;
  250. /* r <- b */
  251. ret = copy_handle(r_handle, b_handle, nblocks);
  252. if (ret == -ENODEV) return ret;
  253. /* r <- r - A x */
  254. ret = gemv_kernel(r_handle, A_handle, x_handle, 1.0, -1.0, nblocks, use_reduction);
  255. if (ret == -ENODEV) return ret;
  256. /* d <- r */
  257. ret = copy_handle(d_handle, r_handle, nblocks);
  258. if (ret == -ENODEV) return ret;
  259. /* delta_new = dot(r,r) */
  260. ret = dot_kernel(r_handle, r_handle, rtr_handle, nblocks, use_reduction);
  261. if (ret == -ENODEV) return ret;
  262. starpu_data_acquire(rtr_handle, STARPU_R);
  263. delta_new = rtr;
  264. delta_0 = delta_new;
  265. starpu_data_release(rtr_handle);
  266. FPRINTF(stderr, "Delta limit: %e\n", (double) (eps*eps*delta_0));
  267. FPRINTF(stderr, "**************** INITIAL ****************\n");
  268. FPRINTF(stderr, "Delta 0: %e\n", delta_new);
  269. double start;
  270. double end;
  271. start = starpu_timing_now();
  272. while ((i < i_max) && ((double)delta_new > (double)(eps*eps*delta_0)))
  273. {
  274. double delta_old;
  275. double alpha, beta;
  276. starpu_iteration_push(i);
  277. /* q <- A d */
  278. gemv_kernel(q_handle, A_handle, d_handle, 0.0, 1.0, nblocks, use_reduction);
  279. /* dtq <- dot(d,q) */
  280. dot_kernel(d_handle, q_handle, dtq_handle, nblocks, use_reduction);
  281. /* alpha = delta_new / dtq */
  282. starpu_data_acquire(dtq_handle, STARPU_R);
  283. alpha = delta_new/dtq;
  284. starpu_data_release(dtq_handle);
  285. /* x <- x + alpha d */
  286. axpy_kernel(x_handle, d_handle, alpha, nblocks);
  287. if ((i % 50) == 0)
  288. {
  289. /* r <- b */
  290. copy_handle(r_handle, b_handle, nblocks);
  291. /* r <- r - A x */
  292. gemv_kernel(r_handle, A_handle, x_handle, 1.0, -1.0, nblocks, use_reduction);
  293. }
  294. else
  295. {
  296. /* r <- r - alpha q */
  297. axpy_kernel(r_handle, q_handle, -alpha, nblocks);
  298. }
  299. /* delta_new = dot(r,r) */
  300. dot_kernel(r_handle, r_handle, rtr_handle, nblocks, use_reduction);
  301. starpu_data_acquire(rtr_handle, STARPU_R);
  302. delta_old = delta_new;
  303. delta_new = rtr;
  304. beta = delta_new / delta_old;
  305. starpu_data_release(rtr_handle);
  306. /* d <- beta d + r */
  307. scal_axpy_kernel(d_handle, beta, r_handle, 1.0, nblocks);
  308. if ((i % 10) == 0)
  309. {
  310. /* We here take the error as ||r||_2 / (n||b||_2) */
  311. double error = sqrt(delta_new/delta_0)/(1.0*n);
  312. FPRINTF(stderr, "*****************************************\n");
  313. FPRINTF(stderr, "iter %d DELTA %e - %e\n", i, delta_new, error);
  314. }
  315. starpu_iteration_pop();
  316. i++;
  317. }
  318. end = starpu_timing_now();
  319. double timing = end - start;
  320. FPRINTF(stderr, "Total timing : %2.2f seconds\n", timing/10e6);
  321. FPRINTF(stderr, "Seconds per iteration : %2.2e seconds\n", timing/10e6/i);
  322. FPRINTF(stderr, "Number of iterations per second : %2.2e it/s\n", i/(timing/10e6));
  323. return 0;
  324. }
  325. static void parse_args(int argc, char **argv)
  326. {
  327. int i;
  328. for (i = 1; i < argc; i++)
  329. {
  330. if (strcmp(argv[i], "-n") == 0)
  331. {
  332. n = (int long long)atoi(argv[++i]);
  333. continue;
  334. }
  335. if (strcmp(argv[i], "-display-result") == 0)
  336. {
  337. display_result = 1;
  338. continue;
  339. }
  340. if (strcmp(argv[i], "-maxiter") == 0)
  341. {
  342. i_max = atoi(argv[++i]);
  343. if (i_max <= 0)
  344. {
  345. FPRINTF(stderr, "the number of iterations must be positive, not %d\n", i_max);
  346. exit(EXIT_FAILURE);
  347. }
  348. continue;
  349. }
  350. if (strcmp(argv[i], "-nblocks") == 0)
  351. {
  352. nblocks = atoi(argv[++i]);
  353. continue;
  354. }
  355. if (strcmp(argv[i], "-no-reduction") == 0)
  356. {
  357. use_reduction = 0;
  358. continue;
  359. }
  360. if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-help") == 0)
  361. {
  362. FPRINTF(stderr, "usage: %s [-h] [-nblocks #blocks] [-display-result] [-n problem_size] [-no-reduction] [-maxiter i]\n", argv[0]);
  363. exit(-1);
  364. }
  365. }
  366. }
  367. int main(int argc, char **argv)
  368. {
  369. int ret;
  370. double start, end;
  371. /* Not supported yet */
  372. if (starpu_get_env_number_default("STARPU_GLOBAL_ARBITER", 0) > 0)
  373. return 77;
  374. parse_args(argc, argv);
  375. ret = starpu_init(NULL);
  376. if (ret == -ENODEV)
  377. return 77;
  378. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  379. starpu_cublas_init();
  380. FPRINTF(stderr, "************** PARAMETERS ***************\n");
  381. FPRINTF(stderr, "Problem size (-n): %lld\n", n);
  382. FPRINTF(stderr, "Maximum number of iterations (-maxiter): %d\n", i_max);
  383. FPRINTF(stderr, "Number of blocks (-nblocks): %d\n", nblocks);
  384. FPRINTF(stderr, "Reduction (-no-reduction): %s\n", use_reduction ? "enabled" : "disabled");
  385. start = starpu_timing_now();
  386. generate_random_problem();
  387. register_data();
  388. partition_data();
  389. end = starpu_timing_now();
  390. FPRINTF(stderr, "Problem intialization timing : %2.2f seconds\n", (end-start)/10e6);
  391. ret = cg();
  392. if (ret == -ENODEV)
  393. {
  394. ret = 77;
  395. goto enodev;
  396. }
  397. starpu_task_wait_for_all();
  398. if (display_result)
  399. {
  400. display_x_result();
  401. }
  402. enodev:
  403. unregister_data();
  404. free_data();
  405. starpu_cublas_shutdown();
  406. starpu_shutdown();
  407. return ret;
  408. }