mpi_cholesky_codelets.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. /* TODO: generate from compiler polyhedral analysis of classical algorithm */
  242. static void run_cholesky_prio(starpu_data_handle_t **data_handles, int rank, int nodes)
  243. {
  244. unsigned a;
  245. int k, m, n;
  246. unsigned unbound_prio = STARPU_MAX_PRIO == INT_MAX && STARPU_MIN_PRIO == INT_MIN;
  247. /*
  248. * 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
  249. * double-antidiagonal number:
  250. * - a=0 contains (0,0) plus (1,0)
  251. * - a=1 contains (2,0), (1,1) plus (3,0), (2, 1)
  252. * - etc.
  253. */
  254. for (a = 0; a < 4*nblocks; a++)
  255. {
  256. starpu_iteration_push(a);
  257. for (k = 0; k < nblocks; k++)
  258. {
  259. n = k;
  260. /* Should be m = a-k-n; for potrf and trsm to respect
  261. priorities, but needs to be this for dependencies */
  262. m = a-2*k-n;
  263. if (m < 0 || m >= nblocks)
  264. continue;
  265. if (m == n)
  266. {
  267. /* diagonal block, factorize */
  268. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl11,
  269. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k) : STARPU_MAX_PRIO,
  270. STARPU_RW, data_handles[k][k],
  271. 0);
  272. }
  273. else
  274. {
  275. /* non-diagonal block, solve */
  276. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl21,
  277. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - m) : (m == k+1)?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  278. STARPU_R, data_handles[k][k],
  279. STARPU_RW, data_handles[m][k],
  280. 0);
  281. }
  282. /* column within antidiagonal for a */
  283. for (n = k + 1; n < nblocks; n++)
  284. {
  285. /* row */
  286. m = a-2*k-n;
  287. if (m >= n && m < nblocks)
  288. {
  289. /* Update */
  290. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl22,
  291. 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,
  292. STARPU_R, data_handles[n][k],
  293. STARPU_R, data_handles[m][k],
  294. STARPU_RW | STARPU_COMMUTE, data_handles[m][n],
  295. 0);
  296. }
  297. }
  298. }
  299. starpu_iteration_pop();
  300. }
  301. /* Submit flushes, StarPU will fit them according to the progress */
  302. starpu_mpi_cache_flush_all_data(MPI_COMM_WORLD);
  303. for (m = 0; m < nblocks; m++)
  304. for (n = 0; n < nblocks ; n++)
  305. starpu_data_wont_use(data_handles[m][n]);
  306. }
  307. /*
  308. * code to bootstrap the factorization
  309. * and construct the DAG
  310. */
  311. void dw_cholesky(float ***matA, unsigned ld, int rank, int nodes, double *timing, double *flops)
  312. {
  313. double start;
  314. double end;
  315. starpu_data_handle_t **data_handles;
  316. unsigned k, m, n;
  317. /* create all the DAG nodes */
  318. data_handles = malloc(nblocks*sizeof(starpu_data_handle_t *));
  319. for(m=0 ; m<nblocks ; m++) data_handles[m] = malloc(nblocks*sizeof(starpu_data_handle_t));
  320. for (m = 0; m < nblocks; m++)
  321. {
  322. for(n = 0; n < nblocks ; n++)
  323. {
  324. int mpi_rank = my_distrib(m, n, nodes);
  325. if (mpi_rank == rank || (check && rank == 0))
  326. {
  327. //fprintf(stderr, "[%d] Owning data[%d][%d]\n", rank, n, m);
  328. starpu_matrix_data_register(&data_handles[m][n], STARPU_MAIN_RAM, (uintptr_t)matA[m][n],
  329. ld, size/nblocks, size/nblocks, sizeof(float));
  330. }
  331. #ifdef STARPU_DEVEL
  332. #warning TODO: make better test to only register what is needed
  333. #endif
  334. else
  335. {
  336. /* I don't own this index, but will need it for my computations */
  337. //fprintf(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, n, m);
  338. starpu_matrix_data_register(&data_handles[m][n], -1, (uintptr_t)NULL,
  339. ld, size/nblocks, size/nblocks, sizeof(float));
  340. }
  341. if (data_handles[m][n])
  342. {
  343. starpu_data_set_coordinates(data_handles[m][n], 2, n, m);
  344. starpu_mpi_data_register(data_handles[m][n], (m*nblocks)+n, mpi_rank);
  345. }
  346. }
  347. }
  348. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  349. starpu_mpi_barrier(MPI_COMM_WORLD);
  350. start = starpu_timing_now();
  351. switch (submission)
  352. {
  353. case TRIANGLES: run_cholesky(data_handles, rank, nodes); break;
  354. case COLUMNS: run_cholesky_column(data_handles, rank, nodes); break;
  355. case ANTIDIAGONALS: run_cholesky_antidiagonal(data_handles, rank, nodes); break;
  356. case PRIOS: run_cholesky_prio(data_handles, rank, nodes); break;
  357. default: STARPU_ABORT();
  358. }
  359. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  360. starpu_mpi_barrier(MPI_COMM_WORLD);
  361. end = starpu_timing_now();
  362. for (m = 0; m < nblocks; m++)
  363. {
  364. for(n = 0; n < nblocks ; n++)
  365. {
  366. /* Get back data on node 0 for the check */
  367. if (check && data_handles[m][n])
  368. starpu_mpi_get_data_on_node(MPI_COMM_WORLD, data_handles[m][n], 0);
  369. if (data_handles[m][n])
  370. starpu_data_unregister(data_handles[m][n]);
  371. }
  372. free(data_handles[m]);
  373. }
  374. free(data_handles);
  375. if (rank == 0)
  376. {
  377. *timing = end - start;
  378. *flops = (1.0f*size*size*size)/3.0f;
  379. }
  380. }
  381. void dw_cholesky_check_computation(float ***matA, int rank, int nodes, int *correctness, double *flops, double epsilon)
  382. {
  383. unsigned nn,mm,n,m;
  384. float *rmat = malloc(size*size*sizeof(float));
  385. for(n=0 ; n<nblocks ; n++)
  386. {
  387. for(m=0 ; m<nblocks ; m++)
  388. {
  389. for (nn = 0; nn < BLOCKSIZE; nn++)
  390. {
  391. for (mm = 0; mm < BLOCKSIZE; mm++)
  392. {
  393. rmat[mm+(m*BLOCKSIZE)+(nn+(n*BLOCKSIZE))*size] = matA[m][n][mm +nn*BLOCKSIZE];
  394. }
  395. }
  396. }
  397. }
  398. FPRINTF(stderr, "[%d] compute explicit LLt ...\n", rank);
  399. for (mm = 0; mm < size; mm++)
  400. {
  401. for (nn = 0; nn < size; nn++)
  402. {
  403. if (nn > mm)
  404. {
  405. rmat[mm+nn*size] = 0.0f; // debug
  406. }
  407. }
  408. }
  409. float *test_mat = malloc(size*size*sizeof(float));
  410. STARPU_ASSERT(test_mat);
  411. STARPU_SSYRK("L", "N", size, size, 1.0f,
  412. rmat, size, 0.0f, test_mat, size);
  413. FPRINTF(stderr, "[%d] comparing results ...\n", rank);
  414. if (display)
  415. {
  416. for (mm = 0; mm < size; mm++)
  417. {
  418. for (nn = 0; nn < size; nn++)
  419. {
  420. if (nn <= mm)
  421. {
  422. printf("%2.2f\t", test_mat[mm +nn*size]);
  423. }
  424. else
  425. {
  426. printf(".\t");
  427. }
  428. }
  429. printf("\n");
  430. }
  431. }
  432. *correctness = 1;
  433. for(n = 0; n < nblocks ; n++)
  434. {
  435. for (m = 0; m < nblocks; m++)
  436. {
  437. for (nn = BLOCKSIZE*n ; nn < BLOCKSIZE*(n+1); nn++)
  438. {
  439. for (mm = BLOCKSIZE*m ; mm < BLOCKSIZE*(m+1); mm++)
  440. {
  441. if (nn <= mm)
  442. {
  443. float orig = (1.0f/(1.0f+nn+mm)) + ((nn == mm)?1.0f*size:0.0f);
  444. float err = fabsf(test_mat[mm +nn*size] - orig) / orig;
  445. if (err > epsilon)
  446. {
  447. FPRINTF(stderr, "[%d] Error[%u, %u] --> %2.20f != %2.20f (err %2.20f)\n", rank, nn, mm, test_mat[mm +nn*size], orig, err);
  448. *correctness = 0;
  449. *flops = 0;
  450. break;
  451. }
  452. }
  453. }
  454. }
  455. }
  456. }
  457. free(rmat);
  458. free(test_mat);
  459. }