stencil.c 7.8 KB

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