mpi_cholesky_codelets.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU 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. * StarPU 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 "mpi_cholesky.h"
  17. #include <common/blas.h>
  18. #include <sys/time.h>
  19. #include <limits.h>
  20. #include <math.h>
  21. /*
  22. * Create the codelets
  23. */
  24. static struct starpu_codelet cl11 =
  25. {
  26. .cpu_funcs = {chol_cpu_codelet_update_u11},
  27. #ifdef STARPU_USE_CUDA
  28. .cuda_funcs = {chol_cublas_codelet_update_u11},
  29. #elif defined(STARPU_SIMGRID)
  30. .cuda_funcs = {(void*)1},
  31. #endif
  32. .nbuffers = 1,
  33. .modes = {STARPU_RW},
  34. .model = &chol_model_11,
  35. .color = 0xffff00,
  36. };
  37. static struct starpu_codelet cl21 =
  38. {
  39. .cpu_funcs = {chol_cpu_codelet_update_u21},
  40. #ifdef STARPU_USE_CUDA
  41. .cuda_funcs = {chol_cublas_codelet_update_u21},
  42. #elif defined(STARPU_SIMGRID)
  43. .cuda_funcs = {(void*)1},
  44. #endif
  45. .cuda_flags = {STARPU_CUDA_ASYNC},
  46. .nbuffers = 2,
  47. .modes = {STARPU_R, STARPU_RW},
  48. .model = &chol_model_21,
  49. .color = 0x8080ff,
  50. };
  51. static struct starpu_codelet cl22 =
  52. {
  53. .cpu_funcs = {chol_cpu_codelet_update_u22},
  54. #ifdef STARPU_USE_CUDA
  55. .cuda_funcs = {chol_cublas_codelet_update_u22},
  56. #elif defined(STARPU_SIMGRID)
  57. .cuda_funcs = {(void*)1},
  58. #endif
  59. .cuda_flags = {STARPU_CUDA_ASYNC},
  60. .nbuffers = 3,
  61. .modes = {STARPU_R, STARPU_R, STARPU_RW | STARPU_COMMUTE},
  62. .model = &chol_model_22,
  63. .color = 0x00ff00,
  64. };
  65. static void run_cholesky(starpu_data_handle_t **data_handles, int rank, int nodes)
  66. {
  67. unsigned k, m, n;
  68. unsigned unbound_prio = STARPU_MAX_PRIO == INT_MAX && STARPU_MIN_PRIO == INT_MIN;
  69. for (k = 0; k < nblocks; k++)
  70. {
  71. starpu_iteration_push(k);
  72. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl11,
  73. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k) : STARPU_MAX_PRIO,
  74. STARPU_RW, data_handles[k][k],
  75. 0);
  76. for (m = k+1; m<nblocks; m++)
  77. {
  78. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl21,
  79. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - m) : (m == k+1)?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  80. STARPU_R, data_handles[k][k],
  81. STARPU_RW, data_handles[m][k],
  82. 0);
  83. starpu_mpi_cache_flush(MPI_COMM_WORLD, data_handles[k][k]);
  84. if (my_distrib(k, k, nodes) == rank)
  85. starpu_data_wont_use(data_handles[k][k]);
  86. for (n = k+1; n<nblocks; n++)
  87. {
  88. if (n <= m)
  89. {
  90. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl22,
  91. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - m - n) : ((n == k+1) && (m == k+1))?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  92. STARPU_R, data_handles[n][k],
  93. STARPU_R, data_handles[m][k],
  94. STARPU_RW | STARPU_COMMUTE, data_handles[m][n],
  95. 0);
  96. }
  97. }
  98. starpu_mpi_cache_flush(MPI_COMM_WORLD, data_handles[m][k]);
  99. if (my_distrib(m, k, nodes) == rank)
  100. starpu_data_wont_use(data_handles[m][k]);
  101. }
  102. starpu_iteration_pop();
  103. }
  104. }
  105. /* TODO: generate from compiler polyhedral analysis of classical algorithm */
  106. static void run_cholesky_column(starpu_data_handle_t **data_handles, int rank, int nodes)
  107. {
  108. unsigned k, m, n;
  109. unsigned unbound_prio = STARPU_MAX_PRIO == INT_MAX && STARPU_MIN_PRIO == INT_MIN;
  110. /* Column */
  111. for (n = 0; n<nblocks; n++)
  112. {
  113. starpu_iteration_push(n);
  114. /* Row */
  115. for (m = n; m<nblocks; m++)
  116. {
  117. for (k = 0; k < n; k++)
  118. {
  119. /* Accumulate updates from TRSMs */
  120. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl22,
  121. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - m - n) : ((n == k+1) && (m == k+1))?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  122. STARPU_R, data_handles[n][k],
  123. STARPU_R, data_handles[m][k],
  124. STARPU_RW | STARPU_COMMUTE, data_handles[m][n],
  125. 0);
  126. }
  127. k = n;
  128. if (m > n)
  129. {
  130. /* non-diagonal block, solve */
  131. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl21,
  132. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - m) : (m == k+1)?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  133. STARPU_R, data_handles[k][k],
  134. STARPU_RW, data_handles[m][k],
  135. 0);
  136. }
  137. else
  138. {
  139. /* diagonal block, factorize */
  140. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl11,
  141. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k) : STARPU_MAX_PRIO,
  142. STARPU_RW, data_handles[k][k],
  143. 0);
  144. }
  145. }
  146. starpu_iteration_pop();
  147. }
  148. /* Submit flushes, StarPU will fit them according to the progress */
  149. starpu_mpi_cache_flush_all_data(MPI_COMM_WORLD);
  150. for (m = 0; m < nblocks; m++)
  151. for (n = 0; n < nblocks ; n++)
  152. starpu_data_wont_use(data_handles[m][n]);
  153. }
  154. /* TODO: generate from compiler polyhedral analysis of classical algorithm */
  155. static void run_cholesky_antidiagonal(starpu_data_handle_t **data_handles, int rank, int nodes)
  156. {
  157. unsigned a, c;
  158. unsigned k, m, n;
  159. unsigned unbound_prio = STARPU_MAX_PRIO == INT_MAX && STARPU_MIN_PRIO == INT_MIN;
  160. /* double-antidiagonal number:
  161. * - a=0 contains (0,0) plus (1,0)
  162. * - a=1 contains (2,0), (1,1) plus (3,0), (2, 1)
  163. * - etc.
  164. */
  165. for (a = 0; a < nblocks; a++)
  166. {
  167. starpu_iteration_push(a);
  168. unsigned nfirst;
  169. if (2*a < nblocks)
  170. nfirst = 0;
  171. else
  172. nfirst = 2*a - (nblocks-1);
  173. /* column within first antidiagonal for a */
  174. for (n = nfirst; n <= a; n++)
  175. {
  176. /* row */
  177. m = 2*a-n;
  178. /* Accumulate updates from TRSMs */
  179. for (k = 0; k < n; k++)
  180. {
  181. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl22,
  182. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - m - n) : ((n == k+1) && (m == k+1))?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  183. STARPU_R, data_handles[n][k],
  184. STARPU_R, data_handles[m][k],
  185. STARPU_RW | STARPU_COMMUTE, data_handles[m][n],
  186. 0);
  187. }
  188. /* k = n */
  189. if (n < a)
  190. {
  191. /* non-diagonal block, solve */
  192. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl21,
  193. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - m) : (m == k+1)?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  194. STARPU_R, data_handles[k][k],
  195. STARPU_RW, data_handles[m][k],
  196. 0);
  197. }
  198. else
  199. {
  200. /* diagonal block, factorize */
  201. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl11,
  202. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k) : STARPU_MAX_PRIO,
  203. STARPU_RW, data_handles[k][k],
  204. 0);
  205. }
  206. }
  207. /* column within second antidiagonal for a */
  208. for (n = nfirst; n <= a; n++)
  209. {
  210. /* row */
  211. m = 2*a-n + 1;
  212. if (m >= nblocks)
  213. /* Skip first item when even number of tiles */
  214. continue;
  215. /* Accumulate updates from TRSMs */
  216. for (k = 0; k < n; k++)
  217. {
  218. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl22,
  219. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - m - n) : ((n == k+1) && (m == k+1))?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  220. STARPU_R, data_handles[n][k],
  221. STARPU_R, data_handles[m][k],
  222. STARPU_RW | STARPU_COMMUTE, data_handles[m][n],
  223. 0);
  224. }
  225. /* non-diagonal block, solve */
  226. k = n;
  227. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl21,
  228. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - m) : (m == k+1)?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  229. STARPU_R, data_handles[k][k],
  230. STARPU_RW, data_handles[m][k],
  231. 0);
  232. }
  233. starpu_iteration_pop();
  234. }
  235. /* Submit flushes, StarPU will fit them according to the progress */
  236. starpu_mpi_cache_flush_all_data(MPI_COMM_WORLD);
  237. for (m = 0; m < nblocks; m++)
  238. for (n = 0; n < nblocks ; n++)
  239. starpu_data_wont_use(data_handles[m][n]);
  240. }
  241. /*
  242. * code to bootstrap the factorization
  243. * and construct the DAG
  244. */
  245. void dw_cholesky(float ***matA, unsigned ld, int rank, int nodes, double *timing, double *flops)
  246. {
  247. double start;
  248. double end;
  249. starpu_data_handle_t **data_handles;
  250. unsigned k, m, n;
  251. /* create all the DAG nodes */
  252. data_handles = malloc(nblocks*sizeof(starpu_data_handle_t *));
  253. for(m=0 ; m<nblocks ; m++) data_handles[m] = malloc(nblocks*sizeof(starpu_data_handle_t));
  254. for (m = 0; m < nblocks; m++)
  255. {
  256. for(n = 0; n < nblocks ; n++)
  257. {
  258. int mpi_rank = my_distrib(m, n, nodes);
  259. if (mpi_rank == rank || (check && rank == 0))
  260. {
  261. //fprintf(stderr, "[%d] Owning data[%d][%d]\n", rank, n, m);
  262. starpu_matrix_data_register(&data_handles[m][n], STARPU_MAIN_RAM, (uintptr_t)matA[m][n],
  263. ld, size/nblocks, size/nblocks, sizeof(float));
  264. }
  265. #ifdef STARPU_DEVEL
  266. #warning TODO: make better test to only register what is needed
  267. #endif
  268. else
  269. {
  270. /* I don't own this index, but will need it for my computations */
  271. //fprintf(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, n, m);
  272. starpu_matrix_data_register(&data_handles[m][n], -1, (uintptr_t)NULL,
  273. ld, size/nblocks, size/nblocks, sizeof(float));
  274. }
  275. if (data_handles[m][n])
  276. {
  277. starpu_data_set_coordinates(data_handles[m][n], 2, n, m);
  278. starpu_mpi_data_register(data_handles[m][n], (m*nblocks)+n, mpi_rank);
  279. }
  280. }
  281. }
  282. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  283. starpu_mpi_barrier(MPI_COMM_WORLD);
  284. start = starpu_timing_now();
  285. switch (submission)
  286. {
  287. case TRIANGLES: run_cholesky(data_handles, rank, nodes); break;
  288. case COLUMNS: run_cholesky_column(data_handles, rank, nodes); break;
  289. case ANTIDIAGONALS: run_cholesky_antidiagonal(data_handles, rank, nodes); break;
  290. default: STARPU_ABORT();
  291. }
  292. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  293. starpu_mpi_barrier(MPI_COMM_WORLD);
  294. end = starpu_timing_now();
  295. for (m = 0; m < nblocks; m++)
  296. {
  297. for(n = 0; n < nblocks ; n++)
  298. {
  299. /* Get back data on node 0 for the check */
  300. if (check && data_handles[m][n])
  301. starpu_mpi_get_data_on_node(MPI_COMM_WORLD, data_handles[m][n], 0);
  302. if (data_handles[m][n])
  303. starpu_data_unregister(data_handles[m][n]);
  304. }
  305. free(data_handles[m]);
  306. }
  307. free(data_handles);
  308. if (rank == 0)
  309. {
  310. *timing = end - start;
  311. *flops = (1.0f*size*size*size)/3.0f;
  312. }
  313. }
  314. void dw_cholesky_check_computation(float ***matA, int rank, int nodes, int *correctness, double *flops, double epsilon)
  315. {
  316. unsigned nn,mm,n,m;
  317. float *rmat = malloc(size*size*sizeof(float));
  318. for(n=0 ; n<nblocks ; n++)
  319. {
  320. for(m=0 ; m<nblocks ; m++)
  321. {
  322. for (nn = 0; nn < BLOCKSIZE; nn++)
  323. {
  324. for (mm = 0; mm < BLOCKSIZE; mm++)
  325. {
  326. rmat[mm+(m*BLOCKSIZE)+(nn+(n*BLOCKSIZE))*size] = matA[m][n][mm +nn*BLOCKSIZE];
  327. }
  328. }
  329. }
  330. }
  331. FPRINTF(stderr, "[%d] compute explicit LLt ...\n", rank);
  332. for (mm = 0; mm < size; mm++)
  333. {
  334. for (nn = 0; nn < size; nn++)
  335. {
  336. if (nn > mm)
  337. {
  338. rmat[mm+nn*size] = 0.0f; // debug
  339. }
  340. }
  341. }
  342. float *test_mat = malloc(size*size*sizeof(float));
  343. STARPU_ASSERT(test_mat);
  344. STARPU_SSYRK("L", "N", size, size, 1.0f,
  345. rmat, size, 0.0f, test_mat, size);
  346. FPRINTF(stderr, "[%d] comparing results ...\n", rank);
  347. if (display)
  348. {
  349. for (mm = 0; mm < size; mm++)
  350. {
  351. for (nn = 0; nn < size; nn++)
  352. {
  353. if (nn <= mm)
  354. {
  355. printf("%2.2f\t", test_mat[mm +nn*size]);
  356. }
  357. else
  358. {
  359. printf(".\t");
  360. }
  361. }
  362. printf("\n");
  363. }
  364. }
  365. *correctness = 1;
  366. for(n = 0; n < nblocks ; n++)
  367. {
  368. for (m = 0; m < nblocks; m++)
  369. {
  370. for (nn = BLOCKSIZE*n ; nn < BLOCKSIZE*(n+1); nn++)
  371. {
  372. for (mm = BLOCKSIZE*m ; mm < BLOCKSIZE*(m+1); mm++)
  373. {
  374. if (nn <= mm)
  375. {
  376. float orig = (1.0f/(1.0f+nn+mm)) + ((nn == mm)?1.0f*size:0.0f);
  377. float err = fabsf(test_mat[mm +nn*size] - orig) / orig;
  378. if (err > epsilon)
  379. {
  380. FPRINTF(stderr, "[%d] Error[%u, %u] --> %2.20f != %2.20f (err %2.20f)\n", rank, nn, mm, test_mat[mm +nn*size], orig, err);
  381. *correctness = 0;
  382. *flops = 0;
  383. break;
  384. }
  385. }
  386. }
  387. }
  388. }
  389. }
  390. free(rmat);
  391. free(test_mat);
  392. }