1 // Copyright (C) 2004 The Trustees of Indiana University.
2 // Copyright (C) 2005-2006 Douglas Gregor <doug.gregor -at- gmail.com>
4 // Use, modification and distribution is subject to the Boost Software
5 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // Authors: Douglas Gregor
11 /** @file operations.hpp
13 * This header provides a mapping from function objects to @c MPI_Op
14 * constants used in MPI collective operations. It also provides
15 * several new function object types not present in the standard @c
16 * <functional> header that have direct mappings to @c MPI_Op.
18 #ifndef BOOST_MPI_IS_MPI_OP_HPP
19 #define BOOST_MPI_IS_MPI_OP_HPP
21 #include <boost/mpi/config.hpp>
22 #include <boost/mpl/bool.hpp>
23 #include <boost/mpl/if.hpp>
24 #include <boost/mpl/and.hpp>
25 #include <boost/mpi/datatype.hpp>
26 #include <boost/core/enable_if.hpp>
27 #include <boost/core/uncaught_exceptions.hpp>
30 namespace boost { namespace mpi {
32 template<typename Op, typename T> struct is_mpi_op;
35 * @brief Determine if a function object type is commutative.
37 * This trait determines if an operation @c Op is commutative when
38 * applied to values of type @c T. Parallel operations such as @c
39 * reduce and @c prefix_sum can be implemented more efficiently with
40 * commutative operations. To mark an operation as commutative, users
41 * should specialize @c is_commutative and derive from the class @c
44 template<typename Op, typename T>
45 struct is_commutative : public mpl::false_ { };
47 /**************************************************************************
48 * Function objects for MPI operations not in <functional> header *
49 **************************************************************************/
52 * @brief Compute the maximum of two values.
54 * This binary function object computes the maximum of the two values
55 * it is given. When used with MPI and a type @c T that has an
56 * associated, built-in MPI data type, translates to @c MPI_MAX.
61 typedef T first_argument_type;
62 typedef T second_argument_type;
63 typedef T result_type;
64 /** @returns the maximum of x and y. */
65 const T& operator()(const T& x, const T& y) const
72 * @brief Compute the minimum of two values.
74 * This binary function object computes the minimum of the two values
75 * it is given. When used with MPI and a type @c T that has an
76 * associated, built-in MPI data type, translates to @c MPI_MIN.
81 typedef T first_argument_type;
82 typedef T second_argument_type;
83 typedef T result_type;
84 /** @returns the minimum of x and y. */
85 const T& operator()(const T& x, const T& y) const
93 * @brief Compute the bitwise AND of two integral values.
95 * This binary function object computes the bitwise AND of the two
96 * values it is given. When used with MPI and a type @c T that has an
97 * associated, built-in MPI data type, translates to @c MPI_BAND.
102 typedef T first_argument_type;
103 typedef T second_argument_type;
104 typedef T result_type;
105 /** @returns @c x & y. */
106 T operator()(const T& x, const T& y) const
113 * @brief Compute the bitwise OR of two integral values.
115 * This binary function object computes the bitwise OR of the two
116 * values it is given. When used with MPI and a type @c T that has an
117 * associated, built-in MPI data type, translates to @c MPI_BOR.
122 typedef T first_argument_type;
123 typedef T second_argument_type;
124 typedef T result_type;
125 /** @returns the @c x | y. */
126 T operator()(const T& x, const T& y) const
133 * @brief Compute the logical exclusive OR of two integral values.
135 * This binary function object computes the logical exclusive of the
136 * two values it is given. When used with MPI and a type @c T that has
137 * an associated, built-in MPI data type, translates to @c MPI_LXOR.
142 typedef T first_argument_type;
143 typedef T second_argument_type;
144 typedef T result_type;
145 /** @returns the logical exclusive OR of x and y. */
146 T operator()(const T& x, const T& y) const
148 return (x || y) && !(x && y);
153 * @brief Compute the bitwise exclusive OR of two integral values.
155 * This binary function object computes the bitwise exclusive OR of
156 * the two values it is given. When used with MPI and a type @c T that
157 * has an associated, built-in MPI data type, translates to @c
163 typedef T first_argument_type;
164 typedef T second_argument_type;
165 typedef T result_type;
166 /** @returns @c x ^ y. */
167 T operator()(const T& x, const T& y) const
173 /**************************************************************************
175 **************************************************************************/
178 * @brief Determine if a function object has an associated @c MPI_Op.
180 * This trait determines if a function object type @c Op, when used
181 * with argument type @c T, has an associated @c MPI_Op. If so, @c
182 * is_mpi_op<Op,T> will derive from @c mpl::false_ and will
183 * contain a static member function @c op that takes no arguments but
184 * returns the associated @c MPI_Op value. For instance, @c
185 * is_mpi_op<std::plus<int>,int>::op() returns @c MPI_SUM.
187 * Users may specialize @c is_mpi_op for any other class templates
188 * that map onto operations that have @c MPI_Op equivalences, such as
189 * bitwise OR, logical and, or maximum. However, users are encouraged
190 * to use the standard function objects in the @c functional and @c
191 * boost/mpi/operations.hpp headers whenever possible. For
192 * function objects that are class templates with a single template
193 * parameter, it may be easier to specialize @c is_builtin_mpi_op.
195 template<typename Op, typename T>
196 struct is_mpi_op : public mpl::false_ { };
200 struct is_mpi_op<maximum<T>, T>
201 : public boost::mpl::or_<is_mpi_integer_datatype<T>,
202 is_mpi_floating_point_datatype<T> >
204 static MPI_Op op() { return MPI_MAX; }
209 struct is_mpi_op<minimum<T>, T>
210 : public boost::mpl::or_<is_mpi_integer_datatype<T>,
211 is_mpi_floating_point_datatype<T> >
213 static MPI_Op op() { return MPI_MIN; }
218 struct is_mpi_op<std::plus<T>, T>
219 : public boost::mpl::or_<is_mpi_integer_datatype<T>,
220 is_mpi_floating_point_datatype<T>,
221 is_mpi_complex_datatype<T> >
223 static MPI_Op op() { return MPI_SUM; }
228 struct is_mpi_op<std::multiplies<T>, T>
229 : public boost::mpl::or_<is_mpi_integer_datatype<T>,
230 is_mpi_floating_point_datatype<T>,
231 is_mpi_complex_datatype<T> >
233 static MPI_Op op() { return MPI_PROD; }
238 struct is_mpi_op<std::logical_and<T>, T>
239 : public boost::mpl::or_<is_mpi_integer_datatype<T>,
240 is_mpi_logical_datatype<T> >
242 static MPI_Op op() { return MPI_LAND; }
247 struct is_mpi_op<std::logical_or<T>, T>
248 : public boost::mpl::or_<is_mpi_integer_datatype<T>,
249 is_mpi_logical_datatype<T> >
251 static MPI_Op op() { return MPI_LOR; }
256 struct is_mpi_op<logical_xor<T>, T>
257 : public boost::mpl::or_<is_mpi_integer_datatype<T>,
258 is_mpi_logical_datatype<T> >
260 static MPI_Op op() { return MPI_LXOR; }
265 struct is_mpi_op<bitwise_and<T>, T>
266 : public boost::mpl::or_<is_mpi_integer_datatype<T>,
267 is_mpi_byte_datatype<T> >
269 static MPI_Op op() { return MPI_BAND; }
274 struct is_mpi_op<bitwise_or<T>, T>
275 : public boost::mpl::or_<is_mpi_integer_datatype<T>,
276 is_mpi_byte_datatype<T> >
278 static MPI_Op op() { return MPI_BOR; }
283 struct is_mpi_op<bitwise_xor<T>, T>
284 : public boost::mpl::or_<is_mpi_integer_datatype<T>,
285 is_mpi_byte_datatype<T> >
287 static MPI_Op op() { return MPI_BXOR; }
291 // A helper class used to create user-defined MPI_Ops
292 template<typename Op, typename T>
298 BOOST_MPI_CHECK_RESULT(MPI_Op_create,
299 (&user_op<Op, T>::perform,
300 is_commutative<Op, T>::value,
306 if (boost::core::uncaught_exceptions() > 0) {
307 // Ignore failure cases: there are obviously other problems
308 // already, and we don't want to cause program termination if
309 // MPI_Op_free fails.
310 MPI_Op_free(&mpi_op);
312 BOOST_MPI_CHECK_RESULT(MPI_Op_free, (&mpi_op));
324 static void BOOST_MPI_CALLING_CONVENTION perform(void* vinvec, void* voutvec, int* plen, MPI_Datatype*)
326 T* invec = static_cast<T*>(vinvec);
327 T* outvec = static_cast<T*>(voutvec);
329 std::transform(invec, invec + *plen, outvec, outvec, op);
333 } // end namespace detail
335 } } // end namespace boost::mpi
337 #endif // BOOST_MPI_GET_MPI_OP_HPP