lu_example.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2015 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2015 CNRS
  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. /* Main body for the LU factorization: matrix initialization and result
  18. * checking */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <time.h>
  23. #include <math.h>
  24. #include <starpu.h>
  25. #include "xlu.h"
  26. #include "xlu_kernels.h"
  27. #ifdef STARPU_QUICK_CHECK
  28. static unsigned long size = 320*4;
  29. static unsigned nblocks = 4;
  30. #else
  31. static unsigned long size = 960*16;
  32. static unsigned nblocks = 16;
  33. #endif
  34. static unsigned check = 0;
  35. static unsigned pivot = 0;
  36. static unsigned no_stride = 0;
  37. static unsigned profile = 0;
  38. unsigned bound = 0;
  39. unsigned bounddeps = 0;
  40. unsigned boundprio = 0;
  41. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  42. TYPE *A, *A_saved;
  43. /* in case we use non-strided blocks */
  44. TYPE **A_blocks;
  45. static void parse_args(int argc, char **argv)
  46. {
  47. int i;
  48. for (i = 1; i < argc; i++)
  49. {
  50. if (strcmp(argv[i], "-size") == 0)
  51. {
  52. char *argptr;
  53. size = strtol(argv[++i], &argptr, 10);
  54. }
  55. else if (strcmp(argv[i], "-nblocks") == 0)
  56. {
  57. char *argptr;
  58. nblocks = strtol(argv[++i], &argptr, 10);
  59. }
  60. #ifndef STARPU_SIMGRID
  61. else if (strcmp(argv[i], "-check") == 0)
  62. {
  63. check = 1;
  64. }
  65. else if (strcmp(argv[i], "-piv") == 0)
  66. {
  67. pivot = 1;
  68. }
  69. else if (strcmp(argv[i], "-no-stride") == 0)
  70. {
  71. no_stride = 1;
  72. }
  73. #endif
  74. else if (strcmp(argv[i], "-profile") == 0)
  75. {
  76. profile = 1;
  77. }
  78. else if (strcmp(argv[i], "-bound") == 0)
  79. {
  80. bound = 1;
  81. }
  82. else if (strcmp(argv[i], "-bounddeps") == 0)
  83. {
  84. bound = 1;
  85. bounddeps = 1;
  86. }
  87. else if (strcmp(argv[i], "-bounddepsprio") == 0)
  88. {
  89. bound = 1;
  90. bounddeps = 1;
  91. boundprio = 1;
  92. }
  93. else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
  94. {
  95. fprintf(stderr,"usage: lu [-size n] [-nblocks b] [-piv] [-no-stride] [-profile] [-bound] [-bounddeps] [-bounddepsprio]\n");
  96. fprintf(stderr,"Default is size %lu and nblocks %u\n", size, nblocks);
  97. exit(0);
  98. }
  99. }
  100. }
  101. static void display_matrix(TYPE *m, unsigned n, unsigned ld, char *str)
  102. {
  103. #if 0
  104. FPRINTF(stderr, "***********\n");
  105. FPRINTF(stderr, "Display matrix %s\n", str);
  106. unsigned i,j;
  107. for (j = 0; j < n; j++)
  108. {
  109. for (i = 0; i < n; i++)
  110. {
  111. FPRINTF(stderr, "%2.2f\t", m[i+j*ld]);
  112. }
  113. FPRINTF(stderr, "\n");
  114. }
  115. FPRINTF(stderr, "***********\n");
  116. #endif
  117. }
  118. void copy_blocks_into_matrix(void)
  119. {
  120. unsigned blocksize = (size/nblocks);
  121. unsigned i, j;
  122. unsigned bi, bj;
  123. for (bj = 0; bj < nblocks; bj++)
  124. for (bi = 0; bi < nblocks; bi++)
  125. {
  126. for (j = 0; j < blocksize; j++)
  127. for (i = 0; i < blocksize; i++)
  128. {
  129. A[(i+bi*blocksize) + (j + bj*blocksize)*size] =
  130. A_blocks[bi+nblocks*bj][i + j * blocksize];
  131. }
  132. starpu_free(A_blocks[bi+nblocks*bj]);
  133. }
  134. }
  135. void copy_matrix_into_blocks(void)
  136. {
  137. unsigned blocksize = (size/nblocks);
  138. unsigned i, j;
  139. unsigned bi, bj;
  140. for (bj = 0; bj < nblocks; bj++)
  141. for (bi = 0; bi < nblocks; bi++)
  142. {
  143. starpu_malloc((void **)&A_blocks[bi+nblocks*bj], (size_t)blocksize*blocksize*sizeof(TYPE));
  144. for (j = 0; j < blocksize; j++)
  145. for (i = 0; i < blocksize; i++)
  146. {
  147. A_blocks[bi+nblocks*bj][i + j * blocksize] =
  148. A[(i+bi*blocksize) + (j + bj*blocksize)*size];
  149. }
  150. }
  151. }
  152. static void init_matrix(void)
  153. {
  154. /* allocate matrix */
  155. starpu_malloc((void **)&A, (size_t)size*size*sizeof(TYPE));
  156. STARPU_ASSERT(A);
  157. starpu_srand48((long int)time(NULL));
  158. /* starpu_srand48(0); */
  159. /* initialize matrix content */
  160. unsigned long i,j;
  161. for (j = 0; j < size; j++)
  162. {
  163. for (i = 0; i < size; i++)
  164. {
  165. A[i + j*size] = (TYPE)starpu_drand48();
  166. #ifdef COMPLEX_LU
  167. /* also randomize the imaginary component for complex number cases */
  168. A[i + j*size] += (TYPE)(I*starpu_drand48());
  169. #endif
  170. if (i == j)
  171. A[i + j*size] *= 100;
  172. }
  173. }
  174. }
  175. static void save_matrix(void)
  176. {
  177. A_saved = malloc((size_t)size*size*sizeof(TYPE));
  178. STARPU_ASSERT(A_saved);
  179. memcpy(A_saved, A, (size_t)size*size*sizeof(TYPE));
  180. }
  181. static double frobenius_norm(TYPE *v, unsigned n)
  182. {
  183. double sum2 = 0.0;
  184. /* compute sqrt(Sum(|x|^2)) */
  185. unsigned i,j;
  186. for (j = 0; j < n; j++)
  187. for (i = 0; i < n; i++)
  188. {
  189. double a = fabsl((double)v[i+n*j]);
  190. sum2 += a*a;
  191. }
  192. return sqrt(sum2);
  193. }
  194. static void pivot_saved_matrix(unsigned *ipiv)
  195. {
  196. unsigned k;
  197. for (k = 0; k < size; k++)
  198. {
  199. if (k != ipiv[k])
  200. {
  201. /* FPRINTF(stderr, "SWAP %d and %d\n", k, ipiv[k]); */
  202. CPU_SWAP(size, &A_saved[k*size], 1, &A_saved[ipiv[k]*size], 1);
  203. }
  204. }
  205. }
  206. static void check_result(void)
  207. {
  208. unsigned i,j;
  209. TYPE *L, *U;
  210. L = malloc((size_t)size*size*sizeof(TYPE));
  211. U = malloc((size_t)size*size*sizeof(TYPE));
  212. memset(L, 0, size*size*sizeof(TYPE));
  213. memset(U, 0, size*size*sizeof(TYPE));
  214. /* only keep the lower part */
  215. for (j = 0; j < size; j++)
  216. {
  217. for (i = 0; i < j; i++)
  218. {
  219. L[j+i*size] = A[j+i*size];
  220. }
  221. /* diag i = j */
  222. L[j+j*size] = A[j+j*size];
  223. U[j+j*size] = 1.0;
  224. for (i = j+1; i < size; i++)
  225. {
  226. U[j+i*size] = A[j+i*size];
  227. }
  228. }
  229. display_matrix(L, size, size, "L");
  230. display_matrix(U, size, size, "U");
  231. /* now A_err = L, compute L*U */
  232. CPU_TRMM("R", "U", "N", "U", size, size, 1.0f, U, size, L, size);
  233. display_matrix(A_saved, size, size, "P A_saved");
  234. display_matrix(L, size, size, "LU");
  235. /* compute "LU - A" in L*/
  236. CPU_AXPY(size*size, -1.0, A_saved, 1, L, 1);
  237. display_matrix(L, size, size, "Residuals");
  238. #ifdef COMPLEX_LU
  239. double err = CPU_ASUM(size*size, L, 1);
  240. int max = CPU_IAMAX(size*size, L, 1);
  241. TYPE l_max = L[max];
  242. FPRINTF(stderr, "Avg error : %e\n", err/(size*size));
  243. FPRINTF(stderr, "Max error : %e\n", sqrt(creal(l_max)*creal(l_max)+cimag(l_max)*cimag(l_max)));
  244. #else
  245. TYPE err = CPU_ASUM(size*size, L, 1);
  246. int max = CPU_IAMAX(size*size, L, 1);
  247. FPRINTF(stderr, "Avg error : %e\n", err/(size*size));
  248. FPRINTF(stderr, "Max error : %e\n", L[max]);
  249. #endif
  250. double residual = frobenius_norm(L, size);
  251. double matnorm = frobenius_norm(A_saved, size);
  252. FPRINTF(stderr, "||%sA-LU|| / (||A||*N) : %e\n", pivot?"P":"", residual/(matnorm*size));
  253. if (residual/(matnorm*size) > 1e-5)
  254. exit(-1);
  255. free(L);
  256. free(U);
  257. free(A_saved);
  258. }
  259. int main(int argc, char **argv)
  260. {
  261. int ret;
  262. #ifdef STARPU_QUICK_CHECK
  263. size /= 4;
  264. nblocks /= 4;
  265. #endif
  266. parse_args(argc, argv);
  267. ret = starpu_init(NULL);
  268. if (ret == -ENODEV)
  269. return 77;
  270. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  271. starpu_cublas_init();
  272. #ifndef STARPU_SIMGRID
  273. init_matrix();
  274. unsigned *ipiv = NULL;
  275. if (check)
  276. save_matrix();
  277. display_matrix(A, size, size, "A");
  278. if (profile)
  279. starpu_profiling_status_set(STARPU_PROFILING_ENABLE);
  280. /* Factorize the matrix (in place) */
  281. if (pivot)
  282. {
  283. ipiv = malloc(size*sizeof(unsigned));
  284. if (no_stride)
  285. {
  286. /* in case the LU decomposition uses non-strided blocks, we _copy_ the matrix into smaller blocks */
  287. A_blocks = malloc(nblocks*nblocks*sizeof(TYPE *));
  288. copy_matrix_into_blocks();
  289. ret = STARPU_LU(lu_decomposition_pivot_no_stride)(A_blocks, ipiv, size, size, nblocks);
  290. copy_blocks_into_matrix();
  291. free(A_blocks);
  292. }
  293. else
  294. {
  295. double start;
  296. double end;
  297. start = starpu_timing_now();
  298. ret = STARPU_LU(lu_decomposition_pivot)(A, ipiv, size, size, nblocks);
  299. end = starpu_timing_now();
  300. double timing = end - start;
  301. unsigned n = size;
  302. double flop = (2.0f*n*n*n)/3.0f;
  303. FPRINTF(stderr, "Synthetic GFlops (TOTAL) : \n");
  304. FPRINTF(stdout, "%u %6.2f\n", n, (flop/timing/1000.0f));
  305. }
  306. }
  307. else
  308. #endif
  309. {
  310. ret = STARPU_LU(lu_decomposition)(A, size, size, nblocks);
  311. }
  312. if (profile)
  313. {
  314. FPRINTF(stderr, "Setting profile\n");
  315. starpu_profiling_status_set(STARPU_PROFILING_DISABLE);
  316. starpu_profiling_bus_helper_display_summary();
  317. }
  318. if (bound)
  319. {
  320. double min;
  321. if (bounddeps)
  322. {
  323. FILE *f = fopen("lu.pl", "w");
  324. starpu_bound_print_lp(f);
  325. FPRINTF(stderr,"system printed to lu.pl\n");
  326. fclose(f);
  327. f = fopen("lu.mps", "w");
  328. starpu_bound_print_mps(f);
  329. FPRINTF(stderr,"system printed to lu.mps\n");
  330. fclose(f);
  331. f = fopen("lu.dot", "w");
  332. starpu_bound_print_dot(f);
  333. FPRINTF(stderr,"system printed to lu.mps\n");
  334. fclose(f);
  335. }
  336. }
  337. #ifndef STARPU_SIMGRID
  338. if (check)
  339. {
  340. FPRINTF(stderr, "Checking result\n");
  341. if (pivot)
  342. {
  343. pivot_saved_matrix(ipiv);
  344. free(ipiv);
  345. }
  346. check_result();
  347. }
  348. #endif
  349. starpu_free(A);
  350. starpu_cublas_shutdown();
  351. starpu_shutdown();
  352. if (ret == -ENODEV) return 77; else return 0;
  353. }