insert_task.c 4.2 KB

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