123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- #ifndef BOOST_MPI_REDUCE_HPP
- #define BOOST_MPI_REDUCE_HPP
- #include <boost/mpi/exception.hpp>
- #include <boost/mpi/datatype.hpp>
- #include <boost/mpi/packed_oarchive.hpp>
- #include <boost/mpi/packed_iarchive.hpp>
- #include <boost/mpi/detail/point_to_point.hpp>
- #include <boost/mpi/communicator.hpp>
- #include <boost/mpi/environment.hpp>
- #include <boost/mpi/detail/computation_tree.hpp>
- #include <boost/mpi/operations.hpp>
- #include <algorithm>
- #include <exception>
- #include <boost/assert.hpp>
- #include <boost/scoped_array.hpp>
- namespace boost { namespace mpi {
- namespace detail {
-
-
-
- template<typename T, typename Op>
- void
- reduce_impl(const communicator& comm, const T* in_values, int n,
- T* out_values, Op , int root, mpl::true_ ,
- mpl::true_)
- {
- BOOST_MPI_CHECK_RESULT(MPI_Reduce,
- (const_cast<T*>(in_values), out_values, n,
- boost::mpi::get_mpi_datatype<T>(*in_values),
- (is_mpi_op<Op, T>::op()), root, comm));
- }
-
-
- template<typename T, typename Op>
- void
- reduce_impl(const communicator& comm, const T* in_values, int n, Op ,
- int root, mpl::true_ , mpl::true_)
- {
- BOOST_MPI_CHECK_RESULT(MPI_Reduce,
- (const_cast<T*>(in_values), 0, n,
- boost::mpi::get_mpi_datatype<T>(*in_values),
- (is_mpi_op<Op, T>::op()), root, comm));
- }
-
-
-
-
- template<typename T, typename Op>
- void
- reduce_impl(const communicator& comm, const T* in_values, int n,
- T* out_values, Op op, int root, mpl::false_ ,
- mpl::true_)
- {
- user_op<Op, T> mpi_op;
- BOOST_MPI_CHECK_RESULT(MPI_Reduce,
- (const_cast<T*>(in_values), out_values, n,
- boost::mpi::get_mpi_datatype<T>(*in_values),
- mpi_op.get_mpi_op(), root, comm));
- }
-
-
-
- template<typename T, typename Op>
- void
- reduce_impl(const communicator& comm, const T* in_values, int n, Op op,
- int root, mpl::false_, mpl::true_)
- {
- user_op<Op, T> mpi_op;
- BOOST_MPI_CHECK_RESULT(MPI_Reduce,
- (const_cast<T*>(in_values), 0, n,
- boost::mpi::get_mpi_datatype<T>(*in_values),
- mpi_op.get_mpi_op(), root, comm));
- }
-
-
- template<typename T, typename Op>
- void
- tree_reduce_impl(const communicator& comm, const T* in_values, int n,
- T* out_values, Op op, int root,
- mpl::true_ )
- {
- std::copy(in_values, in_values + n, out_values);
- int size = comm.size();
- int rank = comm.rank();
-
- detail::computation_tree tree(rank, size, root);
- int tag = environment::collectives_tag();
- MPI_Status status;
- int children = 0;
- for (int child = tree.child_begin();
- children < tree.branching_factor() && child != root;
- ++children, child = (child + 1) % size) {
-
- packed_iarchive ia(comm);
- detail::packed_archive_recv(comm, child, tag, ia, status);
- T incoming;
- for (int i = 0; i < n; ++i) {
- ia >> incoming;
- out_values[i] = op(out_values[i], incoming);
- }
- }
-
- if (tree.parent() != rank) {
- packed_oarchive oa(comm);
- for (int i = 0; i < n; ++i)
- oa << out_values[i];
- detail::packed_archive_send(comm, tree.parent(), tag, oa);
- }
- }
-
- template<typename T, typename Op>
- void
- tree_reduce_impl(const communicator& comm, const T* in_values, int n, Op op,
- int root, mpl::true_ )
- {
- scoped_array<T> results(new T[n]);
- detail::tree_reduce_impl(comm, in_values, n, results.get(), op, root,
- mpl::true_());
- }
-
- template<typename T, typename Op>
- void
- tree_reduce_impl(const communicator& comm, const T* in_values, int n,
- T* out_values, Op op, int root,
- mpl::false_ )
- {
- int tag = environment::collectives_tag();
- int left_child = root / 2;
- int right_child = (root + comm.size()) / 2;
- MPI_Status status;
- if (left_child != root) {
-
-
- packed_iarchive ia(comm);
- detail::packed_archive_recv(comm, left_child, tag, ia, status);
- T incoming;
- for (int i = 0; i < n; ++i) {
- ia >> incoming;
- out_values[i] = op(incoming, in_values[i]);
- }
- } else {
-
- std::copy(in_values, in_values + n, out_values);
- }
- if (right_child != root) {
-
-
- packed_iarchive ia(comm);
- detail::packed_archive_recv(comm, right_child, tag, ia, status);
- T incoming;
- for (int i = 0; i < n; ++i) {
- ia >> incoming;
- out_values[i] = op(out_values[i], incoming);
- }
- }
- }
-
- template<typename T, typename Op>
- void
- tree_reduce_impl(const communicator& comm, const T* in_values, int n, Op op,
- int root, mpl::false_ )
- {
- int size = comm.size();
- int rank = comm.rank();
- int tag = environment::collectives_tag();
-
-
- int grandparent = root;
- int parent = root;
- int left_bound = 0;
- int right_bound = size;
- int left_child, right_child;
- do {
- left_child = (left_bound + parent) / 2;
- right_child = (parent + right_bound) / 2;
- if (rank < parent) {
-
- grandparent = parent;
- right_bound = parent;
- parent = left_child;
- } else if (rank > parent) {
-
- grandparent = parent;
- left_bound = parent + 1;
- parent = right_child;
- } else {
-
- break;
- }
- } while (true);
-
-
-
- parent = grandparent;
- MPI_Status status;
- scoped_array<T> out_values(new T[n]);
- if (left_child != rank) {
-
-
- packed_iarchive ia(comm);
- detail::packed_archive_recv(comm, left_child, tag, ia, status);
- T incoming;
- for (int i = 0; i < n; ++i) {
- ia >> incoming;
- out_values[i] = op(incoming, in_values[i]);
- }
- } else {
-
- std::copy(in_values, in_values + n, out_values.get());
- }
- if (right_child != rank) {
-
-
- packed_iarchive ia(comm);
- detail::packed_archive_recv(comm, right_child, tag, ia, status);
- T incoming;
- for (int i = 0; i < n; ++i) {
- ia >> incoming;
- out_values[i] = op(out_values[i], incoming);
- }
- }
-
- packed_oarchive oa(comm);
- for (int i = 0; i < n; ++i)
- oa << out_values[i];
- detail::packed_archive_send(comm, parent, tag, oa);
- }
-
-
-
- template<typename T, typename Op>
- void
- reduce_impl(const communicator& comm, const T* in_values, int n,
- T* out_values, Op op, int root, mpl::false_ ,
- mpl::false_ )
- {
- detail::tree_reduce_impl(comm, in_values, n, out_values, op, root,
- is_commutative<Op, T>());
- }
-
-
-
- template<typename T, typename Op>
- void
- reduce_impl(const communicator& comm, const T* in_values, int n, Op op,
- int root, mpl::false_ ,
- mpl::false_ )
- {
- detail::tree_reduce_impl(comm, in_values, n, op, root,
- is_commutative<Op, T>());
- }
- }
- template<typename T, typename Op>
- void
- reduce(const communicator& comm, const T* in_values, int n, T* out_values,
- Op op, int root)
- {
- if (comm.rank() == root)
- detail::reduce_impl(comm, in_values, n, out_values, op, root,
- is_mpi_op<Op, T>(), is_mpi_datatype<T>());
- else
- detail::reduce_impl(comm, in_values, n, op, root,
- is_mpi_op<Op, T>(), is_mpi_datatype<T>());
- }
- template<typename T, typename Op>
- void
- reduce(const communicator& comm, const T* in_values, int n, Op op, int root)
- {
- BOOST_ASSERT(comm.rank() != root);
- detail::reduce_impl(comm, in_values, n, op, root,
- is_mpi_op<Op, T>(), is_mpi_datatype<T>());
- }
- template<typename T, typename Op>
- void
- reduce(const communicator & comm, std::vector<T> const & in_values, Op op,
- int root)
- {
- reduce(comm, detail::c_data(in_values), in_values.size(), op, root);
- }
- template<typename T, typename Op>
- void
- reduce(const communicator & comm, std::vector<T> const & in_values,
- std::vector<T> & out_values, Op op, int root)
- {
- if (root == comm.rank()) out_values.resize(in_values.size());
- reduce(comm, detail::c_data(in_values), in_values.size(), detail::c_data(out_values), op,
- root);
- }
- template<typename T, typename Op>
- void
- reduce(const communicator& comm, const T& in_value, T& out_value, Op op,
- int root)
- {
- if (comm.rank() == root)
- detail::reduce_impl(comm, &in_value, 1, &out_value, op, root,
- is_mpi_op<Op, T>(), is_mpi_datatype<T>());
- else
- detail::reduce_impl(comm, &in_value, 1, op, root,
- is_mpi_op<Op, T>(), is_mpi_datatype<T>());
- }
- template<typename T, typename Op>
- void reduce(const communicator& comm, const T& in_value, Op op, int root)
- {
- BOOST_ASSERT(comm.rank() != root);
- detail::reduce_impl(comm, &in_value, 1, op, root,
- is_mpi_op<Op, T>(), is_mpi_datatype<T>());
- }
- } }
- #endif
|