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