insert_task.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2015,2017,2018 CNRS
  4. * Copyright (C) 2013 Inria
  5. * Copyright (C) 2011,2013-2015,2017 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_init(NULL);
  50. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  51. ret = starpu_mpi_init(&argc, &argv, 1);
  52. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  53. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  54. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  55. for(x = 0; x < X; x++)
  56. {
  57. for (y = 0; y < Y; y++)
  58. {
  59. matrix[x][y] = (rank+1)*10 + value;
  60. value++;
  61. }
  62. }
  63. #if 0
  64. for(x = 0; x < X; x++)
  65. {
  66. FPRINTF(stdout, "[%d] ", rank);
  67. for (y = 0; y < Y; y++)
  68. {
  69. FPRINTF(stdout, "%3d ", matrix[x][y]);
  70. }
  71. FPRINTF(stdout, "\n");
  72. }
  73. #endif
  74. for(x = 0; x < X; x++)
  75. {
  76. for (y = 0; y < Y; y++)
  77. {
  78. int mpi_rank = my_distrib(x, y, size);
  79. if (mpi_rank == rank)
  80. {
  81. //FPRINTF(stderr, "[%d] Owning data[%d][%d]\n", rank, x, y);
  82. starpu_variable_data_register(&data_handles[x][y], STARPU_MAIN_RAM, (uintptr_t)&(matrix[x][y]), sizeof(unsigned));
  83. }
  84. else
  85. {
  86. /* I don't own this index, but will need it for my computations */
  87. //FPRINTF(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, x, y);
  88. starpu_variable_data_register(&data_handles[x][y], -1, (uintptr_t)NULL, sizeof(unsigned));
  89. }
  90. if (data_handles[x][y])
  91. {
  92. starpu_mpi_data_register(data_handles[x][y], (y*X)+x, mpi_rank);
  93. }
  94. }
  95. }
  96. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[1][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[3][1], STARPU_R, data_handles[0][1], 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[0][1], STARPU_R, data_handles[0][0], 0);
  101. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  102. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[3][1], STARPU_R, data_handles[0][1], 0);
  103. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  104. FPRINTF(stderr, "Waiting ...\n");
  105. starpu_task_wait_for_all();
  106. for(x = 0; x < X; x++)
  107. {
  108. for (y = 0; y < Y; y++)
  109. {
  110. if (data_handles[x][y])
  111. starpu_data_unregister(data_handles[x][y]);
  112. }
  113. }
  114. starpu_mpi_shutdown();
  115. starpu_shutdown();
  116. #if 0
  117. for(x = 0; x < X; x++)
  118. {
  119. FPRINTF(stdout, "[%d] ", rank);
  120. for (y = 0; y < Y; y++)
  121. {
  122. FPRINTF(stdout, "%3d ", matrix[x][y]);
  123. }
  124. FPRINTF(stdout, "\n");
  125. }
  126. #endif
  127. return 0;
  128. }