stencil5.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2013 Inria
  4. * Copyright (C) 2011-2017 CNRS
  5. * Copyright (C) 2011-2017 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 = starpu_init(NULL);
  93. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  94. ret = starpu_mpi_init(&argc, &argv, 1);
  95. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  96. starpu_mpi_comm_rank(MPI_COMM_WORLD, &my_rank);
  97. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  98. if (starpu_cpu_worker_get_count() == 0)
  99. {
  100. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  101. starpu_mpi_shutdown();
  102. starpu_shutdown();
  103. return 77;
  104. }
  105. parse_args(argc, argv);
  106. /* Initial data values */
  107. starpu_srand48((long int)time(NULL));
  108. for(x = 0; x < X; x++)
  109. {
  110. for (y = 0; y < Y; y++)
  111. {
  112. matrix[x][y] = (float)starpu_drand48();
  113. mean += matrix[x][y];
  114. }
  115. }
  116. mean /= (X*Y);
  117. if (display)
  118. {
  119. FPRINTF_MPI(stdout, "mean=%2.2f\n", mean);
  120. for(x = 0; x < X; x++)
  121. {
  122. fprintf(stdout, "[%d] ", my_rank);
  123. for (y = 0; y < Y; y++)
  124. {
  125. fprintf(stdout, "%2.2f ", matrix[x][y]);
  126. }
  127. fprintf(stdout, "\n");
  128. }
  129. }
  130. /* Initial distribution */
  131. for(x = 0; x < X; x++)
  132. {
  133. for (y = 0; y < Y; y++)
  134. {
  135. int mpi_rank = my_distrib(x, y, size);
  136. if (mpi_rank == my_rank)
  137. {
  138. //FPRINTF(stderr, "[%d] Owning data[%d][%d]\n", my_rank, x, y);
  139. starpu_variable_data_register(&data_handles[x][y], 0, (uintptr_t)&(matrix[x][y]), sizeof(float));
  140. }
  141. else if (my_rank == my_distrib(x+1, y, size) || my_rank == my_distrib(x-1, y, size)
  142. || my_rank == my_distrib(x, y+1, size) || my_rank == my_distrib(x, y-1, size))
  143. {
  144. /* I don't own that index, but will need it for my computations */
  145. //FPRINTF(stderr, "[%d] Neighbour of data[%d][%d]\n", my_rank, x, y);
  146. starpu_variable_data_register(&data_handles[x][y], -1, (uintptr_t)NULL, sizeof(float));
  147. }
  148. else
  149. {
  150. /* I know it's useless to allocate anything for this */
  151. data_handles[x][y] = NULL;
  152. }
  153. if (data_handles[x][y])
  154. {
  155. starpu_data_set_coordinates(data_handles[x][y], 2, x, y);
  156. starpu_mpi_data_register(data_handles[x][y], (y*X)+x, mpi_rank);
  157. }
  158. }
  159. }
  160. /* First computation with initial distribution */
  161. for(loop=0 ; loop<niter; loop++)
  162. {
  163. starpu_iteration_push(loop);
  164. for (x = 1; x < X-1; x++)
  165. {
  166. for (y = 1; y < Y-1; y++)
  167. {
  168. starpu_mpi_task_insert(MPI_COMM_WORLD, &stencil5_cl, STARPU_RW, data_handles[x][y],
  169. STARPU_R, data_handles[x-1][y], STARPU_R, data_handles[x+1][y],
  170. STARPU_R, data_handles[x][y-1], STARPU_R, data_handles[x][y+1],
  171. 0);
  172. }
  173. }
  174. starpu_iteration_pop();
  175. }
  176. FPRINTF(stderr, "Waiting ...\n");
  177. starpu_task_wait_for_all();
  178. /* Now migrate data to a new distribution */
  179. /* First register newly needed data */
  180. for(x = 0; x < X; x++)
  181. {
  182. for (y = 0; y < Y; y++)
  183. {
  184. int mpi_rank = my_distrib2(x, y, size);
  185. if (!data_handles[x][y] && (mpi_rank == my_rank
  186. || my_rank == my_distrib2(x+1, y, size) || my_rank == my_distrib2(x-1, y, size)
  187. || my_rank == my_distrib2(x, y+1, size) || my_rank == my_distrib2(x, y-1, size)))
  188. {
  189. /* Register newly-needed data */
  190. starpu_variable_data_register(&data_handles[x][y], -1, (uintptr_t)NULL, sizeof(float));
  191. starpu_mpi_data_register(data_handles[x][y], (y*X)+x, mpi_rank);
  192. }
  193. if (data_handles[x][y] && mpi_rank != starpu_mpi_data_get_rank(data_handles[x][y]))
  194. /* Migrate the data */
  195. starpu_mpi_data_migrate(MPI_COMM_WORLD, data_handles[x][y], mpi_rank);
  196. }
  197. }
  198. /* Second computation with new distribution */
  199. for(loop=0 ; loop<niter; loop++)
  200. {
  201. starpu_iteration_push(niter + loop);
  202. for (x = 1; x < X-1; x++)
  203. {
  204. for (y = 1; y < Y-1; y++)
  205. {
  206. starpu_mpi_task_insert(MPI_COMM_WORLD, &stencil5_cl, STARPU_RW, data_handles[x][y],
  207. STARPU_R, data_handles[x-1][y], STARPU_R, data_handles[x+1][y],
  208. STARPU_R, data_handles[x][y-1], STARPU_R, data_handles[x][y+1],
  209. 0);
  210. }
  211. }
  212. starpu_iteration_pop();
  213. }
  214. FPRINTF(stderr, "Waiting ...\n");
  215. starpu_task_wait_for_all();
  216. /* Unregister data */
  217. for(x = 0; x < X; x++)
  218. {
  219. for (y = 0; y < Y; y++)
  220. {
  221. if (data_handles[x][y])
  222. {
  223. int mpi_rank = my_distrib(x, y, size);
  224. /* Get back data to original place where the user-provided buffer is. */
  225. starpu_mpi_data_migrate(MPI_COMM_WORLD, data_handles[x][y], mpi_rank);
  226. /* And unregister it */
  227. starpu_data_unregister(data_handles[x][y]);
  228. }
  229. }
  230. }
  231. starpu_mpi_shutdown();
  232. starpu_shutdown();
  233. if (display)
  234. {
  235. FPRINTF(stdout, "[%d] mean=%2.2f\n", my_rank, mean);
  236. for(x = 0; x < X; x++)
  237. {
  238. FPRINTF(stdout, "[%d] ", my_rank);
  239. for (y = 0; y < Y; y++)
  240. {
  241. FPRINTF(stdout, "%2.2f ", matrix[x][y]);
  242. }
  243. FPRINTF(stdout, "\n");
  244. }
  245. }
  246. return 0;
  247. }