lu_example.c 8.0 KB

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