lu_example.c 7.1 KB

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