mpi_cholesky_codelets.c 15 KB

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