feat():initial version

This commit is contained in:
2026-06-09 20:16:47 +08:00
commit 85fbb3188c
14809 changed files with 3044607 additions and 0 deletions

View File

@@ -0,0 +1,137 @@
// Copyright (C) 2005, 2006 Douglas Gregor.
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Message Passing Interface 1.1 -- Section 4.5. Gather
#ifndef BOOST_MPI_ALLGATHER_HPP
#define BOOST_MPI_ALLGATHER_HPP
#include <cassert>
#include <cstddef>
#include <numeric>
#include <boost/mpi/exception.hpp>
#include <boost/mpi/datatype.hpp>
#include <vector>
#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/offsets.hpp>
#include <boost/mpi/detail/antiques.hpp>
#include <boost/assert.hpp>
namespace boost { namespace mpi {
namespace detail {
// We're all-gathering for a type that has an associated MPI
// datatype, so we'll use MPI_Gather to do all of the work.
template<typename T>
void
all_gather_impl(const communicator& comm, const T* in_values, int n,
T* out_values, mpl::true_)
{
MPI_Datatype type = get_mpi_datatype<T>(*in_values);
BOOST_MPI_CHECK_RESULT(MPI_Allgather,
(const_cast<T*>(in_values), n, type,
out_values, n, type, comm));
}
// We're all-gathering for a type that does not have an
// associated MPI datatype, so we'll need to serialize
// it.
template<typename T>
void
all_gather_impl(const communicator& comm, const T* in_values, int n,
T* out_values, int const* sizes, int const* skips, mpl::false_)
{
int nproc = comm.size();
// first, gather all size, these size can be different for
// each process
packed_oarchive oa(comm);
for (int i = 0; i < n; ++i) {
oa << in_values[i];
}
std::vector<int> oasizes(nproc);
int oasize = oa.size();
BOOST_MPI_CHECK_RESULT(MPI_Allgather,
(&oasize, 1, MPI_INT,
c_data(oasizes), 1, MPI_INT,
MPI_Comm(comm)));
// Gather the archives, which can be of different sizes, so
// we need to use allgatherv.
// Every thing is contiguous, so the offsets can be
// deduced from the collected sizes.
std::vector<int> offsets(nproc);
sizes2offsets(oasizes, offsets);
packed_iarchive::buffer_type recv_buffer(std::accumulate(oasizes.begin(), oasizes.end(), 0));
BOOST_MPI_CHECK_RESULT(MPI_Allgatherv,
(const_cast<void*>(oa.address()), int(oa.size()), MPI_BYTE,
c_data(recv_buffer), c_data(oasizes), c_data(offsets), MPI_BYTE,
MPI_Comm(comm)));
for (int src = 0; src < nproc; ++src) {
int nb = sizes ? sizes[src] : n;
int skip = skips ? skips[src] : 0;
std::advance(out_values, skip);
if (src == comm.rank()) { // this is our local data
for (int i = 0; i < nb; ++i) {
*out_values++ = *in_values++;
}
} else {
packed_iarchive ia(comm, recv_buffer, boost::archive::no_header, offsets[src]);
for (int i = 0; i < nb; ++i) {
ia >> *out_values++;
}
}
}
}
// We're all-gathering for a type that does not have an
// associated MPI datatype, so we'll need to serialize
// it.
template<typename T>
void
all_gather_impl(const communicator& comm, const T* in_values, int n,
T* out_values, mpl::false_ isnt_mpi_type)
{
all_gather_impl(comm, in_values, n, out_values, (int const*)0, (int const*)0, isnt_mpi_type);
}
} // end namespace detail
template<typename T>
void
all_gather(const communicator& comm, const T& in_value, T* out_values)
{
detail::all_gather_impl(comm, &in_value, 1, out_values, is_mpi_datatype<T>());
}
template<typename T>
void
all_gather(const communicator& comm, const T& in_value, std::vector<T>& out_values)
{
using detail::c_data;
out_values.resize(comm.size());
::boost::mpi::all_gather(comm, in_value, c_data(out_values));
}
template<typename T>
void
all_gather(const communicator& comm, const T* in_values, int n, T* out_values)
{
detail::all_gather_impl(comm, in_values, n, out_values, is_mpi_datatype<T>());
}
template<typename T>
void
all_gather(const communicator& comm, const T* in_values, int n, std::vector<T>& out_values)
{
using detail::c_data;
out_values.resize(comm.size() * n);
::boost::mpi::all_gather(comm, in_values, n, c_data(out_values));
}
} } // end namespace boost::mpi
#endif // BOOST_MPI_ALL_GATHER_HPP

View File

