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