1 // (C) Copyright 2005 Matthias Troyer
2 // (C) Copyright 2006 Douglas Gregor <doug.gregor -at- gmail.com>
4 // Use, modification and distribution is subject to the Boost Software
5 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // Authors: Matthias Troyer
11 /** @file packed_iarchive.hpp
13 * This header provides the facilities for packing Serializable data
14 * types into a buffer using @c MPI_Pack. The buffers can then be
15 * transmitted via MPI and then be unpacked either via the facilities
16 * in @c packed_oarchive.hpp or @c MPI_Unpack.
18 #ifndef BOOST_MPI_PACKED_IARCHIVE_HPP
19 #define BOOST_MPI_PACKED_IARCHIVE_HPP
21 #include <boost/mpi/datatype.hpp>
22 #include <boost/archive/detail/auto_link_archive.hpp>
23 #include <boost/archive/basic_binary_iarchive.hpp>
24 #include <boost/archive/shared_ptr_helper.hpp>
25 #include <boost/mpi/detail/packed_iprimitive.hpp>
26 #include <boost/mpi/detail/binary_buffer_iprimitive.hpp>
27 #include <boost/assert.hpp>
29 namespace boost
{ namespace mpi
{
31 #ifdef BOOST_MPI_HOMOGENEOUS
32 typedef binary_buffer_iprimitive iprimitive
;
34 typedef packed_iprimitive iprimitive
;
37 /** @brief An archive that packs binary data into an MPI buffer.
39 * The @c packed_iarchive class is an Archiver (as in the
40 * Boost.Serialization library) that packs binary data into a buffer
41 * for transmission via MPI. It can operate on any Serializable data
42 * type and will use the @c MPI_Pack function of the underlying MPI
43 * implementation to perform serialization.
45 class BOOST_MPI_DECL packed_iarchive
47 , public archive::basic_binary_iarchive
<packed_iarchive
>
48 , public archive::detail::shared_ptr_helper
52 * Construct a @c packed_iarchive for transmission over the given
53 * MPI communicator and with an initial buffer.
55 * @param comm The communicator over which this archive will be
58 * @param b A user-defined buffer that will be filled with the
59 * binary representation of serialized objects.
61 * @param flags Control the serialization of the data types. Refer
62 * to the Boost.Serialization documentation before changing the
65 * @param position Set the offset into buffer @p b at which
66 * deserialization will begin.
68 packed_iarchive(MPI_Comm
const & comm
, buffer_type
& b
, unsigned int flags
= boost::archive::no_header
, int position
= 0)
69 : iprimitive(b
,comm
,position
),
70 archive::basic_binary_iarchive
<packed_iarchive
>(flags
)
74 * Construct a @c packed_iarchive for transmission over the given
77 * @param comm The communicator over which this archive will be
80 * @param s The size of the buffer to be received.
82 * @param flags Control the serialization of the data types. Refer
83 * to the Boost.Serialization documentation before changing the
87 ( MPI_Comm
const & comm
, std::size_t s
=0,
88 unsigned int flags
= boost::archive::no_header
)
89 : iprimitive(internal_buffer_
,comm
)
90 , archive::basic_binary_iarchive
<packed_iarchive
>(flags
)
94 // Load everything else in the usual way, forwarding on to the Base class
96 void load_override(T
& x
, int version
, mpl::false_
)
98 archive::basic_binary_iarchive
<packed_iarchive
>::load_override(x
,version
);
101 // Load it directly using the primnivites
103 void load_override(T
& x
, int /*version*/, mpl::true_
)
108 // Load all supported datatypes directly
110 void load_override(T
& x
, int version
)
112 typedef typename
mpl::apply1
<use_array_optimization
113 , BOOST_DEDUCED_TYPENAME remove_const
<T
>::type
114 >::type use_optimized
;
115 load_override(x
, version
, use_optimized());
119 /// An internal buffer to be used when the user does not supply his
121 buffer_type internal_buffer_
;
124 } } // end namespace boost::mpi
126 BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(boost::mpi::packed_iarchive
)
127 BOOST_SERIALIZATION_REGISTER_ARCHIVE(boost::mpi::packed_iarchive
)
128 BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(boost::mpi::packed_iarchive
)
130 #endif // BOOST_MPI_PACKED_IARCHIVE_HPP