fix doc example typo
[boost.git] / boost / mpi / collectives / all_gather.hpp
blobda73186c640e08d2c1fd83f1066d3d1a05a69309
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.7. Gather-to-all
8 #ifndef BOOST_MPI_ALL_GATHER_HPP
9 #define BOOST_MPI_ALL_GATHER_HPP
11 #include <boost/mpi/exception.hpp>
12 #include <boost/mpi/datatype.hpp>
13 #include <vector>
14 #include <boost/serialization/vector.hpp>
16 // all_gather falls back to gather+broadcast in some cases
17 #include <boost/mpi/collectives/broadcast.hpp>
18 #include <boost/mpi/collectives/gather.hpp>
20 namespace boost { namespace mpi {
22 namespace detail {
23 // We're all-gathering for a type that has an associated MPI
24 // datatype, so we'll use MPI_Gather to do all of the work.
25 template<typename T>
26 void
27 all_gather_impl(const communicator& comm, const T* in_values, int n,
28 T* out_values, mpl::true_)
30 MPI_Datatype type = boost::mpi::get_mpi_datatype<T>(*in_values);
31 BOOST_MPI_CHECK_RESULT(MPI_Allgather,
32 (const_cast<T*>(in_values), n, type,
33 out_values, n, type, comm));
36 // We're all-gathering for a type that has no associated MPI
37 // type. So, we'll do a manual gather followed by a broadcast.
38 template<typename T>
39 void
40 all_gather_impl(const communicator& comm, const T* in_values, int n,
41 T* out_values, mpl::false_)
43 gather(comm, in_values, n, out_values, 0);
44 broadcast(comm, out_values, comm.size() * n, 0);
46 } // end namespace detail
48 template<typename T>
49 inline void
50 all_gather(const communicator& comm, const T& in_value, T* out_values)
52 detail::all_gather_impl(comm, &in_value, 1, out_values, is_mpi_datatype<T>());
55 template<typename T>
56 void
57 all_gather(const communicator& comm, const T& in_value,
58 std::vector<T>& out_values)
60 out_values.resize(comm.size());
61 ::boost::mpi::all_gather(comm, &in_value, 1, &out_values[0]);
64 template<typename T>
65 inline void
66 all_gather(const communicator& comm, const T* in_values, int n, T* out_values)
68 detail::all_gather_impl(comm, in_values, n, out_values, is_mpi_datatype<T>());
71 template<typename T>
72 void
73 all_gather(const communicator& comm, const T* in_values, int n,
74 std::vector<T>& out_values)
76 out_values.resize(comm.size() * n);
77 ::boost::mpi::all_gather(comm, in_values, n, &out_values[0]);
80 } } // end namespace boost::mpi
82 #endif // BOOST_MPI_ALL_GATHER_HPP