1 // (C) Copyright 2005 Matthias Troyer
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)
7 // Authors: Matthias Troyer
9 #ifndef BOOST_MPI_DETAIL_MPI_DATATYPE_OPRIMITIVE_HPP
10 #define BOOST_MPI_DETAIL_MPI_DATATYPE_OPRIMITIVE_HPP
12 #include <boost/mpi/config.hpp>
13 #include <cstddef> // size_t
15 #include <boost/config.hpp>
16 #if defined(BOOST_NO_STDC_NAMESPACE)
22 #include <boost/mpi/datatype_fwd.hpp>
23 #include <boost/mpi/exception.hpp>
24 #include <boost/mpi/detail/antiques.hpp>
25 #include <boost/throw_exception.hpp>
26 #include <boost/assert.hpp>
27 #include <boost/mpl/placeholders.hpp>
28 #include <boost/serialization/array.hpp>
32 #include <boost/mpi/detail/antiques.hpp>
34 namespace boost { namespace mpi { namespace detail {
36 /////////////////////////////////////////////////////////////////////////
37 // class mpi_data_type_oprimitive - creation of custom MPI data types
39 class mpi_datatype_primitive
43 // trivial default constructor
44 mpi_datatype_primitive()
45 : is_committed(false),
49 mpi_datatype_primitive(void const* orig)
50 : is_committed(false),
53 #if BOOST_MPI_VERSION >= 2
54 BOOST_MPI_CHECK_RESULT(MPI_Get_address,(const_cast<void*>(orig), &origin));
56 BOOST_MPI_CHECK_RESULT(MPI_Address,(const_cast<void*>(orig), &origin));
60 void save_binary(void const *address, std::size_t count)
62 save_impl(address,MPI_BYTE,count);
65 // fast saving of arrays of MPI types
67 void save_array(serialization::array_wrapper<T> const& x, unsigned int /* version */)
70 save_impl(x.address(), boost::mpi::get_mpi_datatype(*x.address()), x.count());
73 typedef is_mpi_datatype<mpl::_1> use_array_optimization;
75 // create and return the custom MPI data type
76 MPI_Datatype get_mpi_datatype()
80 #if BOOST_MPI_VERSION >= 2
81 BOOST_MPI_CHECK_RESULT(MPI_Type_create_struct,
90 BOOST_MPI_CHECK_RESULT(MPI_Type_struct,
99 BOOST_MPI_CHECK_RESULT(MPI_Type_commit,(&datatype_));
107 // default saving of primitives.
109 void save(const T & t)
111 save_impl(&t, boost::mpi::get_mpi_datatype(t), 1);
116 void save_impl(void const * p, MPI_Datatype t, int l)
118 BOOST_ASSERT ( !is_committed );
120 // store address, type and length
123 #if BOOST_MPI_VERSION >= 2
124 BOOST_MPI_CHECK_RESULT(MPI_Get_address,(const_cast<void*>(p), &a));
126 BOOST_MPI_CHECK_RESULT(MPI_Address,(const_cast<void*>(p), &a));
128 addresses.push_back(a-origin);
130 lengths.push_back(l);
134 static T* get_data(std::vector<T>& v)
136 return v.empty() ? 0 : &(v[0]);
139 std::vector<MPI_Aint> addresses;
140 std::vector<MPI_Datatype> types;
141 std::vector<int> lengths;
144 MPI_Datatype datatype_;
149 } } } // end namespace boost::mpi::detail
152 #endif // BOOST_MPI_DETAIL_MPI_DATATYPE_OPRIMITIVE_HPP