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
11 #include <boost/mpi/exception.hpp>
12 #include <boost/mpi/datatype.hpp>
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>
21 namespace boost { namespace mpi {
24 // We're gathering at the root for a type that has an associated MPI
25 // datatype, so we'll use MPI_Gatherv to do all of the work.
28 gatherv_impl(const communicator& comm, const T* in_values, int in_size,
29 T* out_values, const int* sizes, const int* displs, int root, mpl::true_)
31 MPI_Datatype type = get_mpi_datatype<T>(*in_values);
32 BOOST_MPI_CHECK_RESULT(MPI_Gatherv,
33 (const_cast<T*>(in_values), in_size, type,
34 out_values, const_cast<int*>(sizes), const_cast<int*>(displs),
38 // We're gathering from a non-root for a type that has an associated MPI
39 // datatype, so we'll use MPI_Gatherv to do all of the work.
42 gatherv_impl(const communicator& comm, const T* in_values, int in_size, int root,
45 MPI_Datatype type = get_mpi_datatype<T>(*in_values);
46 BOOST_MPI_CHECK_RESULT(MPI_Gatherv,
47 (const_cast<T*>(in_values), in_size, type,
48 0, 0, 0, type, root, comm));
51 // We're gathering at the root for a type that does not have an
52 // associated MPI datatype, so we'll need to serialize
53 // it. Unfortunately, this means that we cannot use MPI_Gatherv, so
54 // we'll just have all of the non-root nodes send individual
55 // messages to the root.
58 gatherv_impl(const communicator& comm, const T* in_values, int in_size,
59 T* out_values, const int* sizes, const int* displs, int root, mpl::false_)
61 int tag = environment::collectives_tag();
62 int nprocs = comm.size();
64 for (int src = 0; src < nprocs; ++src) {
66 // Our own values will never be transmitted: just copy them.
67 std::copy(in_values, in_values + in_size, out_values + displs[src]);
69 // comm.recv(src, tag, out_values + displs[src], sizes[src]);
71 packed_iarchive ia(comm);
73 detail::packed_archive_recv(comm, src, tag, ia, status);
74 for (int i = 0; i < sizes[src]; ++i)
75 ia >> out_values[ displs[src] + i ];
80 // We're gathering at a non-root for a type that does not have an
81 // associated MPI datatype, so we'll need to serialize
82 // it. Unfortunately, this means that we cannot use MPI_Gatherv, so
83 // we'll just have all of the non-root nodes send individual
84 // messages to the root.
87 gatherv_impl(const communicator& comm, const T* in_values, int in_size, int root,
90 int tag = environment::collectives_tag();
91 // comm.send(root, tag, in_values, in_size);
92 packed_oarchive oa(comm);
93 for (int i = 0; i < in_size; ++i)
95 detail::packed_archive_send(comm, root, tag, oa);
97 } // end namespace detail
101 gatherv(const communicator& comm, const T* in_values, int in_size,
102 T* out_values, const std::vector<int>& sizes, const std::vector<int>& displs,
105 if (comm.rank() == root)
106 detail::gatherv_impl(comm, in_values, in_size,
107 out_values, &sizes[0], &displs[0],
108 root, is_mpi_datatype<T>());
110 detail::gatherv_impl(comm, in_values, in_size, root, is_mpi_datatype<T>());
115 gatherv(const communicator& comm, const std::vector<T>& in_values,
116 T* out_values, const std::vector<int>& sizes, const std::vector<int>& displs,
119 ::boost::mpi::gatherv(comm, &in_values[0], in_values.size(), out_values, sizes, displs, root);
123 void gatherv(const communicator& comm, const T* in_values, int in_size, int root)
125 BOOST_ASSERT(comm.rank() != root);
126 detail::gatherv_impl(comm, in_values, in_size, root, is_mpi_datatype<T>());
130 void gatherv(const communicator& comm, const std::vector<T>& in_values, int root)
132 BOOST_ASSERT(comm.rank() != root);
133 detail::gatherv_impl(comm, &in_values[0], in_values.size(), root, is_mpi_datatype<T>());
136 ///////////////////////
137 // common use versions
138 ///////////////////////
141 gatherv(const communicator& comm, const T* in_values, int in_size,
142 T* out_values, const std::vector<int>& sizes, int root)
144 int nprocs = comm.size();
146 std::vector<int> displs( nprocs );
147 for (int rank = 0, aux = 0; rank < nprocs; ++rank) {
151 ::boost::mpi::gatherv(comm, in_values, in_size, out_values, sizes, displs, root);
156 gatherv(const communicator& comm, const std::vector<T>& in_values,
157 T* out_values, const std::vector<int>& sizes, int root)
159 ::boost::mpi::gatherv(comm, &in_values[0], in_values.size(), out_values, sizes, root);
162 } } // end namespace boost::mpi
164 #endif // BOOST_MPI_GATHERV_HPP