lu_example.c 9.2 KB

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