plu_outofcore_example.c 9.5 KB

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