stencil5.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2013, 2015-2016 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_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. for (x = 1; x < X-1; x++)
  160. {
  161. for (y = 1; y < Y-1; y++)
  162. {
  163. starpu_mpi_task_insert(MPI_COMM_WORLD, &stencil5_cl, STARPU_RW, data_handles[x][y],
  164. STARPU_R, data_handles[x-1][y], STARPU_R, data_handles[x+1][y],
  165. STARPU_R, data_handles[x][y-1], STARPU_R, data_handles[x][y+1],
  166. 0);
  167. }
  168. }
  169. }
  170. FPRINTF(stderr, "Waiting ...\n");
  171. starpu_task_wait_for_all();
  172. /* Now migrate data to a new distribution */
  173. /* First register newly needed data */
  174. for(x = 0; x < X; x++)
  175. {
  176. for (y = 0; y < Y; y++)
  177. {
  178. int mpi_rank = my_distrib2(x, y, size);
  179. if (!data_handles[x][y] && (mpi_rank == my_rank
  180. || my_rank == my_distrib2(x+1, y, size) || my_rank == my_distrib2(x-1, y, size)
  181. || my_rank == my_distrib2(x, y+1, size) || my_rank == my_distrib2(x, y-1, size)))
  182. {
  183. /* Register newly-needed data */
  184. starpu_variable_data_register(&data_handles[x][y], -1, (uintptr_t)NULL, sizeof(float));
  185. starpu_mpi_data_register(data_handles[x][y], (y*X)+x, mpi_rank);
  186. }
  187. if (data_handles[x][y] && mpi_rank != starpu_mpi_data_get_rank(data_handles[x][y]))
  188. /* Migrate the data */
  189. starpu_mpi_data_migrate(MPI_COMM_WORLD, data_handles[x][y], mpi_rank);
  190. }
  191. }
  192. /* Second computation with new distribution */
  193. for(loop=0 ; loop<niter; loop++)
  194. {
  195. for (x = 1; x < X-1; x++)
  196. {
  197. for (y = 1; y < Y-1; y++)
  198. {
  199. starpu_mpi_task_insert(MPI_COMM_WORLD, &stencil5_cl, STARPU_RW, data_handles[x][y],
  200. STARPU_R, data_handles[x-1][y], STARPU_R, data_handles[x+1][y],
  201. STARPU_R, data_handles[x][y-1], STARPU_R, data_handles[x][y+1],
  202. 0);
  203. }
  204. }
  205. }
  206. FPRINTF(stderr, "Waiting ...\n");
  207. starpu_task_wait_for_all();
  208. /* Unregister data */
  209. for(x = 0; x < X; x++)
  210. {
  211. for (y = 0; y < Y; y++)
  212. {
  213. if (data_handles[x][y])
  214. {
  215. int mpi_rank = my_distrib(x, y, size);
  216. /* Get back data to original place where the user-provided buffer is. */
  217. starpu_mpi_data_migrate(MPI_COMM_WORLD, data_handles[x][y], mpi_rank);
  218. /* And unregister it */
  219. starpu_data_unregister(data_handles[x][y]);
  220. }
  221. }
  222. }
  223. starpu_mpi_shutdown();
  224. starpu_shutdown();
  225. if (display)
  226. {
  227. FPRINTF(stdout, "[%d] mean=%2.2f\n", my_rank, mean);
  228. for(x = 0; x < X; x++)
  229. {
  230. FPRINTF(stdout, "[%d] ", my_rank);
  231. for (y = 0; y < Y; y++)
  232. {
  233. FPRINTF(stdout, "%2.2f ", matrix[x][y]);
  234. }
  235. FPRINTF(stdout, "\n");
  236. }
  237. }
  238. return 0;
  239. }