1 // Copyright (C) 2005, 2006 Douglas Gregor.
2
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6
7 // Message Passing Interface 1.1 -- Section 4.5. Gather
8 #ifndef BOOST_MPI_GATHER_HPP
9 #define BOOST_MPI_GATHER_HPP
10
11 #include <cassert>
12 #include <cstddef>
13 #include <numeric>
14 #include <boost/mpi/exception.hpp>
15 #include <boost/mpi/datatype.hpp>
16 #include <vector>
17 #include <boost/mpi/packed_oarchive.hpp>
18 #include <boost/mpi/packed_iarchive.hpp>
19 #include <boost/mpi/detail/point_to_point.hpp>
20 #include <boost/mpi/communicator.hpp>
21 #include <boost/mpi/environment.hpp>
22 #include <boost/mpi/detail/offsets.hpp>
23 #include <boost/mpi/detail/antiques.hpp>
24 #include <boost/assert.hpp>
25
26 namespace boost { namespace mpi {
27
28 namespace detail {
29 // We're gathering at the root for a type that has an associated MPI
30 // datatype, so we'll use MPI_Gather to do all of the work.
31 template<typename T>
32 void
gather_impl(const communicator & comm,const T * in_values,int n,T * out_values,int root,mpl::true_)33 gather_impl(const communicator& comm, const T* in_values, int n,
34 T* out_values, int root, mpl::true_)
35 {
36 MPI_Datatype type = get_mpi_datatype<T>(*in_values);
37 BOOST_MPI_CHECK_RESULT(MPI_Gather,
38 (const_cast<T*>(in_values), n, type,
39 out_values, n, type, root, comm));
40 }
41
42 // We're gathering from a non-root for a type that has an associated MPI
43 // datatype, so we'll use MPI_Gather to do all of the work.
44 template<typename T>
45 void
gather_impl(const communicator & comm,const T * in_values,int n,int root,mpl::true_ is_mpi_type)46 gather_impl(const communicator& comm, const T* in_values, int n, int root,
47 mpl::true_ is_mpi_type)
48 {
49 assert(comm.rank() != root);
50 gather_impl(comm, in_values, n, (T*)0, root, is_mpi_type);
51 }
52
53 // We're gathering at the root for a type that does not have an
54 // associated MPI datatype, so we'll need to serialize
55 // it.
56 template<typename T>
57 void
gather_impl(const communicator & comm,const T * in_values,int n,T * out_values,int const * nslot,int const * nskip,int root,mpl::false_)58 gather_impl(const communicator& comm, const T* in_values, int n, T* out_values,
59 int const* nslot, int const* nskip, int root, mpl::false_)
60 {
61 int nproc = comm.size();
62 // first, gather all size, these size can be different for
63 // each process
64 packed_oarchive oa(comm);
65 for (int i = 0; i < n; ++i) {
66 oa << in_values[i];
67 }
68 bool is_root = comm.rank() == root;
69 std::vector<int> oasizes(is_root ? nproc : 0);
70 int oasize = oa.size();
71 BOOST_MPI_CHECK_RESULT(MPI_Gather,
72 (&oasize, 1, MPI_INT,
73 c_data(oasizes), 1, MPI_INT,
74 root, MPI_Comm(comm)));
75 // Gather the archives, which can be of different sizes, so
76 // we need to use gatherv.
77 // Everything is contiguous (in the transmitted archive), so
78 // the offsets can be deduced from the collected sizes.
79 std::vector<int> offsets;
80 if (is_root) sizes2offsets(oasizes, offsets);
81 packed_iarchive::buffer_type recv_buffer(is_root ? std::accumulate(oasizes.begin(), oasizes.end(), 0) : 0);
82 BOOST_MPI_CHECK_RESULT(MPI_Gatherv,
83 (const_cast<void*>(oa.address()), int(oa.size()), MPI_BYTE,
84 c_data(recv_buffer), c_data(oasizes), c_data(offsets), MPI_BYTE,
85 root, MPI_Comm(comm)));
86 if (is_root) {
87 for (int src = 0; src < nproc; ++src) {
88 // handle variadic case
89 int nb = nslot ? nslot[src] : n;
90 int skip = nskip ? nskip[src] : 0;
91 std::advance(out_values, skip);
92 if (src == root) {
93 BOOST_ASSERT(nb == n);
94 for (int i = 0; i < nb; ++i) {
95 *out_values++ = *in_values++;
96 }
97 } else {
98 packed_iarchive ia(comm, recv_buffer, boost::archive::no_header, offsets[src]);
99 for (int i = 0; i < nb; ++i) {
100 ia >> *out_values++;
101 }
102 }
103 }
104 }
105 }
106
107 // We're gathering at a non-root for a type that does not have an
108 // associated MPI datatype, so we'll need to serialize
109 // it.
110 template<typename T>
111 void
gather_impl(const communicator & comm,const T * in_values,int n,T * out_values,int root,mpl::false_ is_mpi_type)112 gather_impl(const communicator& comm, const T* in_values, int n, T* out_values,int root,
113 mpl::false_ is_mpi_type)
114 {
115 gather_impl(comm, in_values, n, out_values, (int const*)0, (int const*)0, root, is_mpi_type);
116 }
117 } // end namespace detail
118
119 template<typename T>
120 void
gather(const communicator & comm,const T & in_value,T * out_values,int root)121 gather(const communicator& comm, const T& in_value, T* out_values, int root)
122 {
123 BOOST_ASSERT(out_values || (comm.rank() != root));
124 detail::gather_impl(comm, &in_value, 1, out_values, root, is_mpi_datatype<T>());
125 }
126
127 template<typename T>
gather(const communicator & comm,const T & in_value,int root)128 void gather(const communicator& comm, const T& in_value, int root)
129 {
130 BOOST_ASSERT(comm.rank() != root);
131 detail::gather_impl(comm, &in_value, 1, (T*)0, root, is_mpi_datatype<T>());
132 }
133
134 template<typename T>
135 void
gather(const communicator & comm,const T & in_value,std::vector<T> & out_values,int root)136 gather(const communicator& comm, const T& in_value, std::vector<T>& out_values,
137 int root)
138 {
139 using detail::c_data;
140 if (comm.rank() == root) {
141 out_values.resize(comm.size());
142 }
143 ::boost::mpi::gather(comm, in_value, c_data(out_values), root);
144 }
145
146 template<typename T>
147 void
gather(const communicator & comm,const T * in_values,int n,T * out_values,int root)148 gather(const communicator& comm, const T* in_values, int n, T* out_values,
149 int root)
150 {
151 detail::gather_impl(comm, in_values, n, out_values, root,
152 is_mpi_datatype<T>());
153 }
154
155 template<typename T>
156 void
gather(const communicator & comm,const T * in_values,int n,std::vector<T> & out_values,int root)157 gather(const communicator& comm, const T* in_values, int n,
158 std::vector<T>& out_values, int root)
159 {
160 if (comm.rank() == root) {
161 out_values.resize(comm.size() * n);
162 }
163 ::boost::mpi::gather(comm, in_values, n, out_values.data(), root);
164 }
165
166 template<typename T>
gather(const communicator & comm,const T * in_values,int n,int root)167 void gather(const communicator& comm, const T* in_values, int n, int root)
168 {
169 BOOST_ASSERT(comm.rank() != root);
170 detail::gather_impl(comm, in_values, n, root, is_mpi_datatype<T>());
171 }
172
173
174 } } // end namespace boost::mpi
175
176 #endif // BOOST_MPI_GATHER_HPP
177