1 // Copyright 2004 The Trustees of Indiana University.
2 // Copyright 2005 Matthias Troyer.
3 // Copyright 2006 Douglas Gregor <doug.gregor -at- gmail.com>.
5 // Use, modification and distribution is subject to the Boost Software
6 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // Authors: Douglas Gregor
13 /** @file datatype.hpp
15 * This header provides the mapping from C++ types to MPI data types.
17 #ifndef BOOST_MPI_DATATYPE_HPP
18 #define BOOST_MPI_DATATYPE_HPP
20 #include <boost/mpi/config.hpp>
21 #include <boost/mpi/datatype_fwd.hpp>
23 #include <boost/config.hpp>
24 #include <boost/mpl/bool.hpp>
25 #include <boost/mpl/or.hpp>
26 #include <boost/mpl/and.hpp>
27 #include <boost/mpi/detail/mpi_datatype_cache.hpp>
28 #include <boost/mpl/assert.hpp>
29 #include <boost/archive/basic_archive.hpp>
30 #include <boost/serialization/item_version_type.hpp>
31 #include <utility> // for std::pair
33 #if defined(__cplusplus) && (201103L <= __cplusplus)
37 namespace boost { namespace mpi {
40 * @brief Type trait that determines if there exists a built-in
41 * integer MPI data type for a given C++ type.
43 * This type trait determines when there is a direct mapping from a
44 * C++ type to an MPI data type that is classified as an integer data
45 * type. See @c is_mpi_builtin_datatype for general information about
46 * built-in MPI data types.
49 struct is_mpi_integer_datatype
50 : public boost::mpl::false_ { };
53 * @brief Type trait that determines if there exists a built-in
54 * floating point MPI data type for a given C++ type.
56 * This type trait determines when there is a direct mapping from a
57 * C++ type to an MPI data type that is classified as a floating
58 * point data type. See @c is_mpi_builtin_datatype for general
59 * information about built-in MPI data types.
62 struct is_mpi_floating_point_datatype
63 : public boost::mpl::false_ { };
66 * @brief Type trait that determines if there exists a built-in
67 * logical MPI data type for a given C++ type.
69 * This type trait determines when there is a direct mapping from a
70 * C++ type to an MPI data type that is classified as an logical data
71 * type. See @c is_mpi_builtin_datatype for general information about
72 * built-in MPI data types.
75 struct is_mpi_logical_datatype
76 : public boost::mpl::false_ { };
79 * @brief Type trait that determines if there exists a built-in
80 * complex MPI data type for a given C++ type.
82 * This type trait determines when there is a direct mapping from a
83 * C++ type to an MPI data type that is classified as an complex data
84 * type. See @c is_mpi_builtin_datatype for general information about
85 * built-in MPI data types.
88 struct is_mpi_complex_datatype
89 : public boost::mpl::false_ { };
92 * @brief Type trait that determines if there exists a built-in
93 * byte MPI data type for a given C++ type.
95 * This type trait determines when there is a direct mapping from a
96 * C++ type to an MPI data type that is classified as an byte data
97 * type. See @c is_mpi_builtin_datatype for general information about
98 * built-in MPI data types.
101 struct is_mpi_byte_datatype
102 : public boost::mpl::false_ { };
104 /** @brief Type trait that determines if there exists a built-in MPI
105 * data type for a given C++ type.
107 * This type trait determines when there is a direct mapping from a
108 * C++ type to an MPI type. For instance, the C++ @c int type maps
109 * directly to the MPI type @c MPI_INT. When there is a direct
110 * mapping from the type @c T to an MPI type, @c
111 * is_mpi_builtin_datatype will derive from @c mpl::true_ and the MPI
112 * data type will be accessible via @c get_mpi_datatype.
114 * In general, users should not need to specialize this
115 * trait. However, if you have an additional C++ type that can map
116 * directly to only of MPI's built-in types, specialize either this
117 * trait or one of the traits corresponding to categories of MPI data
118 * types (@c is_mpi_integer_datatype, @c
119 * is_mpi_floating_point_datatype, @c is_mpi_logical_datatype, @c
120 * is_mpi_complex_datatype, or @c is_mpi_builtin_datatype). @c
121 * is_mpi_builtin_datatype derives @c mpl::true_ if any of the traits
122 * corresponding to MPI data type categories derived @c mpl::true_.
125 struct is_mpi_builtin_datatype
126 : boost::mpl::or_<is_mpi_integer_datatype<T>,
127 is_mpi_floating_point_datatype<T>,
128 is_mpi_logical_datatype<T>,
129 is_mpi_complex_datatype<T>,
130 is_mpi_byte_datatype<T> >
134 /** @brief Type trait that determines if a C++ type can be mapped to
137 * This type trait determines if it is possible to build an MPI data
138 * type that represents a C++ data type. When this is the case, @c
139 * is_mpi_datatype derives @c mpl::true_ and the MPI data type will
140 * be accessible via @c get_mpi_datatype.
142 * For any C++ type that maps to a built-in MPI data type (see @c
143 * is_mpi_builtin_datatype), @c is_mpi_data_type is trivially
144 * true. However, any POD ("Plain Old Data") type containing types
145 * that themselves can be represented by MPI data types can itself be
146 * represented as an MPI data type. For instance, a @c point3d class
147 * containing three @c double values can be represented as an MPI
148 * data type. To do so, first make the data type Serializable (using
149 * the Boost.Serialization library); then, specialize the @c
150 * is_mpi_datatype trait for the point type so that it will derive @c
154 * namespace boost { namespace mpi {
155 * template<> struct is_mpi_datatype<point>
156 * : public mpl::true_ { };
161 struct is_mpi_datatype
162 : public is_mpi_builtin_datatype<T>
166 /** @brief Returns an MPI data type for a C++ type.
168 * The function creates an MPI data type for the given object @c
169 * x. The first time it is called for a class @c T, the MPI data type
170 * is created and cached. Subsequent calls for objects of the same
171 * type @c T return the cached MPI data type. The type @c T must
172 * allow creation of an MPI data type. That is, it must be
173 * Serializable and @c is_mpi_datatype<T> must derive @c mpl::true_.
175 * For fundamental MPI types, a copy of the MPI data type of the MPI
176 * library is returned.
178 * Note that since the data types are cached, the caller should never
179 * call @c MPI_Type_free() for the MPI data type returned by this
182 * @param x for an optimized call, a constructed object of the type
183 * should be passed; otherwise, an object will be
184 * default-constructed.
186 * @returns The MPI data type corresponding to type @c T.
188 template<typename T> MPI_Datatype get_mpi_datatype(const T& x)
190 BOOST_MPL_ASSERT((is_mpi_datatype<T>));
191 return detail::mpi_datatype_cache().datatype(x);
194 // Don't parse this part when we're generating Doxygen documentation.
195 #ifndef BOOST_MPI_DOXYGEN
198 #define BOOST_MPI_DATATYPE(CppType, MPIType, Kind) \
200 inline MPI_Datatype \
201 get_mpi_datatype< CppType >(const CppType&) { return MPIType; } \
204 struct BOOST_JOIN(is_mpi_,BOOST_JOIN(Kind,_datatype))< CppType > \
205 : boost::mpl::true_ \
209 BOOST_MPI_DATATYPE(packed, MPI_PACKED, builtin);
212 BOOST_MPI_DATATYPE(char, MPI_CHAR, builtin);
215 BOOST_MPI_DATATYPE(short, MPI_SHORT, integer);
218 BOOST_MPI_DATATYPE(int, MPI_INT, integer);
221 BOOST_MPI_DATATYPE(long, MPI_LONG, integer);
224 BOOST_MPI_DATATYPE(float, MPI_FLOAT, floating_point);
227 BOOST_MPI_DATATYPE(double, MPI_DOUBLE, floating_point);
230 BOOST_MPI_DATATYPE(long double, MPI_LONG_DOUBLE, floating_point);
233 BOOST_MPI_DATATYPE(unsigned char, MPI_UNSIGNED_CHAR, builtin);
236 BOOST_MPI_DATATYPE(unsigned short, MPI_UNSIGNED_SHORT, integer);
239 BOOST_MPI_DATATYPE(unsigned, MPI_UNSIGNED, integer);
242 BOOST_MPI_DATATYPE(unsigned long, MPI_UNSIGNED_LONG, integer);
245 #define BOOST_MPI_LIST2(A, B) A, B
247 BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(float, int)>, MPI_FLOAT_INT,
250 BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(double, int)>, MPI_DOUBLE_INT,
253 BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(long double, int)>,
254 MPI_LONG_DOUBLE_INT, builtin);
256 BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(long, int>), MPI_LONG_INT,
259 BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(short, int>), MPI_SHORT_INT,
262 BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(int, int>), MPI_2INT, builtin);
263 #undef BOOST_MPI_LIST2
265 /// specialization of is_mpi_datatype for pairs
266 template <class T, class U>
267 struct is_mpi_datatype<std::pair<T,U> >
268 : public mpl::and_<is_mpi_datatype<T>,is_mpi_datatype<U> >
272 /// specialization of is_mpi_datatype for arrays
273 #if defined(__cplusplus) && (201103L <= __cplusplus)
274 template<class T, std::size_t N>
275 struct is_mpi_datatype<std::array<T, N> >
276 : public is_mpi_datatype<T>
281 // Define wchar_t specialization of is_mpi_datatype, if possible.
282 #if !defined(BOOST_NO_INTRINSIC_WCHAR_T) && \
283 (defined(MPI_WCHAR) || (defined(MPI_VERSION) && MPI_VERSION >= 2))
284 BOOST_MPI_DATATYPE(wchar_t, MPI_WCHAR, builtin);
287 // Define long long or __int64 specialization of is_mpi_datatype, if possible.
288 #if defined(BOOST_HAS_LONG_LONG) && \
289 (defined(MPI_LONG_LONG_INT) || (defined(MPI_VERSION) && MPI_VERSION >= 2))
290 BOOST_MPI_DATATYPE(long long, MPI_LONG_LONG_INT, builtin);
291 #elif defined(BOOST_HAS_MS_INT64) && \
292 (defined(MPI_LONG_LONG_INT) || (defined(MPI_VERSION) && MPI_VERSION >= 2))
293 BOOST_MPI_DATATYPE(__int64, MPI_LONG_LONG_INT, builtin);
296 // Define unsigned long long or unsigned __int64 specialization of
297 // is_mpi_datatype, if possible. We separate this from the check for
298 // the (signed) long long/__int64 because some MPI implementations
299 // (e.g., MPICH-MX) have MPI_LONG_LONG_INT but not
300 // MPI_UNSIGNED_LONG_LONG.
301 #if defined(BOOST_HAS_LONG_LONG) && \
302 (defined(MPI_UNSIGNED_LONG_LONG) \
303 || (defined(MPI_VERSION) && MPI_VERSION >= 2))
304 BOOST_MPI_DATATYPE(unsigned long long, MPI_UNSIGNED_LONG_LONG, builtin);
305 #elif defined(BOOST_HAS_MS_INT64) && \
306 (defined(MPI_UNSIGNED_LONG_LONG) \
307 || (defined(MPI_VERSION) && MPI_VERSION >= 2))
308 BOOST_MPI_DATATYPE(unsigned __int64, MPI_UNSIGNED_LONG_LONG, builtin);
311 // Define signed char specialization of is_mpi_datatype, if possible.
312 #if defined(MPI_SIGNED_CHAR) || (defined(MPI_VERSION) && MPI_VERSION >= 2)
313 BOOST_MPI_DATATYPE(signed char, MPI_SIGNED_CHAR, builtin);
320 inline MPI_Datatype build_mpi_datatype_for_bool()
323 MPI_Type_contiguous(sizeof(bool), MPI_BYTE, &type);
324 MPI_Type_commit(&type);
329 /// Support for bool. There is no corresponding MPI_BOOL.
332 inline MPI_Datatype get_mpi_datatype<bool>(const bool&)
334 static MPI_Datatype type = detail::build_mpi_datatype_for_bool();
340 struct is_mpi_datatype<bool>
341 : boost::mpl::bool_<true>
345 #ifndef BOOST_MPI_DOXYGEN
346 // direct support for special primitive data types of the serialization library
347 BOOST_MPI_DATATYPE(boost::archive::library_version_type, get_mpi_datatype(uint_least16_t()), integer);
348 BOOST_MPI_DATATYPE(boost::archive::version_type, get_mpi_datatype(uint_least8_t()), integer);
349 BOOST_MPI_DATATYPE(boost::archive::class_id_type, get_mpi_datatype(int_least16_t()), integer);
350 BOOST_MPI_DATATYPE(boost::archive::class_id_reference_type, get_mpi_datatype(int_least16_t()), integer);
351 BOOST_MPI_DATATYPE(boost::archive::class_id_optional_type, get_mpi_datatype(int_least16_t()), integer);
352 BOOST_MPI_DATATYPE(boost::archive::object_id_type, get_mpi_datatype(uint_least32_t()), integer);
353 BOOST_MPI_DATATYPE(boost::archive::object_reference_type, get_mpi_datatype(uint_least32_t()), integer);
354 BOOST_MPI_DATATYPE(boost::archive::tracking_type, get_mpi_datatype(bool()), builtin);
355 BOOST_MPI_DATATYPE(boost::serialization::collection_size_type, get_mpi_datatype(std::size_t()), integer);
356 BOOST_MPI_DATATYPE(boost::serialization::item_version_type, get_mpi_datatype(uint_least8_t()), integer);
360 } } // end namespace boost::mpi
362 // direct support for special primitive data types of the serialization library
363 // in the case of homogeneous systems
364 // define a macro to make explicit designation of this more transparent
365 #define BOOST_IS_MPI_DATATYPE(T) \
369 struct is_mpi_datatype< T > : mpl::true_ {}; \
374 #endif // BOOST_MPI_MPI_DATATYPE_HPP