Chris@16
|
1 // (C) Copyright 2005 Matthias Troyer
|
Chris@16
|
2 // (C) Copyright 2006 Douglas Gregor <doug.gregor -at- gmail.com>
|
Chris@16
|
3
|
Chris@16
|
4 // Use, modification and distribution is subject to the Boost Software
|
Chris@16
|
5 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7
|
Chris@16
|
8 // Authors: Matthias Troyer
|
Chris@16
|
9 // Douglas Gregor
|
Chris@16
|
10
|
Chris@16
|
11 /** @file packed_iarchive.hpp
|
Chris@16
|
12 *
|
Chris@16
|
13 * This header provides the facilities for packing Serializable data
|
Chris@16
|
14 * types into a buffer using @c MPI_Pack. The buffers can then be
|
Chris@16
|
15 * transmitted via MPI and then be unpacked either via the facilities
|
Chris@16
|
16 * in @c packed_oarchive.hpp or @c MPI_Unpack.
|
Chris@16
|
17 */
|
Chris@16
|
18 #ifndef BOOST_MPI_PACKED_IARCHIVE_HPP
|
Chris@16
|
19 #define BOOST_MPI_PACKED_IARCHIVE_HPP
|
Chris@16
|
20
|
Chris@16
|
21 #include <boost/mpi/datatype.hpp>
|
Chris@16
|
22 #include <boost/archive/detail/auto_link_archive.hpp>
|
Chris@16
|
23 #include <boost/archive/detail/common_iarchive.hpp>
|
Chris@16
|
24 #include <boost/archive/basic_archive.hpp>
|
Chris@16
|
25 #include <boost/mpi/detail/packed_iprimitive.hpp>
|
Chris@16
|
26 #include <boost/mpi/detail/binary_buffer_iprimitive.hpp>
|
Chris@16
|
27 #include <boost/serialization/string.hpp>
|
Chris@16
|
28 #include <boost/serialization/collection_size_type.hpp>
|
Chris@16
|
29 #include <boost/serialization/item_version_type.hpp>
|
Chris@16
|
30 #include <boost/assert.hpp>
|
Chris@16
|
31
|
Chris@16
|
32 namespace boost { namespace mpi {
|
Chris@16
|
33
|
Chris@16
|
34 #ifdef BOOST_MPI_HOMOGENEOUS
|
Chris@16
|
35 typedef binary_buffer_iprimitive iprimitive;
|
Chris@16
|
36 #else
|
Chris@16
|
37 typedef packed_iprimitive iprimitive;
|
Chris@16
|
38 #endif
|
Chris@16
|
39
|
Chris@16
|
40
|
Chris@16
|
41 /** @brief An archive that unpacks binary data from an MPI buffer.
|
Chris@16
|
42 *
|
Chris@16
|
43 * The @c packed_oarchive class is an Archiver (as in the
|
Chris@16
|
44 * Boost.Serialization library) that unpacks binary data from a
|
Chris@16
|
45 * buffer received via MPI. It can operate on any Serializable data
|
Chris@16
|
46 * type and will use the @c MPI_Unpack function of the underlying MPI
|
Chris@16
|
47 * implementation to perform deserialization.
|
Chris@16
|
48 */
|
Chris@16
|
49
|
Chris@16
|
50 class BOOST_MPI_DECL packed_iarchive
|
Chris@16
|
51 : public iprimitive
|
Chris@16
|
52 , public archive::detail::common_iarchive<packed_iarchive>
|
Chris@16
|
53 {
|
Chris@16
|
54 public:
|
Chris@16
|
55 /**
|
Chris@16
|
56 * Construct a @c packed_iarchive to receive data over the given
|
Chris@16
|
57 * MPI communicator and with an initial buffer.
|
Chris@16
|
58 *
|
Chris@16
|
59 * @param comm The communicator over which this archive will be
|
Chris@16
|
60 * received.
|
Chris@16
|
61 *
|
Chris@16
|
62 * @param b A user-defined buffer that contains the binary
|
Chris@16
|
63 * representation of serialized objects.
|
Chris@16
|
64 *
|
Chris@16
|
65 * @param flags Control the serialization of the data types. Refer
|
Chris@16
|
66 * to the Boost.Serialization documentation before changing the
|
Chris@16
|
67 * default flags.
|
Chris@16
|
68 */
|
Chris@16
|
69
|
Chris@16
|
70 packed_iarchive(MPI_Comm const & comm, buffer_type & b, unsigned int flags = boost::archive::no_header, int position = 0)
|
Chris@16
|
71 : iprimitive(b,comm,position),
|
Chris@16
|
72 archive::detail::common_iarchive<packed_iarchive>(flags)
|
Chris@16
|
73 {}
|
Chris@16
|
74
|
Chris@16
|
75 /**
|
Chris@16
|
76 * Construct a @c packed_iarchive to receive data over the given
|
Chris@16
|
77 * MPI communicator.
|
Chris@16
|
78 *
|
Chris@16
|
79 * @param comm The communicator over which this archive will be
|
Chris@16
|
80 * received.
|
Chris@16
|
81 *
|
Chris@16
|
82 * @param flags Control the serialization of the data types. Refer
|
Chris@16
|
83 * to the Boost.Serialization documentation before changing the
|
Chris@16
|
84 * default flags.
|
Chris@16
|
85 */
|
Chris@16
|
86
|
Chris@16
|
87 packed_iarchive
|
Chris@101
|
88 ( MPI_Comm const & comm , std::size_t s=0,
|
Chris@16
|
89 unsigned int flags = boost::archive::no_header)
|
Chris@16
|
90 : iprimitive(internal_buffer_,comm)
|
Chris@16
|
91 , archive::detail::common_iarchive<packed_iarchive>(flags)
|
Chris@16
|
92 , internal_buffer_(s)
|
Chris@16
|
93 {}
|
Chris@16
|
94
|
Chris@16
|
95 // Load everything else in the usual way, forwarding on to the Base class
|
Chris@16
|
96 template<class T>
|
Chris@16
|
97 void load_override(T& x, int version, mpl::false_)
|
Chris@16
|
98 {
|
Chris@16
|
99 archive::detail::common_iarchive<packed_iarchive>::load_override(x,version);
|
Chris@16
|
100 }
|
Chris@16
|
101
|
Chris@16
|
102 // Load it directly using the primnivites
|
Chris@16
|
103 template<class T>
|
Chris@16
|
104 void load_override(T& x, int /*version*/, mpl::true_)
|
Chris@16
|
105 {
|
Chris@16
|
106 iprimitive::load(x);
|
Chris@16
|
107 }
|
Chris@16
|
108
|
Chris@16
|
109 // Load all supported datatypes directly
|
Chris@16
|
110 template<class T>
|
Chris@16
|
111 void load_override(T& x, int version)
|
Chris@16
|
112 {
|
Chris@16
|
113 typedef typename mpl::apply1<use_array_optimization
|
Chris@16
|
114 , BOOST_DEDUCED_TYPENAME remove_const<T>::type
|
Chris@16
|
115 >::type use_optimized;
|
Chris@16
|
116 load_override(x, version, use_optimized());
|
Chris@16
|
117 }
|
Chris@16
|
118
|
Chris@101
|
119 // input archives need to ignore the optional information
|
Chris@16
|
120 void load_override(archive::class_id_optional_type & /*t*/, int){}
|
Chris@16
|
121
|
Chris@16
|
122 void load_override(archive::class_id_type & t, int version){
|
Chris@16
|
123 int_least16_t x=0;
|
Chris@16
|
124 * this->This() >> x;
|
Chris@16
|
125 t = boost::archive::class_id_type(x);
|
Chris@16
|
126 }
|
Chris@16
|
127
|
Chris@101
|
128 void load_override(archive::version_type & t, int version){
|
Chris@101
|
129 int_least8_t x=0;
|
Chris@101
|
130 * this->This() >> x;
|
Chris@101
|
131 t = boost::archive::version_type(x);
|
Chris@101
|
132 }
|
Chris@101
|
133
|
Chris@16
|
134 void load_override(archive::class_id_reference_type & t, int version){
|
Chris@16
|
135 load_override(static_cast<archive::class_id_type &>(t), version);
|
Chris@16
|
136 }
|
Chris@16
|
137
|
Chris@16
|
138 void load_override(archive::class_name_type & t, int)
|
Chris@16
|
139 {
|
Chris@16
|
140 std::string cn;
|
Chris@16
|
141 cn.reserve(BOOST_SERIALIZATION_MAX_KEY_SIZE);
|
Chris@16
|
142 * this->This() >> cn;
|
Chris@16
|
143 std::memcpy(t, cn.data(), cn.size());
|
Chris@16
|
144 // borland tweak
|
Chris@16
|
145 t.t[cn.size()] = '\0';
|
Chris@16
|
146 }
|
Chris@16
|
147
|
Chris@16
|
148 private:
|
Chris@16
|
149 /// An internal buffer to be used when the user does not supply his
|
Chris@16
|
150 /// own buffer.
|
Chris@16
|
151 buffer_type internal_buffer_;
|
Chris@16
|
152 };
|
Chris@16
|
153
|
Chris@16
|
154 } } // end namespace boost::mpi
|
Chris@16
|
155
|
Chris@16
|
156 BOOST_SERIALIZATION_REGISTER_ARCHIVE(boost::mpi::packed_iarchive)
|
Chris@16
|
157 BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(boost::mpi::packed_iarchive)
|
Chris@16
|
158
|
Chris@16
|
159 #endif // BOOST_MPI_PACKED_IARCHIVE_HPP
|