all_to_all.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 performing 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.
  36. template<typename T>
  37. void
  38. all_to_all_impl(const communicator& comm, const T* in_values, int n,
  39. T* out_values, mpl::false_)
  40. {
  41. int size = comm.size();
  42. int rank = comm.rank();
  43. // The amount of data to be sent to each process
  44. std::vector<int> send_sizes(size);
  45. // The displacements for each outgoing value.
  46. std::vector<int> send_disps(size);
  47. // The buffer that will store all of the outgoing values
  48. std::vector<char, allocator<char> > outgoing;
  49. // Pack the buffer with all of the outgoing values.
  50. for (int dest = 0; dest < size; ++dest) {
  51. // Keep track of the displacements
  52. send_disps[dest] = outgoing.size();
  53. // Our own value will never be transmitted, so don't pack it.
  54. if (dest != rank) {
  55. packed_oarchive oa(comm, outgoing);
  56. for (int i = 0; i < n; ++i)
  57. oa << in_values[dest * n + i];
  58. }
  59. // Keep track of the sizes
  60. send_sizes[dest] = outgoing.size() - send_disps[dest];
  61. }
  62. // Determine how much data each process will receive.
  63. std::vector<int> recv_sizes(size);
  64. all_to_all(comm, send_sizes, recv_sizes);
  65. // Prepare a buffer to receive the incoming data.
  66. std::vector<int> recv_disps(size);
  67. int sum = 0;
  68. for (int src = 0; src < size; ++src) {
  69. recv_disps[src] = sum;
  70. sum += recv_sizes[src];
  71. }
  72. std::vector<char, allocator<char> > incoming(sum > 0? sum : 1);
  73. // Make sure we don't try to reference an empty vector
  74. if (outgoing.empty())
  75. outgoing.push_back(0);
  76. // Transmit the actual data
  77. BOOST_MPI_CHECK_RESULT(MPI_Alltoallv,
  78. (&outgoing[0], &send_sizes[0],
  79. &send_disps[0], MPI_PACKED,
  80. &incoming[0], &recv_sizes[0],
  81. &recv_disps[0], MPI_PACKED,
  82. comm));
  83. // Deserialize data from the iarchive
  84. for (int src = 0; src < size; ++src) {
  85. if (src == rank)
  86. std::copy(in_values + src * n, in_values + (src + 1) * n,
  87. out_values + src * n);
  88. else {
  89. packed_iarchive ia(comm, incoming, boost::archive::no_header,
  90. recv_disps[src]);
  91. for (int i = 0; i < n; ++i)
  92. ia >> out_values[src * n + i];
  93. }
  94. }
  95. }
  96. } // end namespace detail
  97. template<typename T>
  98. inline void
  99. all_to_all(const communicator& comm, const T* in_values, T* out_values)
  100. {
  101. detail::all_to_all_impl(comm, in_values, 1, out_values, is_mpi_datatype<T>());
  102. }
  103. template<typename T>
  104. void
  105. all_to_all(const communicator& comm, const std::vector<T>& in_values,
  106. std::vector<T>& out_values)
  107. {
  108. BOOST_ASSERT((int)in_values.size() == comm.size());
  109. out_values.resize(comm.size());
  110. ::boost::mpi::all_to_all(comm, &in_values[0], &out_values[0]);
  111. }
  112. template<typename T>
  113. inline void
  114. all_to_all(const communicator& comm, const T* in_values, int n, T* out_values)
  115. {
  116. detail::all_to_all_impl(comm, in_values, n, out_values, is_mpi_datatype<T>());
  117. }
  118. template<typename T>
  119. void
  120. all_to_all(const communicator& comm, const std::vector<T>& in_values, int n,
  121. std::vector<T>& out_values)
  122. {
  123. BOOST_ASSERT((int)in_values.size() == comm.size() * n);
  124. out_values.resize(comm.size() * n);
  125. ::boost::mpi::all_to_all(comm, &in_values[0], n, &out_values[0]);
  126. }
  127. } } // end namespace boost::mpi
  128. #endif // BOOST_MPI_ALL_TO_ALL_HPP