lu_example.c 9.5 KB

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