lu_example.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (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 "xlu.h"
  23. #include "xlu_kernels.h"
  24. static unsigned long size = 16384;
  25. static unsigned nblocks = 16;
  26. static unsigned check = 0;
  27. static unsigned pivot = 0;
  28. static unsigned no_stride = 0;
  29. TYPE *A, *A_saved;
  30. /* in case we use non-strided blocks */
  31. TYPE **A_blocks;
  32. static void parse_args(int argc, char **argv)
  33. {
  34. int i;
  35. for (i = 1; i < argc; i++) {
  36. if (strcmp(argv[i], "-size") == 0) {
  37. char *argptr;
  38. size = strtol(argv[++i], &argptr, 10);
  39. }
  40. if (strcmp(argv[i], "-nblocks") == 0) {
  41. char *argptr;
  42. nblocks = strtol(argv[++i], &argptr, 10);
  43. }
  44. if (strcmp(argv[i], "-check") == 0) {
  45. check = 1;
  46. }
  47. if (strcmp(argv[i], "-piv") == 0) {
  48. pivot = 1;
  49. }
  50. if (strcmp(argv[i], "-no-stride") == 0) {
  51. no_stride = 1;
  52. }
  53. }
  54. }
  55. static void display_matrix(TYPE *m, unsigned n, unsigned ld, char *str)
  56. {
  57. #if 0
  58. fprintf(stderr, "***********\n");
  59. fprintf(stderr, "Display matrix %s\n", str);
  60. unsigned i,j;
  61. for (j = 0; j < n; j++)
  62. {
  63. for (i = 0; i < n; i++)
  64. {
  65. fprintf(stderr, "%2.2f\t", m[i+j*ld]);
  66. }
  67. fprintf(stderr, "\n");
  68. }
  69. fprintf(stderr, "***********\n");
  70. #endif
  71. }
  72. void copy_blocks_into_matrix(void)
  73. {
  74. unsigned blocksize = (size/nblocks);
  75. unsigned i, j;
  76. unsigned bi, bj;
  77. for (bj = 0; bj < nblocks; bj++)
  78. for (bi = 0; bi < nblocks; bi++)
  79. {
  80. for (j = 0; j < blocksize; j++)
  81. for (i = 0; i < blocksize; i++)
  82. {
  83. A[(i+bi*blocksize) + (j + bj*blocksize)*size] =
  84. A_blocks[bi+nblocks*bj][i + j * blocksize];
  85. }
  86. //free(A_blocks[bi+nblocks*bj]);
  87. }
  88. }
  89. void copy_matrix_into_blocks(void)
  90. {
  91. unsigned blocksize = (size/nblocks);
  92. unsigned i, j;
  93. unsigned bi, bj;
  94. for (bj = 0; bj < nblocks; bj++)
  95. for (bi = 0; bi < nblocks; bi++)
  96. {
  97. starpu_malloc_pinned_if_possible((void **)&A_blocks[bi+nblocks*bj], (size_t)blocksize*blocksize*sizeof(TYPE));
  98. for (j = 0; j < blocksize; j++)
  99. for (i = 0; i < blocksize; i++)
  100. {
  101. A_blocks[bi+nblocks*bj][i + j * blocksize] =
  102. A[(i+bi*blocksize) + (j + bj*blocksize)*size];
  103. }
  104. }
  105. }
  106. static void init_matrix(void)
  107. {
  108. /* allocate matrix */
  109. starpu_malloc_pinned_if_possible((void **)&A, (size_t)size*size*sizeof(TYPE));
  110. STARPU_ASSERT(A);
  111. starpu_srand48((long int)time(NULL));
  112. //starpu_srand48(0);
  113. /* initialize matrix content */
  114. unsigned long i,j;
  115. for (j = 0; j < size; j++)
  116. {
  117. for (i = 0; i < size; i++)
  118. {
  119. A[i + j*size] = (TYPE)starpu_drand48();
  120. }
  121. }
  122. }
  123. static void save_matrix(void)
  124. {
  125. A_saved = malloc((size_t)size*size*sizeof(TYPE));
  126. STARPU_ASSERT(A_saved);
  127. memcpy(A_saved, A, (size_t)size*size*sizeof(TYPE));
  128. }
  129. static double frobenius_norm(TYPE *v, unsigned n)
  130. {
  131. double sum2 = 0.0;
  132. /* compute sqrt(Sum(|x|^2)) */
  133. unsigned i,j;
  134. for (j = 0; j < n; j++)
  135. for (i = 0; i < n; i++)
  136. {
  137. double a = fabsl((double)v[i+n*j]);
  138. sum2 += a*a;
  139. }
  140. return sqrt(sum2);
  141. }
  142. static pivot_saved_matrix(unsigned *ipiv)
  143. {
  144. unsigned k;
  145. for (k = 0; k < size; k++)
  146. {
  147. if (k != ipiv[k])
  148. {
  149. // fprintf(stderr, "SWAP %d and %d\n", k, ipiv[k]);
  150. CPU_SWAP(size, &A_saved[k*size], 1, &A_saved[ipiv[k]*size], 1);
  151. }
  152. }
  153. }
  154. static void check_result(void)
  155. {
  156. unsigned i,j;
  157. TYPE *L, *U;
  158. L = malloc((size_t)size*size*sizeof(TYPE));
  159. U = malloc((size_t)size*size*sizeof(TYPE));
  160. memset(L, 0, size*size*sizeof(TYPE));
  161. memset(U, 0, size*size*sizeof(TYPE));
  162. /* only keep the lower part */
  163. for (j = 0; j < size; j++)
  164. {
  165. for (i = 0; i < j; i++)
  166. {
  167. L[j+i*size] = A[j+i*size];
  168. }
  169. /* diag i = j */
  170. L[j+j*size] = A[j+j*size];
  171. U[j+j*size] = 1.0;
  172. for (i = j+1; i < size; i++)
  173. {
  174. U[j+i*size] = A[j+i*size];
  175. }
  176. }
  177. display_matrix(L, size, size, "L");
  178. display_matrix(U, size, size, "U");
  179. /* now A_err = L, compute L*U */
  180. CPU_TRMM("R", "U", "N", "U", size, size, 1.0f, U, size, L, size);
  181. display_matrix(A_saved, size, size, "P A_saved");
  182. display_matrix(L, size, size, "LU");
  183. /* compute "LU - A" in L*/
  184. CPU_AXPY(size*size, -1.0, A_saved, 1, L, 1);
  185. display_matrix(L, size, size, "Residuals");
  186. TYPE err = CPU_ASUM(size*size, L, 1);
  187. int max = CPU_IAMAX(size*size, L, 1);
  188. fprintf(stderr, "Avg error : %e\n", err/(size*size));
  189. fprintf(stderr, "Max error : %e\n", L[max]);
  190. double residual = frobenius_norm(L, size);
  191. double matnorm = frobenius_norm(A_saved, size);
  192. fprintf(stderr, "||%sA-LU|| / (||A||*N) : %e\n", pivot?"P":"", residual/(matnorm*size));
  193. if (residual/(matnorm*size) > 1e-5)
  194. exit(-1);
  195. }
  196. int main(int argc, char **argv)
  197. {
  198. parse_args(argc, argv);
  199. starpu_init(NULL);
  200. starpu_helper_init_cublas();
  201. init_matrix();
  202. unsigned *ipiv;
  203. if (check)
  204. save_matrix();
  205. display_matrix(A, size, size, "A");
  206. /* Factorize the matrix (in place) */
  207. if (pivot)
  208. {
  209. ipiv = malloc(size*sizeof(unsigned));
  210. if (no_stride)
  211. {
  212. /* in case the LU decomposition uses non-strided blocks, we _copy_ the matrix into smaller blocks */
  213. A_blocks = malloc(nblocks*nblocks*sizeof(TYPE **));
  214. copy_matrix_into_blocks();
  215. STARPU_LU(lu_decomposition_pivot_no_stride)(A_blocks, ipiv, size, size, nblocks);
  216. copy_blocks_into_matrix();
  217. free(A_blocks);
  218. }
  219. else
  220. {
  221. struct timeval start;
  222. struct timeval end;
  223. gettimeofday(&start, NULL);
  224. STARPU_LU(lu_decomposition_pivot)(A, ipiv, size, size, nblocks);
  225. gettimeofday(&end, NULL);
  226. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  227. unsigned n = size;
  228. double flop = (2.0f*n*n*n)/3.0f;
  229. fprintf(stderr, "Synthetic GFlops (TOTAL) : \n");
  230. fprintf(stdout, "%d %6.2f\n", n, (flop/timing/1000.0f));
  231. }
  232. }
  233. else
  234. {
  235. STARPU_LU(lu_decomposition)(A, size, size, nblocks);
  236. }
  237. if (check)
  238. {
  239. if (pivot)
  240. pivot_saved_matrix(ipiv);
  241. check_result();
  242. }
  243. starpu_helper_shutdown_cublas();
  244. starpu_shutdown();
  245. return 0;
  246. }