plu_example.c 14 KB

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