stencil.c 7.9 KB

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