implicit-stencil.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include "implicit-stencil.h"
  17. #ifdef STARPU_HAVE_VALGRIND_H
  18. #include <valgrind/valgrind.h>
  19. #endif
  20. /* Main application */
  21. /* default parameter values */
  22. static unsigned bind_tasks = 0;
  23. static unsigned ticks = 1000;
  24. #ifdef STARPU_QUICK_CHECK
  25. static unsigned niter = 4;
  26. #define SIZE 16
  27. #define NBZ 8
  28. #else
  29. static unsigned niter = 32;
  30. #define SIZE 128
  31. #define NBZ 64
  32. #endif
  33. /* Problem size */
  34. static unsigned sizex = SIZE;
  35. static unsigned sizey = SIZE;
  36. static unsigned sizez = NBZ*SIZE;
  37. /* Number of blocks (scattered over the different MPI processes) */
  38. unsigned nbz = NBZ;
  39. double start;
  40. double begin, end;
  41. double timing;
  42. /*
  43. * Initialization
  44. */
  45. unsigned get_bind_tasks(void)
  46. {
  47. return bind_tasks;
  48. }
  49. unsigned get_nbz(void)
  50. {
  51. return nbz;
  52. }
  53. unsigned get_niter(void)
  54. {
  55. return niter;
  56. }
  57. unsigned get_ticks(void)
  58. {
  59. return ticks;
  60. }
  61. static void parse_args(int argc, char **argv)
  62. {
  63. int i;
  64. for (i = 1; i < argc; i++)
  65. {
  66. if (strcmp(argv[i], "-b") == 0)
  67. {
  68. bind_tasks = 1;
  69. }
  70. if (strcmp(argv[i], "-nbz") == 0)
  71. {
  72. nbz = atoi(argv[++i]);
  73. }
  74. if (strcmp(argv[i], "-sizex") == 0)
  75. {
  76. sizex = atoi(argv[++i]);
  77. }
  78. if (strcmp(argv[i], "-sizey") == 0)
  79. {
  80. sizey = atoi(argv[++i]);
  81. }
  82. if (strcmp(argv[i], "-sizez") == 0)
  83. {
  84. sizez = atoi(argv[++i]);
  85. }
  86. if (strcmp(argv[i], "-niter") == 0)
  87. {
  88. niter = atoi(argv[++i]);
  89. }
  90. if (strcmp(argv[i], "-ticks") == 0)
  91. {
  92. ticks = atoi(argv[++i]);
  93. }
  94. if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
  95. {
  96. fprintf(stderr, "Usage : %s [options...]\n", argv[0]);
  97. fprintf(stderr, "\n");
  98. fprintf(stderr, "Options:\n");
  99. fprintf(stderr, "-b bind tasks on CPUs/GPUs\n");
  100. fprintf(stderr, "-nbz <n> Number of blocks on Z axis (%u by default)\n", nbz);
  101. fprintf(stderr, "-size[xyz] <size> Domain size on x/y/z axis (%ux%ux%u by default)\n", sizex, sizey, sizez);
  102. fprintf(stderr, "-niter <n> Number of iterations (%u by default)\n", niter);
  103. fprintf(stderr, "-ticks <t> How often to put ticks in the output (ms, %u by default)\n", ticks);
  104. exit(0);
  105. }
  106. }
  107. #ifdef STARPU_HAVE_VALGRIND_H
  108. if (RUNNING_ON_VALGRIND)
  109. {
  110. sizex = sizey = 3;
  111. nbz = 10;
  112. sizez = nbz*3;
  113. }
  114. #endif
  115. }
  116. static void init_problem(int argc, char **argv, int rank, int world_size)
  117. {
  118. parse_args(argc, argv);
  119. create_blocks_array(sizex, sizey, sizez, nbz);
  120. /* Select the MPI process which should compute the different blocks */
  121. assign_blocks_to_mpi_nodes(world_size);
  122. assign_blocks_to_workers(rank);
  123. /* Allocate the different memory blocks, if used by the MPI process */
  124. start = starpu_timing_now();
  125. allocate_memory_on_node(rank);
  126. end = starpu_timing_now();
  127. timing = end - begin;
  128. display_memory_consumption(rank, timing);
  129. who_runs_what_len = 2*niter;
  130. who_runs_what = (int *) calloc(nbz * who_runs_what_len, sizeof(*who_runs_what));
  131. who_runs_what_index = (int *) calloc(nbz, sizeof(*who_runs_what_index));
  132. last_tick = (double *) calloc(nbz, sizeof(*last_tick));
  133. }
  134. static void free_problem(int rank)
  135. {
  136. free_memory_on_node(rank);
  137. free_blocks_array();
  138. free(who_runs_what);
  139. free(who_runs_what_index);
  140. free(last_tick);
  141. }
  142. /*
  143. * Main body
  144. */
  145. void f(unsigned task_per_worker[STARPU_NMAXWORKERS])
  146. {
  147. unsigned total = 0;
  148. int worker;
  149. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  150. total += task_per_worker[worker];
  151. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  152. {
  153. if (task_per_worker[worker])
  154. {
  155. char name[64];
  156. starpu_worker_get_name(worker, name, sizeof(name));
  157. FPRINTF(stderr,"\t%s -> %u (%2.2f%%)\n", name, task_per_worker[worker], (100.0*task_per_worker[worker])/total);
  158. }
  159. }
  160. }
  161. unsigned global_workerid(unsigned local_workerid)
  162. {
  163. #if defined(STARPU_USE_MPI) && !defined(STARPU_USE_MPI_MASTER_SLAVE)
  164. int rank;
  165. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  166. unsigned workers_per_node = starpu_worker_get_count();
  167. return (local_workerid + rank*workers_per_node);
  168. #else
  169. return local_workerid;
  170. #endif
  171. }
  172. int main(int argc, char **argv)
  173. {
  174. int rank;
  175. int world_size;
  176. int ret;
  177. #if defined(STARPU_USE_MPI) && !defined(STARPU_SIMGRID) && !defined(STARPU_USE_MPI_MASTER_SLAVE)
  178. int thread_support;
  179. if (MPI_Init_thread(&argc, &argv, MPI_THREAD_SERIALIZED, &thread_support))
  180. {
  181. FPRINTF(stderr, "MPI_Init_thread failed\n");
  182. }
  183. if (thread_support == MPI_THREAD_FUNNELED)
  184. FPRINTF(stderr,"Warning: MPI only has funneled thread support, not serialized, hoping this will work\n");
  185. if (thread_support < MPI_THREAD_FUNNELED)
  186. FPRINTF(stderr,"Warning: MPI does not have thread support!\n");
  187. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  188. MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  189. #else
  190. rank = 0;
  191. world_size = 1;
  192. #endif
  193. if (rank == 0)
  194. {
  195. FPRINTF(stderr, "Running on %d nodes\n", world_size);
  196. fflush(stderr);
  197. }
  198. ret = starpu_init(NULL);
  199. if (ret == -ENODEV) return 77;
  200. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  201. #if defined(STARPU_USE_MPI) && !defined(STARPU_SIMGRID) && !defined(STARPU_USE_MPI_MASTER_SLAVE)
  202. ret = starpu_mpi_init(NULL, NULL, 0);
  203. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  204. #endif
  205. #ifdef STARPU_USE_OPENCL
  206. opencl_life_init();
  207. opencl_shadow_init();
  208. #endif /*STARPU_USE_OPENCL*/
  209. init_problem(argc, argv, rank, world_size);
  210. #if defined(STARPU_USE_MPI) && !defined(STARPU_SIMGRID) && !defined(STARPU_USE_MPI_MASTER_SLAVE)
  211. int barrier_ret = MPI_Barrier(MPI_COMM_WORLD);
  212. STARPU_ASSERT(barrier_ret == MPI_SUCCESS);
  213. #endif
  214. if (rank == 0)
  215. FPRINTF(stderr, "GO !\n");
  216. start = starpu_timing_now();
  217. begin = starpu_timing_now();
  218. create_tasks(rank);
  219. //starpu_tag_notify_from_apps(TAG_INIT_TASK);
  220. //wait_end_tasks(rank);
  221. starpu_task_wait_for_all();
  222. end = starpu_timing_now();
  223. #if defined(STARPU_USE_MPI) && !defined(STARPU_SIMGRID) && !defined(STARPU_USE_MPI_MASTER_SLAVE)
  224. barrier_ret = MPI_Barrier(MPI_COMM_WORLD);
  225. STARPU_ASSERT(barrier_ret == MPI_SUCCESS);
  226. #endif
  227. #if 0
  228. check(rank);
  229. #endif
  230. /*display_debug(nbz, niter, rank);*/
  231. /* timing in us */
  232. timing = end - begin;
  233. double min_timing = timing;
  234. double max_timing = timing;
  235. double sum_timing = timing;
  236. #if defined(STARPU_USE_MPI) && !defined(STARPU_SIMGRID) && !defined(STARPU_USE_MPI_MASTER_SLAVE)
  237. int reduce_ret;
  238. reduce_ret = MPI_Reduce(&timing, &min_timing, 1, MPI_DOUBLE, MPI_MIN, 0, MPI_COMM_WORLD);
  239. STARPU_ASSERT(reduce_ret == MPI_SUCCESS);
  240. reduce_ret = MPI_Reduce(&timing, &max_timing, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
  241. STARPU_ASSERT(reduce_ret == MPI_SUCCESS);
  242. reduce_ret = MPI_Reduce(&timing, &sum_timing, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
  243. STARPU_ASSERT(reduce_ret == MPI_SUCCESS);
  244. /* XXX we should do a gather instead, here we assume that non initialized values are still 0 */
  245. int *who_runs_what_tmp = malloc(nbz * who_runs_what_len * sizeof(*who_runs_what));
  246. reduce_ret = MPI_Reduce(who_runs_what, who_runs_what_tmp, nbz * who_runs_what_len, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  247. STARPU_ASSERT(reduce_ret == MPI_SUCCESS);
  248. memcpy(who_runs_what, who_runs_what_tmp, nbz * who_runs_what_len * sizeof(*who_runs_what));
  249. free(who_runs_what_tmp);
  250. /* XXX we should do a gather instead, here we assume that non initialized values are still 0 */
  251. int *who_runs_what_index_tmp = malloc(nbz * sizeof(*who_runs_what_index));
  252. reduce_ret = MPI_Reduce(who_runs_what_index, who_runs_what_index_tmp, nbz, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  253. STARPU_ASSERT(reduce_ret == MPI_SUCCESS);
  254. memcpy(who_runs_what_index, who_runs_what_index_tmp, nbz * sizeof(*who_runs_what_index));
  255. free(who_runs_what_index_tmp);
  256. #endif
  257. if (rank == 0)
  258. {
  259. #if 1
  260. FPRINTF(stderr, "update:\n");
  261. f(update_per_worker);
  262. FPRINTF(stderr, "top:\n");
  263. f(top_per_worker);
  264. FPRINTF(stderr, "bottom:\n");
  265. f(bottom_per_worker);
  266. #endif
  267. #if 1
  268. unsigned nzblocks_per_process = (nbz + world_size - 1) / world_size;
  269. int iter;
  270. for (iter = 0; iter < who_runs_what_len; iter++)
  271. {
  272. unsigned last, bz;
  273. last = 1;
  274. for (bz = 0; bz < nbz; bz++)
  275. {
  276. if ((bz % nzblocks_per_process) == 0)
  277. FPRINTF(stderr, "| ");
  278. if (who_runs_what_index[bz] <= iter)
  279. FPRINTF(stderr,"_ ");
  280. else
  281. {
  282. last = 0;
  283. if (who_runs_what[bz + iter * nbz] == -1)
  284. FPRINTF(stderr,"* ");
  285. else
  286. FPRINTF(stderr, "%d ", who_runs_what[bz + iter * nbz]);
  287. }
  288. }
  289. FPRINTF(stderr, "\n");
  290. if (last)
  291. break;
  292. }
  293. #endif
  294. fflush(stderr);
  295. FPRINTF(stdout, "Computation took: %f ms on %d MPI processes\n", max_timing/1000, world_size);
  296. FPRINTF(stdout, "\tMIN : %f ms\n", min_timing/1000);
  297. FPRINTF(stdout, "\tMAX : %f ms\n", max_timing/1000);
  298. FPRINTF(stdout, "\tAVG : %f ms\n", sum_timing/(world_size*1000));
  299. }
  300. free_problem(rank);
  301. #if defined(STARPU_USE_MPI) && !defined(STARPU_SIMGRID) && !defined(STARPU_USE_MPI_MASTER_SLAVE)
  302. starpu_mpi_shutdown();
  303. #endif
  304. starpu_shutdown();
  305. #if defined(STARPU_USE_MPI) && !defined(STARPU_SIMGRID) && !defined(STARPU_USE_MPI_MASTER_SLAVE)
  306. MPI_Finalize();
  307. #endif
  308. #ifdef STARPU_USE_OPENCL
  309. opencl_life_free();
  310. opencl_shadow_free();
  311. #endif /*STARPU_USE_OPENCL*/
  312. return 0;
  313. }