Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:37:09

0001 // Copyright 2004 The Trustees of Indiana University.
0002 // Copyright 2005 Matthias Troyer.
0003 // Copyright 2006 Douglas Gregor <doug.gregor -at- gmail.com>.
0004 
0005 // Use, modification and distribution is subject to the Boost Software
0006 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
0007 // http://www.boost.org/LICENSE_1_0.txt)
0008 
0009 //  Authors: Douglas Gregor
0010 //           Andrew Lumsdaine
0011 //           Matthias Troyer
0012 
0013 /** @file datatype.hpp
0014  *
0015  *  This header provides the mapping from C++ types to MPI data types.
0016  */
0017 #ifndef BOOST_MPI_DATATYPE_HPP
0018 #define BOOST_MPI_DATATYPE_HPP
0019 
0020 #include <boost/mpi/config.hpp>
0021 #include <boost/mpi/datatype_fwd.hpp>
0022 #include <mpi.h>
0023 #include <boost/config.hpp>
0024 #include <boost/mpl/bool.hpp>
0025 #include <boost/mpl/or.hpp>
0026 #include <boost/mpl/and.hpp>
0027 #include <boost/mpi/detail/mpi_datatype_cache.hpp>
0028 #include <boost/mpl/assert.hpp>
0029 #include <boost/archive/basic_archive.hpp>
0030 #include <boost/serialization/library_version_type.hpp>
0031 #include <boost/serialization/item_version_type.hpp>
0032 #include <utility> // for std::pair
0033 
0034 #if defined(__cplusplus) && (201103L <= __cplusplus) 
0035 #include <array>
0036 #endif
0037 
0038 namespace boost { namespace mpi {
0039 
0040 /**
0041  *  @brief Type trait that determines if there exists a built-in
0042  *  integer MPI data type for a given C++ type.
0043  *
0044  *  This type trait determines when there is a direct mapping from a
0045  *  C++ type to an MPI data type that is classified as an integer data
0046  *  type. See @c is_mpi_builtin_datatype for general information about
0047  *  built-in MPI data types.
0048  */
0049 template<typename T>
0050 struct is_mpi_integer_datatype
0051   : public boost::mpl::false_ { };
0052 
0053 /**
0054  *  @brief Type trait that determines if there exists a built-in
0055  *  floating point MPI data type for a given C++ type.
0056  *
0057  *  This type trait determines when there is a direct mapping from a
0058  *  C++ type to an MPI data type that is classified as a floating
0059  *  point data type. See @c is_mpi_builtin_datatype for general
0060  *  information about built-in MPI data types.
0061  */
0062 template<typename T>
0063 struct is_mpi_floating_point_datatype
0064   : public boost::mpl::false_ { };
0065 
0066 /**
0067  *  @brief Type trait that determines if there exists a built-in
0068  *  logical MPI data type for a given C++ type.
0069  *
0070  *  This type trait determines when there is a direct mapping from a
0071  *  C++ type to an MPI data type that is classified as an logical data
0072  *  type. See @c is_mpi_builtin_datatype for general information about
0073  *  built-in MPI data types.
0074  */
0075 template<typename T>
0076 struct is_mpi_logical_datatype
0077   : public boost::mpl::false_ { };
0078 
0079 /**
0080  *  @brief Type trait that determines if there exists a built-in
0081  *  complex MPI data type for a given C++ type.
0082  *
0083  *  This type trait determines when there is a direct mapping from a
0084  *  C++ type to an MPI data type that is classified as a complex data
0085  *  type. See @c is_mpi_builtin_datatype for general information about
0086  *  built-in MPI data types.
0087  */
0088 template<typename T>
0089 struct is_mpi_complex_datatype
0090   : public boost::mpl::false_ { };
0091 
0092 /**
0093  *  @brief Type trait that determines if there exists a built-in
0094  *  byte MPI data type for a given C++ type.
0095  *
0096  *  This type trait determines when there is a direct mapping from a
0097  *  C++ type to an MPI data type that is classified as an byte data
0098  *  type. See @c is_mpi_builtin_datatype for general information about
0099  *  built-in MPI data types.
0100  */
0101 template<typename T>
0102 struct is_mpi_byte_datatype
0103   : public boost::mpl::false_ { };
0104 
0105 /** @brief Type trait that determines if there exists a built-in MPI
0106  *  data type for a given C++ type.
0107  *
0108  *  This type trait determines when there is a direct mapping from a
0109  *  C++ type to an MPI type. For instance, the C++ @c int type maps
0110  *  directly to the MPI type @c MPI_INT. When there is a direct
0111  *  mapping from the type @c T to an MPI type, @c
0112  *  is_mpi_builtin_datatype will derive from @c mpl::true_ and the MPI
0113  *  data type will be accessible via @c get_mpi_datatype. 
0114  *
0115  *  In general, users should not need to specialize this
0116  *  trait. However, if you have an additional C++ type that can map
0117  *  directly to only of MPI's built-in types, specialize either this
0118  *  trait or one of the traits corresponding to categories of MPI data
0119  *  types (@c is_mpi_integer_datatype, @c
0120  *  is_mpi_floating_point_datatype, @c is_mpi_logical_datatype, @c
0121  *  is_mpi_complex_datatype, or @c is_mpi_builtin_datatype). @c
0122  *  is_mpi_builtin_datatype derives @c mpl::true_ if any of the traits
0123  *  corresponding to MPI data type categories derived @c mpl::true_.
0124  */
0125 template<typename T>
0126 struct is_mpi_builtin_datatype
0127   : boost::mpl::or_<is_mpi_integer_datatype<T>,
0128                     is_mpi_floating_point_datatype<T>,
0129                     is_mpi_logical_datatype<T>,
0130                     is_mpi_complex_datatype<T>,
0131                     is_mpi_byte_datatype<T> >
0132 {
0133 };
0134 
0135 /** @brief Type trait that determines if a C++ type can be mapped to
0136  *  an MPI data type.
0137  *
0138  *  This type trait determines if it is possible to build an MPI data
0139  *  type that represents a C++ data type. When this is the case, @c
0140  *  is_mpi_datatype derives @c mpl::true_ and the MPI data type will
0141  *  be accessible via @c get_mpi_datatype.
0142 
0143  *  For any C++ type that maps to a built-in MPI data type (see @c
0144  *  is_mpi_builtin_datatype), @c is_mpi_datatype is trivially
0145  *  true. However, any POD ("Plain Old Data") type containing types
0146  *  that themselves can be represented by MPI data types can itself be
0147  *  represented as an MPI data type. For instance, a @c point3d class
0148  *  containing three @c double values can be represented as an MPI
0149  *  data type. To do so, first make the data type Serializable (using
0150  *  the Boost.Serialization library); then, specialize the @c
0151  *  is_mpi_datatype trait for the point type so that it will derive @c
0152  *  mpl::true_:
0153  *
0154  *    @code
0155  *    namespace boost { namespace mpi {
0156  *      template<> struct is_mpi_datatype<point>
0157  *        : public mpl::true_ { };
0158  *    } }
0159  *    @endcode
0160  */
0161 template<typename T>
0162 struct is_mpi_datatype
0163  : public is_mpi_builtin_datatype<T>
0164 {
0165 };
0166 
0167 /** @brief Returns an MPI data type for a C++ type.
0168  *
0169  *  The function creates an MPI data type for the given object @c
0170  *  x. The first time it is called for a class @c T, the MPI data type
0171  *  is created and cached. Subsequent calls for objects of the same
0172  *  type @c T return the cached MPI data type.  The type @c T must
0173  *  allow creation of an MPI data type. That is, it must be
0174  *  Serializable and @c is_mpi_datatype<T> must derive @c mpl::true_.
0175  *
0176  *  For fundamental MPI types, a copy of the MPI data type of the MPI
0177  *  library is returned.
0178  *
0179  *  Note that since the data types are cached, the caller should never
0180  *  call @c MPI_Type_free() for the MPI data type returned by this
0181  *  call.
0182  *
0183  *  @param x for an optimized call, a constructed object of the type
0184  *  should be passed; otherwise, an object will be
0185  *  default-constructed.
0186  *
0187  *  @returns The MPI data type corresponding to type @c T.
0188  */
0189 template<typename T> MPI_Datatype get_mpi_datatype(const T& x)
0190 {
0191   BOOST_MPL_ASSERT((is_mpi_datatype<T>));
0192   return detail::mpi_datatype_cache().datatype(x);
0193 }
0194 
0195 // Don't parse this part when we're generating Doxygen documentation.
0196 #ifndef BOOST_MPI_DOXYGEN
0197 
0198 /// INTERNAL ONLY
0199 #define BOOST_MPI_DATATYPE(CppType, MPIType, Kind)                      \
0200 template<>                                                              \
0201 inline MPI_Datatype                                                     \
0202 get_mpi_datatype< CppType >(const CppType&) { return MPIType; }         \
0203                                                                         \
0204 template<>                                                              \
0205  struct BOOST_JOIN(is_mpi_,BOOST_JOIN(Kind,_datatype))< CppType >       \
0206 : boost::mpl::true_                                                     \
0207 {}
0208 
0209 /// INTERNAL ONLY
0210 BOOST_MPI_DATATYPE(packed, MPI_PACKED, builtin);
0211 
0212 /// INTERNAL ONLY
0213 BOOST_MPI_DATATYPE(char, MPI_CHAR, builtin);
0214 
0215 /// INTERNAL ONLY
0216 /// We need to pick a boolean type, MPI_CXX_BOOL seems appropriate
0217 BOOST_MPI_DATATYPE(bool, MPI_CXX_BOOL, logical);
0218 
0219 /// INTERNAL ONLY
0220 BOOST_MPI_DATATYPE(short, MPI_SHORT, integer);
0221 
0222 /// INTERNAL ONLY
0223 BOOST_MPI_DATATYPE(int, MPI_INT, integer);
0224 
0225 /// INTERNAL ONLY
0226 BOOST_MPI_DATATYPE(long, MPI_LONG, integer);
0227 
0228 /// INTERNAL ONLY
0229 BOOST_MPI_DATATYPE(float, MPI_FLOAT, floating_point);
0230 
0231 /// INTERNAL ONLY
0232 BOOST_MPI_DATATYPE(double, MPI_DOUBLE, floating_point);
0233 
0234 /// INTERNAL ONLY
0235 BOOST_MPI_DATATYPE(long double, MPI_LONG_DOUBLE, floating_point);
0236 
0237 /// INTERNAL ONLY
0238 BOOST_MPI_DATATYPE(unsigned char, MPI_UNSIGNED_CHAR, builtin);
0239 
0240 /// INTERNAL ONLY
0241 BOOST_MPI_DATATYPE(unsigned short, MPI_UNSIGNED_SHORT, integer);
0242 
0243 /// INTERNAL ONLY
0244 BOOST_MPI_DATATYPE(unsigned, MPI_UNSIGNED, integer);
0245 
0246 /// INTERNAL ONLY
0247 BOOST_MPI_DATATYPE(unsigned long, MPI_UNSIGNED_LONG, integer);
0248 
0249 /// INTERNAL ONLY
0250 #define BOOST_MPI_LIST2(A, B) A, B
0251 /// INTERNAL ONLY
0252 BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(float, int)>, MPI_FLOAT_INT, 
0253                    builtin);
0254 /// INTERNAL ONLY
0255 BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(double, int)>, MPI_DOUBLE_INT, 
0256                    builtin);
0257 /// INTERNAL ONLY
0258 BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(long double, int)>,
0259                    MPI_LONG_DOUBLE_INT, builtin);
0260 /// INTERNAL ONLY
0261 BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(long, int>), MPI_LONG_INT, 
0262                    builtin);
0263 /// INTERNAL ONLY
0264 BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(short, int>), MPI_SHORT_INT, 
0265                    builtin);
0266 /// INTERNAL ONLY
0267 BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(int, int>), MPI_2INT, builtin);
0268 #undef BOOST_MPI_LIST2
0269 
0270 /// specialization of is_mpi_datatype for pairs
0271 template <class T, class U>
0272 struct is_mpi_datatype<std::pair<T,U> >
0273  : public mpl::and_<is_mpi_datatype<T>,is_mpi_datatype<U> >
0274 {
0275 };
0276 
0277 /// specialization of is_mpi_datatype for arrays
0278 #if defined(__cplusplus) && (201103L <= __cplusplus)
0279 template<class T, std::size_t N>
0280 struct is_mpi_datatype<std::array<T, N> >
0281  : public is_mpi_datatype<T>
0282 {
0283 };
0284 #endif
0285 
0286 // Define wchar_t specialization of is_mpi_datatype, if possible.
0287 #if !defined(BOOST_NO_INTRINSIC_WCHAR_T) && \
0288   (defined(MPI_WCHAR) || (BOOST_MPI_VERSION >= 2))
0289 BOOST_MPI_DATATYPE(wchar_t, MPI_WCHAR, builtin);
0290 #endif
0291 
0292 // Define long long or __int64 specialization of is_mpi_datatype, if possible.
0293 #if defined(BOOST_HAS_LONG_LONG) && \
0294   (defined(MPI_LONG_LONG_INT) || (BOOST_MPI_VERSION >= 2))
0295 BOOST_MPI_DATATYPE(long long, MPI_LONG_LONG_INT, builtin);
0296 #elif defined(BOOST_HAS_MS_INT64) && \
0297   (defined(MPI_LONG_LONG_INT) || (BOOST_MPI_VERSION >= 2))
0298 BOOST_MPI_DATATYPE(__int64, MPI_LONG_LONG_INT, builtin); 
0299 #endif
0300 
0301 // Define unsigned long long or unsigned __int64 specialization of
0302 // is_mpi_datatype, if possible. We separate this from the check for
0303 // the (signed) long long/__int64 because some MPI implementations
0304 // (e.g., MPICH-MX) have MPI_LONG_LONG_INT but not
0305 // MPI_UNSIGNED_LONG_LONG.
0306 #if defined(BOOST_HAS_LONG_LONG) && \
0307   (defined(MPI_UNSIGNED_LONG_LONG) \
0308    || (BOOST_MPI_VERSION >= 2))
0309 BOOST_MPI_DATATYPE(unsigned long long, MPI_UNSIGNED_LONG_LONG, builtin);
0310 #elif defined(BOOST_HAS_MS_INT64) && \
0311   (defined(MPI_UNSIGNED_LONG_LONG) \
0312    || (BOOST_MPI_VERSION >= 2))
0313 BOOST_MPI_DATATYPE(unsigned __int64, MPI_UNSIGNED_LONG_LONG, builtin); 
0314 #endif
0315 
0316 // Define signed char specialization of is_mpi_datatype, if possible.
0317 #if defined(MPI_SIGNED_CHAR) || (BOOST_MPI_VERSION >= 2)
0318 BOOST_MPI_DATATYPE(signed char, MPI_SIGNED_CHAR, builtin);
0319 #endif
0320 
0321 
0322 #endif // Doxygen
0323 
0324 #ifndef BOOST_MPI_DOXYGEN
0325 // direct support for special primitive data types of the serialization library
0326 BOOST_MPI_DATATYPE(boost::serialization::library_version_type, get_mpi_datatype(uint_least16_t()), integer);
0327 BOOST_MPI_DATATYPE(boost::archive::version_type, get_mpi_datatype(uint_least8_t()), integer);
0328 BOOST_MPI_DATATYPE(boost::archive::class_id_type, get_mpi_datatype(int_least16_t()), integer);
0329 BOOST_MPI_DATATYPE(boost::archive::class_id_reference_type, get_mpi_datatype(int_least16_t()), integer);
0330 BOOST_MPI_DATATYPE(boost::archive::class_id_optional_type, get_mpi_datatype(int_least16_t()), integer);
0331 BOOST_MPI_DATATYPE(boost::archive::object_id_type, get_mpi_datatype(uint_least32_t()), integer);
0332 BOOST_MPI_DATATYPE(boost::archive::object_reference_type, get_mpi_datatype(uint_least32_t()), integer);
0333 BOOST_MPI_DATATYPE(boost::archive::tracking_type, get_mpi_datatype(bool()), builtin);
0334 BOOST_MPI_DATATYPE(boost::serialization::collection_size_type, get_mpi_datatype(std::size_t()), integer);
0335 BOOST_MPI_DATATYPE(boost::serialization::item_version_type, get_mpi_datatype(uint_least8_t()), integer);
0336 #endif // Doxygen
0337 
0338 
0339 } } // end namespace boost::mpi
0340 
0341 // direct support for special primitive data types of the serialization library
0342 // in the case of homogeneous systems
0343 // define a macro to make explicit designation of this more transparent
0344 #define BOOST_IS_MPI_DATATYPE(T)              \
0345 namespace boost {                             \
0346 namespace mpi {                               \
0347 template<>                                    \
0348 struct is_mpi_datatype< T > : mpl::true_ {};  \
0349 }}                                            \
0350 /**/
0351 
0352 
0353 #endif // BOOST_MPI_MPI_DATATYPE_HPP