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