@@ -0,0 +1,140 @@
// Copyright (C) 2005, 2006 Douglas Gregor.
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Message Passing Interface 1.1 -- Section 4.5. Gatherv
#ifndef BOOST_MPI_ALLGATHERV_HPP
#define BOOST_MPI_ALLGATHERV_HPP
#include <cassert>
#include <cstddef>
#include <numeric>
#include <vector>
#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/offsets.hpp>
#include <boost/mpi/detail/antiques.hpp>
#include <boost/assert.hpp>
#include <boost/scoped_array.hpp>
namespace boost { namespace mpi {
namespace detail {
// We're all-gathering for a type that has an associated MPI
// datatype, so we'll use MPI_Gather to do all of the work.
template<typename T>
void
all_gatherv_impl(const communicator& comm, const T* in_values,
T* out_values, int const* sizes, int const* displs, mpl::true_)
{
// Make displacements if not provided
scoped_array<int> new_offsets_mem(make_offsets(comm, sizes, displs, -1));
if (new_offsets_mem) displs = new_offsets_mem.get();
MPI_Datatype type = get_mpi_datatype<T>(*in_values);
BOOST_MPI_CHECK_RESULT(MPI_Allgatherv,
(const_cast<T*>(in_values), sizes[comm.rank()], type,
out_values,
const_cast<int*>(sizes),
const_cast<int*>(displs),
type,
comm));
}
// We're all-gathering for a type that does not have an
// associated MPI datatype, so we'll need to serialize
// it.
template<typename T>
void
all_gatherv_impl(const communicator& comm, const T* in_values,
T* out_values, int const* sizes, int const* displs,
mpl::false_ isnt_mpi_type)
{
// convert displacement to offsets to skip
scoped_array<int> skipped(make_skipped_slots(comm, sizes, displs));
all_gather_impl(comm, in_values, sizes[comm.rank()], out_values,
sizes, skipped.get(), isnt_mpi_type);
}
} // end namespace detail
template<typename T>
void
all_gatherv(const communicator& comm, const T& in_value, T* out_values,
const std::vector<int>& sizes)
{
using detail::c_data;
assert(sizes.size() == comm.size());
assert(sizes[comm.rank()] == 1);
detail::all_gatherv_impl(comm, &in_value, out_values, c_data(sizes), 0, is_mpi_datatype<T>());
}
template<typename T>
void
all_gatherv(const communicator& comm, const T* in_values, T* out_values,
const std::vector<int>& sizes)
{
using detail::c_data;
assert(int(sizes.size()) == comm.size());
detail::all_gatherv_impl(comm, in_values, out_values, c_data(sizes), 0, is_mpi_datatype<T>());
}
template<typename T>
void
all_gatherv(const communicator& comm, std::vector<T> const& in_values, std::vector<T>& out_values,
const std::vector<int>& sizes)
{
using detail::c_data;
assert(int(sizes.size()) == comm.size());
assert(int(in_values.size()) == sizes[comm.rank()]);
out_values.resize(std::accumulate(sizes.begin(), sizes.end(), 0));
::boost::mpi::all_gatherv(comm, c_data(in_values), c_data(out_values), sizes);
}
template<typename T>
void
all_gatherv(const communicator& comm, const T& in_value, T* out_values,
const std::vector<int>& sizes, const std::vector<int>& displs)
{
using detail::c_data;
assert(sizes.size() == comm.size());
assert(displs.size() == comm.size());
detail::all_gatherv_impl(comm, &in_value, 1, out_values,
c_data(sizes), c_data(displs), is_mpi_datatype<T>());
}
template<typename T>
void
all_gatherv(const communicator& comm, const T* in_values, T* out_values,
const std::vector<int>& sizes, const std::vector<int>& displs)
{
using detail::c_data;
assert(sizes.size() == comm.size());
assert(displs.size() == comm.size());
detail::all_gatherv_impl(comm, in_values, out_values,
c_data(sizes), c_data(displs), is_mpi_datatype<T>());
}
template<typename T>
void
all_gatherv(const communicator& comm, std::vector<T> const& in_values, std::vector<T>& out_values,
const std::vector<int>& sizes, const std::vector<int>& displs)
{
using detail::c_data;
assert(sizes.size() == comm.size());
assert(displs.size() == comm.size());
assert(in_values.size() == sizes[comm.rank()]);
out_values.resize(std::accumulate(sizes.begin(), sizes.end(), 0));
::boost::mpi::all_gatherv(comm, c_data(in_values), c_data(out_values), sizes, displs);
}
} } // end namespace boost::mpi
#endif // BOOST_MPI_ALL_GATHERV_HPP

View File

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

View File

@@ -0,0 +1,151 @@
// Copyright (C) 2005, 2006 Douglas Gregor.
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Message Passing Interface 1.1 -- Section 4.8. All-to-all
#ifndef BOOST_MPI_ALL_TO_ALL_HPP
#define BOOST_MPI_ALL_TO_ALL_HPP
#include <boost/mpi/exception.hpp>
#include <boost/mpi/datatype.hpp>
#include <vector>
#include <boost/mpi/packed_oarchive.hpp>
#include <boost/mpi/packed_iarchive.hpp>
#include <boost/mpi/communicator.hpp>
#include <boost/mpi/environment.hpp>
#include <boost/assert.hpp>
#include <boost/mpi/collectives_fwd.hpp>
#include <boost/mpi/allocator.hpp>
namespace boost { namespace mpi {
namespace detail {
// We're performing an all-to-all with a type that has an
// associated MPI datatype, so we'll use MPI_Alltoall to do all of
// the work.
template<typename T>
void
all_to_all_impl(const communicator& comm, const T* in_values, int n,
T* out_values, mpl::true_)
{
MPI_Datatype type = get_mpi_datatype<T>(*in_values);
BOOST_MPI_CHECK_RESULT(MPI_Alltoall,
(const_cast<T*>(in_values), n, type,
out_values, n, type, comm));
}
// We're performing an all-to-all with a type that does not have an
// associated MPI datatype, so we'll need to serialize
// it.
template<typename T>
void
all_to_all_impl(const communicator& comm, const T* in_values, int n,
T* out_values, mpl::false_)
{
int size = comm.size();
int rank = comm.rank();
// The amount of data to be sent to each process
std::vector<int> send_sizes(size);
// The displacements for each outgoing value.
std::vector<int> send_disps(size);
// The buffer that will store all of the outgoing values
std::vector<char, allocator<char> > outgoing;
// Pack the buffer with all of the outgoing values.
for (int dest = 0; dest < size; ++dest) {
// Keep track of the displacements
send_disps[dest] = outgoing.size();
// Our own value will never be transmitted, so don't pack it.
if (dest != rank) {
packed_oarchive oa(comm, outgoing);
for (int i = 0; i < n; ++i)
oa << in_values[dest * n + i];
}
// Keep track of the sizes
send_sizes[dest] = outgoing.size() - send_disps[dest];
}
// Determine how much data each process will receive.
std::vector<int> recv_sizes(size);
all_to_all(comm, send_sizes, recv_sizes);
// Prepare a buffer to receive the incoming data.
std::vector<int> recv_disps(size);
int sum = 0;
for (int src = 0; src < size; ++src) {
recv_disps[src] = sum;
sum += recv_sizes[src];
}
std::vector<char, allocator<char> > incoming(sum > 0? sum : 1);
// Make sure we don't try to reference an empty vector
if (outgoing.empty())
outgoing.push_back(0);
// Transmit the actual data
BOOST_MPI_CHECK_RESULT(MPI_Alltoallv,
(detail::c_data(outgoing), detail::c_data(send_sizes),
detail::c_data(send_disps), MPI_PACKED,
detail::c_data(incoming), detail::c_data(recv_sizes),
detail::c_data(recv_disps), MPI_PACKED,
comm));
// Deserialize data from the iarchive
for (int src = 0; src < size; ++src) {
if (src == rank)
std::copy(in_values + src * n, in_values + (src + 1) * n,
out_values + src * n);
else {
packed_iarchive ia(comm, incoming, boost::archive::no_header,
recv_disps[src]);
for (int i = 0; i < n; ++i)
ia >> out_values[src * n + i];
}
}
}
} // end namespace detail
template<typename T>
inline void
all_to_all(const communicator& comm, const T* in_values, T* out_values)
{
detail::all_to_all_impl(comm, in_values, 1, out_values, is_mpi_datatype<T>());
}
template<typename T>
void
all_to_all(const communicator& comm, const std::vector<T>& in_values,
std::vector<T>& out_values)
{
BOOST_ASSERT((int)in_values.size() == comm.size());
out_values.resize(comm.size());
::boost::mpi::all_to_all(comm, detail::c_data(in_values), detail::c_data(out_values));
}
template<typename T>
inline void
all_to_all(const communicator& comm, const T* in_values, int n, T* out_values)
{
detail::all_to_all_impl(comm, in_values, n, out_values, is_mpi_datatype<T>());
}
template<typename T>
void
all_to_all(const communicator& comm, const std::vector<T>& in_values, int n,
std::vector<T>& out_values)
{
BOOST_ASSERT((int)in_values.size() == comm.size() * n);
out_values.resize(comm.size() * n);
::boost::mpi::all_to_all(comm, detail::c_data(in_values), n, detail::c_data(out_values));
}
} } // end namespace boost::mpi
#endif // BOOST_MPI_ALL_TO_ALL_HPP

