stencil5.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2013, 2015 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 5
  44. # define Y 5
  45. #else
  46. # define NITER_DEF 100
  47. # define X 20
  48. # define Y 20
  49. #endif
  50. int display = 0;
  51. int niter = NITER_DEF;
  52. /* Returns the MPI node number where data indexes index is */
  53. int my_distrib(int x, int y, int nb_nodes)
  54. {
  55. /* Block distrib */
  56. return ((int)(x / sqrt(nb_nodes) + (y / sqrt(nb_nodes)) * sqrt(nb_nodes))) % nb_nodes;
  57. }
  58. /* Shifted distribution, for migration example */
  59. int my_distrib2(int x, int y, int nb_nodes)
  60. {
  61. return (my_distrib(x, y, nb_nodes) + 1) % nb_nodes;
  62. }
  63. static void parse_args(int argc, char **argv)
  64. {
  65. int i;
  66. for (i = 1; i < argc; i++)
  67. {
  68. if (strcmp(argv[i], "-iter") == 0)
  69. {
  70. char *argptr;
  71. niter = strtol(argv[++i], &argptr, 10);
  72. }
  73. if (strcmp(argv[i], "-display") == 0)
  74. {
  75. display = 1;
  76. }
  77. }
  78. }
  79. int main(int argc, char **argv)
  80. {
  81. int my_rank, size, x, y, loop;
  82. float mean=0;
  83. float matrix[X][Y];
  84. starpu_data_handle_t data_handles[X][Y];
  85. int ret = starpu_init(NULL);
  86. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  87. ret = starpu_mpi_init(&argc, &argv, 1);
  88. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  89. starpu_mpi_comm_rank(MPI_COMM_WORLD, &my_rank);
  90. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  91. if (starpu_cpu_worker_get_count() == 0)
  92. {
  93. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  94. starpu_mpi_shutdown();
  95. starpu_shutdown();
  96. return 77;
  97. }
  98. parse_args(argc, argv);
  99. /* Initial data values */
  100. starpu_srand48((long int)time(NULL));
  101. for(x = 0; x < X; x++)
  102. {
  103. for (y = 0; y < Y; y++)
  104. {
  105. matrix[x][y] = (float)starpu_drand48();
  106. mean += matrix[x][y];
  107. }
  108. }
  109. mean /= (X*Y);
  110. if (display)
  111. {
  112. FPRINTF_MPI(stdout, "mean=%2.2f\n", mean);
  113. for(x = 0; x < X; x++)
  114. {
  115. fprintf(stdout, "[%d] ", my_rank);
  116. for (y = 0; y < Y; y++)
  117. {
  118. fprintf(stdout, "%2.2f ", matrix[x][y]);
  119. }
  120. fprintf(stdout, "\n");
  121. }
  122. }
  123. /* Initial distribution */
  124. for(x = 0; x < X; x++)
  125. {
  126. for (y = 0; y < Y; y++)
  127. {
  128. int mpi_rank = my_distrib(x, y, size);
  129. if (mpi_rank == my_rank)
  130. {
  131. //FPRINTF(stderr, "[%d] Owning data[%d][%d]\n", my_rank, x, y);
  132. starpu_variable_data_register(&data_handles[x][y], 0, (uintptr_t)&(matrix[x][y]), sizeof(float));
  133. }
  134. else if (my_rank == my_distrib(x+1, y, size) || my_rank == my_distrib(x-1, y, size)
  135. || my_rank == my_distrib(x, y+1, size) || my_rank == my_distrib(x, y-1, size))
  136. {
  137. /* I don't own that index, but will need it for my computations */
  138. //FPRINTF(stderr, "[%d] Neighbour of data[%d][%d]\n", my_rank, x, y);
  139. starpu_variable_data_register(&data_handles[x][y], -1, (uintptr_t)NULL, sizeof(float));
  140. }
  141. else
  142. {
  143. /* I know it's useless to allocate anything for this */
  144. data_handles[x][y] = NULL;
  145. }
  146. if (data_handles[x][y])
  147. {
  148. starpu_mpi_data_register(data_handles[x][y], (y*X)+x, mpi_rank);
  149. }
  150. }
  151. }
  152. /* First computation with initial distribution */
  153. for(loop=0 ; loop<niter; loop++)
  154. {
  155. for (x = 1; x < X-1; x++)
  156. {
  157. for (y = 1; y < Y-1; y++)
  158. {
  159. starpu_mpi_task_insert(MPI_COMM_WORLD, &stencil5_cl, STARPU_RW, data_handles[x][y],
  160. STARPU_R, data_handles[x-1][y], STARPU_R, data_handles[x+1][y],
  161. STARPU_R, data_handles[x][y-1], STARPU_R, data_handles[x][y+1],
  162. 0);
  163. }
  164. }
  165. }
  166. FPRINTF(stderr, "Waiting ...\n");
  167. starpu_task_wait_for_all();
  168. /* Now migrate data to a new distribution */
  169. /* First register newly needed data */
  170. for(x = 0; x < X; x++)
  171. {
  172. for (y = 0; y < Y; y++)
  173. {
  174. int mpi_rank = my_distrib2(x, y, size);
  175. if (!data_handles[x][y] && (mpi_rank == my_rank
  176. || my_rank == my_distrib2(x+1, y, size) || my_rank == my_distrib2(x-1, y, size)
  177. || my_rank == my_distrib2(x, y+1, size) || my_rank == my_distrib2(x, y-1, size)))
  178. {
  179. /* Register newly-needed data */
  180. starpu_variable_data_register(&data_handles[x][y], -1, (uintptr_t)NULL, sizeof(float));
  181. starpu_mpi_data_register(data_handles[x][y], (y*X)+x, mpi_rank);
  182. }
  183. if (data_handles[x][y] && mpi_rank != starpu_mpi_data_get_rank(data_handles[x][y]))
  184. {
  185. /* Migrate the data */
  186. starpu_mpi_get_data_on_node_detached(MPI_COMM_WORLD, data_handles[x][y], mpi_rank, NULL, NULL);
  187. /* And register new rank of the matrix */
  188. starpu_mpi_data_set_rank(data_handles[x][y], mpi_rank);
  189. }
  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_get_data_on_node_detached(MPI_COMM_WORLD, data_handles[x][y], mpi_rank, NULL, NULL);
  218. /* Register original rank of the matrix (although useless) */
  219. starpu_mpi_data_set_rank(data_handles[x][y], mpi_rank);
  220. /* And unregister it */
  221. starpu_data_unregister(data_handles[x][y]);
  222. }
  223. }
  224. }
  225. starpu_mpi_shutdown();
  226. starpu_shutdown();
  227. if (display)
  228. {
  229. FPRINTF(stdout, "[%d] mean=%2.2f\n", my_rank, mean);
  230. for(x = 0; x < X; x++)
  231. {
  232. FPRINTF(stdout, "[%d] ", my_rank);
  233. for (y = 0; y < Y; y++)
  234. {
  235. FPRINTF(stdout, "%2.2f ", matrix[x][y]);
  236. }
  237. FPRINTF(stdout, "\n");
  238. }
  239. }
  240. return 0;
  241. }