stencil.c 8.9 KB

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