stencil5.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 <math.h>
  18. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  19. #define FPRINTF_MPI(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) { \
  20. int _disp_rank; starpu_mpi_comm_rank(MPI_COMM_WORLD, &_disp_rank); \
  21. fprintf(ofile, "[%d][starpu_mpi][%s] " fmt , _disp_rank, __starpu_func__ ,## __VA_ARGS__); \
  22. fflush(ofile); }} while(0);
  23. void stencil5_cpu(void *descr[], void *_args)
  24. {
  25. (void)_args;
  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. struct starpu_codelet stencil5_cl =
  36. {
  37. .cpu_funcs = {stencil5_cpu},
  38. .nbuffers = 5,
  39. .modes = {STARPU_RW, STARPU_R, STARPU_R, STARPU_R, STARPU_R},
  40. .model = &starpu_perfmodel_nop,
  41. };
  42. #ifdef STARPU_QUICK_CHECK
  43. # define NITER_DEF 5
  44. # define X 4
  45. # define Y 4
  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. /* Shifted distribution, for migration example */
  64. int my_distrib2(int x, int y, int nb_nodes)
  65. {
  66. return (my_distrib(x, y, nb_nodes) + 1) % nb_nodes;
  67. }
  68. static void parse_args(int argc, char **argv)
  69. {
  70. int i;
  71. for (i = 1; i < argc; i++)
  72. {
  73. if (strcmp(argv[i], "-iter") == 0)
  74. {
  75. char *argptr;
  76. niter = strtol(argv[++i], &argptr, 10);
  77. }
  78. if (strcmp(argv[i], "-display") == 0)
  79. {
  80. display = 1;
  81. }
  82. }
  83. }
  84. int main(int argc, char **argv)
  85. {
  86. int my_rank, size, x, y, loop;
  87. float mean=0;
  88. float matrix[X][Y];
  89. starpu_data_handle_t data_handles[X][Y];
  90. int ret;
  91. ret = starpu_mpi_init_conf(&argc, &argv, 1, MPI_COMM_WORLD, NULL);
  92. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  93. starpu_mpi_comm_rank(MPI_COMM_WORLD, &my_rank);
  94. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  95. if (starpu_cpu_worker_get_count() == 0)
  96. {
  97. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  98. starpu_mpi_shutdown();
  99. return 77;
  100. }
  101. parse_args(argc, argv);
  102. /* Initial data values */
  103. starpu_srand48((long int)time(NULL));
  104. for(x = 0; x < X; x++)
  105. {
  106. for (y = 0; y < Y; y++)
  107. {
  108. matrix[x][y] = (float)starpu_drand48();
  109. mean += matrix[x][y];
  110. }
  111. }
  112. mean /= (X*Y);
  113. if (display)
  114. {
  115. FPRINTF_MPI(stdout, "mean=%2.2f\n", mean);
  116. for(x = 0; x < X; x++)
  117. {
  118. fprintf(stdout, "[%d] ", my_rank);
  119. for (y = 0; y < Y; y++)
  120. {
  121. fprintf(stdout, "%2.2f ", matrix[x][y]);
  122. }
  123. fprintf(stdout, "\n");
  124. }
  125. }
  126. /* Initial distribution */
  127. for(x = 0; x < X; x++)
  128. {
  129. for (y = 0; y < Y; y++)
  130. {
  131. int mpi_rank = my_distrib(x, y, size);
  132. if (mpi_rank == my_rank)
  133. {
  134. //FPRINTF(stderr, "[%d] Owning data[%d][%d]\n", my_rank, x, y);
  135. starpu_variable_data_register(&data_handles[x][y], 0, (uintptr_t)&(matrix[x][y]), sizeof(float));
  136. }
  137. else if (my_rank == my_distrib(x+1, y, size) || my_rank == my_distrib(x-1, y, size)
  138. || my_rank == my_distrib(x, y+1, size) || my_rank == my_distrib(x, y-1, size))
  139. {
  140. /* I don't own this index, but will need it for my computations */
  141. //FPRINTF(stderr, "[%d] Neighbour of data[%d][%d]\n", my_rank, x, y);
  142. starpu_variable_data_register(&data_handles[x][y], -1, (uintptr_t)NULL, sizeof(float));
  143. }
  144. else
  145. {
  146. /* I know it's useless to allocate anything for this */
  147. data_handles[x][y] = NULL;
  148. }
  149. if (data_handles[x][y])
  150. {
  151. starpu_data_set_coordinates(data_handles[x][y], 2, x, y);
  152. starpu_mpi_data_register(data_handles[x][y], (y*X)+x, mpi_rank);
  153. }
  154. }
  155. }
  156. /* First computation with initial distribution */
  157. for(loop=0 ; loop<niter; loop++)
  158. {
  159. starpu_iteration_push(loop);
  160. for (x = 1; x < X-1; x++)
  161. {
  162. for (y = 1; y < Y-1; y++)
  163. {
  164. starpu_mpi_task_insert(MPI_COMM_WORLD, &stencil5_cl, STARPU_RW, data_handles[x][y],
  165. STARPU_R, data_handles[x-1][y], STARPU_R, data_handles[x+1][y],
  166. STARPU_R, data_handles[x][y-1], STARPU_R, data_handles[x][y+1],
  167. 0);
  168. }
  169. }
  170. starpu_iteration_pop();
  171. }
  172. FPRINTF(stderr, "Waiting ...\n");
  173. starpu_task_wait_for_all();
  174. /* Now migrate data to a new distribution */
  175. /* First register newly needed data */
  176. for(x = 0; x < X; x++)
  177. {
  178. for (y = 0; y < Y; y++)
  179. {
  180. int mpi_rank = my_distrib2(x, y, size);
  181. if (!data_handles[x][y] && (mpi_rank == my_rank
  182. || my_rank == my_distrib2(x+1, y, size) || my_rank == my_distrib2(x-1, y, size)
  183. || my_rank == my_distrib2(x, y+1, size) || my_rank == my_distrib2(x, y-1, size)))
  184. {
  185. /* Register newly-needed data */
  186. starpu_variable_data_register(&data_handles[x][y], -1, (uintptr_t)NULL, sizeof(float));
  187. starpu_mpi_data_register(data_handles[x][y], (y*X)+x, mpi_rank);
  188. }
  189. if (data_handles[x][y] && mpi_rank != starpu_mpi_data_get_rank(data_handles[x][y]))
  190. /* Migrate the data */
  191. starpu_mpi_data_migrate(MPI_COMM_WORLD, data_handles[x][y], mpi_rank);
  192. }
  193. }
  194. /* Second computation with new distribution */
  195. for(loop=0 ; loop<niter; loop++)
  196. {
  197. starpu_iteration_push(niter + loop);
  198. for (x = 1; x < X-1; x++)
  199. {
  200. for (y = 1; y < Y-1; y++)
  201. {
  202. starpu_mpi_task_insert(MPI_COMM_WORLD, &stencil5_cl, STARPU_RW, data_handles[x][y],
  203. STARPU_R, data_handles[x-1][y], STARPU_R, data_handles[x+1][y],
  204. STARPU_R, data_handles[x][y-1], STARPU_R, data_handles[x][y+1],
  205. 0);
  206. }
  207. }
  208. starpu_iteration_pop();
  209. }
  210. FPRINTF(stderr, "Waiting ...\n");
  211. starpu_task_wait_for_all();
  212. /* Unregister data */
  213. for(x = 0; x < X; x++)
  214. {
  215. for (y = 0; y < Y; y++)
  216. {
  217. if (data_handles[x][y])
  218. {
  219. int mpi_rank = my_distrib(x, y, size);
  220. /* Get back data to original place where the user-provided buffer is. */
  221. starpu_mpi_data_migrate(MPI_COMM_WORLD, data_handles[x][y], mpi_rank);
  222. /* And unregister it */
  223. starpu_data_unregister(data_handles[x][y]);
  224. }
  225. }
  226. }
  227. starpu_mpi_shutdown();
  228. if (display)
  229. {
  230. FPRINTF(stdout, "[%d] mean=%2.2f\n", my_rank, mean);
  231. for(x = 0; x < X; x++)
  232. {
  233. FPRINTF(stdout, "[%d] ", my_rank);
  234. for (y = 0; y < Y; y++)
  235. {
  236. FPRINTF(stdout, "%2.2f ", matrix[x][y]);
  237. }
  238. FPRINTF(stdout, "\n");
  239. }
  240. }
  241. return 0;
  242. }