lu_example.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include <math.h>
  22. #include <starpu.h>
  23. #include <starpu_profiling.h>
  24. #include <starpu_bound.h>
  25. #include "xlu.h"
  26. #include "xlu_kernels.h"
  27. static unsigned long lu_size = 4096;
  28. static unsigned lu_nblocks = 16;
  29. static unsigned lu_check = 0;
  30. static unsigned pivot = 0;
  31. static unsigned no_stride = 0;
  32. static unsigned profile = 0;
  33. static unsigned bound = 0;
  34. static unsigned bounddeps = 0;
  35. static unsigned boundprio = 0;
  36. //struct timeval lu_start;
  37. //struct timeval lu_end;
  38. unsigned *ipiv;
  39. TYPE *A, *A_saved;
  40. /* in case we use non-strided blocks */
  41. TYPE **A_blocks;
  42. static void lu_parse_args(int argc, char **argv)
  43. {
  44. int i;
  45. for (i = 1; i < argc; i++) {
  46. if (strcmp(argv[i], "-size") == 0) {
  47. char *argptr;
  48. lu_size = strtol(argv[++i], &argptr, 10);
  49. }
  50. if (strcmp(argv[i], "-nblocks") == 0) {
  51. char *argptr;
  52. lu_nblocks = strtol(argv[++i], &argptr, 10);
  53. }
  54. if (strcmp(argv[i], "-check") == 0) {
  55. lu_check = 1;
  56. }
  57. if (strcmp(argv[i], "-piv") == 0) {
  58. pivot = 1;
  59. }
  60. if (strcmp(argv[i], "-no-stride") == 0) {
  61. no_stride = 1;
  62. }
  63. if (strcmp(argv[i], "-profile") == 0) {
  64. profile = 1;
  65. }
  66. if (strcmp(argv[i], "-bound") == 0) {
  67. bound = 1;
  68. }
  69. if (strcmp(argv[i], "-bounddeps") == 0) {
  70. bound = 1;
  71. bounddeps = 1;
  72. }
  73. if (strcmp(argv[i], "-bounddepsprio") == 0) {
  74. bound = 1;
  75. bounddeps = 1;
  76. boundprio = 1;
  77. }
  78. }
  79. }
  80. static void display_matrix(TYPE *m, unsigned n, unsigned ld, char *str)
  81. {
  82. #if 0
  83. fprintf(stderr, "***********\n");
  84. fprintf(stderr, "Display matrix %s\n", str);
  85. unsigned i,j;
  86. for (j = 0; j < n; j++)
  87. {
  88. for (i = 0; i < n; i++)
  89. {
  90. fprintf(stderr, "%2.2f\t", m[i+j*ld]);
  91. }
  92. fprintf(stderr, "\n");
  93. }
  94. fprintf(stderr, "***********\n");
  95. #endif
  96. }
  97. void copy_blocks_into_matrix(void)
  98. {
  99. unsigned blocklu_size = (lu_size/lu_nblocks);
  100. unsigned i, j;
  101. unsigned bi, bj;
  102. for (bj = 0; bj < lu_nblocks; bj++)
  103. for (bi = 0; bi < lu_nblocks; bi++)
  104. {
  105. for (j = 0; j < blocklu_size; j++)
  106. for (i = 0; i < blocklu_size; i++)
  107. {
  108. A[(i+bi*blocklu_size) + (j + bj*blocklu_size)*lu_size] =
  109. A_blocks[bi+lu_nblocks*bj][i + j * blocklu_size];
  110. }
  111. //free(A_blocks[bi+nblocks*bj]);
  112. }
  113. }
  114. void copy_matrix_into_blocks(void)
  115. {
  116. unsigned blocklu_size = (lu_size/lu_nblocks);
  117. unsigned i, j;
  118. unsigned bi, bj;
  119. for (bj = 0; bj < lu_nblocks; bj++)
  120. for (bi = 0; bi < lu_nblocks; bi++)
  121. {
  122. starpu_data_malloc_pinned_if_possible((void **)&A_blocks[bi+lu_nblocks*bj], (size_t)blocklu_size*blocklu_size*sizeof(TYPE));
  123. for (j = 0; j < blocklu_size; j++)
  124. for (i = 0; i < blocklu_size; i++)
  125. {
  126. A_blocks[bi+lu_nblocks*bj][i + j * blocklu_size] =
  127. A[(i+bi*blocklu_size) + (j + bj*blocklu_size)*lu_size];
  128. }
  129. }
  130. }
  131. static void init_matrix(void)
  132. {
  133. /* allocate matrix */
  134. starpu_data_malloc_pinned_if_possible((void **)&A, (size_t)lu_size*lu_size*sizeof(TYPE));
  135. STARPU_ASSERT(A);
  136. starpu_srand48((long int)time(NULL));
  137. //starpu_srand48(0);
  138. /* initialize matrix content */
  139. unsigned long i,j;
  140. for (j = 0; j < lu_size; j++)
  141. {
  142. for (i = 0; i < lu_size; i++)
  143. {
  144. A[i + j*lu_size] = (TYPE)starpu_drand48();
  145. }
  146. }
  147. }
  148. static void save_matrix(void)
  149. {
  150. A_saved = malloc((size_t)lu_size*lu_size*sizeof(TYPE));
  151. STARPU_ASSERT(A_saved);
  152. memcpy(A_saved, A, (size_t)lu_size*lu_size*sizeof(TYPE));
  153. }
  154. static double frobenius_norm(TYPE *v, unsigned n)
  155. {
  156. double sum2 = 0.0;
  157. /* compute sqrt(Sum(|x|^2)) */
  158. unsigned i,j;
  159. for (j = 0; j < n; j++)
  160. for (i = 0; i < n; i++)
  161. {
  162. double a = fabsl((double)v[i+n*j]);
  163. sum2 += a*a;
  164. }
  165. return sqrt(sum2);
  166. }
  167. static void pivot_saved_matrix(unsigned *ipiv)
  168. {
  169. unsigned k;
  170. for (k = 0; k < lu_size; k++)
  171. {
  172. if (k != ipiv[k])
  173. {
  174. // fprintf(stderr, "SWAP %d and %d\n", k, ipiv[k]);
  175. CPU_SWAP(lu_size, &A_saved[k*lu_size], 1, &A_saved[ipiv[k]*lu_size], 1);
  176. }
  177. }
  178. }
  179. static void check_result(void)
  180. {
  181. unsigned i,j;
  182. TYPE *L, *U;
  183. L = malloc((size_t)lu_size*lu_size*sizeof(TYPE));
  184. U = malloc((size_t)lu_size*lu_size*sizeof(TYPE));
  185. memset(L, 0, lu_size*lu_size*sizeof(TYPE));
  186. memset(U, 0, lu_size*lu_size*sizeof(TYPE));
  187. /* only keep the lower part */
  188. for (j = 0; j < lu_size; j++)
  189. {
  190. for (i = 0; i < j; i++)
  191. {
  192. L[j+i*lu_size] = A[j+i*lu_size];
  193. }
  194. /* diag i = j */
  195. L[j+j*lu_size] = A[j+j*lu_size];
  196. U[j+j*lu_size] = 1.0;
  197. for (i = j+1; i < lu_size; i++)
  198. {
  199. U[j+i*lu_size] = A[j+i*lu_size];
  200. }
  201. }
  202. display_matrix(L, lu_size, lu_size, "L");
  203. display_matrix(U, lu_size, lu_size, "U");
  204. /* now A_err = L, compute L*U */
  205. CPU_TRMM("R", "U", "N", "U", lu_size, lu_size, 1.0f, U, lu_size, L, lu_size);
  206. display_matrix(A_saved, lu_size, lu_size, "P A_saved");
  207. display_matrix(L, lu_size, lu_size, "LU");
  208. /* compute "LU - A" in L*/
  209. CPU_AXPY(lu_size*lu_size, -1.0, A_saved, 1, L, 1);
  210. display_matrix(L, lu_size, lu_size, "Residuals");
  211. TYPE err = CPU_ASUM(lu_size*lu_size, L, 1);
  212. int max = CPU_IAMAX(lu_size*lu_size, L, 1);
  213. fprintf(stderr, "Avg error : %e\n", err/(lu_size*lu_size));
  214. fprintf(stderr, "Max error : %e\n", L[max]);
  215. double residual = frobenius_norm(L, lu_size);
  216. double matnorm = frobenius_norm(A_saved, lu_size);
  217. fprintf(stderr, "||%sA-LU|| / (||A||*N) : %e\n", pivot?"P":"", residual/(matnorm*lu_size));
  218. if (residual/(matnorm*lu_size) > 1e-5)
  219. exit(-1);
  220. }
  221. int run_lu(struct starpu_sched_ctx *sched_ctx, int argc, char **argv, struct timeval *start)
  222. {
  223. lu_parse_args(argc, argv);
  224. // starpu_init(NULL);
  225. // starpu_helper_cublas_init();
  226. init_matrix();
  227. if (lu_check)
  228. save_matrix();
  229. display_matrix(A, lu_size, lu_size, "A");
  230. if (bound)
  231. starpu_bound_start(bounddeps, boundprio);
  232. if (profile)
  233. starpu_profiling_status_set(STARPU_PROFILING_ENABLE);
  234. /* Factorize the matrix (in place) */
  235. if (pivot)
  236. {
  237. ipiv = malloc(lu_size*sizeof(unsigned));
  238. if (no_stride)
  239. {
  240. /* in case the LU decomposition uses non-strided blocks, we _copy_ the matrix into smaller blocks */
  241. A_blocks = malloc(lu_nblocks*lu_nblocks*sizeof(TYPE **));
  242. copy_matrix_into_blocks();
  243. STARPU_LU(lu_decomposition_pivot_no_stride)(A_blocks, ipiv, lu_size, lu_size, lu_nblocks, sched_ctx, start);
  244. copy_blocks_into_matrix();
  245. free(A_blocks);
  246. }
  247. else
  248. {
  249. STARPU_LU(lu_decomposition_pivot)(A, ipiv, lu_size, lu_size, lu_nblocks, sched_ctx, start);
  250. }
  251. }
  252. else
  253. {
  254. STARPU_LU(lu_decomposition)(A, lu_size, lu_size, lu_nblocks, sched_ctx, start);
  255. }
  256. // starpu_shutdown();
  257. return 0;
  258. }
  259. int finish_lu(struct timeval *end)
  260. {
  261. if (pivot)
  262. {
  263. if (no_stride)
  264. {
  265. finish_lu_decomposition_pivot_no_stride(lu_nblocks, end);
  266. }
  267. else
  268. {
  269. finish_lu_decomposition_pivot(lu_nblocks, end);
  270. }
  271. }
  272. else
  273. {
  274. finish_lu_decomposition(lu_nblocks, end);
  275. }
  276. if (profile)
  277. {
  278. starpu_profiling_status_set(STARPU_PROFILING_DISABLE);
  279. starpu_bus_profiling_helper_display_summary();
  280. }
  281. if (bound) {
  282. double min;
  283. starpu_bound_stop();
  284. if (bounddeps) {
  285. FILE *f = fopen("lu.pl", "w");
  286. starpu_bound_print_lp(f);
  287. fprintf(stderr,"system printed to lu.pl\n");
  288. } else {
  289. starpu_bound_compute(&min, NULL, 0);
  290. if (min != 0.)
  291. fprintf(stderr, "theoretical min: %lf ms\n", min);
  292. }
  293. }
  294. if (lu_check)
  295. {
  296. if (pivot)
  297. pivot_saved_matrix(ipiv);
  298. check_result();
  299. }
  300. starpu_helper_cublas_shutdown();
  301. return 0;
  302. }