Chris@16
|
1 // Copyright (C) 2005-2006 Douglas Gregor <doug.gregor -at- gmail.com>
|
Chris@16
|
2 // Copyright (C) 2004 The Trustees of Indiana University
|
Chris@16
|
3
|
Chris@16
|
4 // Use, modification and distribution is subject to the Boost Software
|
Chris@16
|
5 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7
|
Chris@16
|
8 // Authors: Douglas Gregor
|
Chris@16
|
9 // Andrew Lumsdaine
|
Chris@16
|
10
|
Chris@16
|
11 // Message Passing Interface 1.1 -- Section 4.9.1. Reduce
|
Chris@16
|
12 #ifndef BOOST_MPI_ALL_REDUCE_HPP
|
Chris@16
|
13 #define BOOST_MPI_ALL_REDUCE_HPP
|
Chris@16
|
14
|
Chris@16
|
15 #include <vector>
|
Chris@16
|
16
|
Chris@16
|
17 #include <boost/mpi/inplace.hpp>
|
Chris@16
|
18
|
Chris@16
|
19 // All-reduce falls back to reduce() + broadcast() in some cases.
|
Chris@16
|
20 #include <boost/mpi/collectives/broadcast.hpp>
|
Chris@16
|
21 #include <boost/mpi/collectives/reduce.hpp>
|
Chris@16
|
22
|
Chris@16
|
23 namespace boost { namespace mpi {
|
Chris@16
|
24 namespace detail {
|
Chris@16
|
25 /**********************************************************************
|
Chris@16
|
26 * Simple reduction with MPI_Allreduce *
|
Chris@16
|
27 **********************************************************************/
|
Chris@16
|
28 // We are reducing for a type that has an associated MPI
|
Chris@16
|
29 // datatype and operation, so we'll use MPI_Allreduce directly.
|
Chris@16
|
30 template<typename T, typename Op>
|
Chris@16
|
31 void
|
Chris@16
|
32 all_reduce_impl(const communicator& comm, const T* in_values, int n,
|
Chris@16
|
33 T* out_values, Op /*op*/, mpl::true_ /*is_mpi_op*/,
|
Chris@16
|
34 mpl::true_ /*is_mpi_datatype*/)
|
Chris@16
|
35 {
|
Chris@16
|
36 BOOST_MPI_CHECK_RESULT(MPI_Allreduce,
|
Chris@16
|
37 (const_cast<T*>(in_values), out_values, n,
|
Chris@16
|
38 boost::mpi::get_mpi_datatype<T>(*in_values),
|
Chris@16
|
39 (is_mpi_op<Op, T>::op()), comm));
|
Chris@16
|
40 }
|
Chris@16
|
41
|
Chris@16
|
42 /**********************************************************************
|
Chris@16
|
43 * User-defined reduction with MPI_Allreduce *
|
Chris@16
|
44 **********************************************************************/
|
Chris@16
|
45 // We are reducing at the root for a type that has an associated MPI
|
Chris@16
|
46 // datatype but with a custom operation. We'll use MPI_Reduce
|
Chris@16
|
47 // directly, but we'll need to create an MPI_Op manually.
|
Chris@16
|
48 template<typename T, typename Op>
|
Chris@16
|
49 void
|
Chris@16
|
50 all_reduce_impl(const communicator& comm, const T* in_values, int n,
|
Chris@16
|
51 T* out_values, Op op, mpl::false_ /*is_mpi_op*/,
|
Chris@16
|
52 mpl::true_ /*is_mpi_datatype*/)
|
Chris@16
|
53 {
|
Chris@16
|
54 user_op<Op, T> mpi_op(op);
|
Chris@16
|
55 BOOST_MPI_CHECK_RESULT(MPI_Allreduce,
|
Chris@16
|
56 (const_cast<T*>(in_values), out_values, n,
|
Chris@16
|
57 boost::mpi::get_mpi_datatype<T>(*in_values),
|
Chris@16
|
58 mpi_op.get_mpi_op(), comm));
|
Chris@16
|
59 }
|
Chris@16
|
60
|
Chris@16
|
61 /**********************************************************************
|
Chris@16
|
62 * User-defined, tree-based reduction for non-MPI data types *
|
Chris@16
|
63 **********************************************************************/
|
Chris@16
|
64 // We are reducing at the root for a type that has no associated MPI
|
Chris@16
|
65 // datatype and operation, so we'll use a simple tree-based
|
Chris@16
|
66 // algorithm.
|
Chris@16
|
67 template<typename T, typename Op>
|
Chris@16
|
68 void
|
Chris@16
|
69 all_reduce_impl(const communicator& comm, const T* in_values, int n,
|
Chris@16
|
70 T* out_values, Op op, mpl::false_ /*is_mpi_op*/,
|
Chris@16
|
71 mpl::false_ /*is_mpi_datatype*/)
|
Chris@16
|
72 {
|
Chris@16
|
73 if (in_values == MPI_IN_PLACE) {
|
Chris@16
|
74 // if in_values matches the in place tag, then the output
|
Chris@16
|
75 // buffer actually contains the input data.
|
Chris@16
|
76 // But we can just go back to the out of place
|
Chris@16
|
77 // implementation in this case.
|
Chris@16
|
78 // it's not clear how/if we can avoid the copy.
|
Chris@16
|
79 std::vector<T> tmp_in( out_values, out_values + n);
|
Chris@16
|
80 reduce(comm, &(tmp_in[0]), n, out_values, op, 0);
|
Chris@16
|
81 } else {
|
Chris@16
|
82 reduce(comm, in_values, n, out_values, op, 0);
|
Chris@16
|
83 }
|
Chris@16
|
84 broadcast(comm, out_values, n, 0);
|
Chris@16
|
85 }
|
Chris@16
|
86 } // end namespace detail
|
Chris@16
|
87
|
Chris@16
|
88 template<typename T, typename Op>
|
Chris@16
|
89 inline void
|
Chris@16
|
90 all_reduce(const communicator& comm, const T* in_values, int n, T* out_values,
|
Chris@16
|
91 Op op)
|
Chris@16
|
92 {
|
Chris@16
|
93 detail::all_reduce_impl(comm, in_values, n, out_values, op,
|
Chris@16
|
94 is_mpi_op<Op, T>(), is_mpi_datatype<T>());
|
Chris@16
|
95 }
|
Chris@16
|
96
|
Chris@16
|
97 template<typename T, typename Op>
|
Chris@16
|
98 inline void
|
Chris@16
|
99 all_reduce(const communicator& comm, inplace_t<T*> inout_values, int n, Op op)
|
Chris@16
|
100 {
|
Chris@16
|
101 all_reduce(comm, static_cast<const T*>(MPI_IN_PLACE), n, inout_values.buffer, op);
|
Chris@16
|
102 }
|
Chris@16
|
103
|
Chris@16
|
104 template<typename T, typename Op>
|
Chris@16
|
105 inline void
|
Chris@16
|
106 all_reduce(const communicator& comm, inplace_t<T> inout_values, Op op)
|
Chris@16
|
107 {
|
Chris@16
|
108 all_reduce(comm, static_cast<const T*>(MPI_IN_PLACE), 1, &(inout_values.buffer), op);
|
Chris@16
|
109 }
|
Chris@16
|
110
|
Chris@16
|
111 template<typename T, typename Op>
|
Chris@16
|
112 inline void
|
Chris@16
|
113 all_reduce(const communicator& comm, const T& in_value, T& out_value, Op op)
|
Chris@16
|
114 {
|
Chris@16
|
115 detail::all_reduce_impl(comm, &in_value, 1, &out_value, op,
|
Chris@16
|
116 is_mpi_op<Op, T>(), is_mpi_datatype<T>());
|
Chris@16
|
117 }
|
Chris@16
|
118
|
Chris@16
|
119 template<typename T, typename Op>
|
Chris@16
|
120 T all_reduce(const communicator& comm, const T& in_value, Op op)
|
Chris@16
|
121 {
|
Chris@16
|
122 T result;
|
Chris@16
|
123 ::boost::mpi::all_reduce(comm, in_value, result, op);
|
Chris@16
|
124 return result;
|
Chris@16
|
125 }
|
Chris@16
|
126
|
Chris@16
|
127 } } // end namespace boost::mpi
|
Chris@16
|
128
|
Chris@16
|
129 #endif // BOOST_MPI_ALL_REDUCE_HPP
|