plu_example.c 15 KB

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