plu_outofcore_example.c 8.3 KB

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