lu_example.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2012 Inria
  4. * Copyright (C) 2009-2017 Université de Bordeaux
  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. starpu_malloc_flags((void **)&A, (size_t)size*size*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  157. STARPU_ASSERT(A);
  158. starpu_srand48((long int)time(NULL));
  159. /* starpu_srand48(0); */
  160. #ifndef STARPU_SIMGRID
  161. /* initialize matrix content */
  162. unsigned long i,j;
  163. for (j = 0; j < size; j++)
  164. {
  165. for (i = 0; i < size; i++)
  166. {
  167. A[i + j*size] = (TYPE)starpu_drand48();
  168. #ifdef COMPLEX_LU
  169. /* also randomize the imaginary component for complex number cases */
  170. A[i + j*size] += (TYPE)(I*starpu_drand48());
  171. #endif
  172. if (i == j)
  173. {
  174. A[i + j*size] += 1;
  175. A[i + j*size] *= 100;
  176. }
  177. }
  178. }
  179. #endif
  180. }
  181. static void save_matrix(void)
  182. {
  183. A_saved = malloc((size_t)size*size*sizeof(TYPE));
  184. STARPU_ASSERT(A_saved);
  185. memcpy(A_saved, A, (size_t)size*size*sizeof(TYPE));
  186. }
  187. static double frobenius_norm(TYPE *v, unsigned n)
  188. {
  189. double sum2 = 0.0;
  190. /* compute sqrt(Sum(|x|^2)) */
  191. unsigned i,j;
  192. for (j = 0; j < n; j++)
  193. for (i = 0; i < n; i++)
  194. {
  195. double a = fabsl((double)v[i+n*j]);
  196. sum2 += a*a;
  197. }
  198. return sqrt(sum2);
  199. }
  200. static void pivot_saved_matrix(unsigned *ipiv)
  201. {
  202. unsigned k;
  203. for (k = 0; k < size; k++)
  204. {
  205. if (k != ipiv[k])
  206. {
  207. /* FPRINTF(stderr, "SWAP %d and %d\n", k, ipiv[k]); */
  208. CPU_SWAP(size, &A_saved[k*size], 1, &A_saved[ipiv[k]*size], 1);
  209. }
  210. }
  211. }
  212. static void check_result(void)
  213. {
  214. unsigned i,j;
  215. TYPE *L, *U;
  216. L = malloc((size_t)size*size*sizeof(TYPE));
  217. U = malloc((size_t)size*size*sizeof(TYPE));
  218. memset(L, 0, size*size*sizeof(TYPE));
  219. memset(U, 0, size*size*sizeof(TYPE));
  220. /* only keep the lower part */
  221. for (j = 0; j < size; j++)
  222. {
  223. for (i = 0; i < j; i++)
  224. {
  225. L[j+i*size] = A[j+i*size];
  226. }
  227. /* diag i = j */
  228. L[j+j*size] = A[j+j*size];
  229. U[j+j*size] = 1.0;
  230. for (i = j+1; i < size; i++)
  231. {
  232. U[j+i*size] = A[j+i*size];
  233. }
  234. }
  235. display_matrix(L, size, size, "L");
  236. display_matrix(U, size, size, "U");
  237. /* now A_err = L, compute L*U */
  238. CPU_TRMM("R", "U", "N", "U", size, size, 1.0f, U, size, L, size);
  239. display_matrix(A_saved, size, size, "P A_saved");
  240. display_matrix(L, size, size, "LU");
  241. /* compute "LU - A" in L*/
  242. CPU_AXPY(size*size, -1.0, A_saved, 1, L, 1);
  243. display_matrix(L, size, size, "Residuals");
  244. #ifdef COMPLEX_LU
  245. double err = CPU_ASUM(size*size, L, 1);
  246. int max = CPU_IAMAX(size*size, L, 1);
  247. TYPE l_max = L[max];
  248. FPRINTF(stderr, "Avg error : %e\n", err/(size*size));
  249. FPRINTF(stderr, "Max error : %e\n", sqrt(creal(l_max)*creal(l_max)+cimag(l_max)*cimag(l_max)));
  250. #else
  251. TYPE err = CPU_ASUM(size*size, L, 1);
  252. int max = CPU_IAMAX(size*size, L, 1);
  253. FPRINTF(stderr, "Avg error : %e\n", err/(size*size));
  254. FPRINTF(stderr, "Max error : %e\n", L[max]);
  255. #endif
  256. double residual = frobenius_norm(L, size);
  257. double matnorm = frobenius_norm(A_saved, size);
  258. FPRINTF(stderr, "||%sA-LU|| / (||A||*N) : %e\n", pivot?"P":"", residual/(matnorm*size));
  259. if (residual/(matnorm*size) > 1e-5)
  260. exit(-1);
  261. free(L);
  262. free(U);
  263. free(A_saved);
  264. }
  265. int main(int argc, char **argv)
  266. {
  267. int ret;
  268. ret = starpu_init(NULL);
  269. if (ret == -ENODEV)
  270. return 77;
  271. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  272. int power = starpu_cpu_worker_get_count() + 32 * starpu_cuda_worker_get_count();
  273. int power_cbrt = cbrt(power);
  274. #ifndef STARPU_LONG_CHECK
  275. power_cbrt /= 2;
  276. #endif
  277. if (power_cbrt < 1)
  278. power_cbrt = 1;
  279. #ifdef STARPU_QUICK_CHECK
  280. if (!size)
  281. size = 320*2*power_cbrt;
  282. if (!nblocks)
  283. nblocks = 2*power_cbrt;
  284. #else
  285. if (!size)
  286. size = 960*8*power_cbrt;
  287. if (!nblocks)
  288. nblocks = 8*power_cbrt;
  289. #endif
  290. parse_args(argc, argv);
  291. starpu_cublas_init();
  292. init_matrix();
  293. #ifndef STARPU_SIMGRID
  294. unsigned *ipiv = NULL;
  295. if (check)
  296. save_matrix();
  297. display_matrix(A, size, size, "A");
  298. if (profile)
  299. starpu_profiling_status_set(STARPU_PROFILING_ENABLE);
  300. /* Factorize the matrix (in place) */
  301. if (pivot)
  302. {
  303. ipiv = malloc(size*sizeof(unsigned));
  304. if (no_stride)
  305. {
  306. /* in case the LU decomposition uses non-strided blocks, we _copy_ the matrix into smaller blocks */
  307. A_blocks = malloc(nblocks*nblocks*sizeof(TYPE *));
  308. copy_matrix_into_blocks();
  309. ret = STARPU_LU(lu_decomposition_pivot_no_stride)(A_blocks, ipiv, size, size, nblocks, no_prio);
  310. copy_blocks_into_matrix();
  311. free(A_blocks);
  312. }
  313. else
  314. {
  315. double start;
  316. double end;
  317. start = starpu_timing_now();
  318. ret = STARPU_LU(lu_decomposition_pivot)(A, ipiv, size, size, nblocks, no_prio);
  319. end = starpu_timing_now();
  320. double timing = end - start;
  321. unsigned n = size;
  322. double flop = (2.0f*n*n*n)/3.0f;
  323. FPRINTF(stderr, "Synthetic GFlops (TOTAL) : \n");
  324. FPRINTF(stdout, "%u %6.2f\n", n, (flop/timing/1000.0f));
  325. }
  326. }
  327. else
  328. #endif
  329. {
  330. ret = STARPU_LU(lu_decomposition)(A, size, size, nblocks, no_prio);
  331. }
  332. if (profile)
  333. {
  334. FPRINTF(stderr, "Setting profile\n");
  335. starpu_profiling_status_set(STARPU_PROFILING_DISABLE);
  336. starpu_profiling_bus_helper_display_summary();
  337. }
  338. if (bound)
  339. {
  340. if (bounddeps)
  341. {
  342. FILE *f = fopen("lu.pl", "w");
  343. starpu_bound_print_lp(f);
  344. FPRINTF(stderr,"system printed to lu.pl\n");
  345. fclose(f);
  346. f = fopen("lu.mps", "w");
  347. starpu_bound_print_mps(f);
  348. FPRINTF(stderr,"system printed to lu.mps\n");
  349. fclose(f);
  350. f = fopen("lu.dot", "w");
  351. starpu_bound_print_dot(f);
  352. FPRINTF(stderr,"system printed to lu.mps\n");
  353. fclose(f);
  354. }
  355. }
  356. #ifndef STARPU_SIMGRID
  357. if (check)
  358. {
  359. FPRINTF(stderr, "Checking result\n");
  360. if (pivot)
  361. {
  362. pivot_saved_matrix(ipiv);
  363. }
  364. check_result();
  365. }
  366. #endif
  367. if (pivot)
  368. free(ipiv);
  369. starpu_free_flags(A, (size_t)size*size*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  370. starpu_cublas_shutdown();
  371. starpu_shutdown();
  372. if (ret == -ENODEV) return 77; else return 0;
  373. }