plu_example.c 14 KB

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