plu_example.c 14 KB

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