starpu_mpi_collective.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2013 Thibaut Lambert
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <mpi.h>
  18. #include <starpu.h>
  19. #include <starpu_mpi.h>
  20. #include <starpu_mpi_private.h>
  21. struct _callback_arg
  22. {
  23. void (*callback)(void *);
  24. void *arg;
  25. int nb;
  26. int count;
  27. };
  28. static
  29. void _callback_collective(void *arg)
  30. {
  31. struct _callback_arg *callback_arg = arg;
  32. callback_arg->nb ++;
  33. if (callback_arg->nb == callback_arg->count)
  34. {
  35. callback_arg->callback(callback_arg->arg);
  36. free(callback_arg);
  37. }
  38. }
  39. static
  40. int _callback_set(int rank, starpu_data_handle_t *data_handles, int count, int root, void (*scallback)(void *), void *sarg, void (*rcallback)(void *), void *rarg, void (**callback_func)(void *), struct _callback_arg **callback_arg)
  41. {
  42. void (*callback)(void *);
  43. callback = (rank == root) ? scallback : rcallback;
  44. if (*callback)
  45. {
  46. int x;
  47. *callback_func = _callback_collective;
  48. _STARPU_MPI_MALLOC(*callback_arg, sizeof(struct _callback_arg));
  49. (*callback_arg)->count = 0;
  50. (*callback_arg)->nb = 0;
  51. (*callback_arg)->callback = (rank == root) ? scallback : rcallback;
  52. (*callback_arg)->arg = (rank == root) ? sarg : rarg;
  53. for(x = 0; x < count ; x++)
  54. {
  55. if (data_handles[x])
  56. {
  57. int owner = starpu_mpi_data_get_rank(data_handles[x]);
  58. starpu_mpi_tag_t data_tag = starpu_mpi_data_get_tag(data_handles[x]);
  59. STARPU_ASSERT_MSG(data_tag >= 0, "Invalid tag for data handle");
  60. if ((rank == root) && (owner != root))
  61. {
  62. (*callback_arg)->count ++;
  63. }
  64. if ((rank != root) && (owner == rank))
  65. {
  66. (*callback_arg)->count ++;
  67. }
  68. }
  69. }
  70. if (!(*callback_arg)->count)
  71. {
  72. free(*callback_arg);
  73. return 1;
  74. }
  75. }
  76. return 0;
  77. }
  78. int starpu_mpi_scatter_detached(starpu_data_handle_t *data_handles, int count, int root, MPI_Comm comm, void (*scallback)(void *), void *sarg, void (*rcallback)(void *), void *rarg)
  79. {
  80. int rank;
  81. int x;
  82. struct _callback_arg *callback_arg = NULL;
  83. void (*callback_func)(void *) = NULL;
  84. starpu_mpi_comm_rank(comm, &rank);
  85. x = _callback_set(rank, data_handles, count, root, scallback, sarg, rcallback, rarg, &callback_func, &callback_arg);
  86. if (x == 1)
  87. return 0;
  88. for(x = 0; x < count ; x++)
  89. {
  90. if (data_handles[x])
  91. {
  92. int owner = starpu_mpi_data_get_rank(data_handles[x]);
  93. starpu_mpi_tag_t data_tag = starpu_mpi_data_get_tag(data_handles[x]);
  94. STARPU_ASSERT_MSG(data_tag >= 0, "Invalid tag for data handle");
  95. if ((rank == root) && (owner != root))
  96. {
  97. //fprintf(stderr, "[%d] Sending data[%d] to %d\n", rank, x, owner);
  98. starpu_mpi_isend_detached(data_handles[x], owner, data_tag, comm, callback_func, callback_arg);
  99. }
  100. if ((rank != root) && (owner == rank))
  101. {
  102. //fprintf(stderr, "[%d] Receiving data[%d] from %d\n", rank, x, root);
  103. starpu_mpi_irecv_detached(data_handles[x], root, data_tag, comm, callback_func, callback_arg);
  104. }
  105. }
  106. }
  107. return 0;
  108. }
  109. int starpu_mpi_gather_detached(starpu_data_handle_t *data_handles, int count, int root, MPI_Comm comm, void (*scallback)(void *), void *sarg, void (*rcallback)(void *), void *rarg)
  110. {
  111. int rank;
  112. int x;
  113. struct _callback_arg *callback_arg = NULL;
  114. void (*callback_func)(void *) = NULL;
  115. starpu_mpi_comm_rank(comm, &rank);
  116. x = _callback_set(rank, data_handles, count, root, scallback, sarg, rcallback, rarg, &callback_func, &callback_arg);
  117. if (x == 1)
  118. return 0;
  119. for(x = 0; x < count ; x++)
  120. {
  121. if (data_handles[x])
  122. {
  123. int owner = starpu_mpi_data_get_rank(data_handles[x]);
  124. starpu_mpi_tag_t data_tag = starpu_mpi_data_get_tag(data_handles[x]);
  125. STARPU_ASSERT_MSG(data_tag >= 0, "Invalid tag for data handle");
  126. if ((rank == root) && (owner != root))
  127. {
  128. //fprintf(stderr, "[%d] Receiving data[%d] from %d\n", rank, x, owner);
  129. starpu_mpi_irecv_detached(data_handles[x], owner, data_tag, comm, callback_func, callback_arg);
  130. }
  131. if ((rank != root) && (owner == rank))
  132. {
  133. //fprintf(stderr, "[%d] Sending data[%d] to %d\n", rank, x, root);
  134. starpu_mpi_isend_detached(data_handles[x], root, data_tag, comm, callback_func, callback_arg);
  135. }
  136. }
  137. }
  138. return 0;
  139. }