lu_example.c 10 KB

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