View File

@@ -0,0 +1,158 @@
// Copyright (C) 2005, 2006 Douglas Gregor <doug.gregor -at- gmail.com>.
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Message Passing Interface 1.1 -- Section 4.4. Broadcast
#ifndef BOOST_MPI_BROADCAST_HPP
#define BOOST_MPI_BROADCAST_HPP
#include <boost/mpi/collectives_fwd.hpp>
#include <boost/mpi/exception.hpp>
#include <boost/mpi/datatype.hpp>
#include <boost/mpi/communicator.hpp>
namespace boost { namespace mpi {
/************************************************************************
* Specializations *
************************************************************************/
/**
* INTERNAL ONLY
*/
template<>
BOOST_MPI_DECL void
broadcast<const packed_oarchive>(const communicator& comm,
const packed_oarchive& oa,
int root);
/**
* INTERNAL ONLY
*/
template<>
BOOST_MPI_DECL void
broadcast<packed_oarchive>(const communicator& comm, packed_oarchive& oa,
int root);
/**
* INTERNAL ONLY
*/
template<>
BOOST_MPI_DECL void
broadcast<packed_iarchive>(const communicator& comm, packed_iarchive& ia,
int root);
/**
* INTERNAL ONLY
*/
template<>
BOOST_MPI_DECL void
broadcast<const packed_skeleton_oarchive>(const communicator& comm,
const packed_skeleton_oarchive& oa,
int root);
/**
* INTERNAL ONLY
*/
template<>
void
broadcast<packed_skeleton_oarchive>(const communicator& comm,
packed_skeleton_oarchive& oa, int root);
/**
* INTERNAL ONLY
*/
template<>
void
broadcast<packed_skeleton_iarchive>(const communicator& comm,
packed_skeleton_iarchive& ia, int root);
/**
* INTERNAL ONLY
*/
template<>
void broadcast<content>(const communicator& comm, content& c, int root);
/**
* INTERNAL ONLY
*/
template<>
void broadcast<const content>(const communicator& comm, const content& c,
int root);
/************************************************************************
* broadcast() implementation *
************************************************************************/
namespace detail {
// We're sending a type that has an associated MPI datatype, so
// we'll use MPI_Bcast to do all of the work.
template<typename T>
void
broadcast_impl(const communicator& comm, T* values, int n, int root,
mpl::true_)
{
BOOST_MPI_CHECK_RESULT(MPI_Bcast,
(values, n,
boost::mpi::get_mpi_datatype<T>(*values),
root, MPI_Comm(comm)));
}
// We're sending a type that does not have an associated MPI
// datatype, so we'll need to serialize it.
template<typename T>
void
broadcast_impl(const communicator& comm, T* values, int n, int root,
mpl::false_ non_mpi_datatype)
{
// Implementation proposed by Lorenz Hübschle-Schneider
if (comm.rank() == root) {
packed_oarchive oa(comm);
for (int i = 0; i < n; ++i) {
oa << values[i];
}
std::size_t asize = oa.size();
broadcast(comm, asize, root);
void const* aptr = oa.address();
BOOST_MPI_CHECK_RESULT(MPI_Bcast,
(const_cast<void*>(aptr), asize,
MPI_BYTE,
root, MPI_Comm(comm)));
} else {
packed_iarchive ia(comm);
std::size_t asize;
broadcast(comm, asize, root);
ia.resize(asize);
void* aptr = ia.address();
BOOST_MPI_CHECK_RESULT(MPI_Bcast,
(aptr, asize,
MPI_BYTE,
root, MPI_Comm(comm)));
for (int i = 0; i < n; ++i)
ia >> values[i];
}
}
} // end namespace detail
template<typename T>
void broadcast(const communicator& comm, T& value, int root)
{
detail::broadcast_impl(comm, &value, 1, root, is_mpi_datatype<T>());
}
template<typename T>
void broadcast(const communicator& comm, T* values, int n, int root)
{
detail::broadcast_impl(comm, values, n, root, is_mpi_datatype<T>());
}
} } // end namespace boost::mpi
// If the user has already included skeleton_and_content.hpp, include
// the code to broadcast skeletons and content.
#ifdef BOOST_MPI_SKELETON_AND_CONTENT_HPP
# include <boost/mpi/detail/broadcast_sc.hpp>
#endif
#endif // BOOST_MPI_BROADCAST_HPP

View File

