stencil5.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2013, 2015-2017 Université Bordeaux
  4. * Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016 CNRS
  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 <starpu_mpi.h>
  18. #include <math.h>
  19. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  20. #define FPRINTF_MPI(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) { \
  21. int _disp_rank; starpu_mpi_comm_rank(MPI_COMM_WORLD, &_disp_rank); \
  22. fprintf(ofile, "[%d][starpu_mpi][%s] " fmt , _disp_rank, __starpu_func__ ,## __VA_ARGS__); \
  23. fflush(ofile); }} while(0);
  24. void stencil5_cpu(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  25. {
  26. float *xy = (float *)STARPU_VARIABLE_GET_PTR(descr[0]);
  27. float *xm1y = (float *)STARPU_VARIABLE_GET_PTR(descr[1]);
  28. float *xp1y = (float *)STARPU_VARIABLE_GET_PTR(descr[2]);
  29. float *xym1 = (float *)STARPU_VARIABLE_GET_PTR(descr[3]);
  30. float *xyp1 = (float *)STARPU_VARIABLE_GET_PTR(descr[4]);
  31. // fprintf(stdout, "VALUES: %2.2f %2.2f %2.2f %2.2f %2.2f\n", *xy, *xm1y, *xp1y, *xym1, *xyp1);
  32. *xy = (*xy + *xm1y + *xp1y + *xym1 + *xyp1) / 5;
  33. // fprintf(stdout, "VALUES: %2.2f %2.2f %2.2f %2.2f %2.2f\n", *xy, *xm1y, *xp1y, *xym1, *xyp1);
  34. }
  35. /* Dumb performance model for simgrid */
  36. static double stencil5_cost_function(struct starpu_task *task, unsigned nimpl)
  37. {
  38. (void) task;
  39. (void) nimpl;
  40. return 0.000001;
  41. }
  42. static struct starpu_perfmodel stencil5_model =
  43. {
  44. .type = STARPU_COMMON,
  45. .cost_function = stencil5_cost_function,
  46. .symbol = "stencil5"
  47. };
  48. struct starpu_codelet stencil5_cl =
  49. {
  50. .cpu_funcs = {stencil5_cpu},
  51. .nbuffers = 5,
  52. .modes = {STARPU_RW, STARPU_R, STARPU_R, STARPU_R, STARPU_R},
  53. .model = &stencil5_model
  54. };
  55. #ifdef STARPU_QUICK_CHECK
  56. # define NITER_DEF 10
  57. # define X 2
  58. # define Y 2
  59. #elif !defined(STARPU_LONG_CHECK)
  60. # define NITER_DEF 10
  61. # define X 5
  62. # define Y 5
  63. #else
  64. # define NITER_DEF 100
  65. # define X 20
  66. # define Y 20
  67. #endif
  68. int display = 0;
  69. int niter = NITER_DEF;
  70. /* Returns the MPI node number where data indexes index is */
  71. int my_distrib(int x, int y, int nb_nodes)
  72. {
  73. /* Block distrib */
  74. return ((int)(x / sqrt(nb_nodes) + (y / sqrt(nb_nodes)) * sqrt(nb_nodes))) % nb_nodes;
  75. }
  76. /* Shifted distribution, for migration example */
  77. int my_distrib2(int x, int y, int nb_nodes)
  78. {
  79. return (my_distrib(x, y, nb_nodes) + 1) % nb_nodes;
  80. }
  81. static void parse_args(int argc, char **argv)
  82. {
  83. int i;
  84. for (i = 1; i < argc; i++)
  85. {
  86. if (strcmp(argv[i], "-iter") == 0)
  87. {
  88. char *argptr;
  89. niter = strtol(argv[++i], &argptr, 10);
  90. }
  91. if (strcmp(argv[i], "-display") == 0)
  92. {
  93. display = 1;
  94. }
  95. }
  96. }
  97. int main(int argc, char **argv)
  98. {
  99. int my_rank, size, x, y, loop;
  100. float mean=0;
  101. float matrix[X][Y];
  102. starpu_data_handle_t data_handles[X][Y];
  103. int ret = starpu_init(NULL);
  104. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  105. ret = starpu_mpi_init(&argc, &argv, 1);
  106. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  107. starpu_mpi_comm_rank(MPI_COMM_WORLD, &my_rank);
  108. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  109. if (starpu_cpu_worker_get_count() == 0)
  110. {
  111. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  112. starpu_mpi_shutdown();
  113. starpu_shutdown();
  114. return 77;
  115. }
  116. parse_args(argc, argv);
  117. /* Initial data values */
  118. starpu_srand48((long int)time(NULL));
  119. for(x = 0; x < X; x++)
  120. {
  121. for (y = 0; y < Y; y++)
  122. {
  123. matrix[x][y] = (float)starpu_drand48();
  124. mean += matrix[x][y];
  125. }
  126. }
  127. mean /= (X*Y);
  128. if (display)
  129. {
  130. FPRINTF_MPI(stdout, "mean=%2.2f\n", mean);
  131. for(x = 0; x < X; x++)
  132. {
  133. fprintf(stdout, "[%d] ", my_rank);
  134. for (y = 0; y < Y; y++)
  135. {
  136. fprintf(stdout, "%2.2f ", matrix[x][y]);
  137. }
  138. fprintf(stdout, "\n");
  139. }
  140. }
  141. /* Initial distribution */
  142. for(x = 0; x < X; x++)
  143. {
  144. for (y = 0; y < Y; y++)
  145. {
  146. int mpi_rank = my_distrib(x, y, size);
  147. if (mpi_rank == my_rank)
  148. {
  149. //FPRINTF(stderr, "[%d] Owning data[%d][%d]\n", my_rank, x, y);
  150. starpu_variable_data_register(&data_handles[x][y], 0, (uintptr_t)&(matrix[x][y]), sizeof(float));
  151. }
  152. else if (my_rank == my_distrib(x+1, y, size) || my_rank == my_distrib(x-1, y, size)
  153. || my_rank == my_distrib(x, y+1, size) || my_rank == my_distrib(x, y-1, size))
  154. {
  155. /* I don't own that index, but will need it for my computations */
  156. //FPRINTF(stderr, "[%d] Neighbour of data[%d][%d]\n", my_rank, x, y);
  157. starpu_variable_data_register(&data_handles[x][y], -1, (uintptr_t)NULL, sizeof(float));
  158. }
  159. else
  160. {
  161. /* I know it's useless to allocate anything for this */
  162. data_handles[x][y] = NULL;
  163. }
  164. if (data_handles[x][y])
  165. {
  166. starpu_data_set_coordinates(data_handles[x][y], 2, x, y);
  167. starpu_mpi_data_register(data_handles[x][y], (y*X)+x, mpi_rank);
  168. }
  169. }
  170. }
  171. /* First computation with initial distribution */
  172. for(loop=0 ; loop<niter; loop++)
  173. {
  174. starpu_iteration_push(loop);
  175. for (x = 1; x < X-1; x++)
  176. {
  177. for (y = 1; y < Y-1; y++)
  178. {
  179. starpu_mpi_task_insert(MPI_COMM_WORLD, &stencil5_cl, STARPU_RW, data_handles[x][y],
  180. STARPU_R, data_handles[x-1][y], STARPU_R, data_handles[x+1][y],
  181. STARPU_R, data_handles[x][y-1], STARPU_R, data_handles[x][y+1],
  182. 0);
  183. }
  184. }
  185. starpu_iteration_pop();
  186. }
  187. FPRINTF(stderr, "Waiting ...\n");
  188. starpu_task_wait_for_all();
  189. /* Now migrate data to a new distribution */
  190. /* First register newly needed data */
  191. for(x = 0; x < X; x++)
  192. {
  193. for (y = 0; y < Y; y++)
  194. {
  195. int mpi_rank = my_distrib2(x, y, size);
  196. if (!data_handles[x][y] && (mpi_rank == my_rank
  197. || my_rank == my_distrib2(x+1, y, size) || my_rank == my_distrib2(x-1, y, size)
  198. || my_rank == my_distrib2(x, y+1, size) || my_rank == my_distrib2(x, y-1, size)))
  199. {
  200. /* Register newly-needed data */
  201. starpu_variable_data_register(&data_handles[x][y], -1, (uintptr_t)NULL, sizeof(float));
  202. starpu_mpi_data_register(data_handles[x][y], (y*X)+x, mpi_rank);
  203. }
  204. if (data_handles[x][y] && mpi_rank != starpu_mpi_data_get_rank(data_handles[x][y]))
  205. /* Migrate the data */
  206. starpu_mpi_data_migrate(MPI_COMM_WORLD, data_handles[x][y], mpi_rank);
  207. }
  208. }
  209. /* Second computation with new distribution */
  210. for(loop=0 ; loop<niter; loop++)
  211. {
  212. starpu_iteration_push(niter + loop);
  213. for (x = 1; x < X-1; x++)
  214. {
  215. for (y = 1; y < Y-1; y++)
  216. {
  217. starpu_mpi_task_insert(MPI_COMM_WORLD, &stencil5_cl, STARPU_RW, data_handles[x][y],
  218. STARPU_R, data_handles[x-1][y], STARPU_R, data_handles[x+1][y],
  219. STARPU_R, data_handles[x][y-1], STARPU_R, data_handles[x][y+1],
  220. 0);
  221. }
  222. }
  223. starpu_iteration_pop();
  224. }
  225. FPRINTF(stderr, "Waiting ...\n");
  226. starpu_task_wait_for_all();
  227. /* Unregister data */
  228. for(x = 0; x < X; x++)
  229. {
  230. for (y = 0; y < Y; y++)
  231. {
  232. if (data_handles[x][y])
  233. {
  234. int mpi_rank = my_distrib(x, y, size);
  235. /* Get back data to original place where the user-provided buffer is. */
  236. starpu_mpi_data_migrate(MPI_COMM_WORLD, data_handles[x][y], mpi_rank);
  237. /* And unregister it */
  238. starpu_data_unregister(data_handles[x][y]);
  239. }
  240. }
  241. }
  242. starpu_mpi_shutdown();
  243. starpu_shutdown();
  244. if (display)
  245. {
  246. FPRINTF(stdout, "[%d] mean=%2.2f\n", my_rank, mean);
  247. for(x = 0; x < X; x++)
  248. {
  249. FPRINTF(stdout, "[%d] ", my_rank);
  250. for (y = 0; y < Y; y++)
  251. {
  252. FPRINTF(stdout, "%2.2f ", matrix[x][y]);
  253. }
  254. FPRINTF(stdout, "\n");
  255. }
  256. }
  257. return 0;
  258. }