plu_example.c 15 KB

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