stencil.c 8.5 KB

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