plu_example.c 14 KB

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