• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_IPRIMITIVE_HPP
10 #define BOOST_MPI_PACKED_IPRIMITIVE_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 <vector>
20 #include <boost/mpi/detail/antiques.hpp>
21 #include <boost/mpi/allocator.hpp>
22 
23 namespace boost { namespace mpi {
24 
25 /// deserialization using MPI_Unpack
26 
27 class BOOST_MPI_DECL packed_iprimitive
28 {
29 public:
30     /// the type of the buffer from which the data is unpacked upon deserialization
31     typedef std::vector<char, allocator<char> > buffer_type;
32 
packed_iprimitive(buffer_type & b,MPI_Comm const & comm,int position=0)33     packed_iprimitive(buffer_type & b, MPI_Comm const & comm, int position = 0)
34          : buffer_(b),
35            comm(comm),
36            position(position)
37         {
38         }
39 
address()40     void* address ()
41     {
42       return detail::c_data(buffer_);
43     }
44 
address() const45     void const* address () const
46     {
47       return detail::c_data(buffer_);
48     }
49 
size() const50     const std::size_t& size() const
51     {
52       return size_ = buffer_.size();
53     }
54 
resize(std::size_t s)55     void resize(std::size_t s)
56     {
57       buffer_.resize(s);
58     }
59 
load_binary(void * address,std::size_t count)60     void load_binary(void *address, std::size_t count)
61         {
62           load_impl(address,MPI_BYTE,count);
63         }
64 
65     // fast saving of arrays of fundamental types
66     template<class T>
load_array(serialization::array_wrapper<T> const & x,unsigned int)67     void load_array(serialization::array_wrapper<T> const& x, unsigned int /* file_version */)
68     {
69       if (x.count())
70         load_impl(x.address(), get_mpi_datatype(*x.address()), x.count());
71     }
72 
73 /*
74     template<class T>
75     void load(serialization::array_wrapper<T> const& x)
76     {
77       load_array(x,0u);
78     }
79 */
80 
81     typedef is_mpi_datatype<mpl::_1> use_array_optimization;
82 
83     // default saving of primitives.
84     template<class T>
load(T & t)85     void load( T & t)
86     {
87       load_impl(&t, get_mpi_datatype(t), 1);
88     }
89 
90     template<class CharType>
load(std::basic_string<CharType> & s)91     void load(std::basic_string<CharType> & s)
92     {
93         unsigned int l;
94         load(l);
95         s.resize(l);
96         // note breaking a rule here - could be a problem on some platform
97         if (l)
98           load_impl(const_cast<CharType *>(s.data()),
99                     get_mpi_datatype(CharType()),l);
100     }
101 
102 private:
103 
load_impl(void * p,MPI_Datatype t,int l)104     void load_impl(void * p, MPI_Datatype t, int l)
105     {
106       BOOST_MPI_CHECK_RESULT(MPI_Unpack,
107                              (const_cast<char*>(detail::c_data(buffer_)), buffer_.size(), &position, p, l, t, comm));
108     }
109 
110     buffer_type & buffer_;
111     mutable std::size_t size_;
112     MPI_Comm comm;
113     int position;
114 };
115 
116 } } // end namespace boost::mpi
117 
118 #endif // BOOST_MPI_PACKED_IPRIMITIVE_HPP
119