Chris@16: // Copyright (C) 2005, 2006 Douglas Gregor. 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: // Message Passing Interface 1.1 -- Section 4.5. Gather Chris@16: #ifndef BOOST_MPI_GATHER_HPP Chris@16: #define BOOST_MPI_GATHER_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace mpi { Chris@16: Chris@16: namespace detail { Chris@16: // We're gathering at the root for a type that has an associated MPI Chris@16: // datatype, so we'll use MPI_Gather to do all of the work. Chris@16: template Chris@16: void Chris@16: gather_impl(const communicator& comm, const T* in_values, int n, Chris@16: T* out_values, int root, mpl::true_) Chris@16: { Chris@16: MPI_Datatype type = get_mpi_datatype(*in_values); Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Gather, Chris@16: (const_cast(in_values), n, type, Chris@16: out_values, n, type, root, comm)); Chris@16: } Chris@16: Chris@16: // We're gathering from a non-root for a type that has an associated MPI Chris@16: // datatype, so we'll use MPI_Gather to do all of the work. Chris@16: template Chris@16: void Chris@16: gather_impl(const communicator& comm, const T* in_values, int n, int root, Chris@16: mpl::true_) Chris@16: { Chris@16: MPI_Datatype type = get_mpi_datatype(*in_values); Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Gather, Chris@16: (const_cast(in_values), n, type, Chris@16: 0, n, type, root, comm)); Chris@16: } Chris@16: Chris@16: // We're gathering at the root for a type that does not have an Chris@16: // associated MPI datatype, so we'll need to serialize Chris@16: // it. Unfortunately, this means that we cannot use MPI_Gather, so Chris@16: // we'll just have all of the non-root nodes send individual Chris@16: // messages to the root. Chris@16: template Chris@16: void Chris@16: gather_impl(const communicator& comm, const T* in_values, int n, Chris@16: T* out_values, int root, mpl::false_) Chris@16: { Chris@16: int tag = environment::collectives_tag(); Chris@16: int size = comm.size(); Chris@16: Chris@16: for (int src = 0; src < size; ++src) { Chris@16: if (src == root) Chris@16: std::copy(in_values, in_values + n, out_values + n * src); Chris@16: else Chris@16: comm.recv(src, tag, out_values + n * src, n); Chris@16: } Chris@16: } Chris@16: Chris@16: // We're gathering at a non-root for a type that does not have an Chris@16: // associated MPI datatype, so we'll need to serialize Chris@16: // it. Unfortunately, this means that we cannot use MPI_Gather, so Chris@16: // we'll just have all of the non-root nodes send individual Chris@16: // messages to the root. Chris@16: template Chris@16: void Chris@16: gather_impl(const communicator& comm, const T* in_values, int n, int root, Chris@16: mpl::false_) Chris@16: { Chris@16: int tag = environment::collectives_tag(); Chris@16: comm.send(root, tag, in_values, n); Chris@16: } Chris@16: } // end namespace detail Chris@16: Chris@16: template Chris@16: void Chris@16: gather(const communicator& comm, const T& in_value, T* out_values, int root) Chris@16: { Chris@16: if (comm.rank() == root) Chris@16: detail::gather_impl(comm, &in_value, 1, out_values, root, Chris@16: is_mpi_datatype()); Chris@16: else Chris@16: detail::gather_impl(comm, &in_value, 1, root, is_mpi_datatype()); Chris@16: } Chris@16: Chris@16: template Chris@16: void gather(const communicator& comm, const T& in_value, int root) Chris@16: { Chris@16: BOOST_ASSERT(comm.rank() != root); Chris@16: detail::gather_impl(comm, &in_value, 1, root, is_mpi_datatype()); Chris@16: } Chris@16: Chris@16: template Chris@16: void Chris@16: gather(const communicator& comm, const T& in_value, std::vector& out_values, Chris@16: int root) Chris@16: { Chris@16: if (comm.rank() == root) { Chris@16: out_values.resize(comm.size()); Chris@16: ::boost::mpi::gather(comm, in_value, &out_values[0], root); Chris@16: } else { Chris@16: ::boost::mpi::gather(comm, in_value, root); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: void Chris@16: gather(const communicator& comm, const T* in_values, int n, T* out_values, Chris@16: int root) Chris@16: { Chris@16: if (comm.rank() == root) Chris@16: detail::gather_impl(comm, in_values, n, out_values, root, Chris@16: is_mpi_datatype()); Chris@16: else Chris@16: detail::gather_impl(comm, in_values, n, root, is_mpi_datatype()); Chris@16: } Chris@16: Chris@16: template Chris@16: void Chris@16: gather(const communicator& comm, const T* in_values, int n, Chris@16: std::vector& out_values, int root) Chris@16: { Chris@16: if (comm.rank() == root) { Chris@16: out_values.resize(comm.size() * n); Chris@16: ::boost::mpi::gather(comm, in_values, n, &out_values[0], root); Chris@16: } Chris@16: else Chris@16: ::boost::mpi::gather(comm, in_values, n, root); Chris@16: } Chris@16: Chris@16: template Chris@16: void gather(const communicator& comm, const T* in_values, int n, int root) Chris@16: { Chris@16: BOOST_ASSERT(comm.rank() != root); Chris@16: detail::gather_impl(comm, in_values, n, root, is_mpi_datatype()); Chris@16: } Chris@16: Chris@16: Chris@16: } } // end namespace boost::mpi Chris@16: Chris@16: #endif // BOOST_MPI_GATHER_HPP