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 <boost/mpi/exception.hpp>
12 #include <boost/mpi/datatype.hpp>
13 #include <vector>
14 #include <boost/mpi/packed_oarchive.hpp>
15 #include <boost/mpi/packed_iarchive.hpp>
16 #include <boost/mpi/detail/point_to_point.hpp>
17 #include <boost/mpi/communicator.hpp>
18 #include <boost/mpi/environment.hpp>
19 #include <boost/assert.hpp>
20 
21 namespace boost { namespace mpi {
22 
23 namespace detail {
24   // We're gathering at the root for a type that has an associated MPI
25   // datatype, so we'll use MPI_Gather to do all of the work.
26   template<typename T>
27   void
gather_impl(const communicator & comm,const T * in_values,int n,T * out_values,int root,mpl::true_)28   gather_impl(const communicator& comm, const T* in_values, int n,
29               T* out_values, int root, mpl::true_)
30   {
31     MPI_Datatype type = get_mpi_datatype<T>(*in_values);
32     BOOST_MPI_CHECK_RESULT(MPI_Gather,
33                            (const_cast<T*>(in_values), n, type,
34                             out_values, n, type, root, comm));
35   }
36 
37   // We're gathering from a non-root for a type that has an associated MPI
38   // datatype, so we'll use MPI_Gather to do all of the work.
39   template<typename T>
40   void
gather_impl(const communicator & comm,const T * in_values,int n,int root,mpl::true_)41   gather_impl(const communicator& comm, const T* in_values, int n, int root,
42               mpl::true_)
43   {
44     MPI_Datatype type = get_mpi_datatype<T>(*in_values);
45     BOOST_MPI_CHECK_RESULT(MPI_Gather,
46                            (const_cast<T*>(in_values), n, type,
47                             0, n, type, root, comm));
48   }
49 
50   // We're gathering at the root for a type that does not have an
51   // associated MPI datatype, so we'll need to serialize
52   // it. Unfortunately, this means that we cannot use MPI_Gather, so
53   // we'll just have all of the non-root nodes send individual
54   // messages to the root.
55   template<typename T>
56   void
gather_impl(const communicator & comm,const T * in_values,int n,T * out_values,int root,mpl::false_)57   gather_impl(const communicator& comm, const T* in_values, int n,
58               T* out_values, int root, mpl::false_)
59   {
60     int tag = environment::collectives_tag();
61     int size = comm.size();
62 
63     for (int src = 0; src < size; ++src) {
64       if (src == root)
65         std::copy(in_values, in_values + n, out_values + n * src);
66       else
67         comm.recv(src, tag, out_values + n * src, n);
68     }
69   }
70 
71   // We're gathering at a non-root for a type that does not have an
72   // associated MPI datatype, so we'll need to serialize
73   // it. Unfortunately, this means that we cannot use MPI_Gather, so
74   // we'll just have all of the non-root nodes send individual
75   // messages to the root.
76   template<typename T>
77   void
gather_impl(const communicator & comm,const T * in_values,int n,int root,mpl::false_)78   gather_impl(const communicator& comm, const T* in_values, int n, int root,
79               mpl::false_)
80   {
81     int tag = environment::collectives_tag();
82     comm.send(root, tag, in_values, n);
83   }
84 } // end namespace detail
85 
86 template<typename T>
87 void
gather(const communicator & comm,const T & in_value,T * out_values,int root)88 gather(const communicator& comm, const T& in_value, T* out_values, int root)
89 {
90   if (comm.rank() == root)
91     detail::gather_impl(comm, &in_value, 1, out_values, root,
92                         is_mpi_datatype<T>());
93   else
94     detail::gather_impl(comm, &in_value, 1, root, is_mpi_datatype<T>());
95 }
96 
97 template<typename T>
gather(const communicator & comm,const T & in_value,int root)98 void gather(const communicator& comm, const T& in_value, int root)
99 {
100   BOOST_ASSERT(comm.rank() != root);
101   detail::gather_impl(comm, &in_value, 1, root, is_mpi_datatype<T>());
102 }
103 
104 template<typename T>
105 void
gather(const communicator & comm,const T & in_value,std::vector<T> & out_values,int root)106 gather(const communicator& comm, const T& in_value, std::vector<T>& out_values,
107        int root)
108 {
109   if (comm.rank() == root) {
110     out_values.resize(comm.size());
111     ::boost::mpi::gather(comm, in_value, &out_values[0], root);
112   } else {
113     ::boost::mpi::gather(comm, in_value, root);
114   }
115 }
116 
117 template<typename T>
118 void
gather(const communicator & comm,const T * in_values,int n,T * out_values,int root)119 gather(const communicator& comm, const T* in_values, int n, T* out_values,
120        int root)
121 {
122   if (comm.rank() == root)
123     detail::gather_impl(comm, in_values, n, out_values, root,
124                         is_mpi_datatype<T>());
125   else
126     detail::gather_impl(comm, in_values, n, root, is_mpi_datatype<T>());
127 }
128 
129 template<typename T>
130 void
gather(const communicator & comm,const T * in_values,int n,std::vector<T> & out_values,int root)131 gather(const communicator& comm, const T* in_values, int n,
132        std::vector<T>& out_values, int root)
133 {
134   if (comm.rank() == root) {
135     out_values.resize(comm.size() * n);
136     ::boost::mpi::gather(comm, in_values, n, &out_values[0], root);
137   }
138   else
139     ::boost::mpi::gather(comm, in_values, n, root);
140 }
141 
142 template<typename T>
gather(const communicator & comm,const T * in_values,int n,int root)143 void gather(const communicator& comm, const T* in_values, int n, int root)
144 {
145   BOOST_ASSERT(comm.rank() != root);
146   detail::gather_impl(comm, in_values, n, root, is_mpi_datatype<T>());
147 }
148 
149 
150 } } // end namespace boost::mpi
151 
152 #endif // BOOST_MPI_GATHER_HPP
153