PageRenderTime 39ms CodeModel.GetById 25ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/mpi/datatype.hpp

http://hadesmem.googlecode.com/
C++ Header | 361 lines | 152 code | 45 blank | 164 comment | 17 complexity | cc131fd4dded9646fc826e72a381a375 MD5 | raw file
  1// Copyright 2004 The Trustees of Indiana University.
  2// Copyright 2005 Matthias Troyer.
  3// Copyright 2006 Douglas Gregor <doug.gregor -at- gmail.com>.
  4
  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)
  8
  9//  Authors: Douglas Gregor
 10//           Andrew Lumsdaine
 11//           Matthias Troyer
 12
 13/** @file datatype.hpp
 14 *
 15 *  This header provides the mapping from C++ types to MPI data types.
 16 */
 17#ifndef BOOST_MPI_DATATYPE_HPP
 18#define BOOST_MPI_DATATYPE_HPP
 19
 20#include <boost/mpi/config.hpp>
 21#include <boost/mpi/datatype_fwd.hpp>
 22#include <mpi.h>
 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
 32
 33namespace boost { namespace mpi {
 34
 35/**
 36 *  @brief Type trait that determines if there exists a built-in
 37 *  integer MPI data type for a given C++ type.
 38 *
 39 *  This ytpe trait determines when there is a direct mapping from a
 40 *  C++ type to an MPI data type that is classified as an integer data
 41 *  type. See @c is_mpi_builtin_datatype for general information about
 42 *  built-in MPI data types.
 43 */
 44template<typename T>
 45struct is_mpi_integer_datatype
 46  : public boost::mpl::false_ { };
 47
 48/**
 49 *  @brief Type trait that determines if there exists a built-in
 50 *  floating point MPI data type for a given C++ type.
 51 *
 52 *  This ytpe trait determines when there is a direct mapping from a
 53 *  C++ type to an MPI data type that is classified as a floating
 54 *  point data type. See @c is_mpi_builtin_datatype for general
 55 *  information about built-in MPI data types.
 56 */
 57template<typename T>
 58struct is_mpi_floating_point_datatype
 59  : public boost::mpl::false_ { };
 60
 61/**
 62 *  @brief Type trait that determines if there exists a built-in
 63 *  logical MPI data type for a given C++ type.
 64 *
 65 *  This ytpe trait determines when there is a direct mapping from a
 66 *  C++ type to an MPI data type that is classified as an logical data
 67 *  type. See @c is_mpi_builtin_datatype for general information about
 68 *  built-in MPI data types.
 69 */
 70template<typename T>
 71struct is_mpi_logical_datatype
 72  : public boost::mpl::false_ { };
 73
 74/**
 75 *  @brief Type trait that determines if there exists a built-in
 76 *  complex MPI data type for a given C++ type.
 77 *
 78 *  This ytpe trait determines when there is a direct mapping from a
 79 *  C++ type to an MPI data type that is classified as an complex data
 80 *  type. See @c is_mpi_builtin_datatype for general information about
 81 *  built-in MPI data types.
 82 */
 83template<typename T>
 84struct is_mpi_complex_datatype
 85  : public boost::mpl::false_ { };
 86
 87/**
 88 *  @brief Type trait that determines if there exists a built-in
 89 *  byte MPI data type for a given C++ type.
 90 *
 91 *  This ytpe trait determines when there is a direct mapping from a
 92 *  C++ type to an MPI data type that is classified as an byte data
 93 *  type. See @c is_mpi_builtin_datatype for general information about
 94 *  built-in MPI data types.
 95 */
 96template<typename T>
 97struct is_mpi_byte_datatype
 98  : public boost::mpl::false_ { };
 99
100/** @brief Type trait that determines if there exists a built-in MPI
101 *  data type for a given C++ type.
102 *
103 *  This type trait determines when there is a direct mapping from a
104 *  C++ type to an MPI type. For instance, the C++ @c int type maps
105 *  directly to the MPI type @c MPI_INT. When there is a direct
106 *  mapping from the type @c T to an MPI type, @c
107 *  is_mpi_builtin_datatype will derive from @c mpl::true_ and the MPI
108 *  data type will be accessible via @c get_mpi_datatype. 
109 *
110 *  In general, users should not need to specialize this
111 *  trait. However, if you have an additional C++ type that can map
112 *  directly to only of MPI's built-in types, specialize either this
113 *  trait or one of the traits corresponding to categories of MPI data
114 *  types (@c is_mpi_integer_datatype, @c
115 *  is_mpi_floating_point_datatype, @c is_mpi_logical_datatype, @c
116 *  is_mpi_complex_datatype, or @c is_mpi_builtin_datatype). @c
117 *  is_mpi_builtin_datatype derives @c mpl::true_ if any of the traits
118 *  corresponding to MPI data type categories derived @c mpl::true_.
119 */
120template<typename T>
121struct is_mpi_builtin_datatype
122  : boost::mpl::or_<is_mpi_integer_datatype<T>,
123                    is_mpi_floating_point_datatype<T>,
124                    is_mpi_logical_datatype<T>,
125                    is_mpi_complex_datatype<T>,
126                    is_mpi_byte_datatype<T> >
127{
128};
129
130/** @brief Type trait that determines if a C++ type can be mapped to
131 *  an MPI data type.
132 *
133 *  This type trait determines if it is possible to build an MPI data
134 *  type that represents a C++ data type. When this is the case, @c
135 *  is_mpi_datatype derives @c mpl::true_ and the MPI data type will
136 *  be accessible via @c get_mpi_datatype.
137
138 *  For any C++ type that maps to a built-in MPI data type (see @c
139 *  is_mpi_builtin_datatype), @c is_mpi_data_type is trivially
140 *  true. However, any POD ("Plain Old Data") type containing types
141 *  that themselves can be represented by MPI data types can itself be
142 *  represented as an MPI data type. For instance, a @c point3d class
143 *  containing three @c double values can be represented as an MPI
144 *  data type. To do so, first make the data type Serializable (using
145 *  the Boost.Serialization library); then, specialize the @c
146 *  is_mpi_datatype trait for the point type so that it will derive @c
147 *  mpl::true_:
148 *
149 *    @code
150 *    namespace boost { namespace mpi {
151 *      template<> struct is_mpi_datatype<point>
152 *        : public mpl::true_ { };
153 *    } }
154 *    @endcode
155 */
156template<typename T>
157struct is_mpi_datatype
158 : public is_mpi_builtin_datatype<T>
159{
160};
161
162/** @brief Returns an MPI data type for a C++ type.
163 *
164 *  The function creates an MPI data type for the given object @c
165 *  x. The first time it is called for a class @c T, the MPI data type
166 *  is created and cached. Subsequent calls for objects of the same
167 *  type @c T return the cached MPI data type.  The type @c T must
168 *  allow creation of an MPI data type. That is, it must be
169 *  Serializable and @c is_mpi_datatype<T> must derive @c mpl::true_.
170 *
171 *  For fundamental MPI types, a copy of the MPI data type of the MPI
172 *  library is returned.
173 *
174 *  Note that since the data types are cached, the caller should never
175 *  call @c MPI_Type_free() for the MPI data type returned by this
176 *  call.
177 *
178 *  @param x for an optimized call, a constructed object of the type
179 *  should be passed; otherwise, an object will be
180 *  default-constructed.
181 *
182 *  @returns The MPI data type corresponding to type @c T.
183 */
184template<typename T> MPI_Datatype get_mpi_datatype(const T& x)
185{
186  BOOST_MPL_ASSERT((is_mpi_datatype<T>));
187  return detail::mpi_datatype_cache().datatype(x);
188}
189
190// Don't parse this part when we're generating Doxygen documentation.
191#ifndef BOOST_MPI_DOXYGEN
192
193/// INTERNAL ONLY
194#define BOOST_MPI_DATATYPE(CppType, MPIType, Kind)                      \
195template<>                                                              \
196inline MPI_Datatype                                                     \
197get_mpi_datatype< CppType >(const CppType&) { return MPIType; }         \
198                                                                        \
199template<>                                                              \
200 struct BOOST_JOIN(is_mpi_,BOOST_JOIN(Kind,_datatype))< CppType >       \
201: boost::mpl::bool_<true>                                               \
202{}
203
204/// INTERNAL ONLY
205BOOST_MPI_DATATYPE(packed, MPI_PACKED, builtin);
206
207/// INTERNAL ONLY
208BOOST_MPI_DATATYPE(char, MPI_CHAR, builtin);
209
210/// INTERNAL ONLY
211BOOST_MPI_DATATYPE(short, MPI_SHORT, integer);
212
213/// INTERNAL ONLY
214BOOST_MPI_DATATYPE(int, MPI_INT, integer);
215
216/// INTERNAL ONLY
217BOOST_MPI_DATATYPE(long, MPI_LONG, integer);
218
219/// INTERNAL ONLY
220BOOST_MPI_DATATYPE(float, MPI_FLOAT, floating_point);
221
222/// INTERNAL ONLY
223BOOST_MPI_DATATYPE(double, MPI_DOUBLE, floating_point);
224
225/// INTERNAL ONLY
226BOOST_MPI_DATATYPE(long double, MPI_LONG_DOUBLE, floating_point);
227
228/// INTERNAL ONLY
229BOOST_MPI_DATATYPE(unsigned char, MPI_UNSIGNED_CHAR, builtin);
230
231/// INTERNAL ONLY
232BOOST_MPI_DATATYPE(unsigned short, MPI_UNSIGNED_SHORT, integer);
233
234/// INTERNAL ONLY
235BOOST_MPI_DATATYPE(unsigned, MPI_UNSIGNED, integer);
236
237/// INTERNAL ONLY
238BOOST_MPI_DATATYPE(unsigned long, MPI_UNSIGNED_LONG, integer);
239
240/// INTERNAL ONLY
241#define BOOST_MPI_LIST2(A, B) A, B
242/// INTERNAL ONLY
243BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(float, int)>, MPI_FLOAT_INT, 
244                   builtin);
245/// INTERNAL ONLY
246BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(double, int)>, MPI_DOUBLE_INT, 
247                   builtin);
248/// INTERNAL ONLY
249BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(long double, int)>,
250                   MPI_LONG_DOUBLE_INT, builtin);
251/// INTERNAL ONLY
252BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(long, int>), MPI_LONG_INT, 
253                   builtin);
254/// INTERNAL ONLY
255BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(short, int>), MPI_SHORT_INT, 
256                   builtin);
257/// INTERNAL ONLY
258BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(int, int>), MPI_2INT, builtin);
259#undef BOOST_MPI_LIST2
260
261/// specialization of is_mpi_datatype for pairs
262template <class T, class U>
263struct is_mpi_datatype<std::pair<T,U> >
264 : public mpl::and_<is_mpi_datatype<T>,is_mpi_datatype<U> >
265{
266};
267
268// Define wchar_t specialization of is_mpi_datatype, if possible.
269#if !defined(BOOST_NO_INTRINSIC_WCHAR_T) && \
270  (defined(MPI_WCHAR) || (defined(MPI_VERSION) && MPI_VERSION >= 2))
271BOOST_MPI_DATATYPE(wchar_t, MPI_WCHAR, builtin);
272#endif
273
274// Define long long or __int64 specialization of is_mpi_datatype, if possible.
275#if defined(BOOST_HAS_LONG_LONG) && \
276  (defined(MPI_LONG_LONG_INT) || (defined(MPI_VERSION) && MPI_VERSION >= 2))
277BOOST_MPI_DATATYPE(long long, MPI_LONG_LONG_INT, builtin);
278#elif defined(BOOST_HAS_MS_INT64) && \
279  (defined(MPI_LONG_LONG_INT) || (defined(MPI_VERSION) && MPI_VERSION >= 2))
280BOOST_MPI_DATATYPE(__int64, MPI_LONG_LONG_INT, builtin); 
281#endif
282
283// Define unsigned long long or unsigned __int64 specialization of
284// is_mpi_datatype, if possible. We separate this from the check for
285// the (signed) long long/__int64 because some MPI implementations
286// (e.g., MPICH-MX) have MPI_LONG_LONG_INT but not
287// MPI_UNSIGNED_LONG_LONG.
288#if defined(BOOST_HAS_LONG_LONG) && \
289  (defined(MPI_UNSIGNED_LONG_LONG) \
290   || (defined(MPI_VERSION) && MPI_VERSION >= 2))
291BOOST_MPI_DATATYPE(unsigned long long, MPI_UNSIGNED_LONG_LONG, builtin);
292#elif defined(BOOST_HAS_MS_INT64) && \
293  (defined(MPI_UNSIGNED_LONG_LONG) \
294   || (defined(MPI_VERSION) && MPI_VERSION >= 2))
295BOOST_MPI_DATATYPE(unsigned __int64, MPI_UNSIGNED_LONG_LONG, builtin); 
296#endif
297
298// Define signed char specialization of is_mpi_datatype, if possible.
299#if defined(MPI_SIGNED_CHAR) || (defined(MPI_VERSION) && MPI_VERSION >= 2)
300BOOST_MPI_DATATYPE(signed char, MPI_SIGNED_CHAR, builtin);
301#endif
302
303
304#endif // Doxygen
305
306namespace detail {
307  inline MPI_Datatype build_mpi_datatype_for_bool()
308  {
309    MPI_Datatype type;
310    MPI_Type_contiguous(sizeof(bool), MPI_BYTE, &type);
311    MPI_Type_commit(&type);
312    return type;
313  }
314}
315
316/// Support for bool. There is no corresponding MPI_BOOL.
317/// INTERNAL ONLY
318template<>
319inline MPI_Datatype get_mpi_datatype<bool>(const bool&)
320{
321  static MPI_Datatype type = detail::build_mpi_datatype_for_bool();
322  return type;
323}
324
325/// INTERNAL ONLY
326template<>
327struct is_mpi_datatype<bool>
328  : boost::mpl::bool_<true>
329{};
330
331
332#ifndef BOOST_MPI_DOXYGEN
333// direct support for special primitive data types of the serialization library
334BOOST_MPI_DATATYPE(boost::archive::library_version_type, get_mpi_datatype(uint_least16_t()), integer);
335BOOST_MPI_DATATYPE(boost::archive::version_type, get_mpi_datatype(uint_least8_t()), integer);
336BOOST_MPI_DATATYPE(boost::archive::class_id_type, get_mpi_datatype(int_least16_t()), integer);
337BOOST_MPI_DATATYPE(boost::archive::class_id_reference_type, get_mpi_datatype(int_least16_t()), integer);
338BOOST_MPI_DATATYPE(boost::archive::class_id_optional_type, get_mpi_datatype(int_least16_t()), integer);
339BOOST_MPI_DATATYPE(boost::archive::object_id_type, get_mpi_datatype(uint_least32_t()), integer);
340BOOST_MPI_DATATYPE(boost::archive::object_reference_type, get_mpi_datatype(uint_least32_t()), integer);
341BOOST_MPI_DATATYPE(boost::archive::tracking_type, get_mpi_datatype(bool()), builtin);
342BOOST_MPI_DATATYPE(boost::serialization::collection_size_type, get_mpi_datatype(std::size_t()), integer);
343BOOST_MPI_DATATYPE(boost::serialization::item_version_type, get_mpi_datatype(uint_least8_t()), integer);
344#endif // Doxygen
345
346
347} } // end namespace boost::mpi
348
349// direct support for special primitive data types of the serialization library
350// in the case of homogeneous systems
351// define a macro to make explicit designation of this more transparent
352#define BOOST_IS_MPI_DATATYPE(T)              \
353namespace boost {                             \
354namespace mpi {                               \
355template<>                                    \
356struct is_mpi_datatype< T > : mpl::true_ {};  \
357}}                                            \
358/**/
359
360
361#endif // BOOST_MPI_MPI_DATATYPE_HPP