cg.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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. /*
  21. * Conjugate Gradient
  22. *
  23. * Input:
  24. * - matrix A
  25. * - vector b
  26. * - vector x (starting value)
  27. * - int i_max, error tolerance eps < 1.
  28. * Ouput:
  29. * - vector x
  30. *
  31. * Pseudo code:
  32. *
  33. * i <- 0
  34. * r <- b - Ax
  35. * d <- r
  36. * delta_new <- dot(r,r)
  37. * delta_0 <- delta_new
  38. *
  39. * while (i < i_max && delta_new > eps^2 delta_0)
  40. * {
  41. * q <- Ad
  42. * alpha <- delta_new/dot(d, q)
  43. * x <- x + alpha d
  44. *
  45. * If (i is divisible by 50)
  46. * r <- b - Ax
  47. * else
  48. * r <- r - alpha q
  49. *
  50. * delta_old <- delta_new
  51. * delta_new <- dot(r,r)
  52. * beta <- delta_new/delta_old
  53. * d <- r + beta d
  54. * i <- i + 1
  55. * }
  56. *
  57. * The dot() operations makes use of reduction to optimize parallelism.
  58. *
  59. */
  60. #include "cg.h"
  61. static int copy_handle(starpu_data_handle_t dst, starpu_data_handle_t src, unsigned nblocks);
  62. #define HANDLE_TYPE_VECTOR starpu_data_handle_t
  63. #define HANDLE_TYPE_MATRIX starpu_data_handle_t
  64. #define TASK_INSERT(cl, ...) starpu_task_insert(cl, ##__VA_ARGS__)
  65. #define GET_VECTOR_BLOCK(v, i) starpu_data_get_sub_data(v, 1, i)
  66. #define GET_MATRIX_BLOCK(m, i, j) starpu_data_get_sub_data(m, 2, i, j)
  67. #define BARRIER()
  68. #define GET_DATA_HANDLE(handle)
  69. #define FPRINTF_SERVER FPRINTF
  70. #include "cg_kernels.c"
  71. static TYPE *A, *b, *x;
  72. static TYPE *r, *d, *q;
  73. static int copy_handle(starpu_data_handle_t dst, starpu_data_handle_t src, unsigned nb)
  74. {
  75. unsigned block;
  76. for (block = 0; block < nb; block++)
  77. starpu_data_cpy(starpu_data_get_sub_data(dst, 1, block), starpu_data_get_sub_data(src, 1, block), 1, NULL, NULL);
  78. return 0;
  79. }
  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 free_data(void)
  110. {
  111. starpu_free(A);
  112. starpu_free(b);
  113. starpu_free(x);
  114. starpu_free(r);
  115. starpu_free(d);
  116. starpu_free(q);
  117. }
  118. static void register_data(void)
  119. {
  120. starpu_matrix_data_register(&A_handle, STARPU_MAIN_RAM, (uintptr_t)A, n, n, n, sizeof(TYPE));
  121. starpu_vector_data_register(&b_handle, STARPU_MAIN_RAM, (uintptr_t)b, n, sizeof(TYPE));
  122. starpu_vector_data_register(&x_handle, STARPU_MAIN_RAM, (uintptr_t)x, n, sizeof(TYPE));
  123. starpu_vector_data_register(&r_handle, STARPU_MAIN_RAM, (uintptr_t)r, n, sizeof(TYPE));
  124. starpu_vector_data_register(&d_handle, STARPU_MAIN_RAM, (uintptr_t)d, n, sizeof(TYPE));
  125. starpu_vector_data_register(&q_handle, STARPU_MAIN_RAM, (uintptr_t)q, n, sizeof(TYPE));
  126. starpu_variable_data_register(&dtq_handle, STARPU_MAIN_RAM, (uintptr_t)&dtq, sizeof(TYPE));
  127. starpu_variable_data_register(&rtr_handle, STARPU_MAIN_RAM, (uintptr_t)&rtr, sizeof(TYPE));
  128. if (use_reduction)
  129. {
  130. starpu_data_set_reduction_methods(q_handle, &accumulate_vector_cl, &bzero_vector_cl);
  131. starpu_data_set_reduction_methods(r_handle, &accumulate_vector_cl, &bzero_vector_cl);
  132. starpu_data_set_reduction_methods(dtq_handle, &accumulate_variable_cl, &bzero_variable_cl);
  133. starpu_data_set_reduction_methods(rtr_handle, &accumulate_variable_cl, &bzero_variable_cl);
  134. }
  135. }
  136. static void unregister_data(void)
  137. {
  138. starpu_data_unpartition(A_handle, STARPU_MAIN_RAM);
  139. starpu_data_unpartition(b_handle, STARPU_MAIN_RAM);
  140. starpu_data_unpartition(x_handle, STARPU_MAIN_RAM);
  141. starpu_data_unpartition(r_handle, STARPU_MAIN_RAM);
  142. starpu_data_unpartition(d_handle, STARPU_MAIN_RAM);
  143. starpu_data_unpartition(q_handle, STARPU_MAIN_RAM);
  144. starpu_data_unregister(A_handle);
  145. starpu_data_unregister(b_handle);
  146. starpu_data_unregister(x_handle);
  147. starpu_data_unregister(r_handle);
  148. starpu_data_unregister(d_handle);
  149. starpu_data_unregister(q_handle);
  150. starpu_data_unregister(dtq_handle);
  151. starpu_data_unregister(rtr_handle);
  152. }
  153. /*
  154. * Data partitioning filters
  155. */
  156. struct starpu_data_filter vector_filter;
  157. struct starpu_data_filter matrix_filter_1;
  158. struct starpu_data_filter matrix_filter_2;
  159. static void partition_data(void)
  160. {
  161. assert(n % nblocks == 0);
  162. /*
  163. * Partition the A matrix
  164. */
  165. /* Partition into contiguous parts */
  166. matrix_filter_1.filter_func = starpu_matrix_filter_block;
  167. matrix_filter_1.nchildren = nblocks;
  168. /* Partition into non-contiguous parts */
  169. matrix_filter_2.filter_func = starpu_matrix_filter_vertical_block;
  170. matrix_filter_2.nchildren = nblocks;
  171. /* A is in FORTRAN ordering, starpu_data_get_sub_data(A_handle, 2, i,
  172. * j) designates the block in column i and row j. */
  173. starpu_data_map_filters(A_handle, 2, &matrix_filter_1, &matrix_filter_2);
  174. /*
  175. * Partition the vectors
  176. */
  177. vector_filter.filter_func = starpu_vector_filter_block;
  178. vector_filter.nchildren = nblocks;
  179. starpu_data_partition(b_handle, &vector_filter);
  180. starpu_data_partition(x_handle, &vector_filter);
  181. starpu_data_partition(r_handle, &vector_filter);
  182. starpu_data_partition(d_handle, &vector_filter);
  183. starpu_data_partition(q_handle, &vector_filter);
  184. }
  185. /*
  186. * Debug
  187. */
  188. #if 0
  189. static void display_vector(starpu_data_handle_t handle, TYPE *ptr)
  190. {
  191. unsigned block_size = n / nblocks;
  192. unsigned b, ind;
  193. for (b = 0; b < nblocks; b++)
  194. {
  195. starpu_data_acquire(starpu_data_get_sub_data(handle, 1, b), STARPU_R);
  196. for (ind = 0; ind < block_size; ind++)
  197. {
  198. FPRINTF(stderr, "%2.2e ", ptr[b*block_size + ind]);
  199. }
  200. FPRINTF(stderr, "| ");
  201. starpu_data_release(starpu_data_get_sub_data(handle, 1, b));
  202. }
  203. FPRINTF(stderr, "\n");
  204. }
  205. static void display_matrix(void)
  206. {
  207. unsigned i, j;
  208. for (i = 0; i < n; i++)
  209. {
  210. for (j = 0; j < n; j++)
  211. {
  212. FPRINTF(stderr, "%2.2e ", A[j*n + i]);
  213. }
  214. FPRINTF(stderr, "\n");
  215. }
  216. }
  217. #endif
  218. static void display_x_result(void)
  219. {
  220. unsigned j, i;
  221. starpu_data_handle_t sub;
  222. FPRINTF(stderr, "Computed X vector:\n");
  223. unsigned block_size = n / nblocks;
  224. for (j = 0; j < nblocks; j++)
  225. {
  226. sub = starpu_data_get_sub_data(x_handle, 1, j);
  227. starpu_data_acquire(sub, STARPU_R);
  228. for (i = 0; i < block_size; i++)
  229. {
  230. FPRINTF(stderr, "% 02.2e\n", x[j*block_size + i]);
  231. }
  232. starpu_data_release(sub);
  233. }
  234. }
  235. static void parse_args(int argc, char **argv)
  236. {
  237. int i;
  238. for (i = 1; i < argc; i++)
  239. {
  240. if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-help") == 0)
  241. {
  242. FPRINTF_SERVER(stderr, "usage: %s [-h] [-nblocks #blocks] [-display-result] [-n problem_size] [-no-reduction] [-maxiter i]\n", argv[0]);
  243. exit(-1);
  244. }
  245. }
  246. parse_common_args(argc, argv);
  247. }
  248. int main(int argc, char **argv)
  249. {
  250. int ret;
  251. double start, end;
  252. /* Not supported yet */
  253. if (starpu_get_env_number_default("STARPU_GLOBAL_ARBITER", 0) > 0)
  254. return 77;
  255. parse_args(argc, argv);
  256. ret = starpu_init(NULL);
  257. if (ret == -ENODEV)
  258. return 77;
  259. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  260. if (starpu_cpu_worker_get_count() + starpu_cuda_worker_get_count() + starpu_opencl_worker_get_count() == 0)
  261. {
  262. starpu_shutdown();
  263. return 77;
  264. }
  265. starpu_cublas_init();
  266. FPRINTF(stderr, "************** PARAMETERS ***************\n");
  267. FPRINTF(stderr, "Problem size (-n): %lld\n", n);
  268. FPRINTF(stderr, "Maximum number of iterations (-maxiter): %d\n", i_max);
  269. FPRINTF(stderr, "Number of blocks (-nblocks): %d\n", nblocks);
  270. FPRINTF(stderr, "Reduction (-no-reduction): %s\n", use_reduction ? "enabled" : "disabled");
  271. start = starpu_timing_now();
  272. generate_random_problem();
  273. register_data();
  274. partition_data();
  275. end = starpu_timing_now();
  276. FPRINTF(stderr, "Problem intialization timing : %2.2f seconds\n", (end-start)/1e6);
  277. ret = cg();
  278. if (ret == -ENODEV)
  279. {
  280. ret = 77;
  281. goto enodev;
  282. }
  283. starpu_task_wait_for_all();
  284. if (display_result)
  285. {
  286. display_x_result();
  287. }
  288. enodev:
  289. unregister_data();
  290. free_data();
  291. starpu_cublas_shutdown();
  292. starpu_shutdown();
  293. return ret;
  294. }