packed_oprimitive.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // (C) Copyright 2005 Matthias Troyer
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Matthias Troyer
  6. #ifndef BOOST_MPI_PACKED_OPRIMITIVE_HPP
  7. #define BOOST_MPI_PACKED_OPRIMITIVE_HPP
  8. #include <boost/mpi/config.hpp>
  9. #include <cstddef> // size_t
  10. #include <boost/config.hpp>
  11. #include <boost/mpi/datatype.hpp>
  12. #include <boost/mpi/exception.hpp>
  13. #include <boost/mpi/detail/antiques.hpp>
  14. #include <boost/serialization/array.hpp>
  15. #include <boost/assert.hpp>
  16. #include <vector>
  17. #include <boost/mpi/allocator.hpp>
  18. namespace boost { namespace mpi {
  19. /// serialization using MPI::Pack
  20. class BOOST_MPI_DECL packed_oprimitive
  21. {
  22. public:
  23. /// the type of the buffer into which the data is packed upon serialization
  24. typedef std::vector<char, allocator<char> > buffer_type;
  25. packed_oprimitive(buffer_type & b, MPI_Comm const & comm)
  26. : buffer_(b),
  27. comm(comm)
  28. {
  29. }
  30. void const * address() const
  31. {
  32. return detail::c_data(buffer_);
  33. }
  34. const std::size_t& size() const
  35. {
  36. return size_ = buffer_.size();
  37. }
  38. const std::size_t* size_ptr() const
  39. {
  40. return &size();
  41. }
  42. void save_binary(void const *address, std::size_t count)
  43. {
  44. save_impl(address,MPI_BYTE,count);
  45. }
  46. // fast saving of arrays
  47. template<class T>
  48. void save_array(serialization::array_wrapper<T> const& x, unsigned int /* file_version */)
  49. {
  50. if (x.count())
  51. save_impl(x.address(), get_mpi_datatype(*x.address()), x.count());
  52. }
  53. typedef is_mpi_datatype<mpl::_1> use_array_optimization;
  54. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  55. friend class archive::save_access;
  56. protected:
  57. #else
  58. public:
  59. #endif
  60. // default saving of primitives.
  61. template<class T>
  62. void save(const T & t)
  63. {
  64. save_impl(&t, get_mpi_datatype<T>(t), 1);
  65. }
  66. template<class CharType>
  67. void save(const std::basic_string<CharType> &s)
  68. {
  69. unsigned int l = static_cast<unsigned int>(s.size());
  70. save(l);
  71. if (l)
  72. save_impl(s.data(),get_mpi_datatype(CharType()),s.size());
  73. }
  74. private:
  75. void save_impl(void const * p, MPI_Datatype t, int l)
  76. {
  77. // allocate enough memory
  78. int memory_needed;
  79. BOOST_MPI_CHECK_RESULT(MPI_Pack_size,(l,t,comm,&memory_needed));
  80. int position = buffer_.size();
  81. buffer_.resize(position + memory_needed);
  82. // pack the data into the buffer
  83. BOOST_MPI_CHECK_RESULT(MPI_Pack,
  84. (const_cast<void*>(p),l,t,
  85. detail::c_data(buffer_),
  86. buffer_.size(),
  87. &position,comm));
  88. // reduce the buffer size if needed
  89. BOOST_ASSERT(std::size_t(position) <= buffer_.size());
  90. if (std::size_t(position) < buffer_.size())
  91. buffer_.resize(position);
  92. }
  93. static buffer_type::value_type* get_data(buffer_type& b)
  94. {
  95. return detail::c_data(b);
  96. }
  97. buffer_type& buffer_;
  98. mutable std::size_t size_;
  99. MPI_Comm comm;
  100. };
  101. } } // end namespace boost::mpi
  102. #endif // BOOST_MPI_PACKED_OPRIMITIVE_HPP