lu_example.c 8.8 KB

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