stencil5.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012,2013,2018 Inria
  4. * Copyright (C) 2011-2018 CNRS
  5. * Copyright (C) 2011-2018 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu_mpi.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. /* Shifted distribution, for migration example */
  66. int my_distrib2(int x, int y, int nb_nodes)
  67. {
  68. return (my_distrib(x, y, nb_nodes) + 1) % nb_nodes;
  69. }
  70. static void parse_args(int argc, char **argv)
  71. {
  72. int i;
  73. for (i = 1; i < argc; i++)
  74. {
  75. if (strcmp(argv[i], "-iter") == 0)
  76. {
  77. char *argptr;
  78. niter = strtol(argv[++i], &argptr, 10);
  79. }
  80. if (strcmp(argv[i], "-display") == 0)
  81. {
  82. display = 1;
  83. }
  84. }
  85. }
  86. int main(int argc, char **argv)
  87. {
  88. int my_rank, size, x, y, loop;
  89. float mean=0;
  90. float matrix[X][Y];
  91. starpu_data_handle_t data_handles[X][Y];
  92. int ret;
  93. ret = starpu_mpi_init_conf(&argc, &argv, 1, MPI_COMM_WORLD, NULL);
  94. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  95. starpu_mpi_comm_rank(MPI_COMM_WORLD, &my_rank);
  96. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  97. if (starpu_cpu_worker_get_count() == 0)
  98. {
  99. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  100. starpu_mpi_shutdown();
  101. return 77;
  102. }
  103. parse_args(argc, argv);
  104. /* Initial data values */
  105. starpu_srand48((long int)time(NULL));
  106. for(x = 0; x < X; x++)
  107. {
  108. for (y = 0; y < Y; y++)
  109. {
  110. matrix[x][y] = (float)starpu_drand48();
  111. mean += matrix[x][y];
  112. }
  113. }
  114. mean /= (X*Y);
  115. if (display)
  116. {
  117. FPRINTF_MPI(stdout, "mean=%2.2f\n", mean);
  118. for(x = 0; x < X; x++)
  119. {
  120. fprintf(stdout, "[%d] ", my_rank);
  121. for (y = 0; y < Y; y++)
  122. {
  123. fprintf(stdout, "%2.2f ", matrix[x][y]);
  124. }
  125. fprintf(stdout, "\n");
  126. }
  127. }
  128. /* Initial distribution */
  129. for(x = 0; x < X; x++)
  130. {
  131. for (y = 0; y < Y; y++)
  132. {
  133. int mpi_rank = my_distrib(x, y, size);
  134. if (mpi_rank == my_rank)
  135. {
  136. //FPRINTF(stderr, "[%d] Owning data[%d][%d]\n", my_rank, x, y);
  137. starpu_variable_data_register(&data_handles[x][y], 0, (uintptr_t)&(matrix[x][y]), sizeof(float));
  138. }
  139. else if (my_rank == my_distrib(x+1, y, size) || my_rank == my_distrib(x-1, y, size)
  140. || my_rank == my_distrib(x, y+1, size) || my_rank == my_distrib(x, y-1, size))
  141. {
  142. /* I don't own this index, but will need it for my computations */
  143. //FPRINTF(stderr, "[%d] Neighbour of data[%d][%d]\n", my_rank, x, y);
  144. starpu_variable_data_register(&data_handles[x][y], -1, (uintptr_t)NULL, sizeof(float));
  145. }
  146. else
  147. {
  148. /* I know it's useless to allocate anything for this */
  149. data_handles[x][y] = NULL;
  150. }
  151. if (data_handles[x][y])
  152. {
  153. starpu_data_set_coordinates(data_handles[x][y], 2, x, y);
  154. starpu_mpi_data_register(data_handles[x][y], (y*X)+x, mpi_rank);
  155. }
  156. }
  157. }
  158. /* First computation with initial distribution */
  159. for(loop=0 ; loop<niter; loop++)
  160. {
  161. starpu_iteration_push(loop);
  162. for (x = 1; x < X-1; x++)
  163. {
  164. for (y = 1; y < Y-1; y++)
  165. {
  166. starpu_mpi_task_insert(MPI_COMM_WORLD, &stencil5_cl, STARPU_RW, data_handles[x][y],
  167. STARPU_R, data_handles[x-1][y], STARPU_R, data_handles[x+1][y],
  168. STARPU_R, data_handles[x][y-1], STARPU_R, data_handles[x][y+1],
  169. 0);
  170. }
  171. }
  172. starpu_iteration_pop();
  173. }
  174. FPRINTF(stderr, "Waiting ...\n");
  175. starpu_task_wait_for_all();
  176. /* Now migrate data to a new distribution */
  177. /* First register newly needed data */
  178. for(x = 0; x < X; x++)
  179. {
  180. for (y = 0; y < Y; y++)
  181. {
  182. int mpi_rank = my_distrib2(x, y, size);
  183. if (!data_handles[x][y] && (mpi_rank == my_rank
  184. || my_rank == my_distrib2(x+1, y, size) || my_rank == my_distrib2(x-1, y, size)
  185. || my_rank == my_distrib2(x, y+1, size) || my_rank == my_distrib2(x, y-1, size)))
  186. {
  187. /* Register newly-needed data */
  188. starpu_variable_data_register(&data_handles[x][y], -1, (uintptr_t)NULL, sizeof(float));
  189. starpu_mpi_data_register(data_handles[x][y], (y*X)+x, mpi_rank);
  190. }
  191. if (data_handles[x][y] && mpi_rank != starpu_mpi_data_get_rank(data_handles[x][y]))
  192. /* Migrate the data */
  193. starpu_mpi_data_migrate(MPI_COMM_WORLD, data_handles[x][y], mpi_rank);
  194. }
  195. }
  196. /* Second computation with new distribution */
  197. for(loop=0 ; loop<niter; loop++)
  198. {
  199. starpu_iteration_push(niter + loop);
  200. for (x = 1; x < X-1; x++)
  201. {
  202. for (y = 1; y < Y-1; y++)
  203. {
  204. starpu_mpi_task_insert(MPI_COMM_WORLD, &stencil5_cl, STARPU_RW, data_handles[x][y],
  205. STARPU_R, data_handles[x-1][y], STARPU_R, data_handles[x+1][y],
  206. STARPU_R, data_handles[x][y-1], STARPU_R, data_handles[x][y+1],
  207. 0);
  208. }
  209. }
  210. starpu_iteration_pop();
  211. }
  212. FPRINTF(stderr, "Waiting ...\n");
  213. starpu_task_wait_for_all();
  214. /* Unregister data */
  215. for(x = 0; x < X; x++)
  216. {
  217. for (y = 0; y < Y; y++)
  218. {
  219. if (data_handles[x][y])
  220. {
  221. int mpi_rank = my_distrib(x, y, size);
  222. /* Get back data to original place where the user-provided buffer is. */
  223. starpu_mpi_data_migrate(MPI_COMM_WORLD, data_handles[x][y], mpi_rank);
  224. /* And unregister it */
  225. starpu_data_unregister(data_handles[x][y]);
  226. }
  227. }
  228. }
  229. starpu_mpi_shutdown();
  230. if (display)
  231. {
  232. FPRINTF(stdout, "[%d] mean=%2.2f\n", my_rank, mean);
  233. for(x = 0; x < X; x++)
  234. {
  235. FPRINTF(stdout, "[%d] ", my_rank);
  236. for (y = 0; y < Y; y++)
  237. {
  238. FPRINTF(stdout, "%2.2f ", matrix[x][y]);
  239. }
  240. FPRINTF(stdout, "\n");
  241. }
  242. }
  243. return 0;
  244. }