plu_example.c 15 KB

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