]>
Commit | Line | Data |
---|---|---|
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 | ||
36 | binary_buffer_oprimitive(buffer_type & b, MPI_Comm const &) | |
37 | : buffer_(b) | |
38 | { | |
39 | } | |
40 | ||
41 | void const * address() const | |
42 | { | |
43 | return &buffer_.front(); | |
44 | } | |
45 | ||
46 | const std::size_t& size() const | |
47 | { | |
48 | return size_ = buffer_.size(); | |
49 | } | |
50 | ||
51 | void save_binary(void const *address, std::size_t count) | |
52 | { | |
53 | save_impl(address,count); | |
54 | } | |
55 | ||
56 | // fast saving of arrays | |
57 | template<class T> | |
58 | void save_array(serialization::array_wrapper<T> const& x, unsigned int /* file_version */) | |
59 | { | |
60 | ||
61 | BOOST_MPL_ASSERT((serialization::is_bitwise_serializable<BOOST_DEDUCED_TYPENAME remove_const<T>::type>)); | |
62 | if (x.count()) | |
63 | save_impl(x.address(), x.count()*sizeof(T)); | |
64 | } | |
65 | ||
66 | template<class T> | |
67 | void save(serialization::array_wrapper<T> const& x) | |
68 | { | |
69 | save_array(x,0u); | |
70 | } | |
71 | ||
72 | typedef serialization::is_bitwise_serializable<mpl::_1> use_array_optimization; | |
73 | ||
74 | // default saving of primitives. | |
75 | template<class T> | |
76 | void save(const T & t) | |
77 | { | |
78 | BOOST_MPL_ASSERT((serialization::is_bitwise_serializable<BOOST_DEDUCED_TYPENAME remove_const<T>::type>)); | |
79 | save_impl(&t, sizeof(T)); | |
80 | } | |
81 | ||
82 | template<class CharType> | |
83 | void save(const std::basic_string<CharType> &s) | |
84 | { | |
85 | unsigned int l = static_cast<unsigned int>(s.size()); | |
86 | save(l); | |
87 | save_impl(s.data(),s.size()); | |
88 | } | |
89 | ||
90 | private: | |
91 | ||
92 | void save_impl(void const * p, int l) | |
93 | { | |
94 | char const* ptr = reinterpret_cast<char const*>(p); | |
95 | buffer_.insert(buffer_.end(),ptr,ptr+l); | |
96 | } | |
97 | ||
98 | buffer_type& buffer_; | |
99 | mutable std::size_t size_; | |
100 | }; | |
101 | ||
102 | } } // end namespace boost::mpi | |
103 | ||
104 | #endif // BOOST_MPI_BINARY_BUFFER_OPRIMITIVE_HPP |