mpi_cholesky.c 11 KB

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