lu_example.c 8.7 KB

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