annotate DEPENDENCIES/generic/include/boost/mpi/collectives/scatter.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents 2665513ce2d3
children
rev   line source
Chris@16 1 // Copyright (C) 2005, 2006 Douglas Gregor.
Chris@16 2
Chris@16 3 // Use, modification and distribution is subject to the Boost Software
Chris@16 4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
Chris@16 5 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 6
Chris@16 7 // Message Passing Interface 1.1 -- Section 4.6. Scatter
Chris@16 8 #ifndef BOOST_MPI_SCATTER_HPP
Chris@16 9 #define BOOST_MPI_SCATTER_HPP
Chris@16 10
Chris@16 11 #include <boost/mpi/exception.hpp>
Chris@16 12 #include <boost/mpi/datatype.hpp>
Chris@16 13 #include <vector>
Chris@16 14 #include <boost/mpi/packed_oarchive.hpp>
Chris@16 15 #include <boost/mpi/packed_iarchive.hpp>
Chris@16 16 #include <boost/mpi/detail/point_to_point.hpp>
Chris@16 17 #include <boost/mpi/communicator.hpp>
Chris@16 18 #include <boost/mpi/environment.hpp>
Chris@16 19 #include <boost/assert.hpp>
Chris@16 20
Chris@16 21 namespace boost { namespace mpi {
Chris@16 22
Chris@16 23 namespace detail {
Chris@16 24 // We're scattering from the root for a type that has an associated MPI
Chris@16 25 // datatype, so we'll use MPI_Scatter to do all of the work.
Chris@16 26 template<typename T>
Chris@16 27 void
Chris@16 28 scatter_impl(const communicator& comm, const T* in_values, T* out_values,
Chris@16 29 int n, int root, mpl::true_)
Chris@16 30 {
Chris@16 31 MPI_Datatype type = get_mpi_datatype<T>(*in_values);
Chris@16 32 BOOST_MPI_CHECK_RESULT(MPI_Scatter,
Chris@16 33 (const_cast<T*>(in_values), n, type,
Chris@16 34 out_values, n, type, root, comm));
Chris@16 35 }
Chris@16 36
Chris@16 37 // We're scattering from a non-root for a type that has an associated MPI
Chris@16 38 // datatype, so we'll use MPI_Scatter to do all of the work.
Chris@16 39 template<typename T>
Chris@16 40 void
Chris@16 41 scatter_impl(const communicator& comm, T* out_values, int n, int root,
Chris@16 42 mpl::true_)
Chris@16 43 {
Chris@16 44 MPI_Datatype type = get_mpi_datatype<T>(*out_values);
Chris@16 45 BOOST_MPI_CHECK_RESULT(MPI_Scatter,
Chris@16 46 (0, n, type,
Chris@16 47 out_values, n, type,
Chris@16 48 root, comm));
Chris@16 49 }
Chris@16 50
Chris@16 51 // We're scattering from the root for a type that does not have an
Chris@16 52 // associated MPI datatype, so we'll need to serialize
Chris@16 53 // it. Unfortunately, this means that we cannot use MPI_Scatter, so
Chris@16 54 // we'll just have the root send individual messages to the other
Chris@16 55 // processes.
Chris@16 56 template<typename T>
Chris@16 57 void
Chris@16 58 scatter_impl(const communicator& comm, const T* in_values, T* out_values,
Chris@16 59 int n, int root, mpl::false_)
Chris@16 60 {
Chris@16 61 int tag = environment::collectives_tag();
Chris@16 62 int size = comm.size();
Chris@16 63
Chris@16 64 for (int dest = 0; dest < size; ++dest) {
Chris@16 65 if (dest == root) {
Chris@16 66 // Our own values will never be transmitted: just copy them.
Chris@16 67 std::copy(in_values + dest * n, in_values + (dest + 1) * n, out_values);
Chris@16 68 } else {
Chris@16 69 // Send archive
Chris@16 70 packed_oarchive oa(comm);
Chris@16 71 for (int i = 0; i < n; ++i)
Chris@16 72 oa << in_values[dest * n + i];
Chris@16 73 detail::packed_archive_send(comm, dest, tag, oa);
Chris@16 74 }
Chris@16 75 }
Chris@16 76 }
Chris@16 77
Chris@16 78 // We're scattering to a non-root for a type that does not have an
Chris@16 79 // associated MPI datatype, so we'll need to de-serialize
Chris@16 80 // it. Unfortunately, this means that we cannot use MPI_Scatter, so
Chris@16 81 // we'll just have all of the non-root nodes send individual
Chris@16 82 // messages to the root.
Chris@16 83 template<typename T>
Chris@16 84 void
Chris@16 85 scatter_impl(const communicator& comm, T* out_values, int n, int root,
Chris@16 86 mpl::false_)
Chris@16 87 {
Chris@16 88 int tag = environment::collectives_tag();
Chris@16 89
Chris@16 90 packed_iarchive ia(comm);
Chris@16 91 MPI_Status status;
Chris@16 92 detail::packed_archive_recv(comm, root, tag, ia, status);
Chris@16 93 for (int i = 0; i < n; ++i)
Chris@16 94 ia >> out_values[i];
Chris@16 95 }
Chris@16 96 } // end namespace detail
Chris@16 97
Chris@16 98 template<typename T>
Chris@16 99 void
Chris@16 100 scatter(const communicator& comm, const T* in_values, T& out_value, int root)
Chris@16 101 {
Chris@16 102 if (comm.rank() == root)
Chris@16 103 detail::scatter_impl(comm, in_values, &out_value, 1, root,
Chris@16 104 is_mpi_datatype<T>());
Chris@16 105 else
Chris@16 106 detail::scatter_impl(comm, &out_value, 1, root, is_mpi_datatype<T>());
Chris@16 107 }
Chris@16 108
Chris@16 109 template<typename T>
Chris@16 110 void
Chris@16 111 scatter(const communicator& comm, const std::vector<T>& in_values, T& out_value,
Chris@16 112 int root)
Chris@16 113 {
Chris@16 114 if (comm.rank() == root)
Chris@16 115 ::boost::mpi::scatter<T>(comm, &in_values[0], out_value, root);
Chris@16 116 else
Chris@16 117 ::boost::mpi::scatter<T>(comm, static_cast<const T*>(0), out_value,
Chris@16 118 root);
Chris@16 119 }
Chris@16 120
Chris@16 121 template<typename T>
Chris@16 122 void scatter(const communicator& comm, T& out_value, int root)
Chris@16 123 {
Chris@16 124 BOOST_ASSERT(comm.rank() != root);
Chris@16 125 detail::scatter_impl(comm, &out_value, 1, root, is_mpi_datatype<T>());
Chris@16 126 }
Chris@16 127
Chris@16 128 template<typename T>
Chris@16 129 void
Chris@16 130 scatter(const communicator& comm, const T* in_values, T* out_values, int n,
Chris@16 131 int root)
Chris@16 132 {
Chris@16 133 if (comm.rank() == root)
Chris@16 134 detail::scatter_impl(comm, in_values, out_values, n, root,
Chris@16 135 is_mpi_datatype<T>());
Chris@16 136 else
Chris@16 137 detail::scatter_impl(comm, out_values, n, root, is_mpi_datatype<T>());
Chris@16 138 }
Chris@16 139
Chris@16 140 template<typename T>
Chris@16 141 void
Chris@16 142 scatter(const communicator& comm, const std::vector<T>& in_values,
Chris@16 143 T* out_values, int n, int root)
Chris@16 144 {
Chris@16 145 if (comm.rank() == root)
Chris@16 146 ::boost::mpi::scatter(comm, &in_values[0], out_values, n, root);
Chris@16 147 else
Chris@16 148 ::boost::mpi::scatter(comm, static_cast<const T*>(0), out_values,
Chris@16 149 n, root);
Chris@16 150 }
Chris@16 151
Chris@16 152 template<typename T>
Chris@16 153 void scatter(const communicator& comm, T* out_values, int n, int root)
Chris@16 154 {
Chris@16 155 BOOST_ASSERT(comm.rank() != root);
Chris@16 156 detail::scatter_impl(comm, out_values, n, root, is_mpi_datatype<T>());
Chris@16 157 }
Chris@16 158
Chris@16 159 } } // end namespace boost::mpi
Chris@16 160
Chris@16 161 #endif // BOOST_MPI_SCATTER_HPP