plu_solve.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 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 <starpu.h>
  17. #include "pxlu.h"
  18. void STARPU_PLU(display_data_content)(TYPE *data, unsigned blocksize)
  19. {
  20. fprintf(stderr, "DISPLAY BLOCK\n");
  21. unsigned i, j;
  22. for (j = 0; j < blocksize; j++)
  23. {
  24. for (i = 0; i < blocksize; i++)
  25. {
  26. fprintf(stderr, "%f ", data[j+i*blocksize]);
  27. }
  28. fprintf(stderr, "\n");
  29. }
  30. fprintf(stderr, "****\n");
  31. }
  32. static STARPU_PLU(compute_ax_block)(unsigned block_size, TYPE *block_data, TYPE *sub_x, TYPE *sub_y)
  33. {
  34. CPU_GEMV("N", block_size, block_size, 1.0, block_data, block_size, sub_x, 1, 1.0, sub_y, 1);
  35. }
  36. void STARPU_PLU(extract_upper)(unsigned block_size, TYPE *inblock, TYPE *outblock)
  37. {
  38. unsigned li, lj;
  39. for (lj = 0; lj < block_size; lj++)
  40. {
  41. /* Upper block diag is 1 */
  42. outblock[lj*(block_size + 1)] = (TYPE)1.0;
  43. for (li = lj + 1; li < block_size; li++)
  44. {
  45. outblock[lj + li*block_size] = inblock[lj + li*block_size];
  46. }
  47. }
  48. }
  49. void STARPU_PLU(extract_lower)(unsigned block_size, TYPE *inblock, TYPE *outblock)
  50. {
  51. unsigned li, lj;
  52. for (lj = 0; lj < block_size; lj++)
  53. {
  54. for (li = 0; li <= lj; li++)
  55. {
  56. outblock[lj + li*block_size] = inblock[lj + li*block_size];
  57. }
  58. }
  59. }
  60. #if 0
  61. void STARPU_PLU(extract_upper)(unsigned block_size, TYPE *inblock, TYPE *outblock)
  62. {
  63. unsigned li, lj;
  64. for (lj = 0; lj < block_size; lj++)
  65. {
  66. for (li = lj; li < block_size; li++)
  67. {
  68. outblock[lj + li*block_size] = inblock[lj + li*block_size];
  69. }
  70. }
  71. }
  72. void STARPU_PLU(extract_lower)(unsigned block_size, TYPE *inblock, TYPE *outblock)
  73. {
  74. unsigned li, lj;
  75. for (lj = 0; lj < block_size; lj++)
  76. {
  77. for (li = 0; li < lj; li++)
  78. {
  79. outblock[lj + li*block_size] = inblock[lj + li*block_size];
  80. }
  81. outblock[lj*(block_size + 1)] = (TYPE)1.0;
  82. }
  83. }
  84. #endif
  85. static STARPU_PLU(compute_ax_block_upper)(unsigned size, unsigned nblocks,
  86. TYPE *block_data, TYPE *sub_x, TYPE *sub_y)
  87. {
  88. unsigned block_size = size/nblocks;
  89. // fprintf(stderr, "KEEP UPPER\n");
  90. // STARPU_PLU(display_data_content)(block_data, block_size);
  91. /* Take a copy of the upper part of the diagonal block */
  92. TYPE *upper_block_copy = calloc((block_size)*(block_size), sizeof(TYPE));
  93. STARPU_PLU(extract_upper)(block_size, block_data, upper_block_copy);
  94. // STARPU_PLU(display_data_content)(upper_block_copy, block_size);
  95. STARPU_PLU(compute_ax_block)(size/nblocks, upper_block_copy, sub_x, sub_y);
  96. free(upper_block_copy);
  97. }
  98. TYPE *STARPU_PLU(reconstruct_matrix)(unsigned size, unsigned nblocks)
  99. {
  100. TYPE *bigmatrix = calloc(size*size, sizeof(TYPE));
  101. unsigned block_size = size/nblocks;
  102. unsigned bi, bj;
  103. for (bj = 0; bj < nblocks; bj++)
  104. for (bi = 0; bi < nblocks; bi++)
  105. {
  106. TYPE *block = STARPU_PLU(get_block)(bj, bi);
  107. //TYPE *block = STARPU_PLU(get_block)(bj, bi);
  108. unsigned j, i;
  109. for (j = 0; j < block_size; j++)
  110. for (i = 0; i < block_size; i++)
  111. {
  112. bigmatrix[(j + bj*block_size)+(i+bi*block_size)*size] =
  113. block[j+i*block_size];
  114. }
  115. }
  116. return bigmatrix;
  117. }
  118. static TYPE *reconstruct_lower(unsigned size, unsigned nblocks)
  119. {
  120. TYPE *lower = calloc(size*size, sizeof(TYPE));
  121. TYPE *bigmatrix = STARPU_PLU(reconstruct_matrix)(size, nblocks);
  122. STARPU_PLU(extract_lower)(size, bigmatrix, lower);
  123. return lower;
  124. }
  125. static TYPE *reconstruct_upper(unsigned size, unsigned nblocks)
  126. {
  127. TYPE *upper = calloc(size*size, sizeof(TYPE));
  128. TYPE *bigmatrix = STARPU_PLU(reconstruct_matrix)(size, nblocks);
  129. STARPU_PLU(extract_upper)(size, bigmatrix, upper);
  130. return upper;
  131. }
  132. void STARPU_PLU(compute_lu_matrix)(unsigned size, unsigned nblocks, TYPE *Asaved)
  133. {
  134. // fprintf(stderr, "ALL\n\n");
  135. TYPE *all_r = STARPU_PLU(reconstruct_matrix)(size, nblocks);
  136. // STARPU_PLU(display_data_content)(all_r, size);
  137. TYPE *L = malloc((size_t)size*size*sizeof(TYPE));
  138. TYPE *U = malloc((size_t)size*size*sizeof(TYPE));
  139. memset(L, 0, size*size*sizeof(TYPE));
  140. memset(U, 0, size*size*sizeof(TYPE));
  141. /* only keep the lower part */
  142. unsigned i, j;
  143. for (j = 0; j < size; j++)
  144. {
  145. for (i = 0; i < j; i++)
  146. {
  147. L[j+i*size] = all_r[j+i*size];
  148. }
  149. /* diag i = j */
  150. L[j+j*size] = all_r[j+j*size];
  151. U[j+j*size] = 1.0;
  152. for (i = j+1; i < size; i++)
  153. {
  154. U[j+i*size] = all_r[j+i*size];
  155. }
  156. }
  157. // STARPU_PLU(display_data_content)(L, size);
  158. // STARPU_PLU(display_data_content)(U, size);
  159. /* now A_err = L, compute L*U */
  160. CPU_TRMM("R", "U", "N", "U", size, size, 1.0f, U, size, L, size);
  161. // fprintf(stderr, "\nLU\n");
  162. // STARPU_PLU(display_data_content)(L, size);
  163. /* compute "LU - A" in L*/
  164. CPU_AXPY(size*size, -1.0, Asaved, 1, L, 1);
  165. TYPE err = CPU_ASUM(size*size, L, 1);
  166. int max = CPU_IAMAX(size*size, L, 1);
  167. // STARPU_PLU(display_data_content)(L, size);
  168. fprintf(stderr, "(A - LU) Avg error : %e\n", err/(size*size));
  169. fprintf(stderr, "(A - LU) Max error : %e\n", L[max]);
  170. }
  171. static STARPU_PLU(compute_ax_block_lower)(unsigned size, unsigned nblocks,
  172. TYPE *block_data, TYPE *sub_x, TYPE *sub_y)
  173. {
  174. unsigned block_size = size/nblocks;
  175. // fprintf(stderr, "KEEP LOWER\n");
  176. // STARPU_PLU(display_data_content)(block_data, block_size);
  177. /* Take a copy of the upper part of the diagonal block */
  178. TYPE *lower_block_copy = calloc((block_size)*(block_size), sizeof(TYPE));
  179. STARPU_PLU(extract_lower)(block_size, block_data, lower_block_copy);
  180. // STARPU_PLU(display_data_content)(lower_block_copy, block_size);
  181. STARPU_PLU(compute_ax_block)(size/nblocks, lower_block_copy, sub_x, sub_y);
  182. free(lower_block_copy);
  183. }
  184. void STARPU_PLU(compute_lux)(unsigned size, TYPE *x, TYPE *y, unsigned nblocks, int rank)
  185. {
  186. /* Create temporary buffers where all MPI processes are going to
  187. * compute Ui x = yi where Ai is the matrix containing the blocks of U
  188. * affected to process i, and 0 everywhere else. We then have y as the
  189. * sum of all yi. */
  190. TYPE *yi = calloc(size, sizeof(TYPE));
  191. unsigned block_size = size/nblocks;
  192. /* Compute UiX = Yi */
  193. unsigned long i,j;
  194. for (j = 0; j < nblocks; j++)
  195. {
  196. if (get_block_rank(j, j) == rank)
  197. {
  198. TYPE *block_data = STARPU_PLU(get_block)(j, j);
  199. TYPE *sub_x = &x[j*(block_size)];
  200. TYPE *sub_yi = &yi[j*(block_size)];
  201. STARPU_PLU(compute_ax_block_upper)(size, nblocks, block_data, sub_x, sub_yi);
  202. }
  203. for (i = j + 1; i < nblocks; i++)
  204. {
  205. if (get_block_rank(i, j) == rank)
  206. {
  207. /* That block belongs to the current MPI process */
  208. TYPE *block_data = STARPU_PLU(get_block)(j, i);
  209. TYPE *sub_x = &x[i*(block_size)];
  210. TYPE *sub_yi = &yi[j*(block_size)];
  211. STARPU_PLU(compute_ax_block)(size/nblocks, block_data, sub_x, sub_yi);
  212. }
  213. }
  214. }
  215. /* Grab Sum Yi in X */
  216. MPI_Reduce(yi, x, size, MPI_TYPE, MPI_SUM, 0, MPI_COMM_WORLD);
  217. memset(yi, 0, size*sizeof(TYPE));
  218. unsigned ind;
  219. // if (rank == 0)
  220. // {
  221. // fprintf(stderr, "INTERMEDIATE\n");
  222. // for (ind = 0; ind < STARPU_MIN(10, size); ind++)
  223. // {
  224. // fprintf(stderr, "x[%d] = %f\n", ind, (float)x[ind]);
  225. // }
  226. // fprintf(stderr, "****\n");
  227. // }
  228. /* Everyone needs x */
  229. int bcst_ret;
  230. bcst_ret = MPI_Bcast(&x, size, MPI_TYPE, 0, MPI_COMM_WORLD);
  231. STARPU_ASSERT(bcst_ret == MPI_SUCCESS);
  232. /* Compute LiX = Yi (with X = UX) */
  233. for (j = 0; j < nblocks; j++)
  234. {
  235. if (j > 0)
  236. for (i = 0; i < j; i++)
  237. {
  238. if (get_block_rank(i, j) == rank)
  239. {
  240. /* That block belongs to the current MPI process */
  241. TYPE *block_data = STARPU_PLU(get_block)(j, i);
  242. TYPE *sub_x = &x[i*(block_size)];
  243. TYPE *sub_yi = &yi[j*(block_size)];
  244. STARPU_PLU(compute_ax_block)(size/nblocks, block_data, sub_x, sub_yi);
  245. }
  246. }
  247. if (get_block_rank(j, j) == rank)
  248. {
  249. TYPE *block_data = STARPU_PLU(get_block)(j, j);
  250. TYPE *sub_x = &x[j*(block_size)];
  251. TYPE *sub_yi = &yi[j*(block_size)];
  252. STARPU_PLU(compute_ax_block_lower)(size, nblocks, block_data, sub_x, sub_yi);
  253. }
  254. }
  255. /* Grab Sum Yi in Y */
  256. MPI_Reduce(yi, y, size, MPI_TYPE, MPI_SUM, 0, MPI_COMM_WORLD);
  257. free(yi);
  258. }
  259. /* x and y must be valid (at least) on 0 */
  260. void STARPU_PLU(compute_ax)(unsigned size, TYPE *x, TYPE *y, unsigned nblocks, int rank)
  261. {
  262. /* Send x to everyone */
  263. int bcst_ret;
  264. bcst_ret = MPI_Bcast(&x, size, MPI_TYPE, 0, MPI_COMM_WORLD);
  265. STARPU_ASSERT(bcst_ret == MPI_SUCCESS);
  266. /* Create temporary buffers where all MPI processes are going to
  267. * compute Ai x = yi where Ai is the matrix containing the blocks of A
  268. * affected to process i, and 0 everywhere else. We then have y as the
  269. * sum of all yi. */
  270. TYPE *yi = calloc(size, sizeof(TYPE));
  271. /* Compute Aix = yi */
  272. unsigned long i,j;
  273. for (j = 0; j < nblocks; j++)
  274. {
  275. for (i = 0; i < nblocks; i++)
  276. {
  277. if (get_block_rank(i, j) == rank)
  278. {
  279. /* That block belongs to the current MPI process */
  280. TYPE *block_data = STARPU_PLU(get_block)(j, i);
  281. TYPE *sub_x = &x[i*(size/nblocks)];
  282. TYPE *sub_yi = &yi[j*(size/nblocks)];
  283. STARPU_PLU(compute_ax_block)(size/nblocks, block_data, sub_x, sub_yi);
  284. }
  285. }
  286. }
  287. /* Compute the Sum of all yi = y */
  288. MPI_Reduce(yi, y, size, MPI_TYPE, MPI_SUM, 0, MPI_COMM_WORLD);
  289. free(yi);
  290. }