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. Gatherv
8 #ifndef BOOST_MPI_ALLGATHERV_HPP
9 #define BOOST_MPI_ALLGATHERV_HPP
10
11 #include <cassert>
12 #include <cstddef>
13 #include <numeric>
14 #include <vector>
15
16 #include <boost/mpi/exception.hpp>
17 #include <boost/mpi/datatype.hpp>
18 #include <boost/mpi/packed_oarchive.hpp>
19 #include <boost/mpi/packed_iarchive.hpp>
20 #include <boost/mpi/detail/point_to_point.hpp>
21 #include <boost/mpi/communicator.hpp>
22 #include <boost/mpi/environment.hpp>
23 #include <boost/mpi/detail/offsets.hpp>
24 #include <boost/mpi/detail/antiques.hpp>
25 #include <boost/assert.hpp>
26 #include <boost/scoped_array.hpp>
27
28 namespace boost { namespace mpi {
29
30 namespace detail {
31 // We're all-gathering for a type that has an associated MPI
32 // datatype, so we'll use MPI_Gather to do all of the work.
33 template<typename T>
34 void
all_gatherv_impl(const communicator & comm,const T * in_values,T * out_values,int const * sizes,int const * displs,mpl::true_)35 all_gatherv_impl(const communicator& comm, const T* in_values,
36 T* out_values, int const* sizes, int const* displs, mpl::true_)
37 {
38 // Make displacements if not provided
39 scoped_array<int> new_offsets_mem(make_offsets(comm, sizes, displs, -1));
40 if (new_offsets_mem) displs = new_offsets_mem.get();
41 MPI_Datatype type = get_mpi_datatype<T>(*in_values);
42 BOOST_MPI_CHECK_RESULT(MPI_Allgatherv,
43 (const_cast<T*>(in_values), sizes[comm.rank()], type,
44 out_values,
45 const_cast<int*>(sizes),
46 const_cast<int*>(displs),
47 type,
48 comm));
49 }
50
51 // We're all-gathering for a type that does not have an
52 // associated MPI datatype, so we'll need to serialize
53 // it.
54 template<typename T>
55 void
all_gatherv_impl(const communicator & comm,const T * in_values,T * out_values,int const * sizes,int const * displs,mpl::false_ isnt_mpi_type)56 all_gatherv_impl(const communicator& comm, const T* in_values,
57 T* out_values, int const* sizes, int const* displs,
58 mpl::false_ isnt_mpi_type)
59 {
60 // convert displacement to offsets to skip
61 scoped_array<int> skipped(make_skipped_slots(comm, sizes, displs));
62 all_gather_impl(comm, in_values, sizes[comm.rank()], out_values,
63 sizes, skipped.get(), isnt_mpi_type);
64 }
65 } // end namespace detail
66
67 template<typename T>
68 void
all_gatherv(const communicator & comm,const T & in_value,T * out_values,const std::vector<int> & sizes)69 all_gatherv(const communicator& comm, const T& in_value, T* out_values,
70 const std::vector<int>& sizes)
71 {
72 using detail::c_data;
73 assert(sizes.size() == comm.size());
74 assert(sizes[comm.rank()] == 1);
75 detail::all_gatherv_impl(comm, &in_value, out_values, c_data(sizes), 0, is_mpi_datatype<T>());
76 }
77
78 template<typename T>
79 void
all_gatherv(const communicator & comm,const T * in_values,T * out_values,const std::vector<int> & sizes)80 all_gatherv(const communicator& comm, const T* in_values, T* out_values,
81 const std::vector<int>& sizes)
82 {
83 using detail::c_data;
84 assert(int(sizes.size()) == comm.size());
85 detail::all_gatherv_impl(comm, in_values, out_values, c_data(sizes), 0, is_mpi_datatype<T>());
86 }
87
88 template<typename T>
89 void
all_gatherv(const communicator & comm,std::vector<T> const & in_values,std::vector<T> & out_values,const std::vector<int> & sizes)90 all_gatherv(const communicator& comm, std::vector<T> const& in_values, std::vector<T>& out_values,
91 const std::vector<int>& sizes)
92 {
93 using detail::c_data;
94 assert(int(sizes.size()) == comm.size());
95 assert(int(in_values.size()) == sizes[comm.rank()]);
96 out_values.resize(std::accumulate(sizes.begin(), sizes.end(), 0));
97 ::boost::mpi::all_gatherv(comm, c_data(in_values), c_data(out_values), sizes);
98 }
99
100
101 template<typename T>
102 void
all_gatherv(const communicator & comm,const T & in_value,T * out_values,const std::vector<int> & sizes,const std::vector<int> & displs)103 all_gatherv(const communicator& comm, const T& in_value, T* out_values,
104 const std::vector<int>& sizes, const std::vector<int>& displs)
105 {
106 using detail::c_data;
107 assert(sizes.size() == comm.size());
108 assert(displs.size() == comm.size());
109 detail::all_gatherv_impl(comm, &in_value, 1, out_values,
110 c_data(sizes), c_data(displs), is_mpi_datatype<T>());
111 }
112
113 template<typename T>
114 void
all_gatherv(const communicator & comm,const T * in_values,T * out_values,const std::vector<int> & sizes,const std::vector<int> & displs)115 all_gatherv(const communicator& comm, const T* in_values, T* out_values,
116 const std::vector<int>& sizes, const std::vector<int>& displs)
117 {
118 using detail::c_data;
119 assert(sizes.size() == comm.size());
120 assert(displs.size() == comm.size());
121 detail::all_gatherv_impl(comm, in_values, out_values,
122 c_data(sizes), c_data(displs), is_mpi_datatype<T>());
123 }
124
125 template<typename T>
126 void
all_gatherv(const communicator & comm,std::vector<T> const & in_values,std::vector<T> & out_values,const std::vector<int> & sizes,const std::vector<int> & displs)127 all_gatherv(const communicator& comm, std::vector<T> const& in_values, std::vector<T>& out_values,
128 const std::vector<int>& sizes, const std::vector<int>& displs)
129 {
130 using detail::c_data;
131 assert(sizes.size() == comm.size());
132 assert(displs.size() == comm.size());
133 assert(in_values.size() == sizes[comm.rank()]);
134 out_values.resize(std::accumulate(sizes.begin(), sizes.end(), 0));
135 ::boost::mpi::all_gatherv(comm, c_data(in_values), c_data(out_values), sizes, displs);
136 }
137
138 } } // end namespace boost::mpi
139
140 #endif // BOOST_MPI_ALL_GATHERV_HPP
141