binary_buffer_oprimitive.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // (C) Copyright 2005-2007 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_BINARY_BUFFER_OPRIMITIVE_HPP
  7. #define BOOST_MPI_BINARY_BUFFER_OPRIMITIVE_HPP
  8. #include <mpi.h>
  9. #include <iostream>
  10. #include <cstddef> // size_t
  11. #include <boost/config.hpp>
  12. #include <boost/serialization/array.hpp>
  13. #include <boost/serialization/is_bitwise_serializable.hpp>
  14. #include <boost/assert.hpp>
  15. #include <boost/mpl/assert.hpp>
  16. #include <vector>
  17. #include <boost/mpi/allocator.hpp>
  18. #include <boost/mpl/always.hpp>
  19. #include <boost/type_traits/remove_const.hpp>
  20. namespace boost { namespace mpi {
  21. /// serialization using binary copy into a buffer
  22. class BOOST_MPI_DECL binary_buffer_oprimitive
  23. {
  24. public:
  25. /// the type of the buffer into which the data is packed upon serialization
  26. typedef std::vector<char, allocator<char> > buffer_type;
  27. binary_buffer_oprimitive(buffer_type & b, MPI_Comm const &)
  28. : buffer_(b)
  29. {
  30. }
  31. void const * address() const
  32. {
  33. return detail::c_data(buffer_);
  34. }
  35. const std::size_t& size() const
  36. {
  37. return size_ = buffer_.size();
  38. }
  39. const std::size_t* size_ptr() const
  40. {
  41. return &size();
  42. }
  43. void save_binary(void const *address, std::size_t count)
  44. {
  45. save_impl(address,count);
  46. }
  47. // fast saving of arrays
  48. template<class T>
  49. void save_array(serialization::array_wrapper<T> const& x, unsigned int /* file_version */)
  50. {
  51. BOOST_MPL_ASSERT((serialization::is_bitwise_serializable<BOOST_DEDUCED_TYPENAME remove_const<T>::type>));
  52. if (x.count())
  53. save_impl(x.address(), x.count()*sizeof(T));
  54. }
  55. template<class T>
  56. void save(serialization::array_wrapper<T> const& x)
  57. {
  58. save_array(x,0u);
  59. }
  60. typedef serialization::is_bitwise_serializable<mpl::_1> use_array_optimization;
  61. // default saving of primitives.
  62. template<class T>
  63. void save(const T & t)
  64. {
  65. BOOST_MPL_ASSERT((serialization::is_bitwise_serializable<BOOST_DEDUCED_TYPENAME remove_const<T>::type>));
  66. save_impl(&t, sizeof(T));
  67. }
  68. template<class CharType>
  69. void save(const std::basic_string<CharType> &s)
  70. {
  71. unsigned int l = static_cast<unsigned int>(s.size());
  72. save(l);
  73. save_impl(s.data(),s.size());
  74. }
  75. private:
  76. void save_impl(void const * p, int l)
  77. {
  78. char const* ptr = reinterpret_cast<char const*>(p);
  79. buffer_.insert(buffer_.end(),ptr,ptr+l);
  80. }
  81. buffer_type& buffer_;
  82. mutable std::size_t size_;
  83. };
  84. } } // end namespace boost::mpi
  85. #endif // BOOST_MPI_BINARY_BUFFER_OPRIMITIVE_HPP