stencil5.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2013 Université Bordeaux
  4. * Copyright (C) 2011, 2012, 2013, 2014 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. void stencil5_cpu(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  21. {
  22. unsigned *xy = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  23. unsigned *xm1y = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  24. unsigned *xp1y = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[2]);
  25. unsigned *xym1 = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[3]);
  26. unsigned *xyp1 = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[4]);
  27. //FPRINTF(stdout, "VALUES: %d %d %d %d %d\n", *xy, *xm1y, *xp1y, *xym1, *xyp1);
  28. *xy = (*xy + *xm1y + *xp1y + *xym1 + *xyp1) / 5;
  29. }
  30. struct starpu_codelet stencil5_cl =
  31. {
  32. .cpu_funcs = {stencil5_cpu, NULL},
  33. .nbuffers = 5,
  34. .modes = {STARPU_RW, STARPU_R, STARPU_R, STARPU_R, STARPU_R}
  35. };
  36. #ifdef STARPU_QUICK_CHECK
  37. # define NITER_DEF 5
  38. # define X 3
  39. # define Y 3
  40. #else
  41. # define NITER_DEF 500
  42. # define X 20
  43. # define Y 20
  44. #endif
  45. int display = 0;
  46. int niter = NITER_DEF;
  47. /* Returns the MPI node number where data indexes index is */
  48. int my_distrib(int x, int y, int nb_nodes)
  49. {
  50. /* Block distrib */
  51. return ((int)(x / sqrt(nb_nodes) + (y / sqrt(nb_nodes)) * sqrt(nb_nodes))) % nb_nodes;
  52. }
  53. /* Shifted distribution, for migration example */
  54. int my_distrib2(int x, int y, int nb_nodes)
  55. {
  56. return (my_distrib(x, y, nb_nodes) + 1) % nb_nodes;
  57. }
  58. static void parse_args(int argc, char **argv)
  59. {
  60. int i;
  61. for (i = 1; i < argc; i++)
  62. {
  63. if (strcmp(argv[i], "-iter") == 0)
  64. {
  65. char *argptr;
  66. niter = strtol(argv[++i], &argptr, 10);
  67. }
  68. if (strcmp(argv[i], "-display") == 0)
  69. {
  70. display = 1;
  71. }
  72. }
  73. }
  74. int main(int argc, char **argv)
  75. {
  76. int my_rank, size, x, y, loop;
  77. int value=0, mean=0;
  78. unsigned matrix[X][Y];
  79. starpu_data_handle_t data_handles[X][Y];
  80. int ret = starpu_init(NULL);
  81. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  82. starpu_mpi_init(&argc, &argv, 1);
  83. MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  84. MPI_Comm_size(MPI_COMM_WORLD, &size);
  85. parse_args(argc, argv);
  86. /* Initial data values */
  87. for(x = 0; x < X; x++)
  88. {
  89. for (y = 0; y < Y; y++)
  90. {
  91. matrix[x][y] = (my_rank+1)*10 + value;
  92. value++;
  93. mean += matrix[x][y];
  94. }
  95. }
  96. mean /= value;
  97. /* Initial distribution */
  98. for(x = 0; x < X; x++)
  99. {
  100. for (y = 0; y < Y; y++)
  101. {
  102. int mpi_rank = my_distrib(x, y, size);
  103. if (mpi_rank == my_rank)
  104. {
  105. //FPRINTF(stderr, "[%d] Owning data[%d][%d]\n", my_rank, x, y);
  106. starpu_variable_data_register(&data_handles[x][y], STARPU_MAIN_RAM, (uintptr_t)&(matrix[x][y]), sizeof(unsigned));
  107. }
  108. else if (my_rank == my_distrib(x+1, y, size) || my_rank == my_distrib(x-1, y, size)
  109. || my_rank == my_distrib(x, y+1, size) || my_rank == my_distrib(x, y-1, size))
  110. {
  111. /* I don't own that index, but will need it for my computations */
  112. //FPRINTF(stderr, "[%d] Neighbour of data[%d][%d]\n", my_rank, x, y);
  113. starpu_variable_data_register(&data_handles[x][y], -1, (uintptr_t)NULL, sizeof(unsigned));
  114. }
  115. else
  116. {
  117. /* I know it's useless to allocate anything for this */
  118. data_handles[x][y] = NULL;
  119. }
  120. if (data_handles[x][y])
  121. {
  122. starpu_mpi_data_register(data_handles[x][y], (y*X)+x, mpi_rank);
  123. }
  124. }
  125. }
  126. /* First computation with initial distribution */
  127. for(loop=0 ; loop<niter; loop++)
  128. {
  129. for (x = 1; x < X-1; x++)
  130. {
  131. for (y = 1; y < Y-1; y++)
  132. {
  133. starpu_mpi_task_insert(MPI_COMM_WORLD, &stencil5_cl, STARPU_RW, data_handles[x][y],
  134. STARPU_R, data_handles[x-1][y], STARPU_R, data_handles[x+1][y],
  135. STARPU_R, data_handles[x][y-1], STARPU_R, data_handles[x][y+1],
  136. 0);
  137. }
  138. }
  139. }
  140. FPRINTF(stderr, "Waiting ...\n");
  141. starpu_task_wait_for_all();
  142. /* Now migrate data to a new distribution */
  143. /* First register newly needed data */
  144. for(x = 0; x < X; x++)
  145. {
  146. for (y = 0; y < Y; y++)
  147. {
  148. int mpi_rank = my_distrib2(x, y, size);
  149. if (!data_handles[x][y] && (mpi_rank == my_rank
  150. || my_rank == my_distrib2(x+1, y, size) || my_rank == my_distrib2(x-1, y, size)
  151. || my_rank == my_distrib2(x, y+1, size) || my_rank == my_distrib2(x, y-1, size)))
  152. {
  153. /* Register newly-needed data */
  154. starpu_variable_data_register(&data_handles[x][y], -1, (uintptr_t)NULL, sizeof(unsigned));
  155. starpu_mpi_data_register(data_handles[x][y], (y*X)+x, mpi_rank);
  156. }
  157. if (data_handles[x][y] && mpi_rank != starpu_data_get_rank(data_handles[x][y]))
  158. {
  159. /* Migrate the data */
  160. starpu_mpi_get_data_on_node_detached(MPI_COMM_WORLD, data_handles[x][y], mpi_rank, NULL, NULL);
  161. /* And register new rank of the matrix */
  162. starpu_data_set_rank(data_handles[x][y], mpi_rank);
  163. }
  164. }
  165. }
  166. /* Second computation with new distribution */
  167. for(loop=0 ; loop<niter; loop++)
  168. {
  169. for (x = 1; x < X-1; x++)
  170. {
  171. for (y = 1; y < Y-1; y++)
  172. {
  173. starpu_mpi_task_insert(MPI_COMM_WORLD, &stencil5_cl, STARPU_RW, data_handles[x][y],
  174. STARPU_R, data_handles[x-1][y], STARPU_R, data_handles[x+1][y],
  175. STARPU_R, data_handles[x][y-1], STARPU_R, data_handles[x][y+1],
  176. 0);
  177. }
  178. }
  179. }
  180. FPRINTF(stderr, "Waiting ...\n");
  181. starpu_task_wait_for_all();
  182. /* Unregister data */
  183. for(x = 0; x < X; x++)
  184. {
  185. for (y = 0; y < Y; y++)
  186. {
  187. if (data_handles[x][y])
  188. {
  189. int mpi_rank = my_distrib(x, y, size);
  190. /* Get back data to original place where the user-provided buffer is. */
  191. starpu_mpi_get_data_on_node_detached(MPI_COMM_WORLD, data_handles[x][y], mpi_rank, NULL, NULL);
  192. /* Register original rank of the matrix (although useless) */
  193. starpu_data_set_rank(data_handles[x][y], mpi_rank);
  194. /* And unregister it */
  195. starpu_data_unregister(data_handles[x][y]);
  196. }
  197. }
  198. }
  199. starpu_mpi_shutdown();
  200. starpu_shutdown();
  201. if (display)
  202. {
  203. FPRINTF(stdout, "[%d] mean=%d\n", my_rank, mean);
  204. for(x = 0; x < X; x++)
  205. {
  206. FPRINTF(stdout, "[%d] ", my_rank);
  207. for (y = 0; y < Y; y++)
  208. {
  209. FPRINTF(stdout, "%3u ", matrix[x][y]);
  210. }
  211. FPRINTF(stdout, "\n");
  212. }
  213. }
  214. return 0;
  215. }