lu_example.c 9.4 KB

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