lu_example.c 7.3 KB

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