mpi_cholesky.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu_mpi.h>
  19. #include "mpi_cholesky.h"
  20. #include "mpi_cholesky_models.h"
  21. #include "mpi_cholesky_codelets.h"
  22. /* Returns the MPI node number where data indexes index is */
  23. int my_distrib(int x, int y, int nb_nodes)
  24. {
  25. //return (x+y) % nb_nodes;
  26. return (x%dblockx)+(y%dblocky)*dblockx;
  27. }
  28. int main(int argc, char **argv)
  29. {
  30. /* create a simple definite positive symetric matrix example
  31. *
  32. * Hilbert matrix : h(i,j) = 1/(i+j+1)
  33. * */
  34. float ***bmat;
  35. int rank, nodes, ret;
  36. parse_args(argc, argv);
  37. ret = starpu_init(NULL);
  38. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  39. starpu_mpi_init(&argc, &argv);
  40. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  41. MPI_Comm_size(MPI_COMM_WORLD, &nodes);
  42. starpu_helper_cublas_init();
  43. if (dblockx == -1 || dblocky == -1)
  44. {
  45. int factor;
  46. dblockx = nodes;
  47. dblocky = 1;
  48. for(factor=sqrt(nodes) ; factor>1 ; factor--)
  49. {
  50. if (nodes % factor == 0)
  51. {
  52. dblockx = nodes/factor;
  53. dblocky = factor;
  54. break;
  55. }
  56. }
  57. }
  58. unsigned i,j,x,y;
  59. bmat = malloc(nblocks * sizeof(float *));
  60. for(x=0 ; x<nblocks ; x++)
  61. {
  62. bmat[x] = malloc(nblocks * sizeof(float *));
  63. for(y=0 ; y<nblocks ; y++)
  64. {
  65. starpu_malloc((void **)&bmat[x][y], BLOCKSIZE*BLOCKSIZE*sizeof(float));
  66. for (i = 0; i < BLOCKSIZE; i++)
  67. {
  68. for (j = 0; j < BLOCKSIZE; j++)
  69. {
  70. bmat[x][y][j +i*BLOCKSIZE] = (1.0f/(1.0f+(i+(x*BLOCKSIZE)+j+(y*BLOCKSIZE)))) + ((i+(x*BLOCKSIZE) == j+(y*BLOCKSIZE))?1.0f*size:0.0f);
  71. //mat[j +i*size] = ((i == j)?1.0f*size:0.0f);
  72. }
  73. }
  74. }
  75. }
  76. if (display)
  77. {
  78. printf("[%d] Input :\n", rank);
  79. for(y=0 ; y<nblocks ; y++)
  80. {
  81. for(x=0 ; x<nblocks ; x++)
  82. {
  83. printf("Block %u,%u :\n", x, y);
  84. for (j = 0; j < BLOCKSIZE; j++)
  85. {
  86. for (i = 0; i < BLOCKSIZE; i++)
  87. {
  88. if (i <= j)
  89. {
  90. printf("%2.2f\t", bmat[y][x][j +i*BLOCKSIZE]);
  91. }
  92. else
  93. {
  94. printf(".\t");
  95. }
  96. }
  97. printf("\n");
  98. }
  99. }
  100. }
  101. }
  102. double timing, flops;
  103. dw_cholesky(bmat, size, size/nblocks, nblocks, rank, nodes, &timing, &flops);
  104. starpu_mpi_shutdown();
  105. if (display)
  106. {
  107. printf("[%d] Results :\n", rank);
  108. for(y=0 ; y<nblocks ; y++)
  109. {
  110. for(x=0 ; x<nblocks ; x++)
  111. {
  112. printf("Block %u,%u :\n", x, y);
  113. for (j = 0; j < BLOCKSIZE; j++)
  114. {
  115. for (i = 0; i < BLOCKSIZE; i++)
  116. {
  117. if (i <= j)
  118. {
  119. printf("%2.2f\t", bmat[y][x][j +i*BLOCKSIZE]);
  120. }
  121. else
  122. {
  123. printf(".\t");
  124. }
  125. }
  126. printf("\n");
  127. }
  128. }
  129. }
  130. }
  131. float *rmat = malloc(size*size*sizeof(float));
  132. for(x=0 ; x<nblocks ; x++)
  133. {
  134. for(y=0 ; y<nblocks ; y++)
  135. {
  136. for (i = 0; i < BLOCKSIZE; i++)
  137. {
  138. for (j = 0; j < BLOCKSIZE; j++)
  139. {
  140. rmat[j+(y*BLOCKSIZE)+(i+(x*BLOCKSIZE))*size] = bmat[x][y][j +i*BLOCKSIZE];
  141. }
  142. }
  143. }
  144. }
  145. fprintf(stderr, "[%d] compute explicit LLt ...\n", rank);
  146. for (j = 0; j < size; j++)
  147. {
  148. for (i = 0; i < size; i++)
  149. {
  150. if (i > j)
  151. {
  152. rmat[j+i*size] = 0.0f; // debug
  153. }
  154. }
  155. }
  156. float *test_mat = malloc(size*size*sizeof(float));
  157. STARPU_ASSERT(test_mat);
  158. SSYRK("L", "N", size, size, 1.0f,
  159. rmat, size, 0.0f, test_mat, size);
  160. fprintf(stderr, "[%d] comparing results ...\n", rank);
  161. if (display)
  162. {
  163. for (j = 0; j < size; j++)
  164. {
  165. for (i = 0; i < size; i++)
  166. {
  167. if (i <= j)
  168. {
  169. printf("%2.2f\t", test_mat[j +i*size]);
  170. }
  171. else
  172. {
  173. printf(".\t");
  174. }
  175. }
  176. printf("\n");
  177. }
  178. }
  179. int correctness = 1;
  180. for(x = 0; x < nblocks ; x++)
  181. {
  182. for (y = 0; y < nblocks; y++)
  183. {
  184. int mpi_rank = my_distrib(x, y, nodes);
  185. if (mpi_rank == rank)
  186. {
  187. for (i = (size/nblocks)*x ; i < (size/nblocks)*x+(size/nblocks); i++)
  188. {
  189. for (j = (size/nblocks)*y ; j < (size/nblocks)*y+(size/nblocks); j++)
  190. {
  191. if (i <= j)
  192. {
  193. float orig = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  194. float err = abs(test_mat[j +i*size] - orig);
  195. if (err > 0.00001)
  196. {
  197. fprintf(stderr, "[%d] Error[%u, %u] --> %2.2f != %2.2f (err %2.2f)\n", rank, i, j, test_mat[j +i*size], orig, err);
  198. correctness = 0;
  199. flops = 0;
  200. break;
  201. }
  202. }
  203. }
  204. }
  205. }
  206. }
  207. }
  208. for(x=0 ; x<nblocks ; x++)
  209. {
  210. for(y=0 ; y<nblocks ; y++)
  211. {
  212. starpu_free((void *)bmat[x][y]);
  213. }
  214. free(bmat[x]);
  215. }
  216. free(bmat);
  217. free(rmat);
  218. free(test_mat);
  219. starpu_helper_cublas_shutdown();
  220. starpu_shutdown();
  221. assert(correctness);
  222. if (rank == 0)
  223. {
  224. fprintf(stdout, "Computation time (in ms): %2.2f\n", timing/1000);
  225. fprintf(stdout, "Synthetic GFlops : %2.2f\n", (flops/timing/1000.0f));
  226. }
  227. return 0;
  228. }