lu_example.c 8.6 KB

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