Update of /cvsroot/boost/boost/boost/parallel/mpi/collectives
In directory
sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv657/boost/parallel/mpi/collectives
Added Files:
all_gather.hpp all_reduce.hpp all_to_all.hpp broadcast.hpp
gather.hpp reduce.hpp scan.hpp scatter.hpp
Log Message:
Import Boost.MPI with the beginnings of a BBv2-based build system
--- NEW FILE: all_gather.hpp ---
// 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.7. Gather-to-all
#ifndef BOOST_PARALLEL_MPI_ALL_GATHER_HPP
#define BOOST_PARALLEL_MPI_ALL_GATHER_HPP
#include <boost/parallel/mpi/exception.hpp>
#include <boost/parallel/mpi/datatype.hpp>
#include <vector>
#include <boost/serialization/vector.hpp>
// all_gather falls back to gather+broadcast in some cases
#include <boost/parallel/mpi/collectives/broadcast.hpp>
#include <boost/parallel/mpi/collectives/gather.hpp>
namespace boost { namespace parallel { 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& value,
std::vector<T>& values, mpl::true_)
{
values.resize(comm.size());
MPI_Datatype type = boost::parallel::mpi::get_mpi_datatype<T>();
BOOST_MPI_CHECK_RESULT(MPI_Allgather,
(const_cast<T*>(&value), 1, type,
&values[0], 1, type, comm));
}
// We're all-gathering for a type that has no associated MPI. So,
// we'll do a manual gather followed by a broadcast.
template<typename T>
void
all_gather_impl(const communicator& comm, const T& value,
std::vector<T>& values, mpl::false_)
{
gather(comm, value, values, 0);
broadcast(comm, values, 0);
}
} // end namespace detail
template<typename T>
void
all_gather(const communicator& comm, const T& value, std::vector<T>& values)
{
detail::all_gather_impl(comm, value, values, is_mpi_datatype<T>());
}
} } } // end namespace boost::parallel::mpi
#endif // BOOST_PARALLEL_MPI_ALL_GATHER_HPP
--- NEW FILE: all_reduce.hpp ---
// 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_PARALLEL_MPI_ALL_REDUCE_HPP
#define BOOST_PARALLEL_MPI_ALL_REDUCE_HPP
// All-reduce falls back to reduce() + broadcast() in some cases.
#include <boost/parallel/mpi/collectives/broadcast.hpp>
#include <boost/parallel/mpi/collectives/reduce.hpp>
namespace boost { namespace parallel { 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_value, T& out_value,
Op op, mpl::true_ /*is_mpi_op*/,
mpl::true_ /*is_mpi_datatype*/)
{
BOOST_MPI_CHECK_RESULT(MPI_Allreduce,
(const_cast<T*>(&in_value), &out_value, 1,
boost::parallel::mpi::get_mpi_datatype<T>(),
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_value, T& out_value,
Op op, mpl::false_ /*is_mpi_op*/,
mpl::true_ /*is_mpi_datatype*/)
{
user_op<Op, T> mpi_op(op);
BOOST_MPI_CHECK_RESULT(MPI_Allreduce,
(const_cast<T*>(&in_value), &out_value, 1,
boost::parallel::mpi::get_mpi_datatype<T>(),
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_value, T& out_value,
Op op, mpl::false_ /*is_mpi_op*/,
mpl::false_ /*is_mpi_datatype*/)
{
reduce(comm, in_value, out_value, op, 0);
broadcast(comm, out_value, 0);
}
} // end namespace detail
template<typename T, typename Op>
void
all_reduce(const communicator& comm, const T& in_value, T& out_value, Op op)
{
detail::all_reduce_impl(comm, in_value, 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;
all_reduce(comm, in_value, result, op);
return result;
}
} } } // end namespace boost::parallel::mpi
#endif // BOOST_PARALLEL_MPI_ALL_REDUCE_HPP
--- NEW FILE: all_to_all.hpp ---
// 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_PARALLEL_MPI_ALL_TO_ALL_HPP
#define BOOST_PARALLEL_MPI_ALL_TO_ALL_HPP
#include <boost/parallel/mpi/exception.hpp>
#include <boost/parallel/mpi/datatype.hpp>
#include <vector>
#include <boost/parallel/mpi/packed_oarchive.hpp>
#include <boost/parallel/mpi/packed_iarchive.hpp>
#include <boost/parallel/mpi/communicator.hpp>
#include <boost/parallel/mpi/environment.hpp>
#include <boost/assert.hpp>
#include <boost/parallel/mpi/collectives_fwd.hpp>
#include <boost/parallel/mpi/allocator.hpp>
namespace boost { namespace parallel { namespace mpi {
template<typename T>
void
all_to_all(const communicator& comm, const std::vector<T>& in_values,
std::vector<T>& out_values);
namespace detail {
// We're performaing 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 std::vector<T>& in_values,
std::vector<T>& out_values, mpl::true_)
{
MPI_Datatype type = get_mpi_datatype<T>();
out_values.resize(comm.size());
BOOST_MPI_CHECK_RESULT(MPI_Alltoall,
(const_cast<T*>(&in_values.front()), 1, type,
&out_values.front(), 1, 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. Unfortunately, this means that we cannot use MPI_Alltoall, so
// we'll just have to send individual messages to the other
// processes.
template<typename T>
void
all_to_all_impl(const communicator& comm, const std::vector<T>& in_values,
std::vector<T>& out_values, mpl::false_)
{
int size = comm.size();
int rank = comm.rank();
out_values.resize(comm.size());
// 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);
oa << in_values[dest];
}
// 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);
// Transmit the actual data
BOOST_MPI_CHECK_RESULT(MPI_Alltoallv,
(&outgoing[0], &send_sizes[0],
&send_disps[0], MPI_PACKED,
&incoming[0], &recv_sizes[0],
&recv_disps[0], MPI_PACKED,
comm));
// Deserialize data from the iarchive
for (int src = 0; src < size; ++src) {
if (src == rank) out_values[src] = in_values[src];
else {
packed_iarchive ia(comm, incoming, boost::archive::no_header,
recv_disps[src]);
ia >> out_values[src];
}
}
}
} // end namespace detail
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());
detail::all_to_all_impl(comm, in_values, out_values, is_mpi_datatype<T>());
}
} } } // end namespace boost::parallel::mpi
#endif // BOOST_PARALLEL_MPI_ALL_TO_ALL_HPP
--- NEW FILE: broadcast.hpp ---
// 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_PARALLEL_MPI_BROADCAST_HPP
#define BOOST_PARALLEL_MPI_BROADCAST_HPP
#include <boost/parallel/mpi/collectives_fwd.hpp>
#include <boost/parallel/mpi/exception.hpp>
#include <boost/parallel/mpi/datatype.hpp>
#include <boost/parallel/mpi/communicator.hpp>
namespace boost { namespace parallel { namespace mpi {
/************************************************************************
* Specializations *
************************************************************************/
/**
* INTERNAL ONLY
*/
template<>
void
broadcast<const packed_oarchive>(const communicator& comm,
const packed_oarchive& oa,
int root);
/**
* INTERNAL ONLY
*/
template<>
void
broadcast<packed_oarchive>(const communicator& comm, packed_oarchive& oa,
int root);
/**
* INTERNAL ONLY
*/
template<>
void
broadcast<packed_iarchive>(const communicator& comm, packed_iarchive& ia,
int root);
/**
* INTERNAL ONLY
*/
template<>
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& value, int root, mpl::true_)
{
BOOST_MPI_CHECK_RESULT(MPI_Bcast,
(&value, 1,
boost::parallel::mpi::get_mpi_datatype<T>(),
root, MPI_Comm(comm)));
}
// We're sending 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_Bcast, so we'll just send from the
// root to everyone else.
template<typename T>
void
broadcast_impl(const communicator& comm, T& value, int root, mpl::false_)
{
if (comm.rank() == root) {
packed_oarchive oa(comm);
oa << value;
broadcast(comm, oa, root);
} else {
packed_iarchive ia(comm);
broadcast(comm, ia, root);
ia >> value;
}
}
} // end namespace detail
template<typename T>
void broadcast(const communicator& comm, T& value, int root = 0)
{
detail::broadcast_impl(comm, value, root, is_mpi_datatype<T>());
}
} } } // end namespace boost::parallel::mpi
// If the user has already included skeleton_and_content.hpp, include
// the code to broadcast skeletons and content.
#ifdef BOOST_PARALLEL_MPI_SKELETON_AND_CONTENT_HPP
# include <boost/parallel/mpi/detail/broadcast_sc.hpp>
#endif
#endif // BOOST_PARALLEL_MPI_BROADCAST_HPP
--- NEW FILE: gather.hpp ---
// 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_PARALLEL_MPI_GATHER_HPP
#define BOOST_PARALLEL_MPI_GATHER_HPP
#include <boost/parallel/mpi/exception.hpp>
#include <boost/parallel/mpi/datatype.hpp>
#include <vector>
#include <boost/parallel/mpi/packed_oarchive.hpp>
#include <boost/parallel/mpi/packed_iarchive.hpp>
#include <boost/parallel/mpi/detail/point_to_point.hpp>
#include <boost/parallel/mpi/communicator.hpp>
#include <boost/parallel/mpi/environment.hpp>
#include <boost/assert.hpp>
namespace boost { namespace parallel { 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& value, std::vector<T>& values,
int root, mpl::true_)
{
values.resize(comm.size());
MPI_Datatype type = get_mpi_datatype<T>();
BOOST_MPI_CHECK_RESULT(MPI_Gather,
(const_cast<T*>(&value), 1, type,
&values[0], 1, 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& value, int root, mpl::true_)
{
MPI_Datatype type = get_mpi_datatype<T>();
BOOST_MPI_CHECK_RESULT(MPI_Gather,
(const_cast<T*>(&value), 1, type,
0, 1, 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_Gather, so
// we'll just have all of the non-root nodes send individual
// messages to the root.
template<typename T>
void
gather_impl(const communicator& comm, const T& value, std::vector<T>& values,
int root, mpl::false_)
{
int tag = environment::collectives_tag();
int size = comm.size();
// We know we'll get "size" values
values.clear();
values.reserve(size);
MPI_Status status;
for (int src = 0; src < size; ++src) {
if (src == root) {
// Our own value will never be transmitted: just copy it.
values.push_back(value);
} else {
// Receive archive
packed_iarchive ia(comm);
detail::packed_archive_recv(comm, src, tag, ia, status);
// Unpack the data
T value_from_src;
ia >> value_from_src;
// Save the result
values.push_back(value_from_src);
}
}
}
// 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. Unfortunately, this means that we cannot use MPI_Gather, so
// we'll just have all of the non-root nodes send individual
// messages to the root.
template<typename T>
void
gather_impl(const communicator& comm, const T& value, int root, mpl::false_)
{
int tag = environment::collectives_tag();
packed_oarchive oa(comm);
oa << value;
detail::packed_archive_send(comm, root, tag, oa);
}
} // end namespace detail
template<typename T>
void
gather(const communicator& comm, const T& value, std::vector<T>& values,
int root)
{
if (comm.rank() == root)
detail::gather_impl(comm, value, values, root, is_mpi_datatype<T>());
else
detail::gather_impl(comm, value, root, is_mpi_datatype<T>());
}
template<typename T>
void gather(const communicator& comm, const T& value, int root)
{
BOOST_ASSERT(comm.rank() != root);
detail::gather_impl(comm, value, root, is_mpi_datatype<T>());
}
} } } // end namespace boost::parallel::mpi
#endif // BOOST_PARALLEL_MPI_GATHER_HPP
--- NEW FILE: reduce.hpp ---
// Copyright (C) 2005-2006 Douglas Gregor <[EMAIL PROTECTED]>.
// 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_PARALLEL_MPI_REDUCE_HPP
#define BOOST_PARALLEL_MPI_REDUCE_HPP
#include <boost/parallel/mpi/exception.hpp>
#include <boost/parallel/mpi/datatype.hpp>
// For (de-)serializing sends and receives
#include <boost/parallel/mpi/packed_oarchive.hpp>
#include <boost/parallel/mpi/packed_iarchive.hpp>
// For packed_[io]archive sends and receives
#include <boost/parallel/mpi/detail/point_to_point.hpp>
#include <boost/parallel/mpi/communicator.hpp>
#include <boost/parallel/mpi/environment.hpp>
#include <boost/parallel/mpi/detail/computation_tree.hpp>
#include <boost/parallel/mpi/operations.hpp>
#include <algorithm>
#include <exception>
#include <boost/assert.hpp>
namespace boost { namespace parallel { 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_value, T& out_value, Op op,
int root, mpl::true_ /*is_mpi_op*/,
mpl::true_/*is_mpi_datatype*/)
{
BOOST_MPI_CHECK_RESULT(MPI_Reduce,
(const_cast<T*>(&in_value), &out_value, 1,
boost::parallel::mpi::get_mpi_datatype<T>(),
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_value, Op op, int root,
mpl::true_ /*is_mpi_op*/, mpl::true_/*is_mpi_datatype*/)
{
BOOST_MPI_CHECK_RESULT(MPI_Reduce,
(const_cast<T*>(&in_value), 0, 1,
boost::parallel::mpi::get_mpi_datatype<T>(),
is_mpi_op<Op, T>::op(), root, comm));
}
// 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_value, T& out_value,
Op op, int root, mpl::false_ /*is_mpi_op*/,
mpl::true_/*is_mpi_datatype*/)
{
user_op<Op, T> mpi_op(op);
BOOST_MPI_CHECK_RESULT(MPI_Reduce,
(const_cast<T*>(&in_value), &out_value, 1,
boost::parallel::mpi::get_mpi_datatype<T>(),
mpi_op.get_mpi_op(), root, comm));
}
/**********************************************************************
* User-defined reduction with MPI_Reduce *
**********************************************************************/
// 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_value, Op op, int root,
mpl::false_ /*is_mpi_op*/, mpl::true_/*is_mpi_datatype*/)
{
user_op<Op, T> mpi_op(op);
BOOST_MPI_CHECK_RESULT(MPI_Reduce,
(const_cast<T*>(&in_value), 0, 1,
boost::parallel::mpi::get_mpi_datatype<T>(),
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_value, T& out_value,
Op op, int root, mpl::true_ /*is_commutative*/)
{
out_value = in_value;
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;
ia >> incoming;
out_value = op(out_value, incoming);
}
// For non-roots, send the result to the parent.
if (tree.parent() != rank) {
packed_oarchive oa(comm);
oa << out_value;
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_value, Op op,
int root, mpl::true_ /*is_commutative*/)
{
T result;
detail::tree_reduce_impl(comm, in_value, result, op, root, mpl::true_());
}
// Non-commutative reduction
template<typename T, typename Op>
void
tree_reduce_impl(const communicator& comm, const T& in_value, T& out_value,
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;
ia >> incoming;
out_value = op(incoming, in_value);
} else {
// There was no left value, so copy our incoming value.
out_value = in_value;
}
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;
ia >> incoming;
out_value = op(out_value, incoming);
}
}
// Non-commutative reduction from a non-root.
template<typename T, typename Op>
void
tree_reduce_impl(const communicator& comm, const T& in_value, 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;
T out_value;
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;
ia >> incoming;
out_value = op(incoming, in_value);
} else {
// There was no left value, so copy our incoming value.
out_value = in_value;
}
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;
ia >> incoming;
out_value = op(out_value, incoming);
}
// Send the combined value to our parent.
packed_oarchive oa(comm);
oa << out_value;
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_value, T& out_value, Op op,
int root, mpl::false_ /*is_mpi_op*/,
mpl::false_/*is_mpi_datatype*/)
{
detail::tree_reduce_impl(comm, in_value, out_value, 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_value, Op op, int root,
mpl::false_ /*is_mpi_op*/, mpl::false_/*is_mpi_datatype*/)
{
detail::tree_reduce_impl(comm, in_value, op, root,
is_commutative<Op, T>());
}
} // end namespace detail
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, out_value, op, root,
is_mpi_op<Op, T>(), is_mpi_datatype<T>());
else
detail::reduce_impl(comm, in_value, 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, op, root,
is_mpi_op<Op, T>(), is_mpi_datatype<T>());
}
} } } // end namespace boost::parallel::mpi
#endif // BOOST_PARALLEL_MPI_REDUCE_HPP
--- NEW FILE: scan.hpp ---
// Copyright (C) 2005-2006 Douglas Gregor <[EMAIL PROTECTED]>.
// 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_PARALLEL_MPI_SCAN_HPP
#define BOOST_PARALLEL_MPI_SCAN_HPP
#include <boost/parallel/mpi/exception.hpp>
#include <boost/parallel/mpi/datatype.hpp>
// For (de-)serializing sends and receives
#include <boost/parallel/mpi/packed_oarchive.hpp>
#include <boost/parallel/mpi/packed_iarchive.hpp>
// For packed_[io]archive sends and receives
#include <boost/parallel/mpi/detail/point_to_point.hpp>
#include <boost/parallel/mpi/communicator.hpp>
#include <boost/parallel/mpi/environment.hpp>
#include <boost/parallel/mpi/detail/computation_tree.hpp>
#include <boost/parallel/mpi/operations.hpp>
#include <algorithm>
#include <exception>
#include <boost/assert.hpp>
namespace boost { namespace parallel { 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_value, T& out_value, Op op,
mpl::true_ /*is_mpi_op*/, mpl::true_ /*is_mpi_datatype*/)
{
BOOST_MPI_CHECK_RESULT(MPI_Scan,
(const_cast<T*>(&in_value), &out_value, 1,
boost::parallel::mpi::get_mpi_datatype<T>(),
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_value, T& out_value,
Op op, mpl::false_ /*is_mpi_op*/, mpl::true_ /*is_mpi_datatype*/)
{
user_op<Op, T> mpi_op(op);
BOOST_MPI_CHECK_RESULT(MPI_Scan,
(const_cast<T*>(&in_value), &out_value, 1,
boost::parallel::mpi::get_mpi_datatype<T>(),
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_value,
T& out_value, Op& op, int lower, int upper)
{
int tag = environment::collectives_tag();
int rank = comm.rank();
if (lower + 1 == upper) {
out_value = in_value;
} else {
int middle = (lower + upper) / 2;
if (rank < middle) {
// Lower half
upper_lower_scan(comm, in_value, out_value, op, lower, middle);
// If we're the last process in the lower half, send our value
// to everyone in the upper half.
if (rank == middle - 1) {
for (int p = middle; p < upper; ++p)
comm.send(p, tag, out_value);
}
} else {
// Upper half
upper_lower_scan(comm, in_value, out_value, op, middle, upper);
// Receive value from the last process in the lower half.
T left_value;
comm.recv(middle - 1, tag, left_value);
// Combine value that came from the left with our value
out_value = op(left_value, out_value);
}
}
}
// 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_value, T& out_value, Op op,
mpl::false_ /*is_mpi_op*/, mpl::false_/*is_mpi_datatype*/)
{
upper_lower_scan(comm, in_value, out_value, 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, out_value, 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, out_value, op,
is_mpi_op<Op, T>(), is_mpi_datatype<T>());
return out_value;
}
} } } // end namespace boost::parallel::mpi
#endif // BOOST_PARALLEL_MPI_SCAN_HPP
--- NEW FILE: scatter.hpp ---
// 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_PARALLEL_MPI_SCATTER_HPP
#define BOOST_PARALLEL_MPI_SCATTER_HPP
#include <boost/parallel/mpi/exception.hpp>
#include <boost/parallel/mpi/datatype.hpp>
#include <vector>
#include <boost/parallel/mpi/packed_oarchive.hpp>
#include <boost/parallel/mpi/packed_iarchive.hpp>
#include <boost/parallel/mpi/detail/point_to_point.hpp>
#include <boost/parallel/mpi/communicator.hpp>
#include <boost/parallel/mpi/environment.hpp>
#include <boost/assert.hpp>
namespace boost { namespace parallel { 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 std::vector<T>& values,
T& value, int root, mpl::true_)
{
MPI_Datatype type = get_mpi_datatype<T>();
BOOST_MPI_CHECK_RESULT(MPI_Scatter,
(const_cast<T*>(&values.front()), 1, type,
&value, 1, 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& value, int root, mpl::true_)
{
MPI_Datatype type = get_mpi_datatype<T>();
BOOST_MPI_CHECK_RESULT(MPI_Scatter,
(0, 1, type,
&value, 1, type,
root, comm));
}
// We're scattering from 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_Scatter, so
// we'll just have the root send individual messages to the other
// processes.
template<typename T>
void
scatter_impl(const communicator& comm, const std::vector<T>& values,
T& value, int root, mpl::false_)
{
int tag = environment::collectives_tag();
int size = comm.size();
for (int dest = 0; dest < size; ++dest) {
if (dest == root) {
// Our own value will never be transmitted: just copy it.
value = values[dest];
} else {
// Send archive
packed_oarchive oa(comm);
oa << values[dest];
detail::packed_archive_send(comm, dest, tag, oa);
}
}
}
// We're scattering to a non-root for a type that does not have an
// associated MPI datatype, so we'll need to de-serialize
// it. Unfortunately, this means that we cannot use MPI_Scatter, so
// we'll just have all of the non-root nodes send individual
// messages to the root.
template<typename T>
void
scatter_impl(const communicator& comm, T& value, int root, mpl::false_)
{
int tag = environment::collectives_tag();
packed_iarchive ia(comm);
MPI_Status status;
detail::packed_archive_recv(comm, root, tag, ia, status);
ia >> value;
}
} // end namespace detail
template<typename T>
void
scatter(const communicator& comm, const std::vector<T>& values, T& value,
int root)
{
if (comm.rank() == root)
detail::scatter_impl(comm, values, value, root, is_mpi_datatype<T>());
else
detail::scatter_impl(comm, value, root, is_mpi_datatype<T>());
}
template<typename T>
void scatter(const communicator& comm, T& value, int root)
{
BOOST_ASSERT(comm.rank() != root);
detail::scatter_impl(comm, value, root, is_mpi_datatype<T>());
}
} } } // end namespace boost::parallel::mpi
#endif // BOOST_PARALLEL_MPI_SCATTER_HPP
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Boost-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/boost-cvs