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_OPRIMITIVE_HPP
|
Chris@16
|
10 #define BOOST_MPI_PACKED_OPRIMITIVE_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
|
Chris@16
|
16 #include <boost/mpi/datatype.hpp>
|
Chris@16
|
17 #include <boost/mpi/exception.hpp>
|
Chris@16
|
18 #include <boost/serialization/detail/get_data.hpp>
|
Chris@16
|
19 #include <boost/serialization/array.hpp>
|
Chris@16
|
20 #include <boost/assert.hpp>
|
Chris@16
|
21 #include <vector>
|
Chris@16
|
22 #include <boost/mpi/allocator.hpp>
|
Chris@16
|
23
|
Chris@16
|
24 namespace boost { namespace mpi {
|
Chris@16
|
25
|
Chris@16
|
26 /// serialization using MPI::Pack
|
Chris@16
|
27
|
Chris@16
|
28 class BOOST_MPI_DECL packed_oprimitive
|
Chris@16
|
29 {
|
Chris@16
|
30 public:
|
Chris@16
|
31 /// the type of the buffer into which the data is packed upon serialization
|
Chris@16
|
32 typedef std::vector<char, allocator<char> > buffer_type;
|
Chris@16
|
33
|
Chris@16
|
34 packed_oprimitive(buffer_type & b, MPI_Comm const & comm)
|
Chris@16
|
35 : buffer_(b),
|
Chris@16
|
36 comm(comm)
|
Chris@16
|
37 {
|
Chris@16
|
38 }
|
Chris@16
|
39
|
Chris@16
|
40 void const * address() const
|
Chris@16
|
41 {
|
Chris@16
|
42 return &buffer_[0];
|
Chris@16
|
43 }
|
Chris@16
|
44
|
Chris@16
|
45 const std::size_t& size() const
|
Chris@16
|
46 {
|
Chris@16
|
47 return size_ = buffer_.size();
|
Chris@16
|
48 }
|
Chris@16
|
49
|
Chris@16
|
50 void save_binary(void const *address, std::size_t count)
|
Chris@16
|
51 {
|
Chris@16
|
52 save_impl(address,MPI_BYTE,count);
|
Chris@16
|
53 }
|
Chris@16
|
54
|
Chris@16
|
55 // fast saving of arrays
|
Chris@16
|
56 template<class T>
|
Chris@16
|
57 void save_array(serialization::array<T> const& x, unsigned int /* file_version */)
|
Chris@16
|
58 {
|
Chris@16
|
59 if (x.count())
|
Chris@16
|
60 save_impl(x.address(), get_mpi_datatype(*x.address()), x.count());
|
Chris@16
|
61 }
|
Chris@16
|
62
|
Chris@16
|
63 typedef is_mpi_datatype<mpl::_1> use_array_optimization;
|
Chris@16
|
64
|
Chris@16
|
65 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
|
Chris@16
|
66 friend class archive::save_access;
|
Chris@16
|
67 protected:
|
Chris@16
|
68 #else
|
Chris@16
|
69 public:
|
Chris@16
|
70 #endif
|
Chris@16
|
71
|
Chris@16
|
72 // default saving of primitives.
|
Chris@16
|
73 template<class T>
|
Chris@16
|
74 void save(const T & t)
|
Chris@16
|
75 {
|
Chris@16
|
76 save_impl(&t, get_mpi_datatype<T>(t), 1);
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 template<class CharType>
|
Chris@16
|
80 void save(const std::basic_string<CharType> &s)
|
Chris@16
|
81 {
|
Chris@16
|
82 unsigned int l = static_cast<unsigned int>(s.size());
|
Chris@16
|
83 save(l);
|
Chris@16
|
84 if (l)
|
Chris@16
|
85 save_impl(s.data(),get_mpi_datatype(CharType()),s.size());
|
Chris@16
|
86 }
|
Chris@16
|
87
|
Chris@16
|
88 private:
|
Chris@16
|
89
|
Chris@16
|
90 void save_impl(void const * p, MPI_Datatype t, int l)
|
Chris@16
|
91 {
|
Chris@16
|
92 // allocate enough memory
|
Chris@16
|
93 int memory_needed;
|
Chris@16
|
94 BOOST_MPI_CHECK_RESULT(MPI_Pack_size,(l,t,comm,&memory_needed));
|
Chris@16
|
95
|
Chris@16
|
96 int position = buffer_.size();
|
Chris@16
|
97 buffer_.resize(position + memory_needed);
|
Chris@16
|
98
|
Chris@16
|
99 // pack the data into the buffer
|
Chris@16
|
100 BOOST_MPI_CHECK_RESULT(MPI_Pack,
|
Chris@16
|
101 (const_cast<void*>(p), l, t, boost::serialization::detail::get_data(buffer_), buffer_.size(), &position, comm));
|
Chris@16
|
102 // reduce the buffer size if needed
|
Chris@16
|
103 BOOST_ASSERT(std::size_t(position) <= buffer_.size());
|
Chris@16
|
104 if (std::size_t(position) < buffer_.size())
|
Chris@16
|
105 buffer_.resize(position);
|
Chris@16
|
106 }
|
Chris@16
|
107
|
Chris@16
|
108 buffer_type& buffer_;
|
Chris@16
|
109 mutable std::size_t size_;
|
Chris@16
|
110 MPI_Comm comm;
|
Chris@16
|
111 };
|
Chris@16
|
112
|
Chris@16
|
113 } } // end namespace boost::mpi
|
Chris@16
|
114
|
Chris@16
|
115 #endif // BOOST_MPI_PACKED_OPRIMITIVE_HPP
|