1 // Copyright (C) 2005, 2006 Douglas Gregor <doug.gregor -at- gmail.com>.
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // Message Passing Interface 1.1 -- Section 4.4. Broadcast
8 #ifndef BOOST_MPI_BROADCAST_HPP
9 #define BOOST_MPI_BROADCAST_HPP
11 #include <boost/mpi/collectives_fwd.hpp>
12 #include <boost/mpi/exception.hpp>
13 #include <boost/mpi/datatype.hpp>
14 #include <boost/mpi/communicator.hpp>
16 namespace boost { namespace mpi {
18 /************************************************************************
20 ************************************************************************/
27 broadcast<const packed_oarchive>(const communicator& comm,
28 const packed_oarchive& oa,
36 broadcast<packed_oarchive>(const communicator& comm, packed_oarchive& oa,
44 broadcast<packed_iarchive>(const communicator& comm, packed_iarchive& ia,
52 broadcast<const packed_skeleton_oarchive>(const communicator& comm,
53 const packed_skeleton_oarchive& oa,
61 broadcast<packed_skeleton_oarchive>(const communicator& comm,
62 packed_skeleton_oarchive& oa, int root);
69 broadcast<packed_skeleton_iarchive>(const communicator& comm,
70 packed_skeleton_iarchive& ia, int root);
76 void broadcast<content>(const communicator& comm, content& c, int root);
82 void broadcast<const content>(const communicator& comm, const content& c,
85 /************************************************************************
86 * broadcast() implementation *
87 ************************************************************************/
89 // We're sending a type that has an associated MPI datatype, so
90 // we'll use MPI_Bcast to do all of the work.
93 broadcast_impl(const communicator& comm, T* values, int n, int root,
96 BOOST_MPI_CHECK_RESULT(MPI_Bcast,
98 boost::mpi::get_mpi_datatype<T>(*values),
99 root, MPI_Comm(comm)));
102 // We're sending a type that does not have an associated MPI
103 // datatype, so we'll need to serialize it.
106 broadcast_impl(const communicator& comm, T* values, int n, int root,
107 mpl::false_ non_mpi_datatype)
109 // Implementation proposed by Lorenz Hübschle-Schneider
110 if (comm.rank() == root) {
111 packed_oarchive oa(comm);
112 for (int i = 0; i < n; ++i) {
115 std::size_t asize = oa.size();
116 broadcast(comm, asize, root);
117 void const* aptr = oa.address();
118 BOOST_MPI_CHECK_RESULT(MPI_Bcast,
119 (const_cast<void*>(aptr), asize,
121 root, MPI_Comm(comm)));
123 packed_iarchive ia(comm);
125 broadcast(comm, asize, root);
127 void* aptr = ia.address();
128 BOOST_MPI_CHECK_RESULT(MPI_Bcast,
131 root, MPI_Comm(comm)));
132 for (int i = 0; i < n; ++i)
136 } // end namespace detail
139 void broadcast(const communicator& comm, T& value, int root)
141 detail::broadcast_impl(comm, &value, 1, root, is_mpi_datatype<T>());
145 void broadcast(const communicator& comm, T* values, int n, int root)
147 detail::broadcast_impl(comm, values, n, root, is_mpi_datatype<T>());
150 } } // end namespace boost::mpi
152 // If the user has already included skeleton_and_content.hpp, include
153 // the code to broadcast skeletons and content.
154 #ifdef BOOST_MPI_SKELETON_AND_CONTENT_HPP
155 # include <boost/mpi/detail/broadcast_sc.hpp>
158 #endif // BOOST_MPI_BROADCAST_HPP