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/detail/common_iarchive.hpp>
24 #include <boost/archive/basic_archive.hpp>
25 #include <boost/mpi/detail/packed_iprimitive.hpp>
26 #include <boost/mpi/detail/binary_buffer_iprimitive.hpp>
27 #include <boost/serialization/string.hpp>
28 #include <boost/serialization/collection_size_type.hpp>
29 #include <boost/serialization/item_version_type.hpp>
30 #include <boost/assert.hpp>
32 namespace boost { namespace mpi {
34 #ifdef BOOST_MPI_HOMOGENEOUS
35 typedef binary_buffer_iprimitive iprimitive;
37 typedef packed_iprimitive iprimitive;
41 /** @brief An archive that unpacks binary data from an MPI buffer.
43 * The @c packed_oarchive class is an Archiver (as in the
44 * Boost.Serialization library) that unpacks binary data from a
45 * buffer received via MPI. It can operate on any Serializable data
46 * type and will use the @c MPI_Unpack function of the underlying MPI
47 * implementation to perform deserialization.
50 class BOOST_MPI_DECL packed_iarchive
52 , public archive::detail::common_iarchive<packed_iarchive>
56 * Construct a @c packed_iarchive to receive data over the given
57 * MPI communicator and with an initial buffer.
59 * @param comm The communicator over which this archive will be
62 * @param b A user-defined buffer that contains the binary
63 * representation of serialized objects.
65 * @param flags Control the serialization of the data types. Refer
66 * to the Boost.Serialization documentation before changing the
70 packed_iarchive(MPI_Comm const & comm, buffer_type & b, unsigned int flags = boost::archive::no_header, int position = 0)
71 : iprimitive(b,comm,position),
72 archive::detail::common_iarchive<packed_iarchive>(flags)
76 * Construct a @c packed_iarchive to receive data over the given
79 * @param comm The communicator over which this archive will be
82 * @param flags Control the serialization of the data types. Refer
83 * to the Boost.Serialization documentation before changing the
88 ( MPI_Comm const & comm , std::size_t s=0,
89 unsigned int flags = boost::archive::no_header)
90 : iprimitive(internal_buffer_,comm)
91 , archive::detail::common_iarchive<packed_iarchive>(flags)
95 // Load everything else in the usual way, forwarding on to the Base class
97 void load_override(T& x, mpl::false_)
99 archive::detail::common_iarchive<packed_iarchive>::load_override(x);
102 // Load it directly using the primnivites
104 void load_override(T& x, mpl::true_)
109 // Load all supported datatypes directly
111 void load_override(T& x)
113 typedef typename mpl::apply1<use_array_optimization
114 , BOOST_DEDUCED_TYPENAME remove_const<T>::type
115 >::type use_optimized;
116 load_override(x, use_optimized());
119 // input archives need to ignore the optional information
120 void load_override(archive::class_id_optional_type & /*t*/){}
122 void load_override(archive::class_id_type & t){
125 t = boost::archive::class_id_type(x);
128 void load_override(archive::version_type & t){
131 t = boost::archive::version_type(x);
134 void load_override(archive::class_id_reference_type & t){
135 load_override(static_cast<archive::class_id_type &>(t));
138 void load_override(archive::class_name_type & t)
141 cn.reserve(BOOST_SERIALIZATION_MAX_KEY_SIZE);
142 * this->This() >> cn;
143 std::memcpy(t, cn.data(), cn.size());
145 t.t[cn.size()] = '\0';
149 /// An internal buffer to be used when the user does not supply his
151 buffer_type internal_buffer_;
154 } } // end namespace boost::mpi
156 BOOST_SERIALIZATION_REGISTER_ARCHIVE(boost::mpi::packed_iarchive)
157 BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(boost::mpi::packed_iarchive)
159 #endif // BOOST_MPI_PACKED_IARCHIVE_HPP