plu_example.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2013 Thibaut Lambert
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include <math.h>
  22. #include <starpu.h>
  23. #include "pxlu.h"
  24. //#include "pxlu_kernels.h"
  25. #ifdef STARPU_HAVE_LIBNUMA
  26. #include <numaif.h>
  27. #endif
  28. static unsigned long size = 4096;
  29. static unsigned nblocks = 16;
  30. static unsigned check = 0;
  31. static int p = 1;
  32. static int q = 1;
  33. static unsigned display = 0;
  34. static unsigned no_prio = 0;
  35. #ifdef STARPU_HAVE_LIBNUMA
  36. static unsigned numa = 0;
  37. #endif
  38. static size_t allocated_memory = 0;
  39. static size_t allocated_memory_extra = 0;
  40. static starpu_data_handle_t *dataA_handles;
  41. static TYPE **dataA;
  42. /* In order to implement the distributed LU decomposition, we allocate
  43. * temporary buffers */
  44. #ifdef SINGLE_TMP11
  45. static starpu_data_handle_t tmp_11_block_handle;
  46. static TYPE *tmp_11_block;
  47. #else
  48. static starpu_data_handle_t *tmp_11_block_handles;
  49. static TYPE **tmp_11_block;
  50. #endif
  51. #ifdef SINGLE_TMP1221
  52. static starpu_data_handle_t *tmp_12_block_handles;
  53. static TYPE **tmp_12_block;
  54. static starpu_data_handle_t *tmp_21_block_handles;
  55. static TYPE **tmp_21_block;
  56. #else
  57. static starpu_data_handle_t *(tmp_12_block_handles[2]);
  58. static TYPE **(tmp_12_block[2]);
  59. static starpu_data_handle_t *(tmp_21_block_handles[2]);
  60. static TYPE **(tmp_21_block[2]);
  61. #endif
  62. static void parse_args(int rank, int argc, char **argv)
  63. {
  64. (void)rank;
  65. int i;
  66. for (i = 1; i < argc; i++)
  67. {
  68. if (strcmp(argv[i], "-size") == 0)
  69. {
  70. char *argptr;
  71. size = strtol(argv[++i], &argptr, 10);
  72. }
  73. if (strcmp(argv[i], "-nblocks") == 0)
  74. {
  75. char *argptr;
  76. nblocks = strtol(argv[++i], &argptr, 10);
  77. }
  78. if (strcmp(argv[i], "-check") == 0)
  79. {
  80. check = 1;
  81. }
  82. if (strcmp(argv[i], "-display") == 0)
  83. {
  84. display = 1;
  85. }
  86. if (strcmp(argv[i], "-numa") == 0)
  87. {
  88. #ifdef STARPU_HAVE_LIBNUMA
  89. numa = 1;
  90. #else
  91. if (rank == 0)
  92. fprintf(stderr, "Warning: libnuma is not available\n");
  93. #endif
  94. }
  95. if (strcmp(argv[i], "-p") == 0)
  96. {
  97. char *argptr;
  98. p = strtol(argv[++i], &argptr, 10);
  99. }
  100. if (strcmp(argv[i], "-q") == 0)
  101. {
  102. char *argptr;
  103. q = strtol(argv[++i], &argptr, 10);
  104. }
  105. if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "--help") == 0)
  106. {
  107. fprintf(stderr,"usage: %s [-size n] [-nblocks b] [-check] [-display] [-numa] [-p p] [-q q]\n", argv[0]);
  108. fprintf(stderr,"\np * q must be equal to the number of MPI nodes\n");
  109. exit(0);
  110. }
  111. }
  112. }
  113. unsigned STARPU_PLU(display_flag)(void)
  114. {
  115. return display;
  116. }
  117. static void fill_block_with_random(TYPE *blockptr, unsigned psize, unsigned pnblocks)
  118. {
  119. const unsigned block_size = (psize/pnblocks);
  120. unsigned i, j;
  121. for (i = 0; i < block_size; i++)
  122. for (j = 0; j < block_size; j++)
  123. {
  124. blockptr[j+i*block_size] = (TYPE)starpu_drand48();
  125. }
  126. }
  127. #ifdef SINGLE_TMP11
  128. starpu_data_handle_t STARPU_PLU(get_tmp_11_block_handle)(void)
  129. {
  130. return tmp_11_block_handle;
  131. }
  132. #else
  133. starpu_data_handle_t STARPU_PLU(get_tmp_11_block_handle)(unsigned k)
  134. {
  135. return tmp_11_block_handles[k];
  136. }
  137. #endif
  138. #ifdef SINGLE_TMP1221
  139. starpu_data_handle_t STARPU_PLU(get_tmp_12_block_handle)(unsigned j)
  140. {
  141. return tmp_12_block_handles[j];
  142. }
  143. starpu_data_handle_t STARPU_PLU(get_tmp_21_block_handle)(unsigned i)
  144. {
  145. return tmp_21_block_handles[i];
  146. }
  147. #else
  148. starpu_data_handle_t STARPU_PLU(get_tmp_12_block_handle)(unsigned j, unsigned k)
  149. {
  150. return tmp_12_block_handles[k%2][j];
  151. }
  152. starpu_data_handle_t STARPU_PLU(get_tmp_21_block_handle)(unsigned i, unsigned k)
  153. {
  154. return tmp_21_block_handles[k%2][i];
  155. }
  156. #endif
  157. static unsigned tmp_11_block_is_needed(int rank, unsigned pnblocks, unsigned k)
  158. {
  159. (void)rank;
  160. (void)pnblocks;
  161. (void)k;
  162. return 1;
  163. }
  164. static unsigned tmp_12_block_is_needed(int rank, unsigned pnblocks, unsigned j)
  165. {
  166. unsigned i;
  167. for (i = 1; i < pnblocks; i++)
  168. {
  169. if (get_block_rank(i, j) == rank)
  170. return 1;
  171. }
  172. return 0;
  173. }
  174. static unsigned tmp_21_block_is_needed(int rank, unsigned pnblocks, unsigned i)
  175. {
  176. unsigned j;
  177. for (j = 1; j < pnblocks; j++)
  178. {
  179. if (get_block_rank(i, j) == rank)
  180. return 1;
  181. }
  182. return 0;
  183. }
  184. static void init_matrix(int rank)
  185. {
  186. #ifdef STARPU_HAVE_LIBNUMA
  187. if (numa)
  188. {
  189. fprintf(stderr, "Using INTERLEAVE policy\n");
  190. unsigned long nodemask = ((1<<0)|(1<<1));
  191. int ret = set_mempolicy(MPOL_INTERLEAVE, &nodemask, 3);
  192. if (ret)
  193. perror("set_mempolicy failed");
  194. }
  195. #endif
  196. /* Allocate a grid of data handles, not all of them have to be allocated later on */
  197. dataA_handles = calloc(nblocks*nblocks, sizeof(starpu_data_handle_t));
  198. dataA = calloc(nblocks*nblocks, sizeof(TYPE *));
  199. allocated_memory_extra += nblocks*nblocks*(sizeof(starpu_data_handle_t) + sizeof(TYPE *));
  200. size_t blocksize = (size_t)(size/nblocks)*(size/nblocks)*sizeof(TYPE);
  201. /* Allocate all the blocks that belong to this mpi node */
  202. unsigned long i,j;
  203. for (j = 0; j < nblocks; j++)
  204. {
  205. for (i = 0; i < nblocks; i++)
  206. {
  207. TYPE **blockptr = &dataA[j+i*nblocks];
  208. // starpu_data_handle_t *handleptr = &dataA_handles[j+nblocks*i];
  209. starpu_data_handle_t *handleptr = &dataA_handles[j+nblocks*i];
  210. if (get_block_rank(i, j) == rank)
  211. {
  212. /* This blocks should be treated by the current MPI process */
  213. /* Allocate and fill it */
  214. starpu_malloc((void **)blockptr, blocksize);
  215. allocated_memory += blocksize;
  216. //fprintf(stderr, "Rank %d : fill block (i = %d, j = %d)\n", rank, i, j);
  217. fill_block_with_random(*blockptr, size, nblocks);
  218. //fprintf(stderr, "Rank %d : fill block (i = %d, j = %d)\n", rank, i, j);
  219. if (i == j)
  220. {
  221. unsigned tmp;
  222. for (tmp = 0; tmp < size/nblocks; tmp++)
  223. {
  224. (*blockptr)[tmp*((size/nblocks)+1)] += (TYPE)10*nblocks;
  225. }
  226. }
  227. /* Register it to StarPU */
  228. starpu_matrix_data_register(handleptr, STARPU_MAIN_RAM,
  229. (uintptr_t)*blockptr, size/nblocks,
  230. size/nblocks, size/nblocks, sizeof(TYPE));
  231. starpu_data_set_coordinates(*handleptr, 2, j, i);
  232. }
  233. else
  234. {
  235. *blockptr = STARPU_POISON_PTR;
  236. *handleptr = STARPU_POISON_PTR;
  237. }
  238. }
  239. }
  240. /* Allocate the temporary buffers required for the distributed algorithm */
  241. unsigned k;
  242. /* tmp buffer 11 */
  243. #ifdef SINGLE_TMP11
  244. starpu_malloc((void **)&tmp_11_block, blocksize);
  245. allocated_memory_extra += blocksize;
  246. starpu_matrix_data_register(&tmp_11_block_handle, STARPU_MAIN_RAM, (uintptr_t)tmp_11_block,
  247. size/nblocks, size/nblocks, size/nblocks, sizeof(TYPE));
  248. #else
  249. tmp_11_block_handles = calloc(nblocks, sizeof(starpu_data_handle_t));
  250. tmp_11_block = calloc(nblocks, sizeof(TYPE *));
  251. allocated_memory_extra += nblocks*(sizeof(starpu_data_handle_t) + sizeof(TYPE *));
  252. for (k = 0; k < nblocks; k++)
  253. {
  254. if (tmp_11_block_is_needed(rank, nblocks, k))
  255. {
  256. starpu_malloc((void **)&tmp_11_block[k], blocksize);
  257. allocated_memory_extra += blocksize;
  258. STARPU_ASSERT(tmp_11_block[k]);
  259. starpu_matrix_data_register(&tmp_11_block_handles[k], STARPU_MAIN_RAM,
  260. (uintptr_t)tmp_11_block[k],
  261. size/nblocks, size/nblocks, size/nblocks, sizeof(TYPE));
  262. }
  263. }
  264. #endif
  265. /* tmp buffers 12 and 21 */
  266. #ifdef SINGLE_TMP1221
  267. tmp_12_block_handles = calloc(nblocks, sizeof(starpu_data_handle_t));
  268. tmp_21_block_handles = calloc(nblocks, sizeof(starpu_data_handle_t));
  269. tmp_12_block = calloc(nblocks, sizeof(TYPE *));
  270. tmp_21_block = calloc(nblocks, sizeof(TYPE *));
  271. allocated_memory_extra += 2*nblocks*(sizeof(starpu_data_handle_t) + sizeof(TYPE *));
  272. #else
  273. for (i = 0; i < 2; i++)
  274. {
  275. tmp_12_block_handles[i] = calloc(nblocks, sizeof(starpu_data_handle_t));
  276. tmp_21_block_handles[i] = calloc(nblocks, sizeof(starpu_data_handle_t));
  277. tmp_12_block[i] = calloc(nblocks, sizeof(TYPE *));
  278. tmp_21_block[i] = calloc(nblocks, sizeof(TYPE *));
  279. allocated_memory_extra += 2*nblocks*(sizeof(starpu_data_handle_t) + sizeof(TYPE *));
  280. }
  281. #endif
  282. for (k = 0; k < nblocks; k++)
  283. {
  284. #ifdef SINGLE_TMP1221
  285. if (tmp_12_block_is_needed(rank, nblocks, k))
  286. {
  287. starpu_malloc((void **)&tmp_12_block[k], blocksize);
  288. allocated_memory_extra += blocksize;
  289. STARPU_ASSERT(tmp_12_block[k]);
  290. starpu_matrix_data_register(&tmp_12_block_handles[k], STARPU_MAIN_RAM,
  291. (uintptr_t)tmp_12_block[k],
  292. size/nblocks, size/nblocks, size/nblocks, sizeof(TYPE));
  293. }
  294. if (tmp_21_block_is_needed(rank, nblocks, k))
  295. {
  296. starpu_malloc((void **)&tmp_21_block[k], blocksize);
  297. allocated_memory_extra += blocksize;
  298. STARPU_ASSERT(tmp_21_block[k]);
  299. starpu_matrix_data_register(&tmp_21_block_handles[k], STARPU_MAIN_RAM,
  300. (uintptr_t)tmp_21_block[k],
  301. size/nblocks, size/nblocks, size/nblocks, sizeof(TYPE));
  302. }
  303. #else
  304. for (i = 0; i < 2; i++)
  305. {
  306. if (tmp_12_block_is_needed(rank, nblocks, k))
  307. {
  308. starpu_malloc((void **)&tmp_12_block[i][k], blocksize);
  309. allocated_memory_extra += blocksize;
  310. STARPU_ASSERT(tmp_12_block[i][k]);
  311. starpu_matrix_data_register(&tmp_12_block_handles[i][k], STARPU_MAIN_RAM,
  312. (uintptr_t)tmp_12_block[i][k],
  313. size/nblocks, size/nblocks, size/nblocks, sizeof(TYPE));
  314. }
  315. if (tmp_21_block_is_needed(rank, nblocks, k))
  316. {
  317. starpu_malloc((void **)&tmp_21_block[i][k], blocksize);
  318. allocated_memory_extra += blocksize;
  319. STARPU_ASSERT(tmp_21_block[i][k]);
  320. starpu_matrix_data_register(&tmp_21_block_handles[i][k], STARPU_MAIN_RAM,
  321. (uintptr_t)tmp_21_block[i][k],
  322. size/nblocks, size/nblocks, size/nblocks, sizeof(TYPE));
  323. }
  324. }
  325. #endif
  326. }
  327. //display_all_blocks(nblocks, size/nblocks);
  328. }
  329. TYPE *STARPU_PLU(get_block)(unsigned i, unsigned j)
  330. {
  331. return dataA[j+i*nblocks];
  332. }
  333. int get_block_rank(unsigned i, unsigned j)
  334. {
  335. /* Take a 2D block cyclic distribution */
  336. /* NB: p (resp. q) is for "direction" i (resp. j) */
  337. return (j % q) * p + (i % p);
  338. }
  339. starpu_data_handle_t STARPU_PLU(get_block_handle)(unsigned i, unsigned j)
  340. {
  341. return dataA_handles[j+i*nblocks];
  342. }
  343. static void display_grid(int rank, unsigned pnblocks)
  344. {
  345. if (!display)
  346. return;
  347. //if (rank == 0)
  348. {
  349. fprintf(stderr, "2D grid layout (Rank %d): \n", rank);
  350. unsigned i, j;
  351. for (j = 0; j < pnblocks; j++)
  352. {
  353. for (i = 0; i < pnblocks; i++)
  354. {
  355. TYPE *blockptr = STARPU_PLU(get_block)(i, j);
  356. starpu_data_handle_t handle = STARPU_PLU(get_block_handle)(i, j);
  357. fprintf(stderr, "%d (data %p handle %p)", get_block_rank(i, j), blockptr, handle);
  358. }
  359. fprintf(stderr, "\n");
  360. }
  361. }
  362. }
  363. int main(int argc, char **argv)
  364. {
  365. int rank;
  366. int world_size;
  367. int ret;
  368. /*
  369. * Initialization
  370. */
  371. int thread_support;
  372. if (MPI_Init_thread(&argc, &argv, MPI_THREAD_SERIALIZED, &thread_support) != MPI_SUCCESS)
  373. {
  374. fprintf(stderr,"MPI_Init_thread failed\n");
  375. exit(1);
  376. }
  377. if (thread_support == MPI_THREAD_FUNNELED)
  378. fprintf(stderr,"Warning: MPI only has funneled thread support, not serialized, hoping this will work\n");
  379. if (thread_support < MPI_THREAD_FUNNELED)
  380. fprintf(stderr,"Warning: MPI does not have thread support!\n");
  381. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  382. starpu_mpi_comm_size(MPI_COMM_WORLD, &world_size);
  383. starpu_srand48((long int)time(NULL));
  384. parse_args(rank, argc, argv);
  385. ret = starpu_mpi_init_conf(NULL, NULL, 0, MPI_COMM_WORLD, NULL);
  386. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  387. /* We disable sequential consistency in this example */
  388. starpu_data_set_default_sequential_consistency_flag(0);
  389. STARPU_ASSERT(p*q == world_size);
  390. starpu_cublas_init();
  391. int barrier_ret = MPI_Barrier(MPI_COMM_WORLD);
  392. STARPU_ASSERT(barrier_ret == MPI_SUCCESS);
  393. /*
  394. * Problem Init
  395. */
  396. init_matrix(rank);
  397. fprintf(stderr, "Rank %d: allocated (%d + %d) MB = %d MB\n", rank,
  398. (int)(allocated_memory/(1024*1024)),
  399. (int)(allocated_memory_extra/(1024*1024)),
  400. (int)((allocated_memory+allocated_memory_extra)/(1024*1024)));
  401. display_grid(rank, nblocks);
  402. TYPE *a_r = NULL;
  403. // STARPU_PLU(display_data_content)(a_r, size);
  404. if (check)
  405. {
  406. TYPE *x, *y;
  407. x = calloc(size, sizeof(TYPE));
  408. STARPU_ASSERT(x);
  409. y = calloc(size, sizeof(TYPE));
  410. STARPU_ASSERT(y);
  411. if (rank == 0)
  412. {
  413. unsigned ind;
  414. for (ind = 0; ind < size; ind++)
  415. x[ind] = (TYPE)starpu_drand48();
  416. }
  417. a_r = STARPU_PLU(reconstruct_matrix)(size, nblocks);
  418. if (rank == 0)
  419. STARPU_PLU(display_data_content)(a_r, size);
  420. // STARPU_PLU(compute_ax)(size, x, y, nblocks, rank);
  421. free(x);
  422. free(y);
  423. }
  424. barrier_ret = MPI_Barrier(MPI_COMM_WORLD);
  425. STARPU_ASSERT(barrier_ret == MPI_SUCCESS);
  426. double timing = STARPU_PLU(plu_main)(nblocks, rank, world_size, no_prio);
  427. /*
  428. * Report performance
  429. */
  430. int reduce_ret;
  431. double min_timing = timing;
  432. double max_timing = timing;
  433. double sum_timing = timing;
  434. reduce_ret = MPI_Reduce(&timing, &min_timing, 1, MPI_DOUBLE, MPI_MIN, 0, MPI_COMM_WORLD);
  435. STARPU_ASSERT(reduce_ret == MPI_SUCCESS);
  436. reduce_ret = MPI_Reduce(&timing, &max_timing, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
  437. STARPU_ASSERT(reduce_ret == MPI_SUCCESS);
  438. reduce_ret = MPI_Reduce(&timing, &sum_timing, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
  439. STARPU_ASSERT(reduce_ret == MPI_SUCCESS);
  440. if (rank == 0)
  441. {
  442. fprintf(stderr, "Computation took: %f ms\n", max_timing/1000);
  443. fprintf(stderr, "\tMIN : %f ms\n", min_timing/1000);
  444. fprintf(stderr, "\tMAX : %f ms\n", max_timing/1000);
  445. fprintf(stderr, "\tAVG : %f ms\n", sum_timing/(world_size*1000));
  446. unsigned n = size;
  447. double flop = (2.0f*n*n*n)/3.0f;
  448. fprintf(stderr, "Synthetic GFlops : %2.2f\n", (flop/max_timing/1000.0f));
  449. }
  450. /*
  451. * Test Result Correctness
  452. */
  453. if (check)
  454. {
  455. /*
  456. * Compute || A - LU ||
  457. */
  458. STARPU_PLU(compute_lu_matrix)(size, nblocks, a_r);
  459. #if 0
  460. /*
  461. * Compute || Ax - LUx ||
  462. */
  463. unsigned ind;
  464. y2 = calloc(size, sizeof(TYPE));
  465. STARPU_ASSERT(y);
  466. if (rank == 0)
  467. {
  468. for (ind = 0; ind < size; ind++)
  469. {
  470. y2[ind] = (TYPE)0.0;
  471. }
  472. }
  473. STARPU_PLU(compute_lux)(size, x, y2, nblocks, rank);
  474. /* Compute y2 = y2 - y */
  475. CPU_AXPY(size, -1.0, y, 1, y2, 1);
  476. TYPE err = CPU_ASUM(size, y2, 1);
  477. int max = CPU_IAMAX(size, y2, 1);
  478. fprintf(stderr, "(A - LU)X Avg error : %e\n", err/(size*size));
  479. fprintf(stderr, "(A - LU)X Max error : %e\n", y2[max]);
  480. #endif
  481. }
  482. /*
  483. * Termination
  484. */
  485. barrier_ret = MPI_Barrier(MPI_COMM_WORLD);
  486. STARPU_ASSERT(barrier_ret == MPI_SUCCESS);
  487. starpu_cublas_shutdown();
  488. starpu_mpi_shutdown();
  489. #if 0
  490. MPI_Finalize();
  491. #endif
  492. return 0;
  493. }