plu_example.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2013 Inria
  4. * Copyright (C) 2010-2011,2013-2015,2017 Université de Bordeaux
  5. * Copyright (C) 2010-2013,2015-2017 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. #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. }
  114. unsigned STARPU_PLU(display_flag)(void)
  115. {
  116. return display;
  117. }
  118. static void fill_block_with_random(TYPE *blockptr, unsigned psize, unsigned pnblocks)
  119. {
  120. const unsigned block_size = (psize/pnblocks);
  121. unsigned i, j;
  122. for (i = 0; i < block_size; i++)
  123. for (j = 0; j < block_size; j++)
  124. {
  125. blockptr[j+i*block_size] = (TYPE)starpu_drand48();
  126. }
  127. }
  128. #ifdef SINGLE_TMP11
  129. starpu_data_handle_t STARPU_PLU(get_tmp_11_block_handle)(void)
  130. {
  131. return tmp_11_block_handle;
  132. }
  133. #else
  134. starpu_data_handle_t STARPU_PLU(get_tmp_11_block_handle)(unsigned k)
  135. {
  136. return tmp_11_block_handles[k];
  137. }
  138. #endif
  139. #ifdef SINGLE_TMP1221
  140. starpu_data_handle_t STARPU_PLU(get_tmp_12_block_handle)(unsigned j)
  141. {
  142. return tmp_12_block_handles[j];
  143. }
  144. starpu_data_handle_t STARPU_PLU(get_tmp_21_block_handle)(unsigned i)
  145. {
  146. return tmp_21_block_handles[i];
  147. }
  148. #else
  149. starpu_data_handle_t STARPU_PLU(get_tmp_12_block_handle)(unsigned j, unsigned k)
  150. {
  151. return tmp_12_block_handles[k%2][j];
  152. }
  153. starpu_data_handle_t STARPU_PLU(get_tmp_21_block_handle)(unsigned i, unsigned k)
  154. {
  155. return tmp_21_block_handles[k%2][i];
  156. }
  157. #endif
  158. static unsigned tmp_11_block_is_needed(int rank, unsigned pnblocks, unsigned k)
  159. {
  160. (void)rank;
  161. (void)pnblocks;
  162. (void)k;
  163. return 1;
  164. }
  165. static unsigned tmp_12_block_is_needed(int rank, unsigned pnblocks, unsigned j)
  166. {
  167. unsigned i;
  168. for (i = 1; i < pnblocks; i++)
  169. {
  170. if (get_block_rank(i, j) == rank)
  171. return 1;
  172. }
  173. return 0;
  174. }
  175. static unsigned tmp_21_block_is_needed(int rank, unsigned pnblocks, unsigned i)
  176. {
  177. unsigned j;
  178. for (j = 1; j < pnblocks; j++)
  179. {
  180. if (get_block_rank(i, j) == rank)
  181. return 1;
  182. }
  183. return 0;
  184. }
  185. static void init_matrix(int rank)
  186. {
  187. #ifdef STARPU_HAVE_LIBNUMA
  188. if (numa)
  189. {
  190. fprintf(stderr, "Using INTERLEAVE policy\n");
  191. unsigned long nodemask = ((1<<0)|(1<<1));
  192. int ret = set_mempolicy(MPOL_INTERLEAVE, &nodemask, 3);
  193. if (ret)
  194. perror("set_mempolicy failed");
  195. }
  196. #endif
  197. /* Allocate a grid of data handles, not all of them have to be allocated later on */
  198. dataA_handles = calloc(nblocks*nblocks, sizeof(starpu_data_handle_t));
  199. dataA = calloc(nblocks*nblocks, sizeof(TYPE *));
  200. allocated_memory_extra += nblocks*nblocks*(sizeof(starpu_data_handle_t) + sizeof(TYPE *));
  201. size_t blocksize = (size_t)(size/nblocks)*(size/nblocks)*sizeof(TYPE);
  202. /* Allocate all the blocks that belong to this mpi node */
  203. unsigned long i,j;
  204. for (j = 0; j < nblocks; j++)
  205. {
  206. for (i = 0; i < nblocks; i++)
  207. {
  208. TYPE **blockptr = &dataA[j+i*nblocks];
  209. // starpu_data_handle_t *handleptr = &dataA_handles[j+nblocks*i];
  210. starpu_data_handle_t *handleptr = &dataA_handles[j+nblocks*i];
  211. if (get_block_rank(i, j) == rank)
  212. {
  213. /* This blocks should be treated by the current MPI process */
  214. /* Allocate and fill it */
  215. starpu_malloc((void **)blockptr, blocksize);
  216. allocated_memory += blocksize;
  217. //fprintf(stderr, "Rank %d : fill block (i = %d, j = %d)\n", rank, i, j);
  218. fill_block_with_random(*blockptr, size, nblocks);
  219. //fprintf(stderr, "Rank %d : fill block (i = %d, j = %d)\n", rank, i, j);
  220. if (i == j)
  221. {
  222. unsigned tmp;
  223. for (tmp = 0; tmp < size/nblocks; tmp++)
  224. {
  225. (*blockptr)[tmp*((size/nblocks)+1)] += (TYPE)10*nblocks;
  226. }
  227. }
  228. /* Register it to StarPU */
  229. starpu_matrix_data_register(handleptr, STARPU_MAIN_RAM,
  230. (uintptr_t)*blockptr, size/nblocks,
  231. size/nblocks, size/nblocks, sizeof(TYPE));
  232. starpu_data_set_coordinates(*handleptr, 2, j, i);
  233. }
  234. else
  235. {
  236. *blockptr = STARPU_POISON_PTR;
  237. *handleptr = STARPU_POISON_PTR;
  238. }
  239. }
  240. }
  241. /* Allocate the temporary buffers required for the distributed algorithm */
  242. unsigned k;
  243. /* tmp buffer 11 */
  244. #ifdef SINGLE_TMP11
  245. starpu_malloc((void **)&tmp_11_block, blocksize);
  246. allocated_memory_extra += blocksize;
  247. starpu_matrix_data_register(&tmp_11_block_handle, STARPU_MAIN_RAM, (uintptr_t)tmp_11_block,
  248. size/nblocks, size/nblocks, size/nblocks, sizeof(TYPE));
  249. #else
  250. tmp_11_block_handles = calloc(nblocks, sizeof(starpu_data_handle_t));
  251. tmp_11_block = calloc(nblocks, sizeof(TYPE *));
  252. allocated_memory_extra += nblocks*(sizeof(starpu_data_handle_t) + sizeof(TYPE *));
  253. for (k = 0; k < nblocks; k++)
  254. {
  255. if (tmp_11_block_is_needed(rank, nblocks, k))
  256. {
  257. starpu_malloc((void **)&tmp_11_block[k], blocksize);
  258. allocated_memory_extra += blocksize;
  259. STARPU_ASSERT(tmp_11_block[k]);
  260. starpu_matrix_data_register(&tmp_11_block_handles[k], STARPU_MAIN_RAM,
  261. (uintptr_t)tmp_11_block[k],
  262. size/nblocks, size/nblocks, size/nblocks, sizeof(TYPE));
  263. }
  264. }
  265. #endif
  266. /* tmp buffers 12 and 21 */
  267. #ifdef SINGLE_TMP1221
  268. tmp_12_block_handles = calloc(nblocks, sizeof(starpu_data_handle_t));
  269. tmp_21_block_handles = calloc(nblocks, sizeof(starpu_data_handle_t));
  270. tmp_12_block = calloc(nblocks, sizeof(TYPE *));
  271. tmp_21_block = calloc(nblocks, sizeof(TYPE *));
  272. allocated_memory_extra += 2*nblocks*(sizeof(starpu_data_handle_t) + sizeof(TYPE *));
  273. #else
  274. for (i = 0; i < 2; i++)
  275. {
  276. tmp_12_block_handles[i] = calloc(nblocks, sizeof(starpu_data_handle_t));
  277. tmp_21_block_handles[i] = calloc(nblocks, sizeof(starpu_data_handle_t));
  278. tmp_12_block[i] = calloc(nblocks, sizeof(TYPE *));
  279. tmp_21_block[i] = calloc(nblocks, sizeof(TYPE *));
  280. allocated_memory_extra += 2*nblocks*(sizeof(starpu_data_handle_t) + sizeof(TYPE *));
  281. }
  282. #endif
  283. for (k = 0; k < nblocks; k++)
  284. {
  285. #ifdef SINGLE_TMP1221
  286. if (tmp_12_block_is_needed(rank, nblocks, k))
  287. {
  288. starpu_malloc((void **)&tmp_12_block[k], blocksize);
  289. allocated_memory_extra += blocksize;
  290. STARPU_ASSERT(tmp_12_block[k]);
  291. starpu_matrix_data_register(&tmp_12_block_handles[k], STARPU_MAIN_RAM,
  292. (uintptr_t)tmp_12_block[k],
  293. size/nblocks, size/nblocks, size/nblocks, sizeof(TYPE));
  294. }
  295. if (tmp_21_block_is_needed(rank, nblocks, k))
  296. {
  297. starpu_malloc((void **)&tmp_21_block[k], blocksize);
  298. allocated_memory_extra += blocksize;
  299. STARPU_ASSERT(tmp_21_block[k]);
  300. starpu_matrix_data_register(&tmp_21_block_handles[k], STARPU_MAIN_RAM,
  301. (uintptr_t)tmp_21_block[k],
  302. size/nblocks, size/nblocks, size/nblocks, sizeof(TYPE));
  303. }
  304. #else
  305. for (i = 0; i < 2; i++)
  306. {
  307. if (tmp_12_block_is_needed(rank, nblocks, k))
  308. {
  309. starpu_malloc((void **)&tmp_12_block[i][k], blocksize);
  310. allocated_memory_extra += blocksize;
  311. STARPU_ASSERT(tmp_12_block[i][k]);
  312. starpu_matrix_data_register(&tmp_12_block_handles[i][k], STARPU_MAIN_RAM,
  313. (uintptr_t)tmp_12_block[i][k],
  314. size/nblocks, size/nblocks, size/nblocks, sizeof(TYPE));
  315. }
  316. if (tmp_21_block_is_needed(rank, nblocks, k))
  317. {
  318. starpu_malloc((void **)&tmp_21_block[i][k], blocksize);
  319. allocated_memory_extra += blocksize;
  320. STARPU_ASSERT(tmp_21_block[i][k]);
  321. starpu_matrix_data_register(&tmp_21_block_handles[i][k], STARPU_MAIN_RAM,
  322. (uintptr_t)tmp_21_block[i][k],
  323. size/nblocks, size/nblocks, size/nblocks, sizeof(TYPE));
  324. }
  325. }
  326. #endif
  327. }
  328. //display_all_blocks(nblocks, size/nblocks);
  329. }
  330. TYPE *STARPU_PLU(get_block)(unsigned i, unsigned j)
  331. {
  332. return dataA[j+i*nblocks];
  333. }
  334. int get_block_rank(unsigned i, unsigned j)
  335. {
  336. /* Take a 2D block cyclic distribution */
  337. /* NB: p (resp. q) is for "direction" i (resp. j) */
  338. return (j % q) * p + (i % p);
  339. }
  340. starpu_data_handle_t STARPU_PLU(get_block_handle)(unsigned i, unsigned j)
  341. {
  342. return dataA_handles[j+i*nblocks];
  343. }
  344. static void display_grid(int rank, unsigned pnblocks)
  345. {
  346. if (!display)
  347. return;
  348. //if (rank == 0)
  349. {
  350. fprintf(stderr, "2D grid layout (Rank %d): \n", rank);
  351. unsigned i, j;
  352. for (j = 0; j < pnblocks; j++)
  353. {
  354. for (i = 0; i < pnblocks; i++)
  355. {
  356. TYPE *blockptr = STARPU_PLU(get_block)(i, j);
  357. starpu_data_handle_t handle = STARPU_PLU(get_block_handle)(i, j);
  358. fprintf(stderr, "%d (data %p handle %p)", get_block_rank(i, j), blockptr, handle);
  359. }
  360. fprintf(stderr, "\n");
  361. }
  362. }
  363. }
  364. int main(int argc, char **argv)
  365. {
  366. int rank;
  367. int world_size;
  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. int ret = starpu_init(NULL);
  386. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  387. /* We disable sequential consistency in this example */
  388. starpu_data_set_default_sequential_consistency_flag(0);
  389. ret = starpu_mpi_init(NULL, NULL, 0);
  390. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  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);
  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. starpu_shutdown();
  492. #if 0
  493. MPI_Finalize();
  494. #endif
  495. return 0;
  496. }