cg.c 10 KB

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