1 // Copyright (C) 2011 JĂșlio Hoffimann.
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)
7 // Message Passing Interface 1.1 -- Section 4.5. Gatherv
8 #ifndef BOOST_MPI_GATHERV_HPP
9 #define BOOST_MPI_GATHERV_HPP
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>
24 namespace boost { namespace mpi {
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.
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_)
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),
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.
45 gatherv_impl(const communicator& comm, const T* in_values, int in_size, int root,
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));
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.
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_)
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_());
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
74 gatherv_impl(const communicator& comm, const T* in_values, int in_size, int root,
77 gather_impl(comm, in_values, in_size, (T*)0,(int const*)0,(int const*)0, root,
80 } // end namespace detail
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,
88 if (comm.rank() == root)
89 detail::gatherv_impl(comm, in_values, in_size,
90 out_values, &sizes[0], &displs[0],
91 root, is_mpi_datatype<T>());
93 detail::gatherv_impl(comm, in_values, in_size, root, is_mpi_datatype<T>());
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,
102 ::boost::mpi::gatherv(comm, &in_values[0], in_values.size(), out_values, sizes, displs, root);
106 void gatherv(const communicator& comm, const T* in_values, int in_size, int root)
108 BOOST_ASSERT(comm.rank() != root);
109 detail::gatherv_impl(comm, in_values, in_size, root, is_mpi_datatype<T>());
113 void gatherv(const communicator& comm, const std::vector<T>& in_values, int root)
115 BOOST_ASSERT(comm.rank() != root);
116 detail::gatherv_impl(comm, &in_values[0], in_values.size(), root, is_mpi_datatype<T>());
119 ///////////////////////
120 // common use versions
121 ///////////////////////
124 gatherv(const communicator& comm, const T* in_values, int in_size,
125 T* out_values, const std::vector<int>& sizes, int root)
127 int nprocs = comm.size();
129 std::vector<int> displs( nprocs );
130 for (int rank = 0, aux = 0; rank < nprocs; ++rank) {
134 ::boost::mpi::gatherv(comm, in_values, in_size, out_values, sizes, displs, root);
139 gatherv(const communicator& comm, const std::vector<T>& in_values,
140 T* out_values, const std::vector<int>& sizes, int root)
142 ::boost::mpi::gatherv(comm, &in_values[0], in_values.size(), out_values, sizes, root);
145 } } // end namespace boost::mpi
147 #endif // BOOST_MPI_GATHERV_HPP