mpi_cholesky.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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;
  36. parse_args(argc, argv);
  37. struct starpu_conf conf;
  38. starpu_conf_init(&conf);
  39. conf.sched_policy_name = "heft";
  40. conf.calibrate = 1;
  41. int ret = starpu_init(&conf);
  42. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  43. starpu_mpi_initialize_extended(&rank, &nodes);
  44. starpu_helper_cublas_init();
  45. if (dblockx == -1 || dblocky == -1)
  46. {
  47. int factor;
  48. dblockx = nodes;
  49. dblocky = 1;
  50. for(factor=sqrt(nodes) ; factor>1 ; factor--)
  51. {
  52. if (nodes % factor == 0)
  53. {
  54. dblockx = nodes/factor;
  55. dblocky = factor;
  56. break;
  57. }
  58. }
  59. }
  60. unsigned i,j,x,y;
  61. bmat = malloc(nblocks * sizeof(float *));
  62. for(x=0 ; x<nblocks ; x++)
  63. {
  64. bmat[x] = malloc(nblocks * sizeof(float *));
  65. for(y=0 ; y<nblocks ; y++)
  66. {
  67. starpu_malloc((void **)&bmat[x][y], BLOCKSIZE*BLOCKSIZE*sizeof(float));
  68. for (i = 0; i < BLOCKSIZE; i++)
  69. {
  70. for (j = 0; j < BLOCKSIZE; j++)
  71. {
  72. 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);
  73. //mat[j +i*size] = ((i == j)?1.0f*size:0.0f);
  74. }
  75. }
  76. }
  77. }
  78. if (display)
  79. {
  80. printf("[%d] Input :\n", rank);
  81. for(y=0 ; y<nblocks ; y++)
  82. {
  83. for(x=0 ; x<nblocks ; x++)
  84. {
  85. printf("Block %u,%u :\n", x, y);
  86. for (j = 0; j < BLOCKSIZE; j++)
  87. {
  88. for (i = 0; i < BLOCKSIZE; i++)
  89. {
  90. if (i <= j)
  91. {
  92. printf("%2.2f\t", bmat[y][x][j +i*BLOCKSIZE]);
  93. }
  94. else
  95. {
  96. printf(".\t");
  97. }
  98. }
  99. printf("\n");
  100. }
  101. }
  102. }
  103. }
  104. dw_cholesky(bmat, size, size/nblocks, nblocks, rank, nodes);
  105. starpu_mpi_shutdown();
  106. if (display)
  107. {
  108. printf("[%d] Results :\n", rank);
  109. for(y=0 ; y<nblocks ; y++)
  110. {
  111. for(x=0 ; x<nblocks ; x++)
  112. {
  113. printf("Block %u,%u :\n", x, y);
  114. for (j = 0; j < BLOCKSIZE; j++)
  115. {
  116. for (i = 0; i < BLOCKSIZE; i++)
  117. {
  118. if (i <= j)
  119. {
  120. printf("%2.2f\t", bmat[y][x][j +i*BLOCKSIZE]);
  121. }
  122. else
  123. {
  124. printf(".\t");
  125. }
  126. }
  127. printf("\n");
  128. }
  129. }
  130. }
  131. }
  132. float *rmat = malloc(size*size*sizeof(float));
  133. for(x=0 ; x<nblocks ; x++)
  134. {
  135. for(y=0 ; y<nblocks ; y++)
  136. {
  137. for (i = 0; i < BLOCKSIZE; i++)
  138. {
  139. for (j = 0; j < BLOCKSIZE; j++)
  140. {
  141. rmat[j+(y*BLOCKSIZE)+(i+(x*BLOCKSIZE))*size] = bmat[x][y][j +i*BLOCKSIZE];
  142. }
  143. }
  144. }
  145. }
  146. fprintf(stderr, "[%d] compute explicit LLt ...\n", rank);
  147. for (j = 0; j < size; j++)
  148. {
  149. for (i = 0; i < size; i++)
  150. {
  151. if (i > j)
  152. {
  153. rmat[j+i*size] = 0.0f; // debug
  154. }
  155. }
  156. }
  157. float *test_mat = malloc(size*size*sizeof(float));
  158. STARPU_ASSERT(test_mat);
  159. SSYRK("L", "N", size, size, 1.0f,
  160. rmat, size, 0.0f, test_mat, size);
  161. fprintf(stderr, "[%d] comparing results ...\n", rank);
  162. if (display)
  163. {
  164. for (j = 0; j < size; j++)
  165. {
  166. for (i = 0; i < size; i++)
  167. {
  168. if (i <= j)
  169. {
  170. printf("%2.2f\t", test_mat[j +i*size]);
  171. }
  172. else
  173. {
  174. printf(".\t");
  175. }
  176. }
  177. printf("\n");
  178. }
  179. }
  180. int correctness = 1;
  181. for(x = 0; x < nblocks ; x++)
  182. {
  183. for (y = 0; y < nblocks; y++)
  184. {
  185. int mpi_rank = my_distrib(x, y, nodes);
  186. if (mpi_rank == rank)
  187. {
  188. for (i = (size/nblocks)*x ; i < (size/nblocks)*x+(size/nblocks); i++)
  189. {
  190. for (j = (size/nblocks)*y ; j < (size/nblocks)*y+(size/nblocks); j++)
  191. {
  192. if (i <= j)
  193. {
  194. float orig = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  195. float err = abs(test_mat[j +i*size] - orig);
  196. if (err > 0.00001)
  197. {
  198. fprintf(stderr, "[%d] Error[%u, %u] --> %2.2f != %2.2f (err %2.2f)\n", rank, i, j, test_mat[j +i*size], orig, err);
  199. correctness = 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. return 0;
  223. }