File indexing completed on 2025-01-18 09:40:56
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef BOOST_MPI_GATHER_HPP
0009 #define BOOST_MPI_GATHER_HPP
0010
0011 #include <cassert>
0012 #include <cstddef>
0013 #include <numeric>
0014 #include <boost/mpi/exception.hpp>
0015 #include <boost/mpi/datatype.hpp>
0016 #include <vector>
0017 #include <boost/mpi/packed_oarchive.hpp>
0018 #include <boost/mpi/packed_iarchive.hpp>
0019 #include <boost/mpi/detail/point_to_point.hpp>
0020 #include <boost/mpi/communicator.hpp>
0021 #include <boost/mpi/environment.hpp>
0022 #include <boost/mpi/detail/offsets.hpp>
0023 #include <boost/mpi/detail/antiques.hpp>
0024 #include <boost/assert.hpp>
0025
0026 namespace boost { namespace mpi {
0027
0028 namespace detail {
0029
0030
0031 template<typename T>
0032 void
0033 gather_impl(const communicator& comm, const T* in_values, int n,
0034 T* out_values, int root, mpl::true_)
0035 {
0036 MPI_Datatype type = get_mpi_datatype<T>(*in_values);
0037 BOOST_MPI_CHECK_RESULT(MPI_Gather,
0038 (const_cast<T*>(in_values), n, type,
0039 out_values, n, type, root, comm));
0040 }
0041
0042
0043
0044 template<typename T>
0045 void
0046 gather_impl(const communicator& comm, const T* in_values, int n, int root,
0047 mpl::true_ is_mpi_type)
0048 {
0049 assert(comm.rank() != root);
0050 gather_impl(comm, in_values, n, (T*)0, root, is_mpi_type);
0051 }
0052
0053
0054
0055
0056 template<typename T>
0057 void
0058 gather_impl(const communicator& comm, const T* in_values, int n, T* out_values,
0059 int const* nslot, int const* nskip, int root, mpl::false_)
0060 {
0061 int nproc = comm.size();
0062
0063
0064 packed_oarchive oa(comm);
0065 for (int i = 0; i < n; ++i) {
0066 oa << in_values[i];
0067 }
0068 bool is_root = comm.rank() == root;
0069 std::vector<int> oasizes(is_root ? nproc : 0);
0070 int oasize = oa.size();
0071 BOOST_MPI_CHECK_RESULT(MPI_Gather,
0072 (&oasize, 1, MPI_INT,
0073 c_data(oasizes), 1, MPI_INT,
0074 root, MPI_Comm(comm)));
0075
0076
0077
0078
0079 std::vector<int> offsets;
0080 if (is_root) sizes2offsets(oasizes, offsets);
0081 packed_iarchive::buffer_type recv_buffer(is_root ? std::accumulate(oasizes.begin(), oasizes.end(), 0) : 0);
0082 BOOST_MPI_CHECK_RESULT(MPI_Gatherv,
0083 (const_cast<void*>(oa.address()), int(oa.size()), MPI_BYTE,
0084 c_data(recv_buffer), c_data(oasizes), c_data(offsets), MPI_BYTE,
0085 root, MPI_Comm(comm)));
0086 if (is_root) {
0087 for (int src = 0; src < nproc; ++src) {
0088
0089 int nb = nslot ? nslot[src] : n;
0090 int skip = nskip ? nskip[src] : 0;
0091 std::advance(out_values, skip);
0092 if (src == root) {
0093 BOOST_ASSERT(nb == n);
0094 for (int i = 0; i < nb; ++i) {
0095 *out_values++ = *in_values++;
0096 }
0097 } else {
0098 packed_iarchive ia(comm, recv_buffer, boost::archive::no_header, offsets[src]);
0099 for (int i = 0; i < nb; ++i) {
0100 ia >> *out_values++;
0101 }
0102 }
0103 }
0104 }
0105 }
0106
0107
0108
0109
0110 template<typename T>
0111 void
0112 gather_impl(const communicator& comm, const T* in_values, int n, T* out_values,int root,
0113 mpl::false_ is_mpi_type)
0114 {
0115 gather_impl(comm, in_values, n, out_values, (int const*)0, (int const*)0, root, is_mpi_type);
0116 }
0117 }
0118
0119 template<typename T>
0120 void
0121 gather(const communicator& comm, const T& in_value, T* out_values, int root)
0122 {
0123 BOOST_ASSERT(out_values || (comm.rank() != root));
0124 detail::gather_impl(comm, &in_value, 1, out_values, root, is_mpi_datatype<T>());
0125 }
0126
0127 template<typename T>
0128 void gather(const communicator& comm, const T& in_value, int root)
0129 {
0130 BOOST_ASSERT(comm.rank() != root);
0131 detail::gather_impl(comm, &in_value, 1, (T*)0, root, is_mpi_datatype<T>());
0132 }
0133
0134 template<typename T>
0135 void
0136 gather(const communicator& comm, const T& in_value, std::vector<T>& out_values,
0137 int root)
0138 {
0139 using detail::c_data;
0140 if (comm.rank() == root) {
0141 out_values.resize(comm.size());
0142 }
0143 ::boost::mpi::gather(comm, in_value, c_data(out_values), root);
0144 }
0145
0146 template<typename T>
0147 void
0148 gather(const communicator& comm, const T* in_values, int n, T* out_values,
0149 int root)
0150 {
0151 detail::gather_impl(comm, in_values, n, out_values, root,
0152 is_mpi_datatype<T>());
0153 }
0154
0155 template<typename T>
0156 void
0157 gather(const communicator& comm, const T* in_values, int n,
0158 std::vector<T>& out_values, int root)
0159 {
0160 if (comm.rank() == root) {
0161 out_values.resize(comm.size() * n);
0162 }
0163 ::boost::mpi::gather(comm, in_values, n, out_values.data(), root);
0164 }
0165
0166 template<typename T>
0167 void gather(const communicator& comm, const T* in_values, int n, int root)
0168 {
0169 BOOST_ASSERT(comm.rank() != root);
0170 detail::gather_impl(comm, in_values, n, root, is_mpi_datatype<T>());
0171 }
0172
0173
0174 } }
0175
0176 #endif