1 // (C) Copyright 2005 Matthias Troyer
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)
7 // Authors: Matthias Troyer
9 #ifndef BOOST_MPI_PACKED_IPRIMITIVE_HPP
10 #define BOOST_MPI_PACKED_IPRIMITIVE_HPP
12 #include <boost/mpi/config.hpp>
13 #include <cstddef> // size_t
14 #include <boost/config.hpp>
15 #include <boost/mpi/datatype.hpp>
16 #include <boost/mpi/exception.hpp>
17 #include <boost/assert.hpp>
18 #include <boost/serialization/array.hpp>
19 #include <boost/serialization/detail/get_data.hpp>
21 #include <boost/mpi/allocator.hpp>
23 namespace boost { namespace mpi {
25 /// deserialization using MPI_Unpack
27 class BOOST_MPI_DECL packed_iprimitive
30 /// the type of the buffer from which the data is unpacked upon deserialization
31 typedef std::vector<char, allocator<char> > buffer_type;
33 packed_iprimitive(buffer_type & b, MPI_Comm const & comm, int position = 0)
45 void const* address () const
50 const std::size_t& size() const
52 return size_ = buffer_.size();
55 void resize(std::size_t s)
60 void load_binary(void *address, std::size_t count)
62 load_impl(address,MPI_BYTE,count);
65 // fast saving of arrays of fundamental types
67 void load_array(serialization::array_wrapper<T> const& x, unsigned int /* file_version */)
70 load_impl(x.address(), get_mpi_datatype(*x.address()), x.count());
75 void load(serialization::array_wrapper<T> const& x)
81 typedef is_mpi_datatype<mpl::_1> use_array_optimization;
83 // default saving of primitives.
87 load_impl(&t, get_mpi_datatype(t), 1);
90 template<class CharType>
91 void load(std::basic_string<CharType> & s)
96 // note breaking a rule here - could be a problem on some platform
98 load_impl(const_cast<CharType *>(s.data()),
99 get_mpi_datatype(CharType()),l);
104 void load_impl(void * p, MPI_Datatype t, int l)
106 BOOST_MPI_CHECK_RESULT(MPI_Unpack,
107 (const_cast<char*>(boost::serialization::detail::get_data(buffer_)), buffer_.size(), &position, p, l, t, comm));
110 buffer_type & buffer_;
111 mutable std::size_t size_;
116 } } // end namespace boost::mpi
118 #endif // BOOST_MPI_PACKED_IPRIMITIVE_HPP