implicit-stencil.c 9.3 KB

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