stencil5.c 7.6 KB

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