plu_example.c 15 KB

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