stencil5.c 7.3 KB

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