cg.c 10 KB

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