@@ -0,0 +1,176 @@
// Copyright (C) 2005, 2006 Douglas Gregor.
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Message Passing Interface 1.1 -- Section 4.5. Gather
#ifndef BOOST_MPI_GATHER_HPP
#define BOOST_MPI_GATHER_HPP
#include <cassert>
#include <cstddef>
#include <numeric>
#include <boost/mpi/exception.hpp>
#include <boost/mpi/datatype.hpp>
#include <vector>
#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/offsets.hpp>
#include <boost/mpi/detail/antiques.hpp>
#include <boost/assert.hpp>
namespace boost { namespace mpi {
namespace detail {
// We're gathering at the root for a type that has an associated MPI
// datatype, so we'll use MPI_Gather to do all of the work.
template<typename T>
void
gather_impl(const communicator& comm, const T* in_values, int n,
T* out_values, int root, mpl::true_)
{
MPI_Datatype type = get_mpi_datatype<T>(*in_values);
BOOST_MPI_CHECK_RESULT(MPI_Gather,
(const_cast<T*>(in_values), n, type,
out_values, n, type, root, comm));
}
// We're gathering from a non-root for a type that has an associated MPI
// datatype, so we'll use MPI_Gather to do all of the work.
template<typename T>
void
gather_impl(const communicator& comm, const T* in_values, int n, int root,
mpl::true_ is_mpi_type)
{
assert(comm.rank() != root);
gather_impl(comm, in_values, n, (T*)0, root, is_mpi_type);
}
// We're gathering at the root for a type that does not have an
// associated MPI datatype, so we'll need to serialize
// it.
template<typename T>
void
gather_impl(const communicator& comm, const T* in_values, int n, T* out_values,
int const* nslot, int const* nskip, int root, mpl::false_)
{
int nproc = comm.size();
// first, gather all size, these size can be different for
// each process
packed_oarchive oa(comm);
for (int i = 0; i < n; ++i) {
oa << in_values[i];
}
bool is_root = comm.rank() == root;
std::vector<int> oasizes(is_root ? nproc : 0);
int oasize = oa.size();
BOOST_MPI_CHECK_RESULT(MPI_Gather,
(&oasize, 1, MPI_INT,
c_data(oasizes), 1, MPI_INT,
root, MPI_Comm(comm)));
// Gather the archives, which can be of different sizes, so
// we need to use gatherv.
// Everything is contiguous (in the transmitted archive), so
// the offsets can be deduced from the collected sizes.
std::vector<int> offsets;
if (is_root) sizes2offsets(oasizes, offsets);
packed_iarchive::buffer_type recv_buffer(is_root ? std::accumulate(oasizes.begin(), oasizes.end(), 0) : 0);
BOOST_MPI_CHECK_RESULT(MPI_Gatherv,
(const_cast<void*>(oa.address()), int(oa.size()), MPI_BYTE,
c_data(recv_buffer), c_data(oasizes), c_data(offsets), MPI_BYTE,
root, MPI_Comm(comm)));
if (is_root) {
for (int src = 0; src < nproc; ++src) {
// handle variadic case
int nb = nslot ? nslot[src] : n;
int skip = nskip ? nskip[src] : 0;
std::advance(out_values, skip);
if (src == root) {
BOOST_ASSERT(nb == n);
for (int i = 0; i < nb; ++i) {
*out_values++ = *in_values++;
}
} else {
packed_iarchive ia(comm, recv_buffer, boost::archive::no_header, offsets[src]);
for (int i = 0; i < nb; ++i) {
ia >> *out_values++;
}
}
}
}
}
// We're gathering at a non-root for a type that does not have an
// associated MPI datatype, so we'll need to serialize
// it.
template<typename T>
void
gather_impl(const communicator& comm, const T* in_values, int n, T* out_values,int root,
mpl::false_ is_mpi_type)
{
gather_impl(comm, in_values, n, out_values, (int const*)0, (int const*)0, root, is_mpi_type);
}
} // end namespace detail
template<typename T>
void
gather(const communicator& comm, const T& in_value, T* out_values, int root)
{
BOOST_ASSERT(out_values || (comm.rank() != root));
detail::gather_impl(comm, &in_value, 1, out_values, root, is_mpi_datatype<T>());
}
template<typename T>
void gather(const communicator& comm, const T& in_value, int root)
{
BOOST_ASSERT(comm.rank() != root);
detail::gather_impl(comm, &in_value, 1, (T*)0, root, is_mpi_datatype<T>());
}
template<typename T>
void
gather(const communicator& comm, const T& in_value, std::vector<T>& out_values,
int root)
{
using detail::c_data;
if (comm.rank() == root) {
out_values.resize(comm.size());
}
::boost::mpi::gather(comm, in_value, c_data(out_values), root);
}
template<typename T>
void
gather(const communicator& comm, const T* in_values, int n, T* out_values,
int root)
{
detail::gather_impl(comm, in_values, n, out_values, root,
is_mpi_datatype<T>());
}
template<typename T>
void
gather(const communicator& comm, const T* in_values, int n,
std::vector<T>& out_values, int root)
{
if (comm.rank() == root) {
out_values.resize(comm.size() * n);
}
::boost::mpi::gather(comm, in_values, n, out_values.data(), root);
}
template<typename T>
void gather(const communicator& comm, const T* in_values, int n, int root)
{
BOOST_ASSERT(comm.rank() != root);
detail::gather_impl(comm, in_values, n, root, is_mpi_datatype<T>());
}
} } // end namespace boost::mpi
#endif // BOOST_MPI_GATHER_HPP

View File

