plu_outofcore_example.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 <unistd.h>
  21. #include <string.h>
  22. #include <time.h>
  23. #include <math.h>
  24. #include <starpu.h>
  25. #include <fcntl.h>
  26. #include <sys/stat.h>
  27. #include "pxlu.h"
  28. //#include "pxlu_kernels.h"
  29. #ifdef STARPU_HAVE_LIBNUMA
  30. #include <numaif.h>
  31. #endif
  32. #ifdef STARPU_HAVE_VALGRIND_H
  33. #include <valgrind/valgrind.h>
  34. #endif
  35. static unsigned long size = 4096;
  36. static unsigned nblocks = 16;
  37. static unsigned check = 0;
  38. static int p = 1;
  39. static int q = 1;
  40. static unsigned display = 0;
  41. static unsigned no_prio = 0;
  42. static char *path = "./starpu-ooc-files";
  43. #ifdef STARPU_HAVE_LIBNUMA
  44. static unsigned numa = 0;
  45. #endif
  46. static size_t allocated_memory = 0;
  47. static starpu_data_handle_t *dataA_handles;
  48. int get_block_rank(unsigned i, unsigned j);
  49. static void parse_args(int argc, char **argv)
  50. {
  51. int i;
  52. for (i = 1; i < argc; i++)
  53. {
  54. if (strcmp(argv[i], "-size") == 0)
  55. {
  56. char *argptr;
  57. size = strtol(argv[++i], &argptr, 10);
  58. }
  59. if (strcmp(argv[i], "-nblocks") == 0)
  60. {
  61. char *argptr;
  62. nblocks = strtol(argv[++i], &argptr, 10);
  63. }
  64. if (strcmp(argv[i], "-check") == 0)
  65. {
  66. check = 1;
  67. }
  68. if (strcmp(argv[i], "-display") == 0)
  69. {
  70. display = 1;
  71. }
  72. if (strcmp(argv[i], "-numa") == 0)
  73. {
  74. #ifdef STARPU_HAVE_LIBNUMA
  75. numa = 1;
  76. #else
  77. fprintf(stderr, "Warning: libnuma is not available\n");
  78. #endif
  79. }
  80. if (strcmp(argv[i], "-p") == 0)
  81. {
  82. char *argptr;
  83. p = strtol(argv[++i], &argptr, 10);
  84. }
  85. if (strcmp(argv[i], "-q") == 0)
  86. {
  87. char *argptr;
  88. q = strtol(argv[++i], &argptr, 10);
  89. }
  90. if (strcmp(argv[i], "-path") == 0)
  91. {
  92. path = argv[++i];
  93. }
  94. if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "--help") == 0)
  95. {
  96. fprintf(stderr,"usage: %s [-size n] [-nblocks b] [-check] [-display] [-numa] [-p p] [-q q] [-path PATH]\n", argv[0]);
  97. fprintf(stderr,"\np * q must be equal to the number of MPI nodes\n");
  98. exit(0);
  99. }
  100. }
  101. #ifdef STARPU_HAVE_VALGRIND_H
  102. if (RUNNING_ON_VALGRIND)
  103. size = 16;
  104. #endif
  105. }
  106. unsigned STARPU_PLU(display_flag)(void)
  107. {
  108. return display;
  109. }
  110. static void fill_block_with_random(TYPE *blockptr, unsigned psize, unsigned pnblocks)
  111. {
  112. const unsigned block_size = (psize/pnblocks);
  113. unsigned i, j;
  114. for (i = 0; i < block_size; i++)
  115. for (j = 0; j < block_size; j++)
  116. {
  117. blockptr[j+i*block_size] = (TYPE)starpu_drand48();
  118. }
  119. }
  120. static void create_matrix()
  121. {
  122. size_t blocksize = (size_t)(size/nblocks)*(size/nblocks)*sizeof(TYPE);
  123. TYPE *blockptr = malloc(blocksize);
  124. int fd;
  125. char *filename;
  126. unsigned filename_length = strlen(path) + 1 + sizeof(nblocks)*3 + 1 + sizeof(nblocks)*3 + 1;
  127. filename = malloc(filename_length);
  128. allocated_memory += nblocks*nblocks*blocksize;
  129. /* Create the whole matrix on the disk */
  130. unsigned i,j;
  131. for (j = 0; j < nblocks; j++)
  132. {
  133. for (i = 0; i < nblocks; i++)
  134. {
  135. fill_block_with_random(blockptr, size, nblocks);
  136. if (i == j)
  137. {
  138. unsigned tmp;
  139. for (tmp = 0; tmp < size/nblocks; tmp++)
  140. {
  141. blockptr[tmp*((size/nblocks)+1)] += (TYPE)10*nblocks;
  142. }
  143. }
  144. snprintf(filename, filename_length, "%s/%u,%u", path, i, j);
  145. fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0777);
  146. if (fd < 0)
  147. {
  148. perror("open");
  149. exit(1);
  150. }
  151. if (write(fd, blockptr, blocksize) != (starpu_ssize_t) blocksize)
  152. {
  153. fprintf(stderr,"short write");
  154. exit(1);
  155. }
  156. if (close(fd) < 0)
  157. {
  158. perror("close");
  159. exit(1);
  160. }
  161. }
  162. }
  163. free(blockptr);
  164. free(filename);
  165. }
  166. static void init_matrix(int rank)
  167. {
  168. /* Allocate a grid of data handles, not all of them have to be allocated later on */
  169. dataA_handles = calloc(nblocks*nblocks, sizeof(starpu_data_handle_t));
  170. size_t blocksize = (size_t)(size/nblocks)*(size/nblocks)*sizeof(TYPE);
  171. int disk_node = starpu_disk_register(&starpu_disk_unistd_ops, path, STARPU_MAX(1024*1024, size*size*sizeof(TYPE)));
  172. assert(disk_node >= 0);
  173. char filename[sizeof(nblocks)*3 + 1 + sizeof(nblocks)*3 + 1];
  174. /* Allocate all the blocks that belong to this mpi node */
  175. unsigned i,j;
  176. for (j = 0; j < nblocks; j++)
  177. {
  178. for (i = 0; i < nblocks; i++)
  179. {
  180. int block_rank = get_block_rank(i, j);
  181. // starpu_data_handle_t *handleptr = &dataA_handles[j+nblocks*i];
  182. starpu_data_handle_t *handleptr = &dataA_handles[j+nblocks*i];
  183. if (block_rank == rank)
  184. {
  185. void *disk_obj;
  186. snprintf(filename, sizeof(filename), "%u,%u", i, j);
  187. /* Register it to StarPU */
  188. disk_obj = starpu_disk_open(disk_node, filename, blocksize);
  189. if (!disk_obj)
  190. {
  191. fprintf(stderr,"could not open %s\n", filename);
  192. exit(1);
  193. }
  194. starpu_matrix_data_register(handleptr, disk_node,
  195. (uintptr_t) disk_obj, size/nblocks,
  196. size/nblocks, size/nblocks, sizeof(TYPE));
  197. }
  198. else
  199. {
  200. starpu_matrix_data_register(handleptr, -1,
  201. 0, size/nblocks,
  202. size/nblocks, size/nblocks, sizeof(TYPE));
  203. }
  204. starpu_data_set_coordinates(*handleptr, 2, j, i);
  205. starpu_mpi_data_register(*handleptr, j+i*nblocks, block_rank);
  206. }
  207. }
  208. //display_all_blocks(nblocks, size/nblocks);
  209. }
  210. TYPE *STARPU_PLU(get_block)(unsigned i, unsigned j)
  211. {
  212. (void)i;
  213. (void)j;
  214. /* This does not really make sense in out of core */
  215. assert(0);
  216. }
  217. int get_block_rank(unsigned i, unsigned j)
  218. {
  219. /* Take a 2D block cyclic distribution */
  220. /* NB: p (resp. q) is for "direction" i (resp. j) */
  221. return (j % q) * p + (i % p);
  222. }
  223. starpu_data_handle_t STARPU_PLU(get_block_handle)(unsigned i, unsigned j)
  224. {
  225. return dataA_handles[j+i*nblocks];
  226. }
  227. int main(int argc, char **argv)
  228. {
  229. int rank;
  230. int world_size;
  231. int ret;
  232. unsigned i, j;
  233. starpu_srand48((long int)time(NULL));
  234. parse_args(argc, argv);
  235. ret = mkdir(path, 0777);
  236. if (ret != 0 && errno != EEXIST)
  237. {
  238. fprintf(stderr,"%s does not exist\n", path);
  239. exit(1);
  240. }
  241. ret = starpu_mpi_init_conf(&argc, &argv, 1, MPI_COMM_WORLD, NULL);
  242. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  243. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  244. starpu_mpi_comm_size(MPI_COMM_WORLD, &world_size);
  245. STARPU_ASSERT(p*q == world_size);
  246. starpu_cublas_init();
  247. /*
  248. * Problem Init
  249. */
  250. if (rank == 0)
  251. create_matrix();
  252. starpu_mpi_barrier(MPI_COMM_WORLD);
  253. init_matrix(rank);
  254. if (rank == 0)
  255. fprintf(stderr, "%dMB on disk\n", (int)(allocated_memory/(1024*1024)));
  256. TYPE *a_r = NULL;
  257. // STARPU_PLU(display_data_content)(a_r, size);
  258. if (check)
  259. {
  260. TYPE *x, *y;
  261. x = calloc(size, sizeof(TYPE));
  262. STARPU_ASSERT(x);
  263. y = calloc(size, sizeof(TYPE));
  264. STARPU_ASSERT(y);
  265. if (rank == 0)
  266. {
  267. unsigned ind;
  268. for (ind = 0; ind < size; ind++)
  269. x[ind] = (TYPE)starpu_drand48();
  270. }
  271. a_r = STARPU_PLU(reconstruct_matrix)(size, nblocks);
  272. if (rank == 0)
  273. STARPU_PLU(display_data_content)(a_r, size);
  274. // STARPU_PLU(compute_ax)(size, x, y, nblocks, rank);
  275. free(x);
  276. free(y);
  277. }
  278. double timing = STARPU_PLU(plu_main)(nblocks, rank, world_size, no_prio);
  279. /*
  280. * Report performance
  281. */
  282. if (rank == 0)
  283. {
  284. fprintf(stderr, "Computation took: %f ms\n", timing/1000);
  285. unsigned n = size;
  286. double flop = (2.0f*n*n*n)/3.0f;
  287. fprintf(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  288. }
  289. /*
  290. * Test Result Correctness
  291. */
  292. if (check)
  293. {
  294. /*
  295. * Compute || A - LU ||
  296. */
  297. STARPU_PLU(compute_lu_matrix)(size, nblocks, a_r);
  298. #if 0
  299. /*
  300. * Compute || Ax - LUx ||
  301. */
  302. unsigned ind;
  303. y2 = calloc(size, sizeof(TYPE));
  304. STARPU_ASSERT(y);
  305. if (rank == 0)
  306. {
  307. for (ind = 0; ind < size; ind++)
  308. {
  309. y2[ind] = (TYPE)0.0;
  310. }
  311. }
  312. STARPU_PLU(compute_lux)(size, x, y2, nblocks, rank);
  313. /* Compute y2 = y2 - y */
  314. CPU_AXPY(size, -1.0, y, 1, y2, 1);
  315. TYPE err = CPU_ASUM(size, y2, 1);
  316. int max = CPU_IAMAX(size, y2, 1);
  317. fprintf(stderr, "(A - LU)X Avg error : %e\n", err/(size*size));
  318. fprintf(stderr, "(A - LU)X Max error : %e\n", y2[max]);
  319. #endif
  320. }
  321. /*
  322. * Termination
  323. */
  324. for (j = 0; j < nblocks; j++)
  325. {
  326. for (i = 0; i < nblocks; i++)
  327. {
  328. starpu_data_unregister(dataA_handles[j+nblocks*i]);
  329. }
  330. }
  331. starpu_cublas_shutdown();
  332. starpu_mpi_shutdown();
  333. return 0;
  334. }