Chris@16: // Copyright (C) 2005-2006 Douglas Gregor Chris@16: // Copyright (C) 2004 The Trustees of Indiana University Chris@16: Chris@16: // Use, modification and distribution is subject to the Boost Software Chris@16: // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // Authors: Douglas Gregor Chris@16: // Andrew Lumsdaine Chris@16: Chris@16: // Message Passing Interface 1.1 -- Section 4.9.1. Reduce Chris@16: #ifndef BOOST_MPI_ALL_REDUCE_HPP Chris@16: #define BOOST_MPI_ALL_REDUCE_HPP Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: // All-reduce falls back to reduce() + broadcast() in some cases. Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace mpi { Chris@16: namespace detail { Chris@16: /********************************************************************** Chris@16: * Simple reduction with MPI_Allreduce * Chris@16: **********************************************************************/ Chris@16: // We are reducing for a type that has an associated MPI Chris@16: // datatype and operation, so we'll use MPI_Allreduce directly. Chris@16: template Chris@16: void Chris@16: all_reduce_impl(const communicator& comm, const T* in_values, int n, Chris@16: T* out_values, Op /*op*/, mpl::true_ /*is_mpi_op*/, Chris@16: mpl::true_ /*is_mpi_datatype*/) Chris@16: { Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Allreduce, Chris@16: (const_cast(in_values), out_values, n, Chris@16: boost::mpi::get_mpi_datatype(*in_values), Chris@16: (is_mpi_op::op()), comm)); Chris@16: } Chris@16: Chris@16: /********************************************************************** Chris@16: * User-defined reduction with MPI_Allreduce * Chris@16: **********************************************************************/ Chris@16: // We are reducing at the root for a type that has an associated MPI Chris@16: // datatype but with a custom operation. We'll use MPI_Reduce Chris@16: // directly, but we'll need to create an MPI_Op manually. Chris@16: template Chris@16: void Chris@16: all_reduce_impl(const communicator& comm, const T* in_values, int n, Chris@16: T* out_values, Op op, mpl::false_ /*is_mpi_op*/, Chris@16: mpl::true_ /*is_mpi_datatype*/) Chris@16: { Chris@16: user_op mpi_op(op); Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Allreduce, Chris@16: (const_cast(in_values), out_values, n, Chris@16: boost::mpi::get_mpi_datatype(*in_values), Chris@16: mpi_op.get_mpi_op(), comm)); Chris@16: } Chris@16: Chris@16: /********************************************************************** Chris@16: * User-defined, tree-based reduction for non-MPI data types * Chris@16: **********************************************************************/ Chris@16: // We are reducing at the root for a type that has no associated MPI Chris@16: // datatype and operation, so we'll use a simple tree-based Chris@16: // algorithm. Chris@16: template Chris@16: void Chris@16: all_reduce_impl(const communicator& comm, const T* in_values, int n, Chris@16: T* out_values, Op op, mpl::false_ /*is_mpi_op*/, Chris@16: mpl::false_ /*is_mpi_datatype*/) Chris@16: { Chris@16: if (in_values == MPI_IN_PLACE) { Chris@16: // if in_values matches the in place tag, then the output Chris@16: // buffer actually contains the input data. Chris@16: // But we can just go back to the out of place Chris@16: // implementation in this case. Chris@16: // it's not clear how/if we can avoid the copy. Chris@16: std::vector tmp_in( out_values, out_values + n); Chris@16: reduce(comm, &(tmp_in[0]), n, out_values, op, 0); Chris@16: } else { Chris@16: reduce(comm, in_values, n, out_values, op, 0); Chris@16: } Chris@16: broadcast(comm, out_values, n, 0); Chris@16: } Chris@16: } // end namespace detail Chris@16: Chris@16: template Chris@16: inline void Chris@16: all_reduce(const communicator& comm, const T* in_values, int n, T* out_values, Chris@16: Op op) Chris@16: { Chris@16: detail::all_reduce_impl(comm, in_values, n, out_values, op, Chris@16: is_mpi_op(), is_mpi_datatype()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void Chris@16: all_reduce(const communicator& comm, inplace_t inout_values, int n, Op op) Chris@16: { Chris@16: all_reduce(comm, static_cast(MPI_IN_PLACE), n, inout_values.buffer, op); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void Chris@16: all_reduce(const communicator& comm, inplace_t inout_values, Op op) Chris@16: { Chris@16: all_reduce(comm, static_cast(MPI_IN_PLACE), 1, &(inout_values.buffer), op); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void Chris@16: all_reduce(const communicator& comm, const T& in_value, T& out_value, Op op) Chris@16: { Chris@16: detail::all_reduce_impl(comm, &in_value, 1, &out_value, op, Chris@16: is_mpi_op(), is_mpi_datatype()); Chris@16: } Chris@16: Chris@16: template Chris@16: T all_reduce(const communicator& comm, const T& in_value, Op op) Chris@16: { Chris@16: T result; Chris@16: ::boost::mpi::all_reduce(comm, in_value, result, op); Chris@16: return result; Chris@16: } Chris@16: Chris@16: } } // end namespace boost::mpi Chris@16: Chris@16: #endif // BOOST_MPI_ALL_REDUCE_HPP