@@ -0,0 +1,147 @@
// Copyright (C) 2011 Júlio Hoffimann.
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Message Passing Interface 1.1 -- Section 4.5. Gatherv
#ifndef BOOST_MPI_GATHERV_HPP
#define BOOST_MPI_GATHERV_HPP
#include <vector>
#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/offsets.hpp>
#include <boost/assert.hpp>
#include <boost/scoped_array.hpp>
namespace boost { namespace mpi {
namespace detail {
// We're gathering at the root for a type that has an associated MPI
// datatype, so we'll use MPI_Gatherv to do all of the work.
template<typename T>
void
gatherv_impl(const communicator& comm, const T* in_values, int in_size,
T* out_values, const int* sizes, const int* displs, int root, mpl::true_)
{
MPI_Datatype type = get_mpi_datatype<T>(*in_values);
BOOST_MPI_CHECK_RESULT(MPI_Gatherv,
(const_cast<T*>(in_values), in_size, type,
out_values, const_cast<int*>(sizes), const_cast<int*>(displs),
type, root, comm));
}
// We're gathering from a non-root for a type that has an associated MPI
// datatype, so we'll use MPI_Gatherv to do all of the work.
template<typename T>
void
gatherv_impl(const communicator& comm, const T* in_values, int in_size, int root,
mpl::true_)
{
MPI_Datatype type = get_mpi_datatype<T>(*in_values);
BOOST_MPI_CHECK_RESULT(MPI_Gatherv,
(const_cast<T*>(in_values), in_size, type,
0, 0, 0, type, root, comm));
}
// We're gathering at the root for a type that does not have an
// associated MPI datatype, so we'll need to serialize
// it. Unfortunately, this means that we cannot use MPI_Gatherv, so
// we'll just have all of the non-root nodes send individual
// messages to the root.
template<typename T>
void
gatherv_impl(const communicator& comm, const T* in_values, int in_size,
T* out_values, const int* sizes, const int* displs, int root, mpl::false_)
{
// convert displacement to offsets to skip
scoped_array<int> skipped(make_skipped_slots(comm, sizes, displs, root));
gather_impl(comm, in_values, in_size, out_values, sizes, skipped.get(), root, mpl::false_());
}
// We're gathering at a non-root for a type that does not have an
// associated MPI datatype, so we'll need to serialize
// it.
template<typename T>
void
gatherv_impl(const communicator& comm, const T* in_values, int in_size, int root,
mpl::false_)
{
gather_impl(comm, in_values, in_size, (T*)0,(int const*)0,(int const*)0, root,
mpl::false_());
}
} // end namespace detail
template<typename T>
void
gatherv(const communicator& comm, const T* in_values, int in_size,
T* out_values, const std::vector<int>& sizes, const std::vector<int>& displs,
int root)
{
if (comm.rank() == root)
detail::gatherv_impl(comm, in_values, in_size,
out_values, detail::c_data(sizes), detail::c_data(displs),
root, is_mpi_datatype<T>());
else
detail::gatherv_impl(comm, in_values, in_size, root, is_mpi_datatype<T>());
}
template<typename T>
void
gatherv(const communicator& comm, const std::vector<T>& in_values,
T* out_values, const std::vector<int>& sizes, const std::vector<int>& displs,
int root)
{
::boost::mpi::gatherv(comm, detail::c_data(in_values), in_values.size(), out_values, sizes, displs, root);
}
template<typename T>
void gatherv(const communicator& comm, const T* in_values, int in_size, int root)
{
BOOST_ASSERT(comm.rank() != root);
detail::gatherv_impl(comm, in_values, in_size, root, is_mpi_datatype<T>());
}
template<typename T>
void gatherv(const communicator& comm, const std::vector<T>& in_values, int root)
{
BOOST_ASSERT(comm.rank() != root);
detail::gatherv_impl(comm, detail::c_data(in_values), in_values.size(), root, is_mpi_datatype<T>());
}
///////////////////////
// common use versions
///////////////////////
template<typename T>
void
gatherv(const communicator& comm, const T* in_values, int in_size,
T* out_values, const std::vector<int>& sizes, int root)
{
int nprocs = comm.size();
std::vector<int> displs( nprocs );
for (int rank = 0, aux = 0; rank < nprocs; ++rank) {
displs[rank] = aux;
aux += sizes[rank];
}
::boost::mpi::gatherv(comm, in_values, in_size, out_values, sizes, displs, root);
}
template<typename T>
void
gatherv(const communicator& comm, const std::vector<T>& in_values,
T* out_values, const std::vector<int>& sizes, int root)
{
::boost::mpi::gatherv(comm, detail::c_data(in_values), in_values.size(), out_values, sizes, root);
}
} } // end namespace boost::mpi
#endif // BOOST_MPI_GATHERV_HPP

View File

@@ -0,0 +1,376 @@
// Copyright (C) 2005-2006 Douglas Gregor <doug.gregor@gmail.com>.
// Copyright (C) 2004 The Trustees of Indiana University
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Authors: Douglas Gregor
// Andrew Lumsdaine
// Message Passing Interface 1.1 -- Section 4.9.1. Reduce
#ifndef BOOST_MPI_REDUCE_HPP
#define BOOST_MPI_REDUCE_HPP
#include <boost/mpi/exception.hpp>
#include <boost/mpi/datatype.hpp>
// For (de-)serializing sends and receives
#include <boost/mpi/packed_oarchive.hpp>
#include <boost/mpi/packed_iarchive.hpp>
// For packed_[io]archive sends and receives
#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 {
/************************************************************************
* Implementation details *
************************************************************************/
namespace detail {
/**********************************************************************
* Simple reduction with MPI_Reduce *
**********************************************************************/
// We are reducing at the root for a type that has an associated MPI
// datatype and operation, so we'll use MPI_Reduce directly.
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::true_ /*is_mpi_op*/,
mpl::true_/*is_mpi_datatype*/)
{
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));
}
// We are reducing to the root for a type that has an associated MPI
// datatype and operation, so we'll use MPI_Reduce directly.
template<typename T, typename Op>
void
reduce_impl(const communicator& comm, const T* in_values, int n, Op /*op*/,
int root, mpl::true_ /*is_mpi_op*/, mpl::true_/*is_mpi_datatype*/)
{
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));
}
/**********************************************************************
* User-defined reduction with MPI_Reduce *
**********************************************************************/
// We are reducing at the root for a type that has an associated MPI
// datatype but with a custom operation. We'll use MPI_Reduce
// directly, but we'll need to create an MPI_Op manually.
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_ /*is_mpi_op*/,
mpl::true_/*is_mpi_datatype*/)
{
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));
}
// We are reducing to the root for a type that has an associated MPI
// datatype but with a custom operation. We'll use MPI_Reduce
// directly, but we'll need to create an MPI_Op manually.
template<typename T, typename Op>
void
reduce_impl(const communicator& comm, const T* in_values, int n, Op op,
int root, mpl::false_/*is_mpi_op*/, mpl::true_/*is_mpi_datatype*/)
{
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));
}
/**********************************************************************
* User-defined, tree-based reduction for non-MPI data types *
**********************************************************************/
// Commutative reduction
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_ /*is_commutative*/)
{
std::copy(in_values, in_values + n, out_values);
int size = comm.size();
int rank = comm.rank();
// The computation tree we will use.
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) {
// Receive archive
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);
}
}
// For non-roots, send the result to the parent.
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);
}
}
// Commutative reduction from a non-root.
template<typename T, typename Op>
void
tree_reduce_impl(const communicator& comm, const T* in_values, int n, Op op,
int root, mpl::true_ /*is_commutative*/)
{
scoped_array<T> results(new T[n]);
detail::tree_reduce_impl(comm, in_values, n, results.get(), op, root,
mpl::true_());
}
// Non-commutative reduction
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_ /*is_commutative*/)
{
int tag = environment::collectives_tag();
int left_child = root / 2;
int right_child = (root + comm.size()) / 2;
MPI_Status status;
if (left_child != root) {
// Receive value from the left child and merge it with the value
// we had incoming.
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 {
// There was no left value, so copy our incoming value.
std::copy(in_values, in_values + n, out_values);
}
if (right_child != root) {
// Receive value from the right child and merge it with the
// value we had incoming.
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);
}
}
}
// Non-commutative reduction from a non-root.
template<typename T, typename Op>
void
tree_reduce_impl(const communicator& comm, const T* in_values, int n, Op op,
int root, mpl::false_ /*is_commutative*/)
{
int size = comm.size();
int rank = comm.rank();
int tag = environment::collectives_tag();
// Determine our parents and children in the commutative binary
// computation tree.
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) {
// Go left.
grandparent = parent;
right_bound = parent;
parent = left_child;
} else if (rank > parent) {
// Go right.
grandparent = parent;
left_bound = parent + 1;
parent = right_child;
} else {
// We've found the parent
break;
}
} while (true);
// Our parent is the grandparent of our children. This is a slight
// abuse of notation, but it makes the send-to-parent below make
// more sense.
parent = grandparent;
MPI_Status status;
scoped_array<T> out_values(new T[n]);
if (left_child != rank) {
// Receive value from the left child and merge it with the value
// we had incoming.
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 {
// There was no left value, so copy our incoming value.
std::copy(in_values, in_values + n, out_values.get());
}
if (right_child != rank) {
// Receive value from the right child and merge it with the
// value we had incoming.
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);
}
}
// Send the combined value to our parent.
packed_oarchive oa(comm);
for (int i = 0; i < n; ++i)
oa << out_values[i];
detail::packed_archive_send(comm, parent, tag, oa);
}
// We are reducing at the root for a type that has no associated MPI
// datatype and operation, so we'll use a simple tree-based
// algorithm.
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_ /*is_mpi_op*/,
mpl::false_ /*is_mpi_datatype*/)
{
detail::tree_reduce_impl(comm, in_values, n, out_values, op, root,
is_commutative<Op, T>());
}
// We are reducing to the root for a type that has no associated MPI
// datatype and operation, so we'll use a simple tree-based
// algorithm.
template<typename T, typename Op>
void
reduce_impl(const communicator& comm, const T* in_values, int n, Op op,
int root, mpl::false_ /*is_mpi_op*/,
mpl::false_ /*is_mpi_datatype*/)
{
detail::tree_reduce_impl(comm, in_values, n, op, root,
is_commutative<Op, T>());
}
} // end namespace detail
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>());
}
} } // end namespace boost::mpi
#endif // BOOST_MPI_REDUCE_HPP

