insert_task.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 Inria
  4. * Copyright (C) 2011-2015,2017,2018 CNRS
  5. * Copyright (C) 2011,2013-2015,2017,2018 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu_mpi.h>
  19. #include <math.h>
  20. #include "helper.h"
  21. void func_cpu(void *descr[], void *_args)
  22. {
  23. (void)_args;
  24. unsigned *x = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  25. unsigned *y = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  26. FPRINTF(stdout, "VALUES: %u %u\n", *x, *y);
  27. *x = (*x + *y) / 2;
  28. }
  29. struct starpu_codelet mycodelet =
  30. {
  31. .cpu_funcs = {func_cpu},
  32. .nbuffers = 2,
  33. .modes = {STARPU_RW, STARPU_R},
  34. .model = &starpu_perfmodel_nop,
  35. };
  36. #define X 4
  37. #define Y 5
  38. /* Returns the MPI node number where data indexes index is */
  39. int my_distrib(int x, int y, int nb_nodes)
  40. {
  41. return (x + y) % nb_nodes;
  42. }
  43. int main(int argc, char **argv)
  44. {
  45. int rank, size, x, y;
  46. int value=0, ret;
  47. unsigned matrix[X][Y];
  48. starpu_data_handle_t data_handles[X][Y];
  49. ret = starpu_mpi_init_conf(&argc, &argv, 1, MPI_COMM_WORLD, NULL);
  50. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  51. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  52. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  53. for(x = 0; x < X; x++)
  54. {
  55. for (y = 0; y < Y; y++)
  56. {
  57. matrix[x][y] = (rank+1)*10 + value;
  58. value++;
  59. }
  60. }
  61. #if 0
  62. for(x = 0; x < X; x++)
  63. {
  64. FPRINTF(stdout, "[%d] ", rank);
  65. for (y = 0; y < Y; y++)
  66. {
  67. FPRINTF(stdout, "%3d ", matrix[x][y]);
  68. }
  69. FPRINTF(stdout, "\n");
  70. }
  71. #endif
  72. for(x = 0; x < X; x++)
  73. {
  74. for (y = 0; y < Y; y++)
  75. {
  76. int mpi_rank = my_distrib(x, y, size);
  77. if (mpi_rank == rank)
  78. {
  79. //FPRINTF(stderr, "[%d] Owning data[%d][%d]\n", rank, x, y);
  80. starpu_variable_data_register(&data_handles[x][y], STARPU_MAIN_RAM, (uintptr_t)&(matrix[x][y]), sizeof(unsigned));
  81. }
  82. else
  83. {
  84. /* I don't own this index, but will need it for my computations */
  85. //FPRINTF(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, x, y);
  86. starpu_variable_data_register(&data_handles[x][y], -1, (uintptr_t)NULL, sizeof(unsigned));
  87. }
  88. if (data_handles[x][y])
  89. {
  90. starpu_mpi_data_register(data_handles[x][y], (y*X)+x, mpi_rank);
  91. }
  92. }
  93. }
  94. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[1][1], STARPU_R, data_handles[0][1], 0);
  95. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  96. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[3][1], STARPU_R, data_handles[0][1], 0);
  97. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  98. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[0][1], STARPU_R, data_handles[0][0], 0);
  99. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  100. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[3][1], STARPU_R, data_handles[0][1], 0);
  101. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  102. FPRINTF(stderr, "Waiting ...\n");
  103. starpu_task_wait_for_all();
  104. for(x = 0; x < X; x++)
  105. {
  106. for (y = 0; y < Y; y++)
  107. {
  108. if (data_handles[x][y])
  109. starpu_data_unregister(data_handles[x][y]);
  110. }
  111. }
  112. starpu_mpi_shutdown();
  113. #if 0
  114. for(x = 0; x < X; x++)
  115. {
  116. FPRINTF(stdout, "[%d] ", rank);
  117. for (y = 0; y < Y; y++)
  118. {
  119. FPRINTF(stdout, "%3d ", matrix[x][y]);
  120. }
  121. FPRINTF(stdout, "\n");
  122. }
  123. #endif
  124. return 0;
  125. }