mpi_cholesky_codelets.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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: generated 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: generated 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, b, 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 bfirst;
  169. if (2*a < nblocks)
  170. bfirst = 0;
  171. else
  172. bfirst = 2*a - (nblocks-1);
  173. /* column within first antidiagonal for a */
  174. for (b = bfirst; b <= a; b++)
  175. {
  176. /* column */
  177. n = b;
  178. /* row */
  179. m = 2*a-b;
  180. /* Accumulate updates from TRSMs */
  181. for (c = 0; c < n; c++)
  182. {
  183. k = c;
  184. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl22,
  185. 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,
  186. STARPU_R, data_handles[n][k],
  187. STARPU_R, data_handles[m][k],
  188. STARPU_RW | STARPU_COMMUTE, data_handles[m][n],
  189. 0);
  190. }
  191. if (b < a)
  192. {
  193. /* non-diagonal block, solve */
  194. k = n;
  195. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl21,
  196. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - m) : (m == k+1)?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  197. STARPU_R, data_handles[k][k],
  198. STARPU_RW, data_handles[m][k],
  199. 0);
  200. }
  201. else
  202. {
  203. /* diagonal block, factorize */
  204. k = a;
  205. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl11,
  206. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k) : STARPU_MAX_PRIO,
  207. STARPU_RW, data_handles[k][k],
  208. 0);
  209. }
  210. }
  211. /* column within second antidiagonal for a */
  212. for (b = bfirst; b <= a; b++)
  213. {
  214. /* column */
  215. n = b;
  216. /* row */
  217. m = 2*a-b + 1;
  218. if (m >= nblocks)
  219. /* Skip first item when even number of tiles */
  220. continue;
  221. /* Accumulate updates from TRSMs */
  222. for (c = 0; c < n; c++)
  223. {
  224. k = c;
  225. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl22,
  226. 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,
  227. STARPU_R, data_handles[n][k],
  228. STARPU_R, data_handles[m][k],
  229. STARPU_RW | STARPU_COMMUTE, data_handles[m][n],
  230. 0);
  231. }
  232. /* non-diagonal block, solve */
  233. k = n;
  234. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl21,
  235. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - m) : (m == k+1)?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  236. STARPU_R, data_handles[k][k],
  237. STARPU_RW, data_handles[m][k],
  238. 0);
  239. }
  240. starpu_iteration_pop();
  241. }
  242. /* Submit flushes, StarPU will fit them according to the progress */
  243. starpu_mpi_cache_flush_all_data(MPI_COMM_WORLD);
  244. for (m = 0; m < nblocks; m++)
  245. for (n = 0; n < nblocks ; n++)
  246. starpu_data_wont_use(data_handles[m][n]);
  247. }
  248. /*
  249. * code to bootstrap the factorization
  250. * and construct the DAG
  251. */
  252. void dw_cholesky(float ***matA, unsigned ld, int rank, int nodes, double *timing, double *flops)
  253. {
  254. double start;
  255. double end;
  256. starpu_data_handle_t **data_handles;
  257. unsigned k, m, n;
  258. /* create all the DAG nodes */
  259. data_handles = malloc(nblocks*sizeof(starpu_data_handle_t *));
  260. for(m=0 ; m<nblocks ; m++) data_handles[m] = malloc(nblocks*sizeof(starpu_data_handle_t));
  261. for (m = 0; m < nblocks; m++)
  262. {
  263. for(n = 0; n < nblocks ; n++)
  264. {
  265. int mpi_rank = my_distrib(m, n, nodes);
  266. if (mpi_rank == rank || (check && rank == 0))
  267. {
  268. //fprintf(stderr, "[%d] Owning data[%d][%d]\n", rank, n, m);
  269. starpu_matrix_data_register(&data_handles[m][n], STARPU_MAIN_RAM, (uintptr_t)matA[m][n],
  270. ld, size/nblocks, size/nblocks, sizeof(float));
  271. }
  272. #ifdef STARPU_DEVEL
  273. #warning TODO: make better test to only register what is needed
  274. #endif
  275. else
  276. {
  277. /* I don't own this index, but will need it for my computations */
  278. //fprintf(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, n, m);
  279. starpu_matrix_data_register(&data_handles[m][n], -1, (uintptr_t)NULL,
  280. ld, size/nblocks, size/nblocks, sizeof(float));
  281. }
  282. if (data_handles[m][n])
  283. {
  284. starpu_data_set_coordinates(data_handles[m][n], 2, n, m);
  285. starpu_mpi_data_register(data_handles[m][n], (m*nblocks)+n, mpi_rank);
  286. }
  287. }
  288. }
  289. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  290. starpu_mpi_barrier(MPI_COMM_WORLD);
  291. start = starpu_timing_now();
  292. switch (submission)
  293. {
  294. case TRIANGLES: run_cholesky(data_handles, rank, nodes); break;
  295. case COLUMNS: run_cholesky_column(data_handles, rank, nodes); break;
  296. case ANTIDIAGONALS: run_cholesky_antidiagonal(data_handles, rank, nodes); break;
  297. default: STARPU_ABORT();
  298. }
  299. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  300. starpu_mpi_barrier(MPI_COMM_WORLD);
  301. end = starpu_timing_now();
  302. for (m = 0; m < nblocks; m++)
  303. {
  304. for(n = 0; n < nblocks ; n++)
  305. {
  306. /* Get back data on node 0 for the check */
  307. if (check && data_handles[m][n])
  308. starpu_mpi_get_data_on_node(MPI_COMM_WORLD, data_handles[m][n], 0);
  309. if (data_handles[m][n])
  310. starpu_data_unregister(data_handles[m][n]);
  311. }
  312. free(data_handles[m]);
  313. }
  314. free(data_handles);
  315. if (rank == 0)
  316. {
  317. *timing = end - start;
  318. *flops = (1.0f*size*size*size)/3.0f;
  319. }
  320. }
  321. void dw_cholesky_check_computation(float ***matA, int rank, int nodes, int *correctness, double *flops, double epsilon)
  322. {
  323. unsigned nn,mm,n,m;
  324. float *rmat = malloc(size*size*sizeof(float));
  325. for(n=0 ; n<nblocks ; n++)
  326. {
  327. for(m=0 ; m<nblocks ; m++)
  328. {
  329. for (nn = 0; nn < BLOCKSIZE; nn++)
  330. {
  331. for (mm = 0; mm < BLOCKSIZE; mm++)
  332. {
  333. rmat[mm+(m*BLOCKSIZE)+(nn+(n*BLOCKSIZE))*size] = matA[m][n][mm +nn*BLOCKSIZE];
  334. }
  335. }
  336. }
  337. }
  338. FPRINTF(stderr, "[%d] compute explicit LLt ...\n", rank);
  339. for (mm = 0; mm < size; mm++)
  340. {
  341. for (nn = 0; nn < size; nn++)
  342. {
  343. if (nn > mm)
  344. {
  345. rmat[mm+nn*size] = 0.0f; // debug
  346. }
  347. }
  348. }
  349. float *test_mat = malloc(size*size*sizeof(float));
  350. STARPU_ASSERT(test_mat);
  351. STARPU_SSYRK("L", "N", size, size, 1.0f,
  352. rmat, size, 0.0f, test_mat, size);
  353. FPRINTF(stderr, "[%d] comparing results ...\n", rank);
  354. if (display)
  355. {
  356. for (mm = 0; mm < size; mm++)
  357. {
  358. for (nn = 0; nn < size; nn++)
  359. {
  360. if (nn <= mm)
  361. {
  362. printf("%2.2f\t", test_mat[mm +nn*size]);
  363. }
  364. else
  365. {
  366. printf(".\t");
  367. }
  368. }
  369. printf("\n");
  370. }
  371. }
  372. *correctness = 1;
  373. for(n = 0; n < nblocks ; n++)
  374. {
  375. for (m = 0; m < nblocks; m++)
  376. {
  377. for (nn = BLOCKSIZE*n ; nn < BLOCKSIZE*(n+1); nn++)
  378. {
  379. for (mm = BLOCKSIZE*m ; mm < BLOCKSIZE*(m+1); mm++)
  380. {
  381. if (nn <= mm)
  382. {
  383. float orig = (1.0f/(1.0f+nn+mm)) + ((nn == mm)?1.0f*size:0.0f);
  384. float err = fabsf(test_mat[mm +nn*size] - orig) / orig;
  385. if (err > epsilon)
  386. {
  387. FPRINTF(stderr, "[%d] Error[%u, %u] --> %2.20f != %2.20f (err %2.20f)\n", rank, nn, mm, test_mat[mm +nn*size], orig, err);
  388. *correctness = 0;
  389. *flops = 0;
  390. break;
  391. }
  392. }
  393. }
  394. }
  395. }
  396. }
  397. free(rmat);
  398. free(test_mat);
  399. }