View File

@@ -0,0 +1,168 @@
// Copyright (C) 2005-2006 Douglas Gregor <doug.gregor@gmail.com>.
// Copyright (C) 2004 The Trustees of Indiana University
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Authors: Douglas Gregor
// Andrew Lumsdaine
// Message Passing Interface 1.1 -- Section 4.9.1. Scan
#ifndef BOOST_MPI_SCAN_HPP
#define BOOST_MPI_SCAN_HPP
#include <boost/mpi/exception.hpp>
#include <boost/mpi/datatype.hpp>
// For (de-)serializing sends and receives
#include <boost/mpi/packed_oarchive.hpp>
#include <boost/mpi/packed_iarchive.hpp>
// For packed_[io]archive sends and receives
#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>
namespace boost { namespace mpi {
/************************************************************************
* Implementation details *
************************************************************************/
namespace detail {
/**********************************************************************
* Simple prefix reduction with MPI_Scan *
**********************************************************************/
// We are performing prefix reduction for a type that has an
// associated MPI datatype and operation, so we'll use MPI_Scan
// directly.
template<typename T, typename Op>
void
scan_impl(const communicator& comm, const T* in_values, int n, T* out_values,
Op /*op*/, mpl::true_ /*is_mpi_op*/, mpl::true_ /*is_mpi_datatype*/)
{
BOOST_MPI_CHECK_RESULT(MPI_Scan,
(const_cast<T*>(in_values), out_values, n,
boost::mpi::get_mpi_datatype<T>(*in_values),
(is_mpi_op<Op, T>::op()), comm));
}
/**********************************************************************
* User-defined prefix reduction with MPI_Scan *
**********************************************************************/
// We are performing prefix reduction for a type that has an
// associated MPI datatype but with a custom operation. We'll use
// MPI_Scan directly, but we'll need to create an MPI_Op manually.
template<typename T, typename Op>
void
scan_impl(const communicator& comm, const T* in_values, int n, T* out_values,
Op op, mpl::false_ /*is_mpi_op*/, mpl::true_ /*is_mpi_datatype*/)
{
user_op<Op, T> mpi_op;
BOOST_MPI_CHECK_RESULT(MPI_Scan,
(const_cast<T*>(in_values), out_values, n,
boost::mpi::get_mpi_datatype<T>(*in_values),
mpi_op.get_mpi_op(), comm));
}
/**********************************************************************
* User-defined, tree-based reduction for non-MPI data types *
**********************************************************************/
template<typename T, typename Op>
void
upper_lower_scan(const communicator& comm, const T* in_values, int n,
T* out_values, Op& op, int lower, int upper)
{
int tag = environment::collectives_tag();
int rank = comm.rank();
if (lower + 1 == upper) {
std::copy(in_values, in_values + n, out_values);
} else {
int middle = (lower + upper) / 2;
if (rank < middle) {
// Lower half
upper_lower_scan(comm, in_values, n, out_values, op, lower, middle);
// If we're the last process in the lower half, send our values
// to everyone in the upper half.
if (rank == middle - 1) {
packed_oarchive oa(comm);
for (int i = 0; i < n; ++i)
oa << out_values[i];
for (int p = middle; p < upper; ++p)
comm.send(p, tag, oa);
}
} else {
// Upper half
upper_lower_scan(comm, in_values, n, out_values, op, middle, upper);
// Receive value from the last process in the lower half.
packed_iarchive ia(comm);
comm.recv(middle - 1, tag, ia);
// Combine value that came from the left with our value
T left_value;
for (int i = 0; i < n; ++i)
{
ia >> left_value;
out_values[i] = op(left_value, out_values[i]);
}
}
}
}
// We are performing prefix reduction for a type that has no
// associated MPI datatype and operation, so we'll use a simple
// upper/lower algorithm.
template<typename T, typename Op>
inline void
scan_impl(const communicator& comm, const T* in_values, int n, T* out_values,
Op op, mpl::false_ /*is_mpi_op*/, mpl::false_/*is_mpi_datatype*/)
{
upper_lower_scan(comm, in_values, n, out_values, op, 0, comm.size());
}
} // end namespace detail
template<typename T, typename Op>
inline void
scan(const communicator& comm, const T& in_value, T& out_value, Op op)
{
detail::scan_impl(comm, &in_value, 1, &out_value, op,
is_mpi_op<Op, T>(), is_mpi_datatype<T>());
}
template<typename T, typename Op>
inline void
scan(const communicator& comm, const T* in_values, int n, T* out_values, Op op)
{
detail::scan_impl(comm, in_values, n, out_values, op,
is_mpi_op<Op, T>(), is_mpi_datatype<T>());
}
template<typename T, typename Op>
inline T
scan(const communicator& comm, const T& in_value, Op op)
{
T out_value;
detail::scan_impl(comm, &in_value, 1, &out_value, op,
is_mpi_op<Op, T>(), is_mpi_datatype<T>());
return out_value;
}
} } // end namespace boost::mpi
#endif // BOOST_MPI_SCAN_HPP

