stencil5_lb.c 7.7 KB

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