]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright (C) 2005-2006 Douglas Gregor <doug.gregor -at- gmail.com>. |
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.7. Gather-to-all | |
8 | #ifndef BOOST_MPI_ALL_GATHER_HPP | |
9 | #define BOOST_MPI_ALL_GATHER_HPP | |
10 | ||
11 | #include <boost/mpi/exception.hpp> | |
12 | #include <boost/mpi/datatype.hpp> | |
13 | #include <vector> | |
14 | #include <boost/serialization/vector.hpp> | |
15 | ||
16 | // all_gather falls back to gather+broadcast in some cases | |
17 | #include <boost/mpi/collectives/broadcast.hpp> | |
18 | #include <boost/mpi/collectives/gather.hpp> | |
19 | ||
20 | namespace boost { namespace mpi { | |
21 | ||
22 | namespace detail { | |
23 | // We're all-gathering for a type that has an associated MPI | |
24 | // datatype, so we'll use MPI_Gather to do all of the work. | |
25 | template<typename T> | |
26 | void | |
27 | all_gather_impl(const communicator& comm, const T* in_values, int n, | |
28 | T* out_values, mpl::true_) | |
29 | { | |
30 | MPI_Datatype type = boost::mpi::get_mpi_datatype<T>(*in_values); | |
31 | BOOST_MPI_CHECK_RESULT(MPI_Allgather, | |
32 | (const_cast<T*>(in_values), n, type, | |
33 | out_values, n, type, comm)); | |
34 | } | |
35 | ||
36 | // We're all-gathering for a type that has no associated MPI | |
37 | // type. So, we'll do a manual gather followed by a broadcast. | |
38 | template<typename T> | |
39 | void | |
40 | all_gather_impl(const communicator& comm, const T* in_values, int n, | |
41 | T* out_values, mpl::false_) | |
42 | { | |
43 | gather(comm, in_values, n, out_values, 0); | |
44 | broadcast(comm, out_values, comm.size() * n, 0); | |
45 | } | |
46 | } // end namespace detail | |
47 | ||
48 | template<typename T> | |
49 | inline void | |
50 | all_gather(const communicator& comm, const T& in_value, T* out_values) | |
51 | { | |
52 | detail::all_gather_impl(comm, &in_value, 1, out_values, is_mpi_datatype<T>()); | |
53 | } | |
54 | ||
55 | template<typename T> | |
56 | void | |
57 | all_gather(const communicator& comm, const T& in_value, | |
58 | std::vector<T>& out_values) | |
59 | { | |
60 | out_values.resize(comm.size()); | |
61 | ::boost::mpi::all_gather(comm, &in_value, 1, &out_values[0]); | |
62 | } | |
63 | ||
64 | template<typename T> | |
65 | inline void | |
66 | all_gather(const communicator& comm, const T* in_values, int n, T* out_values) | |
67 | { | |
68 | detail::all_gather_impl(comm, in_values, n, out_values, is_mpi_datatype<T>()); | |
69 | } | |
70 | ||
71 | template<typename T> | |
72 | void | |
73 | all_gather(const communicator& comm, const T* in_values, int n, | |
74 | std::vector<T>& out_values) | |
75 | { | |
76 | out_values.resize(comm.size() * n); | |
77 | ::boost::mpi::all_gather(comm, in_values, n, &out_values[0]); | |
78 | } | |
79 | ||
80 | } } // end namespace boost::mpi | |
81 | ||
82 | #endif // BOOST_MPI_ALL_GATHER_HPP |