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 <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_Gatherv to do all of the work.
|
---|
26 | template<typename T>
|
---|
27 | void
|
---|
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_)
|
---|
30 | {
|
---|
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<T*>(sizes), const_cast<T*>(displs),
|
---|
35 | type, root, comm));
|
---|
36 | }
|
---|
37 |
|
---|
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.
|
---|
40 | template<typename T>
|
---|
41 | void
|
---|
42 | gatherv_impl(const communicator& comm, const T* in_values, int in_size, int root,
|
---|
43 | mpl::true_)
|
---|
44 | {
|
---|
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));
|
---|
49 | }
|
---|
50 |
|
---|
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.
|
---|
56 | template<typename T>
|
---|
57 | void
|
---|
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_)
|
---|
60 | {
|
---|
61 | int tag = environment::collectives_tag();
|
---|
62 | int nprocs = comm.size();
|
---|
63 |
|
---|
64 | for (int src = 0; src < nprocs; ++src) {
|
---|
65 | if (src == root)
|
---|
66 | // Our own values will never be transmitted: just copy them.
|
---|
67 | std::copy(in_values, in_values + in_size, out_values + displs[src]);
|
---|
68 | else {
|
---|
69 | // comm.recv(src, tag, out_values + displs[src], sizes[src]);
|
---|
70 | // Receive archive
|
---|
71 | packed_iarchive ia(comm);
|
---|
72 | MPI_Status status;
|
---|
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 ];
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
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.
|
---|
85 | template<typename T>
|
---|
86 | void
|
---|
87 | gatherv_impl(const communicator& comm, const T* in_values, int in_size, int root,
|
---|
88 | mpl::false_)
|
---|
89 | {
|
---|
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)
|
---|
94 | oa << in_values[i];
|
---|
95 | detail::packed_archive_send(comm, root, tag, oa);
|
---|
96 | }
|
---|
97 | } // end namespace detail
|
---|
98 |
|
---|
99 | template<typename T>
|
---|
100 | void
|
---|
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,
|
---|
103 | int root)
|
---|
104 | {
|
---|
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>());
|
---|
109 | else
|
---|
110 | detail::gatherv_impl(comm, in_values, in_size, root, is_mpi_datatype<T>());
|
---|
111 | }
|
---|
112 |
|
---|
113 | template<typename T>
|
---|
114 | void
|
---|
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,
|
---|
117 | int root)
|
---|
118 | {
|
---|
119 | ::boost::mpi::gatherv(comm, &in_values[0], in_values.size(), out_values, sizes, displs, root);
|
---|
120 | }
|
---|
121 |
|
---|
122 | template<typename T>
|
---|
123 | void gatherv(const communicator& comm, const T* in_values, int in_size, int root)
|
---|
124 | {
|
---|
125 | BOOST_ASSERT(comm.rank() != root);
|
---|
126 | detail::gatherv_impl(comm, in_values, in_size, root, is_mpi_datatype<T>());
|
---|
127 | }
|
---|
128 |
|
---|
129 | template<typename T>
|
---|
130 | void gatherv(const communicator& comm, const std::vector<T>& in_values, int root)
|
---|
131 | {
|
---|
132 | BOOST_ASSERT(comm.rank() != root);
|
---|
133 | detail::gatherv_impl(comm, &in_values[0], in_values.size(), root, is_mpi_datatype<T>());
|
---|
134 | }
|
---|
135 |
|
---|
136 | ///////////////////////
|
---|
137 | // common use versions
|
---|
138 | ///////////////////////
|
---|
139 | template<typename T>
|
---|
140 | void
|
---|
141 | gatherv(const communicator& comm, const T* in_values, int in_size,
|
---|
142 | T* out_values, int root)
|
---|
143 | {
|
---|
144 | int nprocs = comm.size();
|
---|
145 |
|
---|
146 | std::vector<int> sizes(nprocs);
|
---|
147 | ::boost::mpi::gather(comm, in_size, sizes, root);
|
---|
148 | std::vector<int> displs( nprocs );
|
---|
149 | for ( int rank = 0, aux = 0; rank < nprocs; ++rank ) {
|
---|
150 | displs[rank] = aux;
|
---|
151 | aux += sizes[rank];
|
---|
152 | }
|
---|
153 | ::boost::mpi::gatherv(comm, in_values, in_size, out_values, sizes, displs, root);
|
---|
154 | }
|
---|
155 |
|
---|
156 | template<typename T>
|
---|
157 | void
|
---|
158 | gatherv(const communicator& comm, const std::vector<T>& in_values,
|
---|
159 | T* out_values, int root)
|
---|
160 | {
|
---|
161 | ::boost::mpi::gatherv(comm, &in_values[0], in_values.size(), out_values, root);
|
---|
162 | }
|
---|
163 |
|
---|
164 | } } // end namespace boost::mpi
|
---|
165 |
|
---|
166 | #endif // BOOST_MPI_GATHERV_HPP
|
---|