mpi_cholesky.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011 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. /*
  22. * Create the codelets
  23. */
  24. static struct starpu_codelet cl11 =
  25. {
  26. .where = STARPU_CPU|STARPU_CUDA,
  27. .cpu_funcs = {chol_cpu_codelet_update_u11, NULL},
  28. #ifdef STARPU_USE_CUDA
  29. .cuda_funcs = {chol_cublas_codelet_update_u11, NULL},
  30. #endif
  31. .nbuffers = 1,
  32. .modes = {STARPU_RW},
  33. .model = &chol_model_11
  34. };
  35. static struct starpu_codelet cl21 =
  36. {
  37. .where = STARPU_CPU|STARPU_CUDA,
  38. .cpu_funcs = {chol_cpu_codelet_update_u21, NULL},
  39. #ifdef STARPU_USE_CUDA
  40. .cuda_funcs = {chol_cublas_codelet_update_u21, NULL},
  41. #endif
  42. .nbuffers = 2,
  43. .modes = {STARPU_R, STARPU_RW},
  44. .model = &chol_model_21
  45. };
  46. static struct starpu_codelet cl22 =
  47. {
  48. .where = STARPU_CPU|STARPU_CUDA,
  49. .cpu_funcs = {chol_cpu_codelet_update_u22, NULL},
  50. #ifdef STARPU_USE_CUDA
  51. .cuda_funcs = {chol_cublas_codelet_update_u22, NULL},
  52. #endif
  53. .nbuffers = 3,
  54. .modes = {STARPU_R, STARPU_R, STARPU_RW},
  55. .model = &chol_model_22
  56. };
  57. /* Returns the MPI node number where data indexes index is */
  58. int my_distrib(int x, int y, int nb_nodes)
  59. {
  60. //return (x+y) % nb_nodes;
  61. return (x%dblockx)+(y%dblocky)*dblockx;
  62. }
  63. /*
  64. * code to bootstrap the factorization
  65. * and construct the DAG
  66. */
  67. static void dw_cholesky(float ***matA, unsigned size, unsigned ld, unsigned nblocks, int rank, int nodes)
  68. {
  69. struct timeval start;
  70. struct timeval end;
  71. starpu_data_handle_t **data_handles;
  72. int x, y;
  73. /* create all the DAG nodes */
  74. unsigned i,j,k;
  75. data_handles = malloc(nblocks*sizeof(starpu_data_handle_t *));
  76. for(x=0 ; x<nblocks ; x++) data_handles[x] = malloc(nblocks*sizeof(starpu_data_handle_t));
  77. for(x = 0; x < nblocks ; x++)
  78. {
  79. for (y = 0; y < nblocks; y++)
  80. {
  81. int mpi_rank = my_distrib(x, y, nodes);
  82. if (mpi_rank == rank)
  83. {
  84. //fprintf(stderr, "[%d] Owning data[%d][%d]\n", rank, x, y);
  85. starpu_matrix_data_register(&data_handles[x][y], 0, (uintptr_t)matA[x][y],
  86. ld, size/nblocks, size/nblocks, sizeof(float));
  87. }
  88. /* TODO: make better test to only registering what is needed */
  89. else
  90. {
  91. /* I don't own that index, but will need it for my computations */
  92. //fprintf(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, x, y);
  93. starpu_matrix_data_register(&data_handles[x][y], -1, (uintptr_t)NULL,
  94. ld, size/nblocks, size/nblocks, sizeof(float));
  95. }
  96. if (data_handles[x][y])
  97. {
  98. starpu_data_set_rank(data_handles[x][y], mpi_rank);
  99. starpu_data_set_tag(data_handles[x][y], (y*nblocks)+x);
  100. }
  101. }
  102. }
  103. starpu_mpi_barrier(MPI_COMM_WORLD);
  104. gettimeofday(&start, NULL);
  105. for (k = 0; k < nblocks; k++)
  106. {
  107. int prio = STARPU_DEFAULT_PRIO;
  108. if (!noprio) prio = STARPU_MAX_PRIO;
  109. starpu_mpi_insert_task(MPI_COMM_WORLD, &cl11,
  110. STARPU_PRIORITY, prio,
  111. STARPU_RW, data_handles[k][k],
  112. 0);
  113. for (j = k+1; j<nblocks; j++)
  114. {
  115. prio = STARPU_DEFAULT_PRIO;
  116. if (!noprio&& (j == k+1)) prio = STARPU_MAX_PRIO;
  117. starpu_mpi_insert_task(MPI_COMM_WORLD, &cl21,
  118. STARPU_PRIORITY, prio,
  119. STARPU_R, data_handles[k][k],
  120. STARPU_RW, data_handles[k][j],
  121. 0);
  122. for (i = k+1; i<nblocks; i++)
  123. {
  124. if (i <= j)
  125. {
  126. prio = STARPU_DEFAULT_PRIO;
  127. if (!noprio && (i == k + 1) && (j == k +1) ) prio = STARPU_MAX_PRIO;
  128. starpu_mpi_insert_task(MPI_COMM_WORLD, &cl22,
  129. STARPU_PRIORITY, prio,
  130. STARPU_R, data_handles[k][i],
  131. STARPU_R, data_handles[k][j],
  132. STARPU_RW, data_handles[i][j],
  133. 0);
  134. }
  135. }
  136. }
  137. }
  138. starpu_task_wait_for_all();
  139. for(x = 0; x < nblocks ; x++)
  140. {
  141. for (y = 0; y < nblocks; y++)
  142. {
  143. if (data_handles[x][y])
  144. starpu_data_unregister(data_handles[x][y]);
  145. }
  146. free(data_handles[x]);
  147. }
  148. free(data_handles);
  149. starpu_mpi_barrier(MPI_COMM_WORLD);
  150. gettimeofday(&end, NULL);
  151. if (rank == 0)
  152. {
  153. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  154. fprintf(stderr, "Computation took (in ms)\n");
  155. fprintf(stdout, "%2.2f\n", timing/1000);
  156. double flop = (1.0f*size*size*size)/3.0f;
  157. fprintf(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  158. }
  159. }
  160. int main(int argc, char **argv)
  161. {
  162. /* create a simple definite positive symetric matrix example
  163. *
  164. * Hilbert matrix : h(i,j) = 1/(i+j+1)
  165. * */
  166. float ***bmat;
  167. int rank, nodes;
  168. parse_args(argc, argv);
  169. struct starpu_conf conf;
  170. starpu_conf_init(&conf);
  171. conf.sched_policy_name = "heft";
  172. conf.calibrate = 1;
  173. int ret = starpu_init(&conf);
  174. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  175. starpu_mpi_initialize_extended(&rank, &nodes);
  176. starpu_helper_cublas_init();
  177. if (dblockx == -1 || dblocky == -1)
  178. {
  179. int factor;
  180. dblockx = nodes;
  181. dblocky = 1;
  182. for(factor=sqrt(nodes) ; factor>1 ; factor--)
  183. {
  184. if (nodes % factor == 0)
  185. {
  186. dblockx = nodes/factor;
  187. dblocky = factor;
  188. }
  189. }
  190. }
  191. unsigned i,j,x,y;
  192. bmat = malloc(nblocks * sizeof(float *));
  193. for(x=0 ; x<nblocks ; x++)
  194. {
  195. bmat[x] = malloc(nblocks * sizeof(float *));
  196. for(y=0 ; y<nblocks ; y++)
  197. {
  198. starpu_malloc((void **)&bmat[x][y], BLOCKSIZE*BLOCKSIZE*sizeof(float));
  199. for (i = 0; i < BLOCKSIZE; i++)
  200. {
  201. for (j = 0; j < BLOCKSIZE; j++)
  202. {
  203. 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);
  204. //mat[j +i*size] = ((i == j)?1.0f*size:0.0f);
  205. }
  206. }
  207. }
  208. }
  209. if (display)
  210. {
  211. printf("[%d] Input :\n", rank);
  212. for(y=0 ; y<nblocks ; y++)
  213. {
  214. for(x=0 ; x<nblocks ; x++)
  215. {
  216. printf("Block %d,%d :\n", x, y);
  217. for (j = 0; j < BLOCKSIZE; j++)
  218. {
  219. for (i = 0; i < BLOCKSIZE; i++)
  220. {
  221. if (i <= j)
  222. {
  223. printf("%2.2f\t", bmat[y][x][j +i*BLOCKSIZE]);
  224. }
  225. else
  226. {
  227. printf(".\t");
  228. }
  229. }
  230. printf("\n");
  231. }
  232. }
  233. }
  234. }
  235. dw_cholesky(bmat, size, size/nblocks, nblocks, rank, nodes);
  236. starpu_mpi_shutdown();
  237. if (display)
  238. {
  239. printf("[%d] Results :\n", rank);
  240. for(y=0 ; y<nblocks ; y++)
  241. {
  242. for(x=0 ; x<nblocks ; x++)
  243. {
  244. printf("Block %d,%d :\n", x, y);
  245. for (j = 0; j < BLOCKSIZE; j++)
  246. {
  247. for (i = 0; i < BLOCKSIZE; i++)
  248. {
  249. if (i <= j)
  250. {
  251. printf("%2.2f\t", bmat[y][x][j +i*BLOCKSIZE]);
  252. }
  253. else
  254. {
  255. printf(".\t");
  256. }
  257. }
  258. printf("\n");
  259. }
  260. }
  261. }
  262. }
  263. float *rmat = malloc(size*size*sizeof(float));
  264. for(x=0 ; x<nblocks ; x++)
  265. {
  266. for(y=0 ; y<nblocks ; y++)
  267. {
  268. for (i = 0; i < BLOCKSIZE; i++)
  269. {
  270. for (j = 0; j < BLOCKSIZE; j++)
  271. {
  272. rmat[j+(y*BLOCKSIZE)+(i+(x*BLOCKSIZE))*size] = bmat[x][y][j +i*BLOCKSIZE];
  273. }
  274. }
  275. }
  276. }
  277. fprintf(stderr, "[%d] compute explicit LLt ...\n", rank);
  278. for (j = 0; j < size; j++)
  279. {
  280. for (i = 0; i < size; i++)
  281. {
  282. if (i > j)
  283. {
  284. rmat[j+i*size] = 0.0f; // debug
  285. }
  286. }
  287. }
  288. float *test_mat = malloc(size*size*sizeof(float));
  289. STARPU_ASSERT(test_mat);
  290. SSYRK("L", "N", size, size, 1.0f,
  291. rmat, size, 0.0f, test_mat, size);
  292. fprintf(stderr, "[%d] comparing results ...\n", rank);
  293. if (display)
  294. {
  295. for (j = 0; j < size; j++)
  296. {
  297. for (i = 0; i < size; i++)
  298. {
  299. if (i <= j)
  300. {
  301. printf("%2.2f\t", test_mat[j +i*size]);
  302. }
  303. else
  304. {
  305. printf(".\t");
  306. }
  307. }
  308. printf("\n");
  309. }
  310. }
  311. int correctness = 1;
  312. for(x = 0; x < nblocks ; x++)
  313. {
  314. for (y = 0; y < nblocks; y++)
  315. {
  316. int mpi_rank = my_distrib(x, y, nodes);
  317. if (mpi_rank == rank)
  318. {
  319. for (i = (size/nblocks)*x ; i < (size/nblocks)*x+(size/nblocks); i++)
  320. {
  321. for (j = (size/nblocks)*y ; j < (size/nblocks)*y+(size/nblocks); j++)
  322. {
  323. if (i <= j)
  324. {
  325. float orig = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  326. float err = abs(test_mat[j +i*size] - orig);
  327. if (err > 0.00001)
  328. {
  329. fprintf(stderr, "[%d] Error[%d, %d] --> %2.2f != %2.2f (err %2.2f)\n", rank, i, j, test_mat[j +i*size], orig, err);
  330. correctness = 0;
  331. break;
  332. }
  333. }
  334. }
  335. }
  336. }
  337. }
  338. }
  339. for(x=0 ; x<nblocks ; x++)
  340. {
  341. for(y=0 ; y<nblocks ; y++)
  342. {
  343. starpu_free((void *)bmat[x][y]);
  344. }
  345. free(bmat[x]);
  346. }
  347. free(bmat);
  348. free(rmat);
  349. free(test_mat);
  350. starpu_helper_cublas_shutdown();
  351. starpu_shutdown();
  352. assert(correctness);
  353. return 0;
  354. }