stencil5_lb.c 7.7 KB

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