stencil5.c 7.5 KB

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