lu_example.c 7.9 KB

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