starpu_mpi_collective.c 4.6 KB

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