implicit-stencil.c 9.8 KB

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