cg.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011 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_unregister(A_handle);
  130. starpu_data_unregister(b_handle);
  131. starpu_data_unregister(x_handle);
  132. starpu_data_unregister(r_handle);
  133. starpu_data_unregister(d_handle);
  134. starpu_data_unregister(q_handle);
  135. starpu_data_unregister(dtq_handle);
  136. starpu_data_unregister(rtr_handle);
  137. }
  138. /*
  139. * Data partitioning filters
  140. */
  141. struct starpu_data_filter vector_filter;
  142. struct starpu_data_filter matrix_filter_1;
  143. struct starpu_data_filter matrix_filter_2;
  144. static void partition_data(void)
  145. {
  146. assert(n % nblocks == 0);
  147. /*
  148. * Partition the A matrix
  149. */
  150. /* Partition into contiguous parts */
  151. matrix_filter_1.filter_func = starpu_block_filter_func;
  152. matrix_filter_1.nchildren = nblocks;
  153. /* Partition into non-contiguous parts */
  154. matrix_filter_2.filter_func = starpu_vertical_block_filter_func;
  155. matrix_filter_2.nchildren = nblocks;
  156. /* A is in FORTRAN ordering, starpu_data_get_sub_data(A_handle, 2, i,
  157. * j) designates the block in column i and row j. */
  158. starpu_data_map_filters(A_handle, 2, &matrix_filter_1, &matrix_filter_2);
  159. /*
  160. * Partition the vectors
  161. */
  162. vector_filter.filter_func = starpu_block_filter_func_vector;
  163. vector_filter.nchildren = nblocks;
  164. starpu_data_partition(b_handle, &vector_filter);
  165. starpu_data_partition(x_handle, &vector_filter);
  166. starpu_data_partition(r_handle, &vector_filter);
  167. starpu_data_partition(d_handle, &vector_filter);
  168. starpu_data_partition(q_handle, &vector_filter);
  169. }
  170. /*
  171. * Debug
  172. */
  173. #if 0
  174. static void display_vector(starpu_data_handle_t handle, TYPE *ptr)
  175. {
  176. unsigned block_size = n / nblocks;
  177. unsigned b, ind;
  178. for (b = 0; b < nblocks; b++)
  179. {
  180. starpu_data_acquire(starpu_data_get_sub_data(handle, 1, b), STARPU_R);
  181. for (ind = 0; ind < block_size; ind++)
  182. {
  183. FPRINTF(stderr, "%2.2e ", ptr[b*block_size + ind]);
  184. }
  185. FPRINTF(stderr, "| ");
  186. starpu_data_release(starpu_data_get_sub_data(handle, 1, b));
  187. }
  188. FPRINTF(stderr, "\n");
  189. }
  190. static void display_matrix(void)
  191. {
  192. unsigned i, j;
  193. for (i = 0; i < n; i++)
  194. {
  195. for (j = 0; j < n; j++)
  196. {
  197. FPRINTF(stderr, "%2.2e ", A[j*n + i]);
  198. }
  199. FPRINTF(stderr, "\n");
  200. }
  201. }
  202. #endif
  203. /*
  204. * Main loop
  205. */
  206. static int cg(void)
  207. {
  208. double delta_new, delta_old, delta_0;
  209. double alpha, beta;
  210. int i = 0;
  211. int ret;
  212. /* r <- b */
  213. ret = copy_handle(r_handle, b_handle, nblocks);
  214. if (ret == -ENODEV) return ret;
  215. /* r <- r - A x */
  216. ret = gemv_kernel(r_handle, A_handle, x_handle, 1.0, -1.0, nblocks, use_reduction);
  217. if (ret == -ENODEV) return ret;
  218. /* d <- r */
  219. ret = copy_handle(d_handle, r_handle, nblocks);
  220. if (ret == -ENODEV) return ret;
  221. /* delta_new = dot(r,r) */
  222. ret = dot_kernel(r_handle, r_handle, rtr_handle, nblocks, use_reduction);
  223. if (ret == -ENODEV) return ret;
  224. starpu_data_acquire(rtr_handle, STARPU_R);
  225. delta_new = rtr;
  226. delta_0 = delta_new;
  227. starpu_data_release(rtr_handle);
  228. FPRINTF(stderr, "*************** INITIAL ************ \n");
  229. FPRINTF(stderr, "Delta 0: %e\n", delta_new);
  230. struct timeval start;
  231. struct timeval end;
  232. gettimeofday(&start, NULL);
  233. while ((i < i_max) && ((double)delta_new > (double)(eps*eps*delta_0)))
  234. {
  235. /* q <- A d */
  236. gemv_kernel(q_handle, A_handle, d_handle, 0.0, 1.0, nblocks, use_reduction);
  237. /* dtq <- dot(d,q) */
  238. dot_kernel(d_handle, q_handle, dtq_handle, nblocks, use_reduction);
  239. /* alpha = delta_new / dtq */
  240. starpu_data_acquire(dtq_handle, STARPU_R);
  241. alpha = delta_new/dtq;
  242. starpu_data_release(dtq_handle);
  243. /* x <- x + alpha d */
  244. axpy_kernel(x_handle, d_handle, alpha, nblocks);
  245. if ((i % 50) == 0)
  246. {
  247. /* r <- b */
  248. copy_handle(r_handle, b_handle, nblocks);
  249. /* r <- r - A x */
  250. gemv_kernel(r_handle, A_handle, x_handle, 1.0, -1.0, nblocks, use_reduction);
  251. }
  252. else
  253. {
  254. /* r <- r - alpha q */
  255. axpy_kernel(r_handle, q_handle, -alpha, nblocks);
  256. }
  257. /* delta_new = dot(r,r) */
  258. dot_kernel(r_handle, r_handle, rtr_handle, nblocks, use_reduction);
  259. starpu_data_acquire(rtr_handle, STARPU_R);
  260. delta_old = delta_new;
  261. delta_new = rtr;
  262. beta = delta_new / delta_old;
  263. starpu_data_release(rtr_handle);
  264. /* d <- beta d + r */
  265. scal_axpy_kernel(d_handle, beta, r_handle, 1.0, nblocks);
  266. if ((i % 10) == 0)
  267. {
  268. /* We here take the error as ||r||_2 / (n||b||_2) */
  269. double error = sqrt(delta_new/delta_0)/(1.0*n);
  270. FPRINTF(stderr, "*****************************************\n");
  271. FPRINTF(stderr, "iter %d DELTA %e - %e\n", i, delta_new, error);
  272. }
  273. i++;
  274. }
  275. gettimeofday(&end, NULL);
  276. double timing = (double)(((double)end.tv_sec - (double)start.tv_sec)*10e6 + ((double)end.tv_usec - (double)start.tv_usec));
  277. FPRINTF(stderr, "Total timing : %2.2f seconds\n", timing/10e6);
  278. FPRINTF(stderr, "Seconds per iteration : %2.2e\n", timing/10e6/i);
  279. return 0;
  280. }
  281. static int check(void)
  282. {
  283. return 0;
  284. }
  285. static void parse_args(int argc, char **argv)
  286. {
  287. int i;
  288. for (i = 1; i < argc; i++)
  289. {
  290. if (strcmp(argv[i], "-n") == 0)
  291. {
  292. n = (int long long)atoi(argv[++i]);
  293. continue;
  294. }
  295. if (strcmp(argv[i], "-maxiter") == 0)
  296. {
  297. i_max = atoi(argv[++i]);
  298. continue;
  299. }
  300. if (strcmp(argv[i], "-nblocks") == 0)
  301. {
  302. nblocks = atoi(argv[++i]);
  303. continue;
  304. }
  305. if (strcmp(argv[i], "-no-reduction") == 0)
  306. {
  307. use_reduction = 0;
  308. continue;
  309. }
  310. if (strcmp(argv[i], "-h") == 0)
  311. {
  312. FPRINTF(stderr, "usage: %s [-h] [-nblocks #blocks] [-n problem_size] [-no-reduction] [-maxiter i]\n", argv[0]);
  313. exit(-1);
  314. continue;
  315. }
  316. }
  317. }
  318. int main(int argc, char **argv)
  319. {
  320. int ret;
  321. parse_args(argc, argv);
  322. ret = starpu_init(NULL);
  323. if (ret == -ENODEV)
  324. return 77;
  325. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  326. starpu_helper_cublas_init();
  327. generate_random_problem();
  328. register_data();
  329. partition_data();
  330. ret = cg();
  331. if (ret == -ENODEV) goto enodev;
  332. ret = check();
  333. starpu_task_wait_for_all();
  334. unregister_data();
  335. starpu_helper_cublas_shutdown();
  336. starpu_shutdown();
  337. return ret;
  338. enodev:
  339. starpu_shutdown();
  340. return 77;
  341. }