stencil5_lb.c 7.8 KB

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