Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:40:59

0001 // (C) Copyright 2005 Matthias Troyer
0002 // (C) Copyright 2006 Douglas Gregor <doug.gregor -at- gmail.com>
0003 
0004 // Use, modification and distribution is subject to the Boost Software
0005 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt)
0007 
0008 //  Authors: Matthias Troyer
0009 //           Douglas Gregor
0010 
0011 /** @file packed_oarchive.hpp
0012  *
0013  *  This header provides the facilities for unpacking Serializable
0014  *  data types from a buffer using @c MPI_Unpack. The buffers are
0015  *  typically received via MPI and have been packed either by via the
0016  *  facilities in @c packed_iarchive.hpp or @c MPI_Pack.
0017  */
0018 #ifndef BOOST_MPI_PACKED_OARCHIVE_HPP
0019 #define BOOST_MPI_PACKED_OARCHIVE_HPP
0020 
0021 #include <boost/mpi/datatype.hpp>
0022 #include <boost/archive/basic_archive.hpp>
0023 #include <boost/archive/detail/auto_link_archive.hpp>
0024 #include <boost/archive/detail/common_oarchive.hpp>
0025 #include <boost/mpi/detail/packed_oprimitive.hpp>
0026 #include <boost/mpi/detail/binary_buffer_oprimitive.hpp>
0027 #include <boost/serialization/string.hpp>
0028 #include <boost/serialization/collection_size_type.hpp>
0029 #include <boost/serialization/item_version_type.hpp>
0030 
0031 namespace boost { namespace mpi {
0032 
0033 #ifdef BOOST_MPI_HOMOGENEOUS
0034   typedef binary_buffer_oprimitive oprimitive;
0035 #else
0036   typedef packed_oprimitive oprimitive;
0037 #endif
0038 
0039 /** @brief An archive that packs binary data into an MPI buffer.
0040  *
0041  *  The @c packed_iarchive class is an Archiver (as in the
0042  *  Boost.Serialization library) that packs binary data into a buffer
0043  *  for transmission via MPI. It can operate on any Serializable data
0044  *  type and will use the @c MPI_Pack function of the underlying MPI
0045  *  implementation to perform serialization.
0046  */
0047 
0048 class BOOST_MPI_DECL packed_oarchive
0049   : public oprimitive
0050   , public archive::detail::common_oarchive<packed_oarchive>
0051 {
0052 public:
0053   /**
0054    *  Construct a @c packed_oarchive for transmission over the given
0055    *  MPI communicator and with an initial buffer.
0056    *
0057    *  @param comm The communicator over which this archive will be
0058    *  sent.
0059    *
0060    *  @param b A user-defined buffer that will be filled with the
0061    *  binary representation of serialized objects.
0062    *
0063    *  @param flags Control the serialization of the data types. Refer
0064    *  to the Boost.Serialization documentation before changing the
0065    *  default flags.
0066    *
0067    *  @param position Set the offset into buffer @p b at which
0068    *  deserialization will begin.
0069    */
0070   packed_oarchive( MPI_Comm const & comm, buffer_type & b, unsigned int flags = boost::archive::no_header)
0071          : oprimitive(b,comm),
0072            archive::detail::common_oarchive<packed_oarchive>(flags)
0073         {}
0074 
0075   /**
0076    *  Construct a @c packed_oarchive for transmission over the given
0077    *  MPI communicator.
0078    *
0079    *  @param comm The communicator over which this archive will be
0080    *  sent.
0081    *
0082    *  @param s The size of the buffer to be received.
0083    *
0084    *  @param flags Control the serialization of the data types. Refer
0085    *  to the Boost.Serialization documentation before changing the
0086    *  default flags.
0087    */
0088   packed_oarchive ( MPI_Comm const & comm, unsigned int flags =  boost::archive::no_header)
0089          : oprimitive(internal_buffer_,comm),
0090            archive::detail::common_oarchive<packed_oarchive>(flags)
0091         {}
0092 
0093   // Save everything else in the usual way, forwarding on to the Base class
0094   template<class T>
0095   void save_override(T const& x, mpl::false_)
0096   {
0097     archive::detail::common_oarchive<packed_oarchive>::save_override(x);
0098   }
0099 
0100   // Save it directly using the primitives
0101   template<class T>
0102   void save_override(T const& x, mpl::true_)
0103   {
0104     oprimitive::save(x);
0105   }
0106 
0107   // Save all supported datatypes directly
0108   template<class T>
0109   void save_override(T const& x)
0110   {
0111     typedef typename mpl::apply1<use_array_optimization,T>::type use_optimized;
0112     save_override(x, use_optimized());
0113   }
0114 
0115   // output archives need to ignore  the optional information
0116   void save_override(const archive::class_id_optional_type & ){}
0117 
0118   // explicitly convert to char * to avoid compile ambiguities
0119   void save_override(const archive::class_name_type & t){
0120       const std::string s(t);
0121       * this->This() << s;
0122   }
0123 
0124   void save_override(const archive::class_id_type & t){
0125     const boost::int_least16_t x = t;
0126     * this->This() << x;
0127   }
0128 
0129   void save_override(const archive::version_type & t){
0130     const boost::int_least8_t x = t;
0131     * this->This() << x;
0132   }
0133 private:
0134   /// An internal buffer to be used when the user does not supply his
0135   /// own buffer.
0136   buffer_type internal_buffer_;
0137 };
0138 
0139 } } // end namespace boost::mpi
0140 
0141 // required by export
0142 BOOST_SERIALIZATION_REGISTER_ARCHIVE(boost::mpi::packed_oarchive)
0143 BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(boost::mpi::packed_oarchive)
0144 
0145 
0146 
0147 #endif // BOOST_MPI_PACKED_OARCHIVE_HPP