plu_example.c 15 KB

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