stencil5.c 6.5 KB

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