View File

@@ -0,0 +1,203 @@
// Copyright (C) 2005, 2006 Douglas Gregor.
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Message Passing Interface 1.1 -- Section 4.6. Scatter
#ifndef BOOST_MPI_SCATTER_HPP
#define BOOST_MPI_SCATTER_HPP
#include <boost/mpi/exception.hpp>
#include <boost/mpi/datatype.hpp>
#include <vector>
#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/offsets.hpp>
#include <boost/mpi/detail/antiques.hpp>
#include <boost/assert.hpp>
namespace boost { namespace mpi {
namespace detail {
// We're scattering from the root for a type that has an associated MPI
// datatype, so we'll use MPI_Scatter to do all of the work.
template<typename T>
void
scatter_impl(const communicator& comm, const T* in_values, T* out_values,
int n, int root, mpl::true_)
{
MPI_Datatype type = get_mpi_datatype<T>(*in_values);
BOOST_MPI_CHECK_RESULT(MPI_Scatter,
(const_cast<T*>(in_values), n, type,
out_values, n, type, root, comm));
}
// We're scattering from a non-root for a type that has an associated MPI
// datatype, so we'll use MPI_Scatter to do all of the work.
template<typename T>
void
scatter_impl(const communicator& comm, T* out_values, int n, int root,
mpl::true_)
{
MPI_Datatype type = get_mpi_datatype<T>(*out_values);
BOOST_MPI_CHECK_RESULT(MPI_Scatter,
(0, n, type,
out_values, n, type,
root, comm));
}
// Fill the sendbuf while keeping trac of the slot's footprints
// Used in the first steps of both scatter and scatterv
// Nslots contains the number of slots being sent
// to each process (identical values for scatter).
// skiped_slots, if present, is deduced from the
// displacement array authorised be the MPI API,
// for some yet to be determined reason.
template<typename T>
void
fill_scatter_sendbuf(const communicator& comm, T const* values,
int const* nslots, int const* skipped_slots,
packed_oarchive::buffer_type& sendbuf, std::vector<int>& archsizes) {
int nproc = comm.size();
archsizes.resize(nproc);
for (int dest = 0; dest < nproc; ++dest) {
if (skipped_slots) { // wee need to keep this for backward compatibility
for(int k= 0; k < skipped_slots[dest]; ++k) ++values;
}
packed_oarchive procarchive(comm);
for (int i = 0; i < nslots[dest]; ++i) {
procarchive << *values++;
}
int archsize = procarchive.size();
sendbuf.resize(sendbuf.size() + archsize);
archsizes[dest] = archsize;
char const* aptr = static_cast<char const*>(procarchive.address());
std::copy(aptr, aptr+archsize, sendbuf.end()-archsize);
}
}
template<typename T, class A>
T*
non_const_data(std::vector<T,A> const& v) {
using detail::c_data;
return const_cast<T*>(c_data(v));
}
// Dispatch the sendbuf among proc.
// Used in the second steps of both scatter and scatterv
// in_value is only provide in the non variadic case.
template<typename T>
void
dispatch_scatter_sendbuf(const communicator& comm,
packed_oarchive::buffer_type const& sendbuf, std::vector<int> const& archsizes,
T const* in_values,
T* out_values, int n, int root) {
// Distribute the sizes
int myarchsize;
BOOST_MPI_CHECK_RESULT(MPI_Scatter,
(non_const_data(archsizes), 1, MPI_INT,
&myarchsize, 1, MPI_INT, root, comm));
std::vector<int> offsets;
if (root == comm.rank()) {
sizes2offsets(archsizes, offsets);
}
// Get my proc archive
packed_iarchive::buffer_type recvbuf;
recvbuf.resize(myarchsize);
BOOST_MPI_CHECK_RESULT(MPI_Scatterv,
(non_const_data(sendbuf), non_const_data(archsizes), c_data(offsets), MPI_BYTE,
c_data(recvbuf), recvbuf.size(), MPI_BYTE,
root, MPI_Comm(comm)));
// Unserialize
if ( in_values != 0 && root == comm.rank()) {
// Our own local values are already here: just copy them.
std::copy(in_values + root * n, in_values + (root + 1) * n, out_values);
} else {
// Otherwise deserialize:
packed_iarchive iarchv(comm, recvbuf);
for (int i = 0; i < n; ++i) {
iarchv >> out_values[i];
}
}
}
// We're scattering from the root for a type that does not have an
// associated MPI datatype, so we'll need to serialize it.
template<typename T>
void
scatter_impl(const communicator& comm, const T* in_values, T* out_values,
int n, int root, mpl::false_)
{
packed_oarchive::buffer_type sendbuf;
std::vector<int> archsizes;
if (root == comm.rank()) {
std::vector<int> nslots(comm.size(), n);
fill_scatter_sendbuf(comm, in_values, c_data(nslots), (int const*)0, sendbuf, archsizes);
}
dispatch_scatter_sendbuf(comm, sendbuf, archsizes, in_values, out_values, n, root);
}
template<typename T>
void
scatter_impl(const communicator& comm, T* out_values, int n, int root,
mpl::false_ is_mpi_type)
{
scatter_impl(comm, (T const*)0, out_values, n, root, is_mpi_type);
}
} // end namespace detail
template<typename T>
void
scatter(const communicator& comm, const T* in_values, T& out_value, int root)
{
detail::scatter_impl(comm, in_values, &out_value, 1, root, is_mpi_datatype<T>());
}
template<typename T>
void
scatter(const communicator& comm, const std::vector<T>& in_values, T& out_value,
int root)
{
using detail::c_data;
::boost::mpi::scatter<T>(comm, c_data(in_values), out_value, root);
}
template<typename T>
void scatter(const communicator& comm, T& out_value, int root)
{
BOOST_ASSERT(comm.rank() != root);
detail::scatter_impl(comm, &out_value, 1, root, is_mpi_datatype<T>());
}
template<typename T>
void
scatter(const communicator& comm, const T* in_values, T* out_values, int n,
int root)
{
detail::scatter_impl(comm, in_values, out_values, n, root, is_mpi_datatype<T>());
}
template<typename T>
void
scatter(const communicator& comm, const std::vector<T>& in_values,
T* out_values, int n, int root)
{
::boost::mpi::scatter(comm, detail::c_data(in_values), out_values, n, root);
}
template<typename T>
void scatter(const communicator& comm, T* out_values, int n, int root)
{
BOOST_ASSERT(comm.rank() != root);
detail::scatter_impl(comm, out_values, n, root, is_mpi_datatype<T>());
}
} } // end namespace boost::mpi
#endif // BOOST_MPI_SCATTER_HPP

