all_to_all.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright (C) 2005, 2006 Douglas Gregor.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Message Passing Interface 1.1 -- Section 4.8. All-to-all
  6. #ifndef BOOST_MPI_ALL_TO_ALL_HPP
  7. #define BOOST_MPI_ALL_TO_ALL_HPP
  8. #include <boost/mpi/exception.hpp>
  9. #include <boost/mpi/datatype.hpp>
  10. #include <vector>
  11. #include <boost/mpi/packed_oarchive.hpp>
  12. #include <boost/mpi/packed_iarchive.hpp>
  13. #include <boost/mpi/communicator.hpp>
  14. #include <boost/mpi/environment.hpp>
  15. #include <boost/assert.hpp>
  16. #include <boost/mpi/collectives_fwd.hpp>
  17. #include <boost/mpi/allocator.hpp>
  18. namespace boost { namespace mpi {
  19. namespace detail {
  20. // We're performaing an all-to-all with a type that has an
  21. // associated MPI datatype, so we'll use MPI_Alltoall to do all of
  22. // the work.
  23. template<typename T>
  24. void
  25. all_to_all_impl(const communicator& comm, const T* in_values, int n,
  26. T* out_values, mpl::true_)
  27. {
  28. MPI_Datatype type = get_mpi_datatype<T>(*in_values);
  29. BOOST_MPI_CHECK_RESULT(MPI_Alltoall,
  30. (const_cast<T*>(in_values), n, type,
  31. out_values, n, type, comm));
  32. }
  33. // We're performing an all-to-all with a type that does not have an
  34. // associated MPI datatype, so we'll need to serialize
  35. // it. Unfortunately, this means that we cannot use MPI_Alltoall, so
  36. // we'll just have to send individual messages to the other
  37. // processes.
  38. template<typename T>
  39. void
  40. all_to_all_impl(const communicator& comm, const T* in_values, int n,
  41. T* out_values, mpl::false_)
  42. {
  43. int size = comm.size();
  44. int rank = comm.rank();
  45. // The amount of data to be sent to each process
  46. std::vector<int> send_sizes(size);
  47. // The displacements for each outgoing value.
  48. std::vector<int> send_disps(size);
  49. // The buffer that will store all of the outgoing values
  50. std::vector<char, allocator<char> > outgoing;
  51. // Pack the buffer with all of the outgoing values.
  52. for (int dest = 0; dest < size; ++dest) {
  53. // Keep track of the displacements
  54. send_disps[dest] = outgoing.size();
  55. // Our own value will never be transmitted, so don't pack it.
  56. if (dest != rank) {
  57. packed_oarchive oa(comm, outgoing);
  58. for (int i = 0; i < n; ++i)
  59. oa << in_values[dest * n + i];
  60. }
  61. // Keep track of the sizes
  62. send_sizes[dest] = outgoing.size() - send_disps[dest];
  63. }
  64. // Determine how much data each process will receive.
  65. std::vector<int> recv_sizes(size);
  66. all_to_all(comm, send_sizes, recv_sizes);
  67. // Prepare a buffer to receive the incoming data.
  68. std::vector<int> recv_disps(size);
  69. int sum = 0;
  70. for (int src = 0; src < size; ++src) {
  71. recv_disps[src] = sum;
  72. sum += recv_sizes[src];
  73. }
  74. std::vector<char, allocator<char> > incoming(sum > 0? sum : 1);
  75. // Make sure we don't try to reference an empty vector
  76. if (outgoing.empty())
  77. outgoing.push_back(0);
  78. // Transmit the actual data
  79. BOOST_MPI_CHECK_RESULT(MPI_Alltoallv,
  80. (&outgoing[0], &send_sizes[0],
  81. &send_disps[0], MPI_PACKED,
  82. &incoming[0], &recv_sizes[0],
  83. &recv_disps[0], MPI_PACKED,
  84. comm));
  85. // Deserialize data from the iarchive
  86. for (int src = 0; src < size; ++src) {
  87. if (src == rank)
  88. std::copy(in_values + src * n, in_values + (src + 1) * n,
  89. out_values + src * n);
  90. else {
  91. packed_iarchive ia(comm, incoming, boost::archive::no_header,
  92. recv_disps[src]);
  93. for (int i = 0; i < n; ++i)
  94. ia >> out_values[src * n + i];
  95. }
  96. }
  97. }
  98. } // end namespace detail
  99. template<typename T>
  100. inline void
  101. all_to_all(const communicator& comm, const T* in_values, T* out_values)
  102. {
  103. detail::all_to_all_impl(comm, in_values, 1, out_values, is_mpi_datatype<T>());
  104. }
  105. template<typename T>
  106. void
  107. all_to_all(const communicator& comm, const std::vector<T>& in_values,
  108. std::vector<T>& out_values)
  109. {
  110. BOOST_ASSERT((int)in_values.size() == comm.size());
  111. out_values.resize(comm.size());
  112. ::boost::mpi::all_to_all(comm, &in_values[0], &out_values[0]);
  113. }
  114. template<typename T>
  115. inline void
  116. all_to_all(const communicator& comm, const T* in_values, int n, T* out_values)
  117. {
  118. detail::all_to_all_impl(comm, in_values, n, out_values, is_mpi_datatype<T>());
  119. }
  120. template<typename T>
  121. void
  122. all_to_all(const communicator& comm, const std::vector<T>& in_values, int n,
  123. std::vector<T>& out_values)
  124. {
  125. BOOST_ASSERT((int)in_values.size() == comm.size() * n);
  126. out_values.resize(comm.size() * n);
  127. ::boost::mpi::all_to_all(comm, &in_values[0], n, &out_values[0]);
  128. }
  129. } } // end namespace boost::mpi
  130. #endif // BOOST_MPI_ALL_TO_ALL_HPP