lu_example.c 6.9 KB

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