View File

@@ -0,0 +1,167 @@
// Copyright (C) 2011 Júlio Hoffimann.
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Message Passing Interface 1.1 -- Section 4.6. Scatterv
#ifndef BOOST_MPI_SCATTERV_HPP
#define BOOST_MPI_SCATTERV_HPP
#include <boost/scoped_array.hpp>
#include <boost/mpi/collectives/scatter.hpp>
#include <boost/mpi/detail/offsets.hpp>
#include <boost/mpi/detail/antiques.hpp>
namespace boost { namespace mpi {
namespace detail {
//////////////////////////////////////////////
/// Implementation for MPI primitive types ///
//////////////////////////////////////////////
// We're scattering from the root for a type that has an associated MPI
// datatype, so we'll use MPI_Scatterv to do all of the work.
template<typename T>
void
scatterv_impl(const communicator& comm, const T* in_values, T* out_values, int out_size,
const int* sizes, const int* displs, int root, mpl::true_)
{
assert(!sizes || out_size == sizes[comm.rank()]);
assert(bool(sizes) == bool(in_values));
scoped_array<int> new_offsets_mem(make_offsets(comm, sizes, displs, root));
if (new_offsets_mem) displs = new_offsets_mem.get();
MPI_Datatype type = get_mpi_datatype<T>(*in_values);
BOOST_MPI_CHECK_RESULT(MPI_Scatterv,
(const_cast<T*>(in_values), const_cast<int*>(sizes),
const_cast<int*>(displs), type,
out_values, out_size, type, root, comm));
}
// We're scattering from a non-root for a type that has an associated MPI
// datatype, so we'll use MPI_Scatterv to do all of the work.
template<typename T>
void
scatterv_impl(const communicator& comm, T* out_values, int out_size, int root,
mpl::true_ is_mpi_type)
{
scatterv_impl(comm, (T const*)0, out_values, out_size,
(const int*)0, (const int*)0, root, is_mpi_type);
}
//////////////////////////////////////////////////
/// Implementation for non MPI primitive types ///
//////////////////////////////////////////////////
// We're scattering from the root for a type that does not have an
// associated MPI datatype, so we'll need to serialize it.
template<typename T>
void
scatterv_impl(const communicator& comm, const T* in_values, T* out_values, int out_size,
int const* sizes, int const* displs, int root, mpl::false_)
{
packed_oarchive::buffer_type sendbuf;
bool is_root = comm.rank() == root;
int nproc = comm.size();
std::vector<int> archsizes;
if (is_root) {
assert(out_size == sizes[comm.rank()]);
archsizes.resize(nproc);
std::vector<int> skipped;
if (displs) {
skipped.resize(nproc);
offsets2skipped(sizes, displs, c_data(skipped), nproc);
displs = c_data(skipped);
}
fill_scatter_sendbuf(comm, in_values, sizes, (int const*)0, sendbuf, archsizes);
}
dispatch_scatter_sendbuf(comm, sendbuf, archsizes, (T const*)0, out_values, out_size, root);
}
// We're scattering to a non-root for a type that does not have an
// associated MPI datatype. input data not needed.
// it.
template<typename T>
void
scatterv_impl(const communicator& comm, T* out_values, int n, int root,
mpl::false_ isnt_mpi_type)
{
assert(root != comm.rank());
scatterv_impl(comm, (T const*)0, out_values, n, (int const*)0, (int const*)0, root, isnt_mpi_type);
}
} // end namespace detail
template<typename T>
void
scatterv(const communicator& comm, const T* in_values,
const std::vector<int>& sizes, const std::vector<int>& displs,
T* out_values, int out_size, int root)
{
using detail::c_data;
detail::scatterv_impl(comm, in_values, out_values, out_size, c_data(sizes), c_data(displs),
root, is_mpi_datatype<T>());
}
template<typename T>
void
scatterv(const communicator& comm, const std::vector<T>& in_values,
const std::vector<int>& sizes, const std::vector<int>& displs,
T* out_values, int out_size, int root)
{
using detail::c_data;
::boost::mpi::scatterv(comm, c_data(in_values), sizes, displs,
out_values, out_size, root);
}
template<typename T>
void scatterv(const communicator& comm, T* out_values, int out_size, int root)
{
BOOST_ASSERT(comm.rank() != root);
detail::scatterv_impl(comm, out_values, out_size, root, is_mpi_datatype<T>());
}
///////////////////////
// common use versions
///////////////////////
template<typename T>
void
scatterv(const communicator& comm, const T* in_values,
const std::vector<int>& sizes, T* out_values, int root)
{
using detail::c_data;
detail::scatterv_impl(comm, in_values, out_values, sizes[comm.rank()],
c_data(sizes), (int const*)0,
root, is_mpi_datatype<T>());
}
template<typename T>
void
scatterv(const communicator& comm, const std::vector<T>& in_values,
const std::vector<int>& sizes, T* out_values, int root)
{
::boost::mpi::scatterv(comm, detail::c_data(in_values), sizes, out_values, root);
}
template<typename T>
void
scatterv(const communicator& comm, const T* in_values,
T* out_values, int n, int root)
{
detail::scatterv_impl(comm, in_values, out_values, n, (int const*)0, (int const*)0,
root, is_mpi_datatype<T>());
}
template<typename T>
void
scatterv(const communicator& comm, const std::vector<T>& in_values,
T* out_values, int out_size, int root)
{
::boost::mpi::scatterv(comm, detail::c_data(in_values), out_values, out_size, root);
}
} } // end namespace boost::mpi
#endif // BOOST_MPI_SCATTERV_HPP