insert_task.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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. MPI_Comm_rank(MPI_COMM_WORLD, xy);
  27. // *xy += *xm1y + *xp1y + *xym1 + *xyp1;
  28. }
  29. starpu_codelet stencil5_cl = {
  30. .where = STARPU_CPU,
  31. .cpu_func = stencil5_cpu,
  32. .nbuffers = 5
  33. };
  34. #define X 3
  35. #define Y 5
  36. /* Returns the MPI node number where data indexes index is */
  37. int my_distrib(int x, int y, int nb_nodes) {
  38. /* Cyclic distrib */
  39. return ((int)(x / sqrt(nb_nodes) + (y / sqrt(nb_nodes)) * sqrt(nb_nodes))) % nb_nodes;
  40. // /* Linear distrib */
  41. // return x / sqrt(nb_nodes) + (y / sqrt(nb_nodes)) * X;
  42. }
  43. int main(int argc, char **argv)
  44. {
  45. int rank, size, x, y;
  46. int value=10;
  47. unsigned matrix[X][Y];
  48. starpu_data_handle data_handles[X][Y];
  49. starpu_init(NULL);
  50. starpu_mpi_initialize_extended(1, &rank, &size);
  51. for(x = 0; x < X; x++) {
  52. for (y = 0; y < Y; y++) {
  53. matrix[x][y] = value;
  54. value++;
  55. }
  56. }
  57. for(x = 0; x < X; x++) {
  58. for (y = 0; y < Y; y++) {
  59. fprintf(stdout, "%4d ", matrix[x][y]);
  60. }
  61. fprintf(stdout, "\n");
  62. }
  63. for(x = 0; x < X; x++) {
  64. for (y = 0; y < Y; y++) {
  65. int mpi_rank = my_distrib(x, y, size);
  66. if (mpi_rank == rank) {
  67. fprintf(stderr, "[%d] Owning data[%d][%d]\n", rank, x, y);
  68. starpu_variable_data_register(&data_handles[x][y], 0, (uintptr_t)&(matrix[x][y]), sizeof(unsigned));
  69. }
  70. else if (rank == mpi_rank+1 || rank == mpi_rank-1) {
  71. /* I don't own that index, but will need it for my computations */
  72. //fprintf(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, x, y);
  73. #warning should use NULL instead of matrix[x][y]
  74. starpu_variable_data_register(&data_handles[x][y], 0, (uintptr_t)&(matrix[x][y]), sizeof(unsigned));
  75. }
  76. else {
  77. /* I know it's useless to allocate anything for this */
  78. data_handles[x][y] = NULL;
  79. }
  80. if (data_handles[x][y])
  81. starpu_data_set_rank(data_handles[x][y], mpi_rank);
  82. }
  83. }
  84. for (x = 1; x < X-1; x++) {
  85. for (y = 1; y < Y-1; y++) {
  86. starpu_mpi_insert_task(MPI_COMM_WORLD, &stencil5_cl, STARPU_RW, data_handles[x][y],
  87. STARPU_R, data_handles[x-1][y], STARPU_R, data_handles[x+1][y],
  88. STARPU_R, data_handles[x][y-1], STARPU_R, data_handles[x][y+1],
  89. 0);
  90. }
  91. }
  92. starpu_mpi_shutdown();
  93. starpu_shutdown();
  94. for(x = 0; x < X; x++) {
  95. for (y = 0; y < Y; y++) {
  96. fprintf(stdout, "%4d ", matrix[x][y]);
  97. }
  98. fprintf(stdout, "\n");
  99. }
  100. return 0;
  101. }