cg.c 11 KB

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