plu_example.c 12 KB

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