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_oarchive.hpp
|
Chris@16
|
12 *
|
Chris@16
|
13 * This header provides the facilities for unpacking Serializable
|
Chris@16
|
14 * data types from a buffer using @c MPI_Unpack. The buffers are
|
Chris@16
|
15 * typically received via MPI and have been packed either by via the
|
Chris@16
|
16 * facilities in @c packed_iarchive.hpp or @c MPI_Pack.
|
Chris@16
|
17 */
|
Chris@16
|
18 #ifndef BOOST_MPI_PACKED_OARCHIVE_HPP
|
Chris@16
|
19 #define BOOST_MPI_PACKED_OARCHIVE_HPP
|
Chris@16
|
20
|
Chris@16
|
21 #include <boost/mpi/datatype.hpp>
|
Chris@16
|
22 #include <boost/archive/basic_archive.hpp>
|
Chris@16
|
23 #include <boost/archive/detail/auto_link_archive.hpp>
|
Chris@16
|
24 #include <boost/archive/detail/common_oarchive.hpp>
|
Chris@16
|
25 #include <boost/mpi/detail/packed_oprimitive.hpp>
|
Chris@16
|
26 #include <boost/mpi/detail/binary_buffer_oprimitive.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
|
Chris@16
|
31 namespace boost { namespace mpi {
|
Chris@16
|
32
|
Chris@16
|
33 #ifdef BOOST_MPI_HOMOGENEOUS
|
Chris@16
|
34 typedef binary_buffer_oprimitive oprimitive;
|
Chris@16
|
35 #else
|
Chris@16
|
36 typedef packed_oprimitive oprimitive;
|
Chris@16
|
37 #endif
|
Chris@16
|
38
|
Chris@16
|
39 /** @brief An archive that packs binary data into an MPI buffer.
|
Chris@16
|
40 *
|
Chris@16
|
41 * The @c packed_iarchive class is an Archiver (as in the
|
Chris@16
|
42 * Boost.Serialization library) that packs binary data into a buffer
|
Chris@16
|
43 * for transmission via MPI. It can operate on any Serializable data
|
Chris@16
|
44 * type and will use the @c MPI_Pack function of the underlying MPI
|
Chris@16
|
45 * implementation to perform serialization.
|
Chris@16
|
46 */
|
Chris@101
|
47
|
Chris@16
|
48 class BOOST_MPI_DECL packed_oarchive
|
Chris@16
|
49 : public oprimitive
|
Chris@16
|
50 , public archive::detail::common_oarchive<packed_oarchive>
|
Chris@16
|
51 {
|
Chris@16
|
52 public:
|
Chris@16
|
53 /**
|
Chris@16
|
54 * Construct a @c packed_oarchive for transmission over the given
|
Chris@16
|
55 * MPI communicator and with an initial buffer.
|
Chris@16
|
56 *
|
Chris@16
|
57 * @param comm The communicator over which this archive will be
|
Chris@16
|
58 * sent.
|
Chris@16
|
59 *
|
Chris@16
|
60 * @param b A user-defined buffer that will be filled with the
|
Chris@16
|
61 * binary representation of serialized objects.
|
Chris@16
|
62 *
|
Chris@16
|
63 * @param flags Control the serialization of the data types. Refer
|
Chris@16
|
64 * to the Boost.Serialization documentation before changing the
|
Chris@16
|
65 * default flags.
|
Chris@16
|
66 *
|
Chris@16
|
67 * @param position Set the offset into buffer @p b at which
|
Chris@16
|
68 * deserialization will begin.
|
Chris@16
|
69 */
|
Chris@16
|
70 packed_oarchive( MPI_Comm const & comm, buffer_type & b, unsigned int flags = boost::archive::no_header)
|
Chris@16
|
71 : oprimitive(b,comm),
|
Chris@16
|
72 archive::detail::common_oarchive<packed_oarchive>(flags)
|
Chris@16
|
73 {}
|
Chris@16
|
74
|
Chris@16
|
75 /**
|
Chris@16
|
76 * Construct a @c packed_oarchive for transmission 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 * sent.
|
Chris@16
|
81 *
|
Chris@16
|
82 * @param s The size of the buffer to be received.
|
Chris@16
|
83 *
|
Chris@16
|
84 * @param flags Control the serialization of the data types. Refer
|
Chris@16
|
85 * to the Boost.Serialization documentation before changing the
|
Chris@16
|
86 * default flags.
|
Chris@16
|
87 */
|
Chris@16
|
88 packed_oarchive ( MPI_Comm const & comm, unsigned int flags = boost::archive::no_header)
|
Chris@16
|
89 : oprimitive(internal_buffer_,comm),
|
Chris@16
|
90 archive::detail::common_oarchive<packed_oarchive>(flags)
|
Chris@16
|
91 {}
|
Chris@16
|
92
|
Chris@16
|
93 // Save everything else in the usual way, forwarding on to the Base class
|
Chris@16
|
94 template<class T>
|
Chris@16
|
95 void save_override(T const& x, int version, mpl::false_)
|
Chris@16
|
96 {
|
Chris@16
|
97 archive::detail::common_oarchive<packed_oarchive>::save_override(x,version);
|
Chris@16
|
98 }
|
Chris@16
|
99
|
Chris@16
|
100 // Save it directly using the primitives
|
Chris@16
|
101 template<class T>
|
Chris@16
|
102 void save_override(T const& x, int /*version*/, mpl::true_)
|
Chris@16
|
103 {
|
Chris@16
|
104 oprimitive::save(x);
|
Chris@16
|
105 }
|
Chris@16
|
106
|
Chris@16
|
107 // Save all supported datatypes directly
|
Chris@16
|
108 template<class T>
|
Chris@16
|
109 void save_override(T const& x, int version)
|
Chris@16
|
110 {
|
Chris@16
|
111 typedef typename mpl::apply1<use_array_optimization,T>::type use_optimized;
|
Chris@16
|
112 save_override(x, version, use_optimized());
|
Chris@16
|
113 }
|
Chris@16
|
114
|
Chris@101
|
115 // input archives need to ignore the optional information
|
Chris@16
|
116 void save_override(const archive::class_id_optional_type & /*t*/, int){}
|
Chris@16
|
117
|
Chris@16
|
118 // explicitly convert to char * to avoid compile ambiguities
|
Chris@16
|
119 void save_override(const archive::class_name_type & t, int){
|
Chris@16
|
120 const std::string s(t);
|
Chris@16
|
121 * this->This() << s;
|
Chris@16
|
122 }
|
Chris@16
|
123
|
Chris@16
|
124 void save_override(archive::class_id_type & t, int version){
|
Chris@16
|
125 const boost::int_least16_t x = t;
|
Chris@16
|
126 * this->This() << x;
|
Chris@16
|
127 }
|
Chris@101
|
128
|
Chris@101
|
129 void save_override(archive::version_type & t, int version){
|
Chris@101
|
130 const boost::int_least8_t x = t;
|
Chris@101
|
131 * this->This() << x;
|
Chris@101
|
132 }
|
Chris@16
|
133 private:
|
Chris@16
|
134 /// An internal buffer to be used when the user does not supply his
|
Chris@16
|
135 /// own buffer.
|
Chris@16
|
136 buffer_type internal_buffer_;
|
Chris@16
|
137 };
|
Chris@16
|
138
|
Chris@16
|
139 } } // end namespace boost::mpi
|
Chris@16
|
140
|
Chris@16
|
141 // required by export
|
Chris@16
|
142 BOOST_SERIALIZATION_REGISTER_ARCHIVE(boost::mpi::packed_oarchive)
|
Chris@16
|
143 BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(boost::mpi::packed_oarchive)
|
Chris@16
|
144
|
Chris@16
|
145
|
Chris@16
|
146
|
Chris@16
|
147 #endif // BOOST_MPI_PACKED_OARCHIVE_HPP
|