42 #ifndef TEUCHOS_DETAILS_MPITYPETRAITS_HPP
43 #define TEUCHOS_DETAILS_MPITYPETRAITS_HPP
49 #include "Teuchos_config.h"
51 #ifdef HAVE_TEUCHOS_MPI
74 static const bool isSpecialized =
false;
92 static const bool needsFree =
false;
110 static MPI_Datatype getType (
const T&) {
116 return MPI_DATATYPE_NULL;
128 class MpiTypeTraits<char> {
131 static const bool isSpecialized =
true;
135 static const bool needsFree =
false;
138 static MPI_Datatype getType (
const char&) {
143 static MPI_Datatype getType () {
152 class MpiTypeTraits<unsigned char> {
155 static const bool isSpecialized =
true;
159 static const bool needsFree =
false;
162 static MPI_Datatype getType (
const unsigned char&) {
163 return MPI_UNSIGNED_CHAR;
167 static MPI_Datatype getType () {
168 return MPI_UNSIGNED_CHAR;
177 class MpiTypeTraits<signed char> {
180 static const bool isSpecialized =
true;
184 static const bool needsFree =
false;
187 static MPI_Datatype getType (
const signed char&) {
188 return MPI_SIGNED_CHAR;
192 static MPI_Datatype getType () {
193 return MPI_SIGNED_CHAR;
196 #endif // MPI_VERSION >= 2
239 computeStdComplexMpiDatatype (
const std::complex<T>& z)
241 #ifdef HAVE_TEUCHOSCORE_CXX11
242 static_assert (MpiTypeTraits<T>::isSpecialized,
"This function only "
243 "works if MpiTypeTraits<T>::isSpecialized.");
244 static_assert (! MpiTypeTraits<T>::needsFree,
"This function requires "
245 "! MpiTypeTraits<T>::needsFree, since otherwise it would "
247 #endif // HAVE_TEUCHOSCORE_CXX11
251 MPI_Datatype innerDatatype = MpiTypeTraits<T>::getType (z.real ());
252 MPI_Datatype outerDatatype;
259 if (
sizeof (std::complex<T>) == 2 *
sizeof (T)) {
260 (void) MPI_Type_contiguous (2, innerDatatype, &outerDatatype);
268 MPI_Aint arrayOfDisplacements[3];
269 MPI_Datatype arrayOfTypes[3];
271 #ifdef HAVE_TEUCHOSCORE_CXX11
273 static_assert (
sizeof (MyComplex<T>) ==
sizeof (std::complex<T>),
274 "Attempt to construct a struct of the same size and layout "
275 "as std::complex<T> failed.");
276 #endif // HAVE_TEUCHOSCORE_CXX11
286 arrayOfDisplacements[0] =
reinterpret_cast<char*
>(&z2.re) - reinterpret_cast<char*>(&z2);
287 arrayOfTypes[0] = innerDatatype;
291 arrayOfDisplacements[1] =
reinterpret_cast<char*
>(&z2.im) - reinterpret_cast<char*>(&z2);
292 arrayOfTypes[1] = innerDatatype;
297 arrayOfDisplacements[2] =
sizeof (MyComplex<T>);
298 arrayOfTypes[2] = MPI_UB;
299 #endif // MPI_VERSION < 2
303 (void) MPI_Type_struct (3, blockLengths, arrayOfDisplacements,
304 arrayOfTypes, &outerDatatype);
307 (void) MPI_Type_create_struct (2, blockLengths, arrayOfDisplacements,
308 arrayOfTypes, &outerDatatype);
309 #endif // MPI_VERSION < 2
312 MPI_Type_commit (&outerDatatype);
313 return outerDatatype;
321 class MpiTypeTraits< std::complex<double> > {
324 static const bool hasMpi3 =
true;
326 static const bool hasMpi3 =
false;
327 #endif // MPI_VERSION >= 3
331 static const bool isSpecialized =
true;
335 static const bool needsFree = ! hasMpi3;
338 static MPI_Datatype getType (
const std::complex<double>& z) {
341 return MPI_C_DOUBLE_COMPLEX;
343 return MPI_DATATYPE_NULL;
344 #endif // MPI_VERSION >= 3
347 return Impl::computeStdComplexMpiDatatype<double> (z);
352 static MPI_Datatype getType () {
355 return MPI_C_DOUBLE_COMPLEX;
357 return MPI_DATATYPE_NULL;
358 #endif // MPI_VERSION >= 3
363 std::complex<double> z (3.0, 4.0);
364 return Impl::computeStdComplexMpiDatatype<double> (z);
370 class MpiTypeTraits< std::complex<float> > {
373 static const bool hasMpi3 =
true;
375 static const bool hasMpi3 =
false;
376 #endif // MPI_VERSION >= 3
380 static const bool isSpecialized =
true;
384 static const bool needsFree = ! hasMpi3;
387 static MPI_Datatype getType (
const std::complex<float>& z) {
390 return MPI_C_FLOAT_COMPLEX;
392 return MPI_DATATYPE_NULL;
393 #endif // MPI_VERSION >= 3
396 return Impl::computeStdComplexMpiDatatype<float> (z);
401 static MPI_Datatype getType () {
404 return MPI_C_FLOAT_COMPLEX;
406 return MPI_DATATYPE_NULL;
407 #endif // MPI_VERSION >= 3
412 std::complex<float> z (3.0, 4.0);
413 return Impl::computeStdComplexMpiDatatype<float> (z);
420 class MpiTypeTraits<double> {
423 static const bool isSpecialized =
true;
427 static const bool needsFree =
false;
430 static MPI_Datatype getType (
const double&) {
435 static MPI_Datatype getType () {
442 class MpiTypeTraits<float> {
445 static const bool isSpecialized =
true;
449 static const bool needsFree =
false;
452 static MPI_Datatype getType (
const float&) {
457 static MPI_Datatype getType () {
464 class MpiTypeTraits<long long> {
467 static const bool isSpecialized =
true;
471 static const bool needsFree =
false;
474 static MPI_Datatype getType (
const long long&) {
475 return MPI_LONG_LONG;
479 static MPI_Datatype getType () {
480 return MPI_LONG_LONG;
490 class MpiTypeTraits<unsigned long long> {
493 static const bool isSpecialized =
true;
497 static const bool needsFree =
false;
500 static MPI_Datatype getType (
const unsigned long long&) {
501 return MPI_UNSIGNED_LONG_LONG;
505 static MPI_Datatype getType () {
506 return MPI_UNSIGNED_LONG_LONG;
509 #endif // MPI_VERSION >= 2
513 class MpiTypeTraits<long> {
516 static const bool isSpecialized =
true;
520 static const bool needsFree =
false;
523 static MPI_Datatype getType (
const long&) {
528 static MPI_Datatype getType () {
535 class MpiTypeTraits<unsigned long> {
538 static const bool isSpecialized =
true;
542 static const bool needsFree =
false;
545 static MPI_Datatype getType (
const unsigned long&) {
546 return MPI_UNSIGNED_LONG;
550 static MPI_Datatype getType () {
551 return MPI_UNSIGNED_LONG;
557 class MpiTypeTraits<int> {
560 static const bool isSpecialized =
true;
564 static const bool needsFree =
false;
567 static MPI_Datatype getType (
const int&) {
572 static MPI_Datatype getType () {
579 class MpiTypeTraits<unsigned int> {
582 static const bool isSpecialized =
true;
586 static const bool needsFree =
false;
589 static MPI_Datatype getType (
const unsigned int&) {
594 static MPI_Datatype getType () {
601 class MpiTypeTraits<short> {
604 static const bool isSpecialized =
true;
608 static const bool needsFree =
false;
611 static MPI_Datatype getType (
const short&) {
616 static MPI_Datatype getType () {
623 class MpiTypeTraits<unsigned short> {
626 static const bool isSpecialized =
true;
630 static const bool needsFree =
false;
633 static MPI_Datatype getType (
const unsigned short&) {
634 return MPI_UNSIGNED_SHORT;
638 static MPI_Datatype getType () {
639 return MPI_UNSIGNED_SHORT;
646 #endif // HAVE_TEUCHOS_MPI
648 #endif // TEUCHOS_DETAILS_MPITYPETRAITS_HPP