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