plu_outofcore_example.c 8.6 KB

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