cg.c 9.1 KB

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