mpi_cholesky_codelets.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. int _nodes;
  22. starpu_mpi_checkpoint_template_t* checkpoint_p;
  23. int backup_function(int rank)
  24. {
  25. if (rank==0)
  26. return 1;
  27. else
  28. return 0;
  29. // return (rank+1)%_nodes;
  30. }
  31. /*
  32. * Create the codelets
  33. */
  34. static struct starpu_codelet cl11 =
  35. {
  36. .cpu_funcs = {chol_cpu_codelet_update_u11},
  37. #ifdef STARPU_USE_CUDA
  38. .cuda_funcs = {chol_cublas_codelet_update_u11},
  39. #elif defined(STARPU_SIMGRID)
  40. .cuda_funcs = {(void*)1},
  41. #endif
  42. .nbuffers = 1,
  43. .modes = {STARPU_RW},
  44. .model = &chol_model_11,
  45. .color = 0xffff00,
  46. };
  47. static struct starpu_codelet cl21 =
  48. {
  49. .cpu_funcs = {chol_cpu_codelet_update_u21},
  50. #ifdef STARPU_USE_CUDA
  51. .cuda_funcs = {chol_cublas_codelet_update_u21},
  52. #elif defined(STARPU_SIMGRID)
  53. .cuda_funcs = {(void*)1},
  54. #endif
  55. .cuda_flags = {STARPU_CUDA_ASYNC},
  56. .nbuffers = 2,
  57. .modes = {STARPU_R, STARPU_RW},
  58. .model = &chol_model_21,
  59. .color = 0x8080ff,
  60. };
  61. static struct starpu_codelet cl22 =
  62. {
  63. .cpu_funcs = {chol_cpu_codelet_update_u22},
  64. #ifdef STARPU_USE_CUDA
  65. .cuda_funcs = {chol_cublas_codelet_update_u22},
  66. #elif defined(STARPU_SIMGRID)
  67. .cuda_funcs = {(void*)1},
  68. #endif
  69. .cuda_flags = {STARPU_CUDA_ASYNC},
  70. .nbuffers = 3,
  71. .modes = {STARPU_R, STARPU_R, STARPU_RW},
  72. // .modes = {STARPU_R, STARPU_R, STARPU_RW | STARPU_COMMUTE},
  73. .model = &chol_model_22,
  74. .color = 0x00ff00,
  75. };
  76. static void run_cholesky(starpu_data_handle_t **data_handles, int rank, int nodes)
  77. {
  78. unsigned k, m, n;
  79. unsigned unbound_prio = STARPU_MAX_PRIO == INT_MAX && STARPU_MIN_PRIO == INT_MIN;
  80. starpu_mpi_checkpoint_template_add_entry(checkpoint_p, STARPU_VALUE, &k, sizeof(unsigned), nblocks*nblocks+10, backup_function);
  81. starpu_mpi_checkpoint_template_freeze(checkpoint_p);
  82. _starpu_mpi_checkpoint_template_print(*checkpoint_p);
  83. for (k = 0; k < nblocks; k++)
  84. {
  85. starpu_iteration_push(k);
  86. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl11,
  87. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k) : STARPU_MAX_PRIO,
  88. STARPU_RW, data_handles[k][k],
  89. 0);
  90. for (m = k+1; m<nblocks; m++)
  91. {
  92. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl21,
  93. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - m) : (m == k+1)?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  94. STARPU_R, data_handles[k][k],
  95. STARPU_RW, data_handles[m][k],
  96. 0);
  97. // starpu_mpi_cache_flush(MPI_COMM_WORLD, data_handles[k][k]);
  98. // if (my_distrib(k, k, nodes) == rank)
  99. // starpu_data_wont_use(data_handles[k][k]);
  100. for (n = k+1; n<nblocks; n++)
  101. {
  102. if (n <= m)
  103. {
  104. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl22,
  105. 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,
  106. STARPU_R, data_handles[n][k],
  107. STARPU_R, data_handles[m][k],
  108. STARPU_RW, data_handles[m][n],
  109. // STARPU_RW | STARPU_COMMUTE, data_handles[m][n],
  110. 0);
  111. }
  112. }
  113. // starpu_mpi_cache_flush(MPI_COMM_WORLD, data_handles[m][k]);
  114. // if (my_distrib(m, k, nodes) == rank)
  115. // starpu_data_wont_use(data_handles[m][k]);
  116. }
  117. starpu_mpi_submit_checkpoint_template(*checkpoint_p);
  118. starpu_iteration_pop();
  119. }
  120. }
  121. /* TODO: generate from compiler polyhedral analysis of classical algorithm */
  122. static void run_cholesky_column(starpu_data_handle_t **data_handles, int rank, int nodes)
  123. {
  124. unsigned k, m, n;
  125. unsigned unbound_prio = STARPU_MAX_PRIO == INT_MAX && STARPU_MIN_PRIO == INT_MIN;
  126. /* Column */
  127. for (n = 0; n<nblocks; n++)
  128. {
  129. starpu_iteration_push(n);
  130. /* Row */
  131. for (m = n; m<nblocks; m++)
  132. {
  133. for (k = 0; k < n; k++)
  134. {
  135. /* Accumulate updates from TRSMs */
  136. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl22,
  137. 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,
  138. STARPU_R, data_handles[n][k],
  139. STARPU_R, data_handles[m][k],
  140. STARPU_RW | STARPU_COMMUTE, data_handles[m][n],
  141. 0);
  142. }
  143. k = n;
  144. if (m > n)
  145. {
  146. /* non-diagonal block, solve */
  147. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl21,
  148. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - m) : (m == k+1)?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  149. STARPU_R, data_handles[k][k],
  150. STARPU_RW, data_handles[m][k],
  151. 0);
  152. }
  153. else
  154. {
  155. /* diagonal block, factorize */
  156. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl11,
  157. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k) : STARPU_MAX_PRIO,
  158. STARPU_RW, data_handles[k][k],
  159. 0);
  160. }
  161. }
  162. starpu_iteration_pop();
  163. }
  164. /* Submit flushes, StarPU will fit them according to the progress */
  165. starpu_mpi_cache_flush_all_data(MPI_COMM_WORLD);
  166. for (m = 0; m < nblocks; m++)
  167. for (n = 0; n < nblocks ; n++)
  168. starpu_data_wont_use(data_handles[m][n]);
  169. }
  170. /* TODO: generate from compiler polyhedral analysis of classical algorithm */
  171. static void run_cholesky_antidiagonal(starpu_data_handle_t **data_handles, int rank, int nodes)
  172. {
  173. unsigned a, c;
  174. unsigned k, m, n;
  175. unsigned unbound_prio = STARPU_MAX_PRIO == INT_MAX && STARPU_MIN_PRIO == INT_MIN;
  176. /* double-antidiagonal number:
  177. * - a=0 contains (0,0) plus (1,0)
  178. * - a=1 contains (2,0), (1,1) plus (3,0), (2, 1)
  179. * - etc.
  180. */
  181. for (a = 0; a < nblocks; a++)
  182. {
  183. starpu_iteration_push(a);
  184. unsigned nfirst;
  185. if (2*a < nblocks)
  186. nfirst = 0;
  187. else
  188. nfirst = 2*a - (nblocks-1);
  189. /* column within first antidiagonal for a */
  190. for (n = nfirst; n <= a; n++)
  191. {
  192. /* row */
  193. m = 2*a-n;
  194. /* Accumulate updates from TRSMs */
  195. for (k = 0; k < n; k++)
  196. {
  197. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl22,
  198. 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,
  199. STARPU_R, data_handles[n][k],
  200. STARPU_R, data_handles[m][k],
  201. STARPU_RW | STARPU_COMMUTE, data_handles[m][n],
  202. 0);
  203. }
  204. /* k = n */
  205. if (n < a)
  206. {
  207. /* non-diagonal block, solve */
  208. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl21,
  209. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - m) : (m == k+1)?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  210. STARPU_R, data_handles[k][k],
  211. STARPU_RW, data_handles[m][k],
  212. 0);
  213. }
  214. else
  215. {
  216. /* diagonal block, factorize */
  217. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl11,
  218. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k) : STARPU_MAX_PRIO,
  219. STARPU_RW, data_handles[k][k],
  220. 0);
  221. }
  222. }
  223. /* column within second antidiagonal for a */
  224. for (n = nfirst; n <= a; n++)
  225. {
  226. /* row */
  227. m = 2*a-n + 1;
  228. if (m >= nblocks)
  229. /* Skip first item when even number of tiles */
  230. continue;
  231. /* Accumulate updates from TRSMs */
  232. for (k = 0; k < n; k++)
  233. {
  234. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl22,
  235. 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,
  236. STARPU_R, data_handles[n][k],
  237. STARPU_R, data_handles[m][k],
  238. STARPU_RW | STARPU_COMMUTE, data_handles[m][n],
  239. 0);
  240. }
  241. /* non-diagonal block, solve */
  242. k = n;
  243. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl21,
  244. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - m) : (m == k+1)?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  245. STARPU_R, data_handles[k][k],
  246. STARPU_RW, data_handles[m][k],
  247. 0);
  248. }
  249. starpu_iteration_pop();
  250. }
  251. /* Submit flushes, StarPU will fit them according to the progress */
  252. starpu_mpi_cache_flush_all_data(MPI_COMM_WORLD);
  253. for (m = 0; m < nblocks; m++)
  254. for (n = 0; n < nblocks ; n++)
  255. starpu_data_wont_use(data_handles[m][n]);
  256. }
  257. /* TODO: generate from compiler polyhedral analysis of classical algorithm */
  258. static void run_cholesky_prio(starpu_data_handle_t **data_handles, int rank, int nodes)
  259. {
  260. unsigned a;
  261. int k, m, n;
  262. unsigned unbound_prio = STARPU_MAX_PRIO == INT_MAX && STARPU_MIN_PRIO == INT_MIN;
  263. /*
  264. * This is basically similar to above, except that we shift k according to the priorities set in the algorithm, so that prio ~ 2*a or 2*a+1
  265. * double-antidiagonal number:
  266. * - a=0 contains (0,0) plus (1,0)
  267. * - a=1 contains (2,0), (1,1) plus (3,0), (2, 1)
  268. * - etc.
  269. */
  270. for (a = 0; a < 4*nblocks; a++)
  271. {
  272. starpu_iteration_push(a);
  273. for (k = 0; k < nblocks; k++)
  274. {
  275. n = k;
  276. /* Should be m = a-k-n; for potrf and trsm to respect
  277. priorities, but needs to be this for dependencies */
  278. m = a-2*k-n;
  279. if (m < 0 || m >= nblocks)
  280. continue;
  281. if (m == n)
  282. {
  283. /* diagonal block, factorize */
  284. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl11,
  285. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k) : STARPU_MAX_PRIO,
  286. STARPU_RW, data_handles[k][k],
  287. 0);
  288. }
  289. else
  290. {
  291. /* non-diagonal block, solve */
  292. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl21,
  293. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - m) : (m == k+1)?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  294. STARPU_R, data_handles[k][k],
  295. STARPU_RW, data_handles[m][k],
  296. 0);
  297. }
  298. /* column within antidiagonal for a */
  299. for (n = k + 1; n < nblocks; n++)
  300. {
  301. /* row */
  302. m = a-2*k-n;
  303. if (m >= n && m < nblocks)
  304. {
  305. /* Update */
  306. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl22,
  307. 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,
  308. STARPU_R, data_handles[n][k],
  309. STARPU_R, data_handles[m][k],
  310. STARPU_RW | STARPU_COMMUTE, data_handles[m][n],
  311. 0);
  312. }
  313. }
  314. }
  315. starpu_iteration_pop();
  316. }
  317. /* Submit flushes, StarPU will fit them according to the progress */
  318. starpu_mpi_cache_flush_all_data(MPI_COMM_WORLD);
  319. for (m = 0; m < nblocks; m++)
  320. for (n = 0; n < nblocks ; n++)
  321. starpu_data_wont_use(data_handles[m][n]);
  322. }
  323. /*
  324. * code to bootstrap the factorization
  325. * and construct the DAG
  326. */
  327. void dw_cholesky(float ***matA, unsigned ld, int rank, int nodes, double *timing, double *flops)
  328. {
  329. double start;
  330. double end;
  331. starpu_data_handle_t **data_handles;
  332. unsigned k, m, n;
  333. /* create all the DAG nodes */
  334. _nodes = nodes;
  335. starpu_malloc((void**)&checkpoint_p, sizeof(starpu_mpi_checkpoint_template_t));
  336. starpu_mpi_checkpoint_template_create(checkpoint_p, 13);
  337. data_handles = malloc(nblocks*sizeof(starpu_data_handle_t *));
  338. for(m=0 ; m<nblocks ; m++) data_handles[m] = malloc(nblocks*sizeof(starpu_data_handle_t));
  339. for (m = 0; m < nblocks; m++)
  340. {
  341. for(n = 0; n < nblocks ; n++)
  342. {
  343. int mpi_rank = my_distrib(m, n, nodes);
  344. if (mpi_rank == rank || (check && rank == 0))
  345. {
  346. //fprintf(stderr, "[%d] Owning data[%d][%d]\n", rank, n, m);
  347. starpu_matrix_data_register(&data_handles[m][n], STARPU_MAIN_RAM, (uintptr_t)matA[m][n],
  348. ld, size/nblocks, size/nblocks, sizeof(float));
  349. }
  350. #ifdef STARPU_DEVEL
  351. #warning TODO: make better test to only register what is needed
  352. #endif
  353. else
  354. {
  355. /* I don't own this index, but will need it for my computations */
  356. //fprintf(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, n, m);
  357. starpu_matrix_data_register(&data_handles[m][n], -1, (uintptr_t)NULL,
  358. ld, size/nblocks, size/nblocks, sizeof(float));
  359. }
  360. if (data_handles[m][n])
  361. {
  362. starpu_data_set_coordinates(data_handles[m][n], 2, n, m);
  363. starpu_mpi_data_register(data_handles[m][n], (m*nblocks)+n, mpi_rank);
  364. starpu_mpi_checkpoint_template_add_entry(checkpoint_p, STARPU_R, &data_handles[m][n], backup_function(mpi_rank));
  365. }
  366. }
  367. }
  368. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  369. starpu_mpi_barrier(MPI_COMM_WORLD);
  370. start = starpu_timing_now();
  371. switch (submission)
  372. {
  373. case TRIANGLES: run_cholesky(data_handles, rank, nodes); break;
  374. case COLUMNS: run_cholesky_column(data_handles, rank, nodes); break;
  375. case ANTIDIAGONALS: run_cholesky_antidiagonal(data_handles, rank, nodes); break;
  376. case PRIOS: run_cholesky_prio(data_handles, rank, nodes); break;
  377. default: STARPU_ABORT();
  378. }
  379. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  380. starpu_mpi_barrier(MPI_COMM_WORLD);
  381. end = starpu_timing_now();
  382. for (m = 0; m < nblocks; m++)
  383. {
  384. for(n = 0; n < nblocks ; n++)
  385. {
  386. /* Get back data on node 0 for the check */
  387. if (check && data_handles[m][n])
  388. starpu_mpi_get_data_on_node(MPI_COMM_WORLD, data_handles[m][n], 0);
  389. if (data_handles[m][n])
  390. starpu_data_unregister(data_handles[m][n]);
  391. }
  392. free(data_handles[m]);
  393. }
  394. free(data_handles);
  395. if (rank == 0)
  396. {
  397. *timing = end - start;
  398. *flops = (1.0f*size*size*size)/3.0f;
  399. }
  400. }
  401. void dw_cholesky_check_computation(float ***matA, int rank, int nodes, int *correctness, double *flops, double epsilon)
  402. {
  403. unsigned nn,mm,n,m;
  404. float *rmat = malloc(size*size*sizeof(float));
  405. for(n=0 ; n<nblocks ; n++)
  406. {
  407. for(m=0 ; m<nblocks ; m++)
  408. {
  409. for (nn = 0; nn < BLOCKSIZE; nn++)
  410. {
  411. for (mm = 0; mm < BLOCKSIZE; mm++)
  412. {
  413. rmat[mm+(m*BLOCKSIZE)+(nn+(n*BLOCKSIZE))*size] = matA[m][n][mm +nn*BLOCKSIZE];
  414. }
  415. }
  416. }
  417. }
  418. FPRINTF(stderr, "[%d] compute explicit LLt ...\n", rank);
  419. for (mm = 0; mm < size; mm++)
  420. {
  421. for (nn = 0; nn < size; nn++)
  422. {
  423. if (nn > mm)
  424. {
  425. rmat[mm+nn*size] = 0.0f; // debug
  426. }
  427. }
  428. }
  429. float *test_mat = malloc(size*size*sizeof(float));
  430. STARPU_ASSERT(test_mat);
  431. STARPU_SSYRK("L", "N", size, size, 1.0f,
  432. rmat, size, 0.0f, test_mat, size);
  433. FPRINTF(stderr, "[%d] comparing results ...\n", rank);
  434. if (display)
  435. {
  436. for (mm = 0; mm < size; mm++)
  437. {
  438. for (nn = 0; nn < size; nn++)
  439. {
  440. if (nn <= mm)
  441. {
  442. printf("%2.2f\t", test_mat[mm +nn*size]);
  443. }
  444. else
  445. {
  446. printf(".\t");
  447. }
  448. }
  449. printf("\n");
  450. }
  451. }
  452. *correctness = 1;
  453. for(n = 0; n < nblocks ; n++)
  454. {
  455. for (m = 0; m < nblocks; m++)
  456. {
  457. for (nn = BLOCKSIZE*n ; nn < BLOCKSIZE*(n+1); nn++)
  458. {
  459. for (mm = BLOCKSIZE*m ; mm < BLOCKSIZE*(m+1); mm++)
  460. {
  461. if (nn <= mm)
  462. {
  463. float orig = (1.0f/(1.0f+nn+mm)) + ((nn == mm)?1.0f*size:0.0f);
  464. float err = fabsf(test_mat[mm +nn*size] - orig) / orig;
  465. if (err > epsilon)
  466. {
  467. FPRINTF(stderr, "[%d] Error[%u, %u] --> %2.20f != %2.20f (err %2.20f)\n", rank, nn, mm, test_mat[mm +nn*size], orig, err);
  468. *correctness = 0;
  469. *flops = 0;
  470. break;
  471. }
  472. }
  473. }
  474. }
  475. }
  476. }
  477. free(rmat);
  478. free(test_mat);
  479. }