123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- #ifndef BOOST_MPI_ALL_REDUCE_HPP
- #define BOOST_MPI_ALL_REDUCE_HPP
- #include <vector>
- #include <boost/mpi/inplace.hpp>
- #include <boost/mpi/collectives/broadcast.hpp>
- #include <boost/mpi/collectives/reduce.hpp>
- namespace boost { namespace mpi {
- namespace detail {
-
-
-
- template<typename T, typename Op>
- void
- all_reduce_impl(const communicator& comm, const T* in_values, int n,
- T* out_values, Op , mpl::true_ ,
- mpl::true_ )
- {
- BOOST_MPI_CHECK_RESULT(MPI_Allreduce,
- (const_cast<T*>(in_values), out_values, n,
- boost::mpi::get_mpi_datatype<T>(*in_values),
- (is_mpi_op<Op, T>::op()), comm));
- }
-
-
-
-
- template<typename T, typename Op>
- void
- all_reduce_impl(const communicator& comm, const T* in_values, int n,
- T* out_values, Op , mpl::false_ ,
- mpl::true_ )
- {
- user_op<Op, T> mpi_op;
- BOOST_MPI_CHECK_RESULT(MPI_Allreduce,
- (const_cast<T*>(in_values), out_values, n,
- boost::mpi::get_mpi_datatype<T>(*in_values),
- mpi_op.get_mpi_op(), comm));
- }
-
-
-
-
- template<typename T, typename Op>
- void
- all_reduce_impl(const communicator& comm, const T* in_values, int n,
- T* out_values, Op op, mpl::false_ ,
- mpl::false_ )
- {
- if (in_values == MPI_IN_PLACE) {
-
-
-
-
-
- std::vector<T> tmp_in( out_values, out_values + n);
- reduce(comm, detail::c_data(tmp_in), n, out_values, op, 0);
- } else {
- reduce(comm, in_values, n, out_values, op, 0);
- }
- broadcast(comm, out_values, n, 0);
- }
- }
- template<typename T, typename Op>
- inline void
- all_reduce(const communicator& comm, const T* in_values, int n, T* out_values,
- Op op)
- {
- detail::all_reduce_impl(comm, in_values, n, out_values, op,
- is_mpi_op<Op, T>(), is_mpi_datatype<T>());
- }
- template<typename T, typename Op>
- inline void
- all_reduce(const communicator& comm, inplace_t<T*> inout_values, int n, Op op)
- {
- all_reduce(comm, static_cast<const T*>(MPI_IN_PLACE), n, inout_values.buffer, op);
- }
- template<typename T, typename Op>
- inline void
- all_reduce(const communicator& comm, inplace_t<T> inout_values, Op op)
- {
- all_reduce(comm, static_cast<const T*>(MPI_IN_PLACE), 1, &(inout_values.buffer), op);
- }
- template<typename T, typename Op>
- inline void
- all_reduce(const communicator& comm, const T& in_value, T& out_value, Op op)
- {
- detail::all_reduce_impl(comm, &in_value, 1, &out_value, op,
- is_mpi_op<Op, T>(), is_mpi_datatype<T>());
- }
- template<typename T, typename Op>
- T all_reduce(const communicator& comm, const T& in_value, Op op)
- {
- T result;
- ::boost::mpi::all_reduce(comm, in_value, result, op);
- return result;
- }
- } }
- #endif
|