plu_example.c 14 KB

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