cg.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 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. /*
  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 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 r_handle, d_handle, q_handle;
  72. static TYPE *r, *d, *q;
  73. static starpu_data_handle dtq_handle, rtr_handle;
  74. static TYPE dtq, rtr;
  75. extern starpu_codelet accumulate_variable_cl;
  76. extern starpu_codelet accumulate_vector_cl;
  77. extern starpu_codelet bzero_variable_cl;
  78. extern 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_data_malloc_pinned_if_possible((void **)&A, n*n*sizeof(TYPE));
  86. starpu_data_malloc_pinned_if_possible((void **)&b, n*sizeof(TYPE));
  87. starpu_data_malloc_pinned_if_possible((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_data_malloc_pinned_if_possible((void **)&r, n*sizeof(TYPE));
  101. starpu_data_malloc_pinned_if_possible((void **)&d, n*sizeof(TYPE));
  102. starpu_data_malloc_pinned_if_possible((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 register_data(void)
  109. {
  110. starpu_matrix_data_register(&A_handle, 0, (uintptr_t)A, n, n, n, sizeof(TYPE));
  111. starpu_vector_data_register(&b_handle, 0, (uintptr_t)b, n, sizeof(TYPE));
  112. starpu_vector_data_register(&x_handle, 0, (uintptr_t)x, n, sizeof(TYPE));
  113. starpu_vector_data_register(&r_handle, 0, (uintptr_t)r, n, sizeof(TYPE));
  114. starpu_vector_data_register(&d_handle, 0, (uintptr_t)d, n, sizeof(TYPE));
  115. starpu_vector_data_register(&q_handle, 0, (uintptr_t)q, n, sizeof(TYPE));
  116. starpu_variable_data_register(&dtq_handle, 0, (uintptr_t)&dtq, sizeof(TYPE));
  117. starpu_variable_data_register(&rtr_handle, 0, (uintptr_t)&rtr, sizeof(TYPE));
  118. if (use_reduction)
  119. {
  120. starpu_data_set_reduction_methods(q_handle, &accumulate_vector_cl, &bzero_vector_cl);
  121. starpu_data_set_reduction_methods(r_handle, &accumulate_vector_cl, &bzero_vector_cl);
  122. starpu_data_set_reduction_methods(dtq_handle, &accumulate_variable_cl, &bzero_variable_cl);
  123. starpu_data_set_reduction_methods(rtr_handle, &accumulate_variable_cl, &bzero_variable_cl);
  124. }
  125. }
  126. /*
  127. * Data partitioning filters
  128. */
  129. struct starpu_data_filter vector_filter;
  130. struct starpu_data_filter matrix_filter_1;
  131. struct starpu_data_filter matrix_filter_2;
  132. static void partition_data(void)
  133. {
  134. assert(n % nblocks == 0);
  135. /*
  136. * Partition the A matrix
  137. */
  138. /* Partition into contiguous parts */
  139. matrix_filter_1.filter_func = starpu_block_filter_func;
  140. matrix_filter_1.nchildren = nblocks;
  141. /* Partition into non-contiguous parts */
  142. matrix_filter_2.filter_func = starpu_vertical_block_filter_func;
  143. matrix_filter_2.nchildren = nblocks;
  144. /* A is in FORTRAN ordering, starpu_data_get_sub_data(A_handle, 2, i,
  145. * j) designates the block in column i and row j. */
  146. starpu_data_map_filters(A_handle, 2, &matrix_filter_1, &matrix_filter_2);
  147. /*
  148. * Partition the vectors
  149. */
  150. vector_filter.filter_func = starpu_block_filter_func_vector;
  151. vector_filter.nchildren = nblocks;
  152. starpu_data_partition(b_handle, &vector_filter);
  153. starpu_data_partition(x_handle, &vector_filter);
  154. starpu_data_partition(r_handle, &vector_filter);
  155. starpu_data_partition(d_handle, &vector_filter);
  156. starpu_data_partition(q_handle, &vector_filter);
  157. }
  158. /*
  159. * Debug
  160. */
  161. #if 0
  162. static void display_vector(starpu_data_handle handle, TYPE *ptr)
  163. {
  164. unsigned block_size = n / nblocks;
  165. unsigned b, ind;
  166. for (b = 0; b < nblocks; b++)
  167. {
  168. starpu_data_acquire(starpu_data_get_sub_data(handle, 1, b), STARPU_R);
  169. for (ind = 0; ind < block_size; ind++)
  170. {
  171. fprintf(stderr, "%2.2e ", ptr[b*block_size + ind]);
  172. }
  173. fprintf(stderr, "| ");
  174. starpu_data_release(starpu_data_get_sub_data(handle, 1, b));
  175. }
  176. fprintf(stderr, "\n");
  177. }
  178. static void display_matrix(void)
  179. {
  180. unsigned i, j;
  181. for (i = 0; i < n; i++)
  182. {
  183. for (j = 0; j < n; j++)
  184. {
  185. fprintf(stderr, "%2.2e ", A[j*n + i]);
  186. }
  187. fprintf(stderr, "\n");
  188. }
  189. }
  190. #endif
  191. /*
  192. * Main loop
  193. */
  194. static void cg(void)
  195. {
  196. double delta_new, delta_old, delta_0;
  197. double alpha, beta;
  198. int i = 0;
  199. /* r <- b */
  200. copy_handle(r_handle, b_handle, nblocks);
  201. /* r <- r - A x */
  202. gemv_kernel(r_handle, A_handle, x_handle, 1.0, -1.0, nblocks, use_reduction);
  203. /* d <- r */
  204. copy_handle(d_handle, r_handle, nblocks);
  205. /* delta_new = dot(r,r) */
  206. dot_kernel(r_handle, r_handle, rtr_handle, nblocks, use_reduction);
  207. starpu_data_acquire(rtr_handle, STARPU_R);
  208. delta_new = rtr;
  209. delta_0 = delta_new;
  210. starpu_data_release(rtr_handle);
  211. fprintf(stderr, "*************** INITIAL ************ \n");
  212. fprintf(stderr, "Delta 0: %e\n", delta_new);
  213. struct timeval start;
  214. struct timeval end;
  215. gettimeofday(&start, NULL);
  216. while ((i < i_max) && ((double)delta_new > (double)(eps*eps*delta_0)))
  217. {
  218. /* q <- A d */
  219. gemv_kernel(q_handle, A_handle, d_handle, 0.0, 1.0, nblocks, use_reduction);
  220. /* dtq <- dot(d,q) */
  221. dot_kernel(d_handle, q_handle, dtq_handle, nblocks, use_reduction);
  222. /* alpha = delta_new / dtq */
  223. starpu_data_acquire(dtq_handle, STARPU_R);
  224. alpha = delta_new/dtq;
  225. starpu_data_release(dtq_handle);
  226. /* x <- x + alpha d */
  227. axpy_kernel(x_handle, d_handle, alpha, nblocks);
  228. if ((i % 50) == 0)
  229. {
  230. /* r <- b */
  231. copy_handle(r_handle, b_handle, nblocks);
  232. /* r <- r - A x */
  233. gemv_kernel(r_handle, A_handle, x_handle, 1.0, -1.0, nblocks, use_reduction);
  234. }
  235. else {
  236. /* r <- r - alpha q */
  237. axpy_kernel(r_handle, q_handle, -alpha, nblocks);
  238. }
  239. /* delta_new = dot(r,r) */
  240. dot_kernel(r_handle, r_handle, rtr_handle, nblocks, use_reduction);
  241. starpu_data_acquire(rtr_handle, STARPU_R);
  242. delta_old = delta_new;
  243. delta_new = rtr;
  244. beta = delta_new / delta_old;
  245. starpu_data_release(rtr_handle);
  246. /* d <- beta d + r */
  247. scal_axpy_kernel(d_handle, beta, r_handle, 1.0, nblocks);
  248. if ((i % 10) == 0)
  249. {
  250. /* We here take the error as ||r||_2 / (n||b||_2) */
  251. double error = sqrt(delta_new/delta_0)/(1.0*n);
  252. fprintf(stderr, "*****************************************\n");
  253. fprintf(stderr, "iter %d DELTA %e - %e\n", i, delta_new, error);
  254. }
  255. i++;
  256. }
  257. gettimeofday(&end, NULL);
  258. double timing = (double)(((double)end.tv_sec - (double)start.tv_sec)*10e6 + ((double)end.tv_usec - (double)start.tv_usec));
  259. fprintf(stderr, "Total timing : %2.2f seconds\n", timing/10e6);
  260. fprintf(stderr, "Seconds per iteration : %2.2e\n", timing/10e6/i);
  261. }
  262. static int check(void)
  263. {
  264. return 0;
  265. }
  266. static void parse_args(int argc, char **argv)
  267. {
  268. int i;
  269. for (i = 1; i < argc; i++) {
  270. if (strcmp(argv[i], "-n") == 0) {
  271. n = (int long long)atoi(argv[++i]);
  272. continue;
  273. }
  274. if (strcmp(argv[i], "-maxiter") == 0) {
  275. i_max = atoi(argv[++i]);
  276. continue;
  277. }
  278. if (strcmp(argv[i], "-nblocks") == 0) {
  279. nblocks = atoi(argv[++i]);
  280. continue;
  281. }
  282. if (strcmp(argv[i], "-no-reduction") == 0) {
  283. use_reduction = 0;
  284. continue;
  285. }
  286. if (strcmp(argv[i], "-h") == 0) {
  287. fprintf(stderr, "usage: %s [-h] [-nblocks #blocks] [-n problem_size] [-no-reduction] [-maxiter i]\n", argv[0]);
  288. exit(-1);
  289. continue;
  290. }
  291. }
  292. }
  293. int main(int argc, char **argv)
  294. {
  295. int ret;
  296. parse_args(argc, argv);
  297. starpu_init(NULL);
  298. starpu_helper_cublas_init();
  299. generate_random_problem();
  300. register_data();
  301. partition_data();
  302. cg();
  303. ret = check();
  304. starpu_helper_cublas_shutdown();
  305. starpu_shutdown();
  306. return ret;
  307. }