Chris@16
|
1 // (C) Copyright 2005 Matthias Troyer
|
Chris@16
|
2
|
Chris@16
|
3 // Use, modification and distribution is subject to the Boost Software
|
Chris@16
|
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
5 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6
|
Chris@16
|
7 // Authors: Matthias Troyer
|
Chris@16
|
8
|
Chris@16
|
9 #ifndef BOOST_MPI_PACKED_IPRIMITIVE_HPP
|
Chris@16
|
10 #define BOOST_MPI_PACKED_IPRIMITIVE_HPP
|
Chris@16
|
11
|
Chris@16
|
12 #include <boost/mpi/config.hpp>
|
Chris@16
|
13 #include <cstddef> // size_t
|
Chris@16
|
14 #include <boost/config.hpp>
|
Chris@16
|
15 #include <boost/mpi/datatype.hpp>
|
Chris@16
|
16 #include <boost/mpi/exception.hpp>
|
Chris@16
|
17 #include <boost/assert.hpp>
|
Chris@16
|
18 #include <boost/serialization/array.hpp>
|
Chris@16
|
19 #include <boost/serialization/detail/get_data.hpp>
|
Chris@16
|
20 #include <vector>
|
Chris@16
|
21 #include <boost/mpi/allocator.hpp>
|
Chris@16
|
22
|
Chris@16
|
23 namespace boost { namespace mpi {
|
Chris@16
|
24
|
Chris@16
|
25 /// deserialization using MPI_Unpack
|
Chris@16
|
26
|
Chris@16
|
27 class BOOST_MPI_DECL packed_iprimitive
|
Chris@16
|
28 {
|
Chris@16
|
29 public:
|
Chris@16
|
30 /// the type of the buffer from which the data is unpacked upon deserialization
|
Chris@16
|
31 typedef std::vector<char, allocator<char> > buffer_type;
|
Chris@16
|
32
|
Chris@16
|
33 packed_iprimitive(buffer_type & b, MPI_Comm const & comm, int position = 0)
|
Chris@16
|
34 : buffer_(b),
|
Chris@16
|
35 comm(comm),
|
Chris@16
|
36 position(position)
|
Chris@16
|
37 {
|
Chris@16
|
38 }
|
Chris@16
|
39
|
Chris@16
|
40 void* address ()
|
Chris@16
|
41 {
|
Chris@16
|
42 return &buffer_[0];
|
Chris@16
|
43 }
|
Chris@16
|
44
|
Chris@16
|
45 void const* address () const
|
Chris@16
|
46 {
|
Chris@16
|
47 return &buffer_[0];
|
Chris@16
|
48 }
|
Chris@16
|
49
|
Chris@16
|
50 const std::size_t& size() const
|
Chris@16
|
51 {
|
Chris@16
|
52 return size_ = buffer_.size();
|
Chris@16
|
53 }
|
Chris@16
|
54
|
Chris@16
|
55 void resize(std::size_t s)
|
Chris@16
|
56 {
|
Chris@16
|
57 buffer_.resize(s);
|
Chris@16
|
58 }
|
Chris@16
|
59
|
Chris@16
|
60 void load_binary(void *address, std::size_t count)
|
Chris@16
|
61 {
|
Chris@16
|
62 load_impl(address,MPI_BYTE,count);
|
Chris@16
|
63 }
|
Chris@16
|
64
|
Chris@16
|
65 // fast saving of arrays of fundamental types
|
Chris@16
|
66 template<class T>
|
Chris@16
|
67 void load_array(serialization::array<T> const& x, unsigned int /* file_version */)
|
Chris@16
|
68 {
|
Chris@16
|
69 if (x.count())
|
Chris@16
|
70 load_impl(x.address(), get_mpi_datatype(*x.address()), x.count());
|
Chris@16
|
71 }
|
Chris@16
|
72
|
Chris@16
|
73 /*
|
Chris@16
|
74 template<class T>
|
Chris@16
|
75 void load(serialization::array<T> const& x)
|
Chris@16
|
76 {
|
Chris@16
|
77 load_array(x,0u);
|
Chris@16
|
78 }
|
Chris@16
|
79 */
|
Chris@16
|
80
|
Chris@16
|
81 typedef is_mpi_datatype<mpl::_1> use_array_optimization;
|
Chris@16
|
82
|
Chris@16
|
83 // default saving of primitives.
|
Chris@16
|
84 template<class T>
|
Chris@16
|
85 void load( T & t)
|
Chris@16
|
86 {
|
Chris@16
|
87 load_impl(&t, get_mpi_datatype(t), 1);
|
Chris@16
|
88 }
|
Chris@16
|
89
|
Chris@16
|
90 template<class CharType>
|
Chris@16
|
91 void load(std::basic_string<CharType> & s)
|
Chris@16
|
92 {
|
Chris@16
|
93 unsigned int l;
|
Chris@16
|
94 load(l);
|
Chris@16
|
95 s.resize(l);
|
Chris@16
|
96 // note breaking a rule here - could be a problem on some platform
|
Chris@16
|
97 if (l)
|
Chris@16
|
98 load_impl(const_cast<CharType *>(s.data()),
|
Chris@16
|
99 get_mpi_datatype(CharType()),l);
|
Chris@16
|
100 }
|
Chris@16
|
101
|
Chris@16
|
102 private:
|
Chris@16
|
103
|
Chris@16
|
104 void load_impl(void * p, MPI_Datatype t, int l)
|
Chris@16
|
105 {
|
Chris@16
|
106 BOOST_MPI_CHECK_RESULT(MPI_Unpack,
|
Chris@16
|
107 (const_cast<char*>(boost::serialization::detail::get_data(buffer_)), buffer_.size(), &position, p, l, t, comm));
|
Chris@16
|
108 }
|
Chris@16
|
109
|
Chris@16
|
110 buffer_type & buffer_;
|
Chris@16
|
111 mutable std::size_t size_;
|
Chris@16
|
112 MPI_Comm comm;
|
Chris@16
|
113 int position;
|
Chris@16
|
114 };
|
Chris@16
|
115
|
Chris@16
|
116 } } // end namespace boost::mpi
|
Chris@16
|
117
|
Chris@16
|
118 #endif // BOOST_MPI_PACKED_IPRIMITIVE_HPP
|