openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
mpi_utils.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // openCARP is an open cardiac electrophysiology simulator.
3 //
4 // Copyright (C) 2020 openCARP project
5 //
6 // This program is licensed under the openCARP Academic Public License (APL)
7 // v1.0: You can use and redistribute it and/or modify it in non-commercial
8 // academic environments under the terms of APL as published by the openCARP
9 // project v1.0, or (at your option) any later version. Commercial use requires
10 // a commercial license (info@opencarp.org).
11 //
12 // This program is distributed without any warranty; see the openCARP APL for
13 // more details.
14 //
15 // You should have received a copy of the openCARP APL along with this program
16 // and can find it online: http://www.opencarp.org/license
17 // ----------------------------------------------------------------------------
18 
19 #ifndef _OPENCARP_MPI_UTILS_H
20 #define _OPENCARP_MPI_UTILS_H
21 
22 #include <cassert>
23 #include <cstdint>
24 #include <limits>
25 #include <type_traits>
26 
27 #include <mpi.h>
28 
29 namespace opencarp {
30 
31 namespace detail {
32 template<typename>
33 struct always_false : std::false_type {};
34 }
35 
36 template<typename T> inline
37 MPI_Datatype mpi_datatype()
38 {
39  using value_t = std::remove_cv_t<T>;
40 
41  if constexpr (std::is_same_v<value_t, char>) {
42  return MPI_CHAR;
43  }
44 #ifdef MPI_INT8_T
45  else if constexpr (std::is_same_v<value_t, std::int8_t>) {
46  return MPI_INT8_T;
47  }
48 #endif
49 #ifdef MPI_UINT8_T
50  else if constexpr (std::is_same_v<value_t, std::uint8_t>) {
51  return MPI_UINT8_T;
52  }
53 #endif
54 #ifdef MPI_INT16_T
55  else if constexpr (std::is_same_v<value_t, std::int16_t>) {
56  return MPI_INT16_T;
57  }
58 #endif
59 #ifdef MPI_UINT16_T
60  else if constexpr (std::is_same_v<value_t, std::uint16_t>) {
61  return MPI_UINT16_T;
62  }
63 #endif
64 #ifdef MPI_INT32_T
65  else if constexpr (std::is_same_v<value_t, std::int32_t>) {
66  return MPI_INT32_T;
67  }
68 #endif
69 #ifdef MPI_UINT32_T
70  else if constexpr (std::is_same_v<value_t, std::uint32_t>) {
71  return MPI_UINT32_T;
72  }
73 #endif
74 #ifdef MPI_INT64_T
75  else if constexpr (std::is_same_v<value_t, std::int64_t>) {
76  return MPI_INT64_T;
77  }
78 #endif
79 #ifdef MPI_UINT64_T
80  else if constexpr (std::is_same_v<value_t, std::uint64_t>) {
81  return MPI_UINT64_T;
82  }
83 #endif
84  else if constexpr (std::is_same_v<value_t, signed char>) {
85  return MPI_SIGNED_CHAR;
86  }
87  else if constexpr (std::is_same_v<value_t, unsigned char>) {
88  return MPI_UNSIGNED_CHAR;
89  }
90  else if constexpr (std::is_same_v<value_t, short>) {
91  return MPI_SHORT;
92  }
93  else if constexpr (std::is_same_v<value_t, unsigned short>) {
94  return MPI_UNSIGNED_SHORT;
95  }
96  else if constexpr (std::is_same_v<value_t, int>) {
97  return MPI_INT;
98  }
99  else if constexpr (std::is_same_v<value_t, unsigned int>) {
100  return MPI_UNSIGNED;
101  }
102  else if constexpr (std::is_same_v<value_t, long>) {
103  return MPI_LONG;
104  }
105  else if constexpr (std::is_same_v<value_t, unsigned long>) {
106  return MPI_UNSIGNED_LONG;
107  }
108  else if constexpr (std::is_same_v<value_t, long long>) {
109  return MPI_LONG_LONG;
110  }
111  else if constexpr (std::is_same_v<value_t, unsigned long long>) {
112  return MPI_UNSIGNED_LONG_LONG;
113  }
114  else if constexpr (std::is_same_v<value_t, float>) {
115  return MPI_FLOAT;
116  }
117  else if constexpr (std::is_same_v<value_t, double>) {
118  return MPI_DOUBLE;
119  }
120  else if constexpr (std::is_same_v<value_t, long double>) {
121  return MPI_LONG_DOUBLE;
122  }
123  else {
125  "Unsupported MPI datatype");
126  }
127 }
128 
129 template<typename T> inline
130 std::intmax_t printable_int(T value)
131 {
132  static_assert(std::is_integral_v<T>, "printable_int requires an integral type");
133 
134  if constexpr (std::is_signed_v<T>) {
135  return static_cast<std::intmax_t>(value);
136  } else {
137  assert(value <= static_cast<T>(std::numeric_limits<std::intmax_t>::max()));
138  return static_cast<std::intmax_t>(value);
139  }
140 }
141 
142 template<typename T> inline
143 std::uintmax_t printable_uint(T value)
144 {
145  static_assert(std::is_integral_v<T>, "printable_uint requires an integral type");
146  return static_cast<std::uintmax_t>(value);
147 }
148 
149 } // namespace opencarp
150 
151 #endif
constexpr T max(T a, T b)
Definition: ion_type.h:31
std::intmax_t printable_int(T value)
Definition: mpi_utils.h:130
MPI_Datatype mpi_datatype()
Definition: mpi_utils.h:37
std::uintmax_t printable_uint(T value)
Definition: mpi_utils.h:143