starpu_mpi_collective.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2017 CNRS
  4. * Copyright (C) 2015 Université de Bordeaux
  5. * Copyright (C) 2013 Thibaut Lambert
  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 <mpi.h>
  19. #include <starpu.h>
  20. #include <starpu_mpi.h>
  21. #include <starpu_mpi_private.h>
  22. struct _callback_arg
  23. {
  24. void (*callback)(void *);
  25. void *arg;
  26. int nb;
  27. int count;
  28. };
  29. static
  30. void _callback_collective(void *arg)
  31. {
  32. struct _callback_arg *callback_arg = arg;
  33. callback_arg->nb ++;
  34. if (callback_arg->nb == callback_arg->count)
  35. {
  36. callback_arg->callback(callback_arg->arg);
  37. free(callback_arg);
  38. }
  39. }
  40. static
  41. 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)
  42. {
  43. void (*callback)(void *);
  44. callback = (rank == root) ? scallback : rcallback;
  45. if (*callback)
  46. {
  47. int x;
  48. *callback_func = _callback_collective;
  49. _STARPU_MPI_MALLOC(*callback_arg, sizeof(struct _callback_arg));
  50. (*callback_arg)->count = 0;
  51. (*callback_arg)->nb = 0;
  52. (*callback_arg)->callback = (rank == root) ? scallback : rcallback;
  53. (*callback_arg)->arg = (rank == root) ? sarg : rarg;
  54. for(x = 0; x < count ; x++)
  55. {
  56. if (data_handles[x])
  57. {
  58. int owner = starpu_mpi_data_get_rank(data_handles[x]);
  59. starpu_mpi_tag_t data_tag = starpu_mpi_data_get_tag(data_handles[x]);
  60. STARPU_ASSERT_MSG(data_tag >= 0, "Invalid tag for data handle");
  61. if ((rank == root) && (owner != root))
  62. {
  63. (*callback_arg)->count ++;
  64. }
  65. if ((rank != root) && (owner == rank))
  66. {
  67. (*callback_arg)->count ++;
  68. }
  69. }
  70. }
  71. if (!(*callback_arg)->count)
  72. {
  73. free(*callback_arg);
  74. return 1;
  75. }
  76. }
  77. return 0;
  78. }
  79. 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)
  80. {
  81. int rank;
  82. int x;
  83. struct _callback_arg *callback_arg = NULL;
  84. void (*callback_func)(void *) = NULL;
  85. starpu_mpi_comm_rank(comm, &rank);
  86. x = _callback_set(rank, data_handles, count, root, scallback, sarg, rcallback, rarg, &callback_func, &callback_arg);
  87. if (x == 1)
  88. return 0;
  89. for(x = 0; x < count ; x++)
  90. {
  91. if (data_handles[x])
  92. {
  93. int owner = starpu_mpi_data_get_rank(data_handles[x]);
  94. starpu_mpi_tag_t data_tag = starpu_mpi_data_get_tag(data_handles[x]);
  95. STARPU_ASSERT_MSG(data_tag >= 0, "Invalid tag for data handle");
  96. if ((rank == root) && (owner != root))
  97. {
  98. //fprintf(stderr, "[%d] Sending data[%d] to %d\n", rank, x, owner);
  99. starpu_mpi_isend_detached(data_handles[x], owner, data_tag, comm, callback_func, callback_arg);
  100. }
  101. if ((rank != root) && (owner == rank))
  102. {
  103. //fprintf(stderr, "[%d] Receiving data[%d] from %d\n", rank, x, root);
  104. starpu_mpi_irecv_detached(data_handles[x], root, data_tag, comm, callback_func, callback_arg);
  105. }
  106. }
  107. }
  108. return 0;
  109. }
  110. 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)
  111. {
  112. int rank;
  113. int x;
  114. struct _callback_arg *callback_arg = NULL;
  115. void (*callback_func)(void *) = NULL;
  116. starpu_mpi_comm_rank(comm, &rank);
  117. x = _callback_set(rank, data_handles, count, root, scallback, sarg, rcallback, rarg, &callback_func, &callback_arg);
  118. if (x == 1)
  119. return 0;
  120. for(x = 0; x < count ; x++)
  121. {
  122. if (data_handles[x])
  123. {
  124. int owner = starpu_mpi_data_get_rank(data_handles[x]);
  125. starpu_mpi_tag_t data_tag = starpu_mpi_data_get_tag(data_handles[x]);
  126. STARPU_ASSERT_MSG(data_tag >= 0, "Invalid tag for data handle");
  127. if ((rank == root) && (owner != root))
  128. {
  129. //fprintf(stderr, "[%d] Receiving data[%d] from %d\n", rank, x, owner);
  130. starpu_mpi_irecv_detached(data_handles[x], owner, data_tag, comm, callback_func, callback_arg);
  131. }
  132. if ((rank != root) && (owner == rank))
  133. {
  134. //fprintf(stderr, "[%d] Sending data[%d] to %d\n", rank, x, root);
  135. starpu_mpi_isend_detached(data_handles[x], root, data_tag, comm, callback_func, callback_arg);
  136. }
  137. }
  138. }
  139. return 0;
  140. }