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