plu_implicit_example.c 8.1 KB

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