PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/mpi/packed_iarchive.hpp

http://hadesmem.googlecode.com/
C++ Header | 146 lines | 70 code | 17 blank | 59 comment | 0 complexity | 1a018c112685424de42aef90246f30e8 MD5 | raw file
  1// (C) Copyright 2005 Matthias Troyer
  2// (C) Copyright 2006 Douglas Gregor <doug.gregor -at- gmail.com>
  3
  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)
  7
  8//  Authors: Matthias Troyer
  9//           Douglas Gregor
 10
 11/** @file packed_iarchive.hpp
 12 *
 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.
 17 */
 18#ifndef BOOST_MPI_PACKED_IARCHIVE_HPP
 19#define BOOST_MPI_PACKED_IARCHIVE_HPP
 20
 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/shared_ptr_helper.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>
 31
 32namespace boost { namespace mpi {
 33
 34#ifdef BOOST_MPI_HOMOGENEOUS
 35  typedef binary_buffer_iprimitive iprimitive;
 36#else
 37  typedef packed_iprimitive iprimitive;
 38#endif
 39
 40/** @brief An archive that packs binary data into an MPI buffer.
 41 *
 42 *  The @c packed_iarchive class is an Archiver (as in the
 43 *  Boost.Serialization library) that packs binary data into a buffer
 44 *  for transmission via MPI. It can operate on any Serializable data
 45 *  type and will use the @c MPI_Pack function of the underlying MPI
 46 *  implementation to perform serialization.
 47 */
 48class BOOST_MPI_DECL packed_iarchive
 49  : public iprimitive
 50  , public archive::detail::common_iarchive<packed_iarchive>
 51  , public archive::detail::shared_ptr_helper
 52{
 53public:
 54  /**
 55   *  Construct a @c packed_iarchive for transmission over the given
 56   *  MPI communicator and with an initial buffer.
 57   *
 58   *  @param comm The communicator over which this archive will be
 59   *  sent.
 60   *
 61   *  @param b A user-defined buffer that will be filled with the
 62   *  binary representation of serialized objects.
 63   *
 64   *  @param flags Control the serialization of the data types. Refer
 65   *  to the Boost.Serialization documentation before changing the
 66   *  default flags.
 67   *
 68   *  @param position Set the offset into buffer @p b at which
 69   *  deserialization will begin.
 70   */
 71  packed_iarchive(MPI_Comm const & comm, buffer_type & b, unsigned int flags = boost::archive::no_header, int position = 0)
 72        : iprimitive(b,comm,position),
 73          archive::detail::common_iarchive<packed_iarchive>(flags)
 74        {}
 75
 76  /**
 77   *  Construct a @c packed_iarchive for transmission over the given
 78   *  MPI communicator.
 79   *
 80   *  @param comm The communicator over which this archive will be
 81   *  sent.
 82   *
 83   *  @param s The size of the buffer to be received.
 84   *
 85   *  @param flags Control the serialization of the data types. Refer
 86   *  to the Boost.Serialization documentation before changing the
 87   *  default flags.
 88   */
 89  packed_iarchive
 90          ( MPI_Comm const & comm , std::size_t s=0, 
 91           unsigned int flags = boost::archive::no_header)
 92         : iprimitive(internal_buffer_,comm)
 93         , archive::detail::common_iarchive<packed_iarchive>(flags)
 94         , internal_buffer_(s)
 95        {}
 96
 97  // Load everything else in the usual way, forwarding on to the Base class
 98  template<class T>
 99  void load_override(T& x, int version, mpl::false_)
100  {
101    archive::detail::common_iarchive<packed_iarchive>::load_override(x,version);
102  }
103
104  // Load it directly using the primnivites
105  template<class T>
106  void load_override(T& x, int /*version*/, mpl::true_)
107  {
108    iprimitive::load(x);
109  }
110
111  // Load all supported datatypes directly
112  template<class T>
113  void load_override(T& x, int version)
114  {
115    typedef typename mpl::apply1<use_array_optimization
116      , BOOST_DEDUCED_TYPENAME remove_const<T>::type
117    >::type use_optimized;
118    load_override(x, version, use_optimized());
119  }
120
121  // input archives need to ignore  the optional information 
122  void load_override(archive::class_id_optional_type & /*t*/, int){}
123
124  void load_override(archive::class_name_type & t, int)
125  {
126    std::string cn;
127    cn.reserve(BOOST_SERIALIZATION_MAX_KEY_SIZE);
128    * this->This() >> cn;
129    std::memcpy(t, cn.data(), cn.size());
130    // borland tweak
131    t.t[cn.size()] = '\0';
132  }
133
134private:
135  /// An internal buffer to be used when the user does not supply his
136  /// own buffer.
137  buffer_type internal_buffer_;
138};
139
140} } // end namespace boost::mpi
141
142BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(boost::mpi::packed_iarchive)
143BOOST_SERIALIZATION_REGISTER_ARCHIVE(boost::mpi::packed_iarchive)
144BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(boost::mpi::packed_iarchive)
145
146#endif // BOOST_MPI_PACKED_IARCHIVE_HPP