implicit-stencil.c 9.7 KB

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