stencil5.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2012 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. void stencil5_cpu(void *descr[], __attribute__ ((unused)) void *_args)
  19. {
  20. unsigned *xy = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  21. unsigned *xm1y = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  22. unsigned *xp1y = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[2]);
  23. unsigned *xym1 = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[3]);
  24. unsigned *xyp1 = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[4]);
  25. // fprintf(stdout, "VALUES: %d %d %d %d %d\n", *xy, *xm1y, *xp1y, *xym1, *xyp1);
  26. *xy = (*xy + *xm1y + *xp1y + *xym1 + *xyp1) / 5;
  27. }
  28. struct starpu_codelet stencil5_cl =
  29. {
  30. .where = STARPU_CPU,
  31. .cpu_funcs = {stencil5_cpu, NULL},
  32. .nbuffers = 5,
  33. .modes = {STARPU_RW, STARPU_R, STARPU_R, STARPU_R, STARPU_R}
  34. };
  35. #define NITER_DEF 500
  36. #define X 20
  37. #define Y 20
  38. int display = 0;
  39. int niter = NITER_DEF;
  40. /* Returns the MPI node number where data indexes index is */
  41. int my_distrib(int x, int y, int nb_nodes)
  42. {
  43. /* Block distrib */
  44. return ((int)(x / sqrt(nb_nodes) + (y / sqrt(nb_nodes)) * sqrt(nb_nodes))) % nb_nodes;
  45. }
  46. static void parse_args(int argc, char **argv)
  47. {
  48. int i;
  49. for (i = 1; i < argc; i++)
  50. {
  51. if (strcmp(argv[i], "-iter") == 0)
  52. {
  53. char *argptr;
  54. niter = strtol(argv[++i], &argptr, 10);
  55. }
  56. if (strcmp(argv[i], "-display") == 0)
  57. {
  58. display = 1;
  59. }
  60. }
  61. }
  62. int main(int argc, char **argv)
  63. {
  64. int my_rank, size, x, y, loop;
  65. int value=0, mean=0;
  66. unsigned matrix[X][Y];
  67. starpu_data_handle_t data_handles[X][Y];
  68. int ret = starpu_init(NULL);
  69. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  70. starpu_mpi_initialize_extended(&my_rank, &size);
  71. parse_args(argc, argv);
  72. for(x = 0; x < X; x++)
  73. {
  74. for (y = 0; y < Y; y++)
  75. {
  76. matrix[x][y] = (my_rank+1)*10 + value;
  77. value++;
  78. mean += matrix[x][y];
  79. }
  80. }
  81. mean /= value;
  82. for(x = 0; x < X; x++)
  83. {
  84. for (y = 0; y < Y; y++)
  85. {
  86. int mpi_rank = my_distrib(x, y, size);
  87. if (mpi_rank == my_rank)
  88. {
  89. //fprintf(stderr, "[%d] Owning data[%d][%d]\n", my_rank, x, y);
  90. starpu_variable_data_register(&data_handles[x][y], 0, (uintptr_t)&(matrix[x][y]), sizeof(unsigned));
  91. }
  92. else if (my_rank == my_distrib(x+1, y, size) || my_rank == my_distrib(x-1, y, size)
  93. || my_rank == my_distrib(x, y+1, size) || my_rank == my_distrib(x, y-1, size))
  94. {
  95. /* I don't own that index, but will need it for my computations */
  96. //fprintf(stderr, "[%d] Neighbour of data[%d][%d]\n", my_rank, x, y);
  97. starpu_variable_data_register(&data_handles[x][y], -1, (uintptr_t)NULL, sizeof(unsigned));
  98. }
  99. else
  100. {
  101. /* I know it's useless to allocate anything for this */
  102. data_handles[x][y] = NULL;
  103. }
  104. if (data_handles[x][y])
  105. {
  106. starpu_data_set_rank(data_handles[x][y], mpi_rank);
  107. starpu_data_set_tag(data_handles[x][y], (y*X)+x);
  108. }
  109. }
  110. }
  111. for(loop=0 ; loop<niter; loop++)
  112. {
  113. for (x = 1; x < X-1; x++)
  114. {
  115. for (y = 1; y < Y-1; y++)
  116. {
  117. starpu_mpi_insert_task(MPI_COMM_WORLD, &stencil5_cl, STARPU_RW, data_handles[x][y],
  118. STARPU_R, data_handles[x-1][y], STARPU_R, data_handles[x+1][y],
  119. STARPU_R, data_handles[x][y-1], STARPU_R, data_handles[x][y+1],
  120. 0);
  121. }
  122. }
  123. }
  124. fprintf(stderr, "Waiting ...\n");
  125. starpu_task_wait_for_all();
  126. starpu_mpi_shutdown();
  127. starpu_shutdown();
  128. if (display)
  129. {
  130. fprintf(stdout, "[%d] mean=%d\n", my_rank, mean);
  131. for(x = 0; x < X; x++)
  132. {
  133. fprintf(stdout, "[%d] ", my_rank);
  134. for (y = 0; y < Y; y++)
  135. {
  136. fprintf(stdout, "%3d ", matrix[x][y]);
  137. }
  138. fprintf(stdout, "\n");
  139. }
  140. }
  141. return 0;
  142. }