packed_oprimitive.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/serialization/detail/get_data.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 &buffer_[0];
  33. }
  34. const std::size_t& size() const
  35. {
  36. return size_ = buffer_.size();
  37. }
  38. void save_binary(void const *address, std::size_t count)
  39. {
  40. save_impl(address,MPI_BYTE,count);
  41. }
  42. // fast saving of arrays
  43. template<class T>
  44. void save_array(serialization::array_wrapper<T> const& x, unsigned int /* file_version */)
  45. {
  46. if (x.count())
  47. save_impl(x.address(), get_mpi_datatype(*x.address()), x.count());
  48. }
  49. typedef is_mpi_datatype<mpl::_1> use_array_optimization;
  50. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  51. friend class archive::save_access;
  52. protected:
  53. #else
  54. public:
  55. #endif
  56. // default saving of primitives.
  57. template<class T>
  58. void save(const T & t)
  59. {
  60. save_impl(&t, get_mpi_datatype<T>(t), 1);
  61. }
  62. template<class CharType>
  63. void save(const std::basic_string<CharType> &s)
  64. {
  65. unsigned int l = static_cast<unsigned int>(s.size());
  66. save(l);
  67. if (l)
  68. save_impl(s.data(),get_mpi_datatype(CharType()),s.size());
  69. }
  70. private:
  71. void save_impl(void const * p, MPI_Datatype t, int l)
  72. {
  73. // allocate enough memory
  74. int memory_needed;
  75. BOOST_MPI_CHECK_RESULT(MPI_Pack_size,(l,t,comm,&memory_needed));
  76. int position = buffer_.size();
  77. buffer_.resize(position + memory_needed);
  78. // pack the data into the buffer
  79. BOOST_MPI_CHECK_RESULT(MPI_Pack,
  80. (const_cast<void*>(p), l, t, boost::serialization::detail::get_data(buffer_), buffer_.size(), &position, comm));
  81. // reduce the buffer size if needed
  82. BOOST_ASSERT(std::size_t(position) <= buffer_.size());
  83. if (std::size_t(position) < buffer_.size())
  84. buffer_.resize(position);
  85. }
  86. buffer_type& buffer_;
  87. mutable std::size_t size_;
  88. MPI_Comm comm;
  89. };
  90. } } // end namespace boost::mpi
  91. #endif // BOOST_MPI_PACKED_OPRIMITIVE_HPP