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