plu_example.c 15 KB

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