plu_implicit_example.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2019 CNRS
  4. * Copyright (C) 2010,2011,2013-2015,2017,2018 Université de Bordeaux
  5. * Copyright (C) 2013 Thibaut Lambert
  6. * Copyright (C) 2012,2013 Inria
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <time.h>
  23. #include <math.h>
  24. #include <starpu.h>
  25. #include "pxlu.h"
  26. //#include "pxlu_kernels.h"
  27. #ifdef STARPU_HAVE_LIBNUMA
  28. #include <numaif.h>
  29. #endif
  30. static unsigned long size = 4096;
  31. static unsigned nblocks = 16;
  32. static unsigned check = 0;
  33. static int p = -1;
  34. static int q = -1;
  35. static unsigned display = 0;
  36. static unsigned no_prio = 0;
  37. #ifdef STARPU_HAVE_LIBNUMA
  38. static unsigned numa = 0;
  39. #endif
  40. static size_t allocated_memory = 0;
  41. static size_t allocated_memory_extra = 0;
  42. static starpu_data_handle_t *dataA_handles;
  43. static TYPE **dataA;
  44. int get_block_rank(unsigned i, unsigned j);
  45. static void parse_args(int argc, char **argv)
  46. {
  47. int i;
  48. for (i = 1; i < argc; i++)
  49. {
  50. if (strcmp(argv[i], "-size") == 0)
  51. {
  52. char *argptr;
  53. size = strtol(argv[++i], &argptr, 10);
  54. }
  55. if (strcmp(argv[i], "-nblocks") == 0)
  56. {
  57. char *argptr;
  58. nblocks = strtol(argv[++i], &argptr, 10);
  59. }
  60. if (strcmp(argv[i], "-check") == 0)
  61. {
  62. check = 1;
  63. }
  64. if (strcmp(argv[i], "-display") == 0)
  65. {
  66. display = 1;
  67. }
  68. if (strcmp(argv[i], "-numa") == 0)
  69. {
  70. #ifdef STARPU_HAVE_LIBNUMA
  71. numa = 1;
  72. #else
  73. fprintf(stderr, "Warning: libnuma is not available\n");
  74. #endif
  75. }
  76. if (strcmp(argv[i], "-p") == 0)
  77. {
  78. char *argptr;
  79. p = strtol(argv[++i], &argptr, 10);
  80. }
  81. if (strcmp(argv[i], "-q") == 0)
  82. {
  83. char *argptr;
  84. q = strtol(argv[++i], &argptr, 10);
  85. }
  86. if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "--help") == 0)
  87. {
  88. fprintf(stderr,"usage: %s [-size n] [-nblocks b] [-check] [-display] [-numa] [-p p] [-q q]\n", argv[0]);
  89. fprintf(stderr,"\np * q must be equal to the number of MPI nodes\n");
  90. exit(0);
  91. }
  92. }
  93. }
  94. unsigned STARPU_PLU(display_flag)(void)
  95. {
  96. return display;
  97. }
  98. static void fill_block_with_random(TYPE *blockptr, unsigned psize, unsigned pnblocks)
  99. {
  100. const unsigned block_size = (psize/pnblocks);
  101. unsigned i, j;
  102. for (i = 0; i < block_size; i++)
  103. for (j = 0; j < block_size; j++)
  104. {
  105. blockptr[j+i*block_size] = (TYPE)starpu_drand48();
  106. }
  107. }
  108. static void init_matrix(int rank)
  109. {
  110. #ifdef STARPU_HAVE_LIBNUMA
  111. if (numa)
  112. {
  113. fprintf(stderr, "Using INTERLEAVE policy\n");
  114. unsigned long nodemask = ((1<<0)|(1<<1));
  115. int ret = set_mempolicy(MPOL_INTERLEAVE, &nodemask, 3);
  116. if (ret)
  117. perror("set_mempolicy failed");
  118. }
  119. #endif
  120. /* Allocate a grid of data handles, not all of them have to be allocated later on */
  121. dataA_handles = calloc(nblocks*nblocks, sizeof(starpu_data_handle_t));
  122. dataA = calloc(nblocks*nblocks, sizeof(TYPE *));
  123. allocated_memory_extra += nblocks*nblocks*(sizeof(starpu_data_handle_t) + sizeof(TYPE *));
  124. size_t blocksize = (size_t)(size/nblocks)*(size/nblocks)*sizeof(TYPE);
  125. /* Allocate all the blocks that belong to this mpi node */
  126. unsigned long i,j;
  127. for (j = 0; j < nblocks; j++)
  128. {
  129. for (i = 0; i < nblocks; i++)
  130. {
  131. int block_rank = get_block_rank(i, j);
  132. TYPE **blockptr = &dataA[j+i*nblocks];
  133. // starpu_data_handle_t *handleptr = &dataA_handles[j+nblocks*i];
  134. starpu_data_handle_t *handleptr = &dataA_handles[j+nblocks*i];
  135. if (block_rank == rank)
  136. {
  137. /* This blocks should be treated by the current MPI process */
  138. /* Allocate and fill it */
  139. starpu_malloc((void **)blockptr, blocksize);
  140. allocated_memory += blocksize;
  141. //fprintf(stderr, "Rank %d : fill block (i = %d, j = %d)\n", rank, i, j);
  142. fill_block_with_random(*blockptr, size, nblocks);
  143. //fprintf(stderr, "Rank %d : fill block (i = %d, j = %d)\n", rank, i, j);
  144. if (i == j)
  145. {
  146. unsigned tmp;
  147. for (tmp = 0; tmp < size/nblocks; tmp++)
  148. {
  149. (*blockptr)[tmp*((size/nblocks)+1)] += (TYPE)10*nblocks;
  150. }
  151. }
  152. /* Register it to StarPU */
  153. starpu_matrix_data_register(handleptr, STARPU_MAIN_RAM,
  154. (uintptr_t)*blockptr, size/nblocks,
  155. size/nblocks, size/nblocks, sizeof(TYPE));
  156. }
  157. else
  158. {
  159. starpu_matrix_data_register(handleptr, -1,
  160. 0, size/nblocks,
  161. size/nblocks, size/nblocks, sizeof(TYPE));
  162. *blockptr = STARPU_POISON_PTR;
  163. }
  164. starpu_data_set_coordinates(*handleptr, 2, j, i);
  165. starpu_mpi_data_register(*handleptr, j+i*nblocks, block_rank);
  166. }
  167. }
  168. //display_all_blocks(nblocks, size/nblocks);
  169. }
  170. TYPE *STARPU_PLU(get_block)(unsigned i, unsigned j)
  171. {
  172. return dataA[j+i*nblocks];
  173. }
  174. int get_block_rank(unsigned i, unsigned j)
  175. {
  176. /* Take a 2D block cyclic distribution */
  177. /* NB: p (resp. q) is for "direction" i (resp. j) */
  178. return (j % q) * p + (i % p);
  179. }
  180. starpu_data_handle_t STARPU_PLU(get_block_handle)(unsigned i, unsigned j)
  181. {
  182. return dataA_handles[j+i*nblocks];
  183. }
  184. static void display_grid(int rank, unsigned pnblocks)
  185. {
  186. if (!display)
  187. return;
  188. //if (rank == 0)
  189. {
  190. fprintf(stderr, "2D grid layout (Rank %d): \n", rank);
  191. unsigned i, j;
  192. for (j = 0; j < pnblocks; j++)
  193. {
  194. for (i = 0; i < pnblocks; i++)
  195. {
  196. TYPE *blockptr = STARPU_PLU(get_block)(i, j);
  197. starpu_data_handle_t handle = STARPU_PLU(get_block_handle)(i, j);
  198. fprintf(stderr, "%d (data %p handle %p)", get_block_rank(i, j), blockptr, handle);
  199. }
  200. fprintf(stderr, "\n");
  201. }
  202. }
  203. }
  204. int main(int argc, char **argv)
  205. {
  206. int rank;
  207. int world_size;
  208. int ret;
  209. starpu_srand48((long int)time(NULL));
  210. parse_args(argc, argv);
  211. ret = starpu_mpi_init_conf(&argc, &argv, 1, MPI_COMM_WORLD, NULL);
  212. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  213. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  214. starpu_mpi_comm_size(MPI_COMM_WORLD, &world_size);
  215. if (p == -1 && q==-1)
  216. {
  217. fprintf(stderr, "Setting default values for p and q\n");
  218. p = (q % 2 == 0) ? 2 : 1;
  219. q = world_size / p;
  220. }
  221. STARPU_ASSERT_MSG(p*q == world_size, "p=%d, q=%d, world_size=%d\n", p, q, world_size);
  222. starpu_cublas_init();
  223. /*
  224. * Problem Init
  225. */
  226. init_matrix(rank);
  227. fprintf(stderr, "Rank %d: allocated (%d + %d) MB = %d MB\n", rank,
  228. (int)(allocated_memory/(1024*1024)),
  229. (int)(allocated_memory_extra/(1024*1024)),
  230. (int)((allocated_memory+allocated_memory_extra)/(1024*1024)));
  231. display_grid(rank, nblocks);
  232. TYPE *a_r = NULL;
  233. // STARPU_PLU(display_data_content)(a_r, size);
  234. if (check)
  235. {
  236. TYPE *x, *y;
  237. x = calloc(size, sizeof(TYPE));
  238. STARPU_ASSERT(x);
  239. y = calloc(size, sizeof(TYPE));
  240. STARPU_ASSERT(y);
  241. if (rank == 0)
  242. {
  243. unsigned ind;
  244. for (ind = 0; ind < size; ind++)
  245. x[ind] = (TYPE)starpu_drand48();
  246. }
  247. a_r = STARPU_PLU(reconstruct_matrix)(size, nblocks);
  248. if (rank == 0)
  249. STARPU_PLU(display_data_content)(a_r, size);
  250. // STARPU_PLU(compute_ax)(size, x, y, nblocks, rank);
  251. free(x);
  252. free(y);
  253. }
  254. double timing = STARPU_PLU(plu_main)(nblocks, rank, world_size, no_prio);
  255. /*
  256. * Report performance
  257. */
  258. if (rank == 0)
  259. {
  260. fprintf(stderr, "Computation took: %f ms\n", timing/1000);
  261. unsigned n = size;
  262. double flop = (2.0f*n*n*n)/3.0f;
  263. fprintf(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  264. }
  265. /*
  266. * Test Result Correctness
  267. */
  268. if (check)
  269. {
  270. /*
  271. * Compute || A - LU ||
  272. */
  273. STARPU_PLU(compute_lu_matrix)(size, nblocks, a_r);
  274. #if 0
  275. /*
  276. * Compute || Ax - LUx ||
  277. */
  278. unsigned ind;
  279. y2 = calloc(size, sizeof(TYPE));
  280. STARPU_ASSERT(y);
  281. if (rank == 0)
  282. {
  283. for (ind = 0; ind < size; ind++)
  284. {
  285. y2[ind] = (TYPE)0.0;
  286. }
  287. }
  288. STARPU_PLU(compute_lux)(size, x, y2, nblocks, rank);
  289. /* Compute y2 = y2 - y */
  290. CPU_AXPY(size, -1.0, y, 1, y2, 1);
  291. TYPE err = CPU_ASUM(size, y2, 1);
  292. int max = CPU_IAMAX(size, y2, 1);
  293. fprintf(stderr, "(A - LU)X Avg error : %e\n", err/(size*size));
  294. fprintf(stderr, "(A - LU)X Max error : %e\n", y2[max]);
  295. #endif
  296. }
  297. /*
  298. * Termination
  299. */
  300. starpu_cublas_shutdown();
  301. starpu_mpi_shutdown();
  302. return 0;
  303. }