plu_implicit_example.c 8.4 KB

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