stencil.c 8.7 KB

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