Chris@16: // (C) Copyright 2005 Matthias Troyer Chris@16: // (C) Copyright 2006 Douglas Gregor Chris@16: Chris@16: // Use, modification and distribution is subject to the Boost Software Chris@16: // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // Authors: Matthias Troyer Chris@16: // Douglas Gregor Chris@16: Chris@16: /** @file packed_oarchive.hpp Chris@16: * Chris@16: * This header provides the facilities for unpacking Serializable Chris@16: * data types from a buffer using @c MPI_Unpack. The buffers are Chris@16: * typically received via MPI and have been packed either by via the Chris@16: * facilities in @c packed_iarchive.hpp or @c MPI_Pack. Chris@16: */ Chris@16: #ifndef BOOST_MPI_PACKED_OARCHIVE_HPP Chris@16: #define BOOST_MPI_PACKED_OARCHIVE_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace mpi { Chris@16: Chris@16: #ifdef BOOST_MPI_HOMOGENEOUS Chris@16: typedef binary_buffer_oprimitive oprimitive; Chris@16: #else Chris@16: typedef packed_oprimitive oprimitive; Chris@16: #endif Chris@16: Chris@16: /** @brief An archive that packs binary data into an MPI buffer. Chris@16: * Chris@16: * The @c packed_iarchive class is an Archiver (as in the Chris@16: * Boost.Serialization library) that packs binary data into a buffer Chris@16: * for transmission via MPI. It can operate on any Serializable data Chris@16: * type and will use the @c MPI_Pack function of the underlying MPI Chris@16: * implementation to perform serialization. Chris@16: */ Chris@101: Chris@16: class BOOST_MPI_DECL packed_oarchive Chris@16: : public oprimitive Chris@16: , public archive::detail::common_oarchive Chris@16: { Chris@16: public: Chris@16: /** Chris@16: * Construct a @c packed_oarchive for transmission over the given Chris@16: * MPI communicator and with an initial buffer. Chris@16: * Chris@16: * @param comm The communicator over which this archive will be Chris@16: * sent. Chris@16: * Chris@16: * @param b A user-defined buffer that will be filled with the Chris@16: * binary representation of serialized objects. Chris@16: * Chris@16: * @param flags Control the serialization of the data types. Refer Chris@16: * to the Boost.Serialization documentation before changing the Chris@16: * default flags. Chris@16: * Chris@16: * @param position Set the offset into buffer @p b at which Chris@16: * deserialization will begin. Chris@16: */ Chris@16: packed_oarchive( MPI_Comm const & comm, buffer_type & b, unsigned int flags = boost::archive::no_header) Chris@16: : oprimitive(b,comm), Chris@16: archive::detail::common_oarchive(flags) Chris@16: {} Chris@16: Chris@16: /** Chris@16: * Construct a @c packed_oarchive for transmission over the given Chris@16: * MPI communicator. Chris@16: * Chris@16: * @param comm The communicator over which this archive will be Chris@16: * sent. Chris@16: * Chris@16: * @param s The size of the buffer to be received. Chris@16: * Chris@16: * @param flags Control the serialization of the data types. Refer Chris@16: * to the Boost.Serialization documentation before changing the Chris@16: * default flags. Chris@16: */ Chris@16: packed_oarchive ( MPI_Comm const & comm, unsigned int flags = boost::archive::no_header) Chris@16: : oprimitive(internal_buffer_,comm), Chris@16: archive::detail::common_oarchive(flags) Chris@16: {} Chris@16: Chris@16: // Save everything else in the usual way, forwarding on to the Base class Chris@16: template Chris@16: void save_override(T const& x, int version, mpl::false_) Chris@16: { Chris@16: archive::detail::common_oarchive::save_override(x,version); Chris@16: } Chris@16: Chris@16: // Save it directly using the primitives Chris@16: template Chris@16: void save_override(T const& x, int /*version*/, mpl::true_) Chris@16: { Chris@16: oprimitive::save(x); Chris@16: } Chris@16: Chris@16: // Save all supported datatypes directly Chris@16: template Chris@16: void save_override(T const& x, int version) Chris@16: { Chris@16: typedef typename mpl::apply1::type use_optimized; Chris@16: save_override(x, version, use_optimized()); Chris@16: } Chris@16: Chris@101: // input archives need to ignore the optional information Chris@16: void save_override(const archive::class_id_optional_type & /*t*/, int){} Chris@16: Chris@16: // explicitly convert to char * to avoid compile ambiguities Chris@16: void save_override(const archive::class_name_type & t, int){ Chris@16: const std::string s(t); Chris@16: * this->This() << s; Chris@16: } Chris@16: Chris@16: void save_override(archive::class_id_type & t, int version){ Chris@16: const boost::int_least16_t x = t; Chris@16: * this->This() << x; Chris@16: } Chris@101: Chris@101: void save_override(archive::version_type & t, int version){ Chris@101: const boost::int_least8_t x = t; Chris@101: * this->This() << x; Chris@101: } Chris@16: private: Chris@16: /// An internal buffer to be used when the user does not supply his Chris@16: /// own buffer. Chris@16: buffer_type internal_buffer_; Chris@16: }; Chris@16: Chris@16: } } // end namespace boost::mpi Chris@16: Chris@16: // required by export Chris@16: BOOST_SERIALIZATION_REGISTER_ARCHIVE(boost::mpi::packed_oarchive) Chris@16: BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(boost::mpi::packed_oarchive) Chris@16: Chris@16: Chris@16: Chris@16: #endif // BOOST_MPI_PACKED_OARCHIVE_HPP