binary_buffer_oprimitive.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 &buffer_.front();
  34. }
  35. const std::size_t& size() const
  36. {
  37. return size_ = buffer_.size();
  38. }
  39. void save_binary(void const *address, std::size_t count)
  40. {
  41. save_impl(address,count);
  42. }
  43. // fast saving of arrays
  44. template<class T>
  45. void save_array(serialization::array<T> const& x, unsigned int /* file_version */)
  46. {
  47. BOOST_MPL_ASSERT((serialization::is_bitwise_serializable<BOOST_DEDUCED_TYPENAME remove_const<T>::type>));
  48. if (x.count())
  49. save_impl(x.address(), x.count()*sizeof(T));
  50. }
  51. template<class T>
  52. void save(serialization::array<T> const& x)
  53. {
  54. save_array(x,0u);
  55. }
  56. typedef serialization::is_bitwise_serializable<mpl::_1> use_array_optimization;
  57. // default saving of primitives.
  58. template<class T>
  59. void save(const T & t)
  60. {
  61. BOOST_MPL_ASSERT((serialization::is_bitwise_serializable<BOOST_DEDUCED_TYPENAME remove_const<T>::type>));
  62. save_impl(&t, sizeof(T));
  63. }
  64. template<class CharType>
  65. void save(const std::basic_string<CharType> &s)
  66. {
  67. unsigned int l = static_cast<unsigned int>(s.size());
  68. save(l);
  69. save_impl(s.data(),s.size());
  70. }
  71. private:
  72. void save_impl(void const * p, int l)
  73. {
  74. char const* ptr = reinterpret_cast<char const*>(p);
  75. buffer_.insert(buffer_.end(),ptr,ptr+l);
  76. }
  77. buffer_type& buffer_;
  78. mutable std::size_t size_;
  79. };
  80. } } // end namespace boost::mpi
  81. #endif // BOOST_MPI_BINARY_BUFFER_OPRIMITIVE_HPP