lu_example.c 7.4 KB

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