KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
mpi_message.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main author: Jordi Cotela
11 //
12 
13 #pragma once
14 
15 // System includes
16 #include <string>
17 #include <vector>
18 #include <type_traits>
19 
20 // External includes
21 #include "mpi.h"
22 
23 // Project includes
24 #include "containers/array_1d.h"
25 #include "containers/flags.h"
27 
28 namespace Kratos
29 {
30 
31 namespace Internals {
32 
33 template<class TDataType> struct MPIDataType;
34 
35 template<> struct MPIDataType<int>
36 {
37  static inline MPI_Datatype DataType()
38  {
39  return MPI_INT;
40  }
41 };
42 
43 template<> struct MPIDataType<unsigned int>
44 {
45  static inline MPI_Datatype DataType()
46  {
47  return MPI_UNSIGNED;
48  }
49 };
50 
51 template<> struct MPIDataType<long unsigned int>
52 {
53  static inline MPI_Datatype DataType()
54  {
55  return MPI_UNSIGNED_LONG;
56  }
57 };
58 
59 template<> struct MPIDataType<double>
60 {
61  static inline MPI_Datatype DataType()
62  {
63  return MPI_DOUBLE;
64  }
65 };
66 
67 template<> struct MPIDataType<bool>
68 {
69  static inline MPI_Datatype DataType()
70  {
71  return MPI_C_BOOL;
72  }
73 };
74 
75 template<> struct MPIDataType<char>
76 {
77  static inline MPI_Datatype DataType()
78  {
79  return MPI_CHAR;
80  }
81 };
82 
83 template<> struct MPIDataType<int64_t>
84 {
85  static inline MPI_Datatype DataType()
86  {
87  return MPI_INT64_T;
88  }
89 };
90 
91 template<> struct MPIDataType<std::pair<char, int>>
92 {
93  static inline MPI_Datatype DataType()
94  {
95  return MPI_2INT;
96  }
97 };
98 
99 template<> struct MPIDataType<std::pair<int, int>>
100 {
101  static inline MPI_Datatype DataType()
102  {
103  return MPI_2INT;
104  }
105 };
106 
107 template<> struct MPIDataType<std::pair<unsigned int, int>>
108 {
109  static inline MPI_Datatype DataType()
110  {
111  return MPI_2INT;
112  }
113 };
114 
115 template<> struct MPIDataType<std::pair<long unsigned int, int>>
116 {
117  static inline MPI_Datatype DataType()
118  {
119  return MPI_LONG_INT;
120  }
121 };
122 
123 template<> struct MPIDataType<std::pair<double, int>>
124 {
125  static inline MPI_Datatype DataType()
126  {
127  return MPI_DOUBLE_INT;
128  }
129 };
130 
131 }
132 
133 template<class TDataType> class MPIMessage
134 {
135 public:
138 
139  using MessageDataType = TDataType;
140 
142 
144 
146 
148 
150 
154 
155  inline MPI_Datatype DataType()
156  {
158  }
159 
160  inline void* Buffer(MessageDataType& rValue)
161  {
162  if constexpr(HasContiguousPrimitiveData) {
164  } else {
165  const auto size = MPIMessageDataTypeTraits::Size(rValue);
166  if (mData.size() != size) {
167  mData.resize(size);
168  }
170  return mData.data();
171  }
172  }
173 
174  inline const void* Buffer(const MessageDataType& rValue)
175  {
176  if constexpr(HasContiguousPrimitiveData) {
178  } else {
179  const auto size = MPIMessageDataTypeTraits::Size(rValue);
180  if (mData.size() != size) {
181  mData.resize(size);
182  }
184  return mData.data();
185  }
186  }
187 
188  inline int Size(const MessageDataType& rValue)
189  {
190  return MPIMessageDataTypeTraits::Size(rValue);
191  }
192 
193  inline int SubDataTypeSize(const MessageDataType& rValue)
194  {
195  if constexpr(!std::is_same_v<SubDataType, TDataType>) {
196  return rValue.size() == 0 ? 0 : MPIMessage<SubDataType>().Size(rValue.front());
197  } else {
198  return 0;
199  }
200  }
201 
202  inline std::vector<unsigned int> Shape(const MessageDataType& rValue)
203  {
204  return MPIMessageDataTypeTraits::Shape(rValue);
205  }
206 
207  inline bool Resize(MessageDataType& rValue, const std::vector<unsigned int>& rShape)
208  {
209  return MPIMessageDataTypeTraits::Reshape(rValue, rShape);
210  }
211 
212  inline void Update(MessageDataType& rValue)
213  {
214  if constexpr(!HasContiguousPrimitiveData) {
215  KRATOS_ERROR_IF_NOT(MPIMessageDataTypeTraits::Size(rValue) == static_cast<unsigned int>(mData.size()))
216  << "Size mismatch [ rValue flat size = " << MPIMessageDataTypeTraits::Size(rValue)
217  << ", buffered data size = " << mData.size() << " ].\n";
219  }
220  }
221 
223 
224 private:
227 
228  std::conditional_t<HasContiguousPrimitiveData, void *, std::vector<PrimitiveDataType>> mData;
229 
231 };
232 
233 } // namespace Kratos
Generic data type traits class for arithmetic types.
Definition: data_type_traits.h:33
static constexpr bool IsDynamic
Definition: data_type_traits.h:46
static std::vector< TIndexType > Shape(const ContainerType &)
Returns a vector with the shape of the value.
Definition: data_type_traits.h:89
static PrimitiveType const * GetContiguousData(const ContainerType &rValue)
Get the contiguous data pointer.
Definition: data_type_traits.h:162
static TIndexType Size(const ContainerType &)
Returns the size of the value.
Definition: data_type_traits.h:76
TDataType ValueType
Definition: data_type_traits.h:40
static void CopyFromContiguousData(ContainerType &rValue, PrimitiveType const *pContiguousDataBegin)
Copies data from contiguous array to rValue.
Definition: data_type_traits.h:208
static void CopyToContiguousData(PrimitiveType *pContiguousDataBegin, const ContainerType &rValue)
Copies the given Value to contiguous array.
Definition: data_type_traits.h:190
static constexpr bool IsContiguous
Definition: data_type_traits.h:44
TDataType PrimitiveType
Definition: data_type_traits.h:42
static bool Reshape(ContainerType &rValue, const std::vector< TIndexType > &rShape)
Reshapes the value.
Definition: data_type_traits.h:126
Definition: mpi_message.h:134
static constexpr bool HasDynamicMemoryAllocation
Definition: mpi_message.h:149
bool Resize(MessageDataType &rValue, const std::vector< unsigned int > &rShape)
Definition: mpi_message.h:207
TDataType MessageDataType
Definition: mpi_message.h:139
const void * Buffer(const MessageDataType &rValue)
Definition: mpi_message.h:174
int Size(const MessageDataType &rValue)
Definition: mpi_message.h:188
void Update(MessageDataType &rValue)
Definition: mpi_message.h:212
typename MPIMessageDataTypeTraits::ValueType SubDataType
Definition: mpi_message.h:143
void * Buffer(MessageDataType &rValue)
Definition: mpi_message.h:160
MPI_Datatype DataType()
Definition: mpi_message.h:155
static constexpr bool HasContiguousPrimitiveData
Definition: mpi_message.h:147
int SubDataTypeSize(const MessageDataType &rValue)
Definition: mpi_message.h:193
std::vector< unsigned int > Shape(const MessageDataType &rValue)
Definition: mpi_message.h:202
typename MPIMessageDataTypeTraits::PrimitiveType PrimitiveDataType
Definition: mpi_message.h:145
#define KRATOS_ERROR_IF_NOT(conditional)
Definition: exception.h:163
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
REACTION_CHECK_STIFFNESS_FACTOR INNER_LOOP_ITERATION DISTANCE_THRESHOLD ACTIVE_CHECK_FACTOR AUXILIAR_COORDINATES NORMAL_GAP WEIGHTED_GAP WEIGHTED_SCALAR_RESIDUAL bool
Definition: contact_structural_mechanics_application_variables.h:93
REACTION_CHECK_STIFFNESS_FACTOR int
Definition: contact_structural_mechanics_application_variables.h:75
TABLE_NUMBER_ANGULAR_VELOCITY TABLE_NUMBER_MOMENT I33 BEAM_INERTIA_ROT_UNIT_LENGHT_Y KRATOS_DEFINE_APPLICATION_VARIABLE(DEM_APPLICATION, double, BEAM_INERTIA_ROT_UNIT_LENGHT_Z) typedef std double
Definition: DEM_application_variables.h:182
namespace
Definition: array_1d.h:793
static MPI_Datatype DataType()
Definition: mpi_message.h:69
static MPI_Datatype DataType()
Definition: mpi_message.h:77
static MPI_Datatype DataType()
Definition: mpi_message.h:61
static MPI_Datatype DataType()
Definition: mpi_message.h:85
static MPI_Datatype DataType()
Definition: mpi_message.h:37
static MPI_Datatype DataType()
Definition: mpi_message.h:53
static MPI_Datatype DataType()
Definition: mpi_message.h:93
static MPI_Datatype DataType()
Definition: mpi_message.h:125
static MPI_Datatype DataType()
Definition: mpi_message.h:101
static MPI_Datatype DataType()
Definition: mpi_message.h:117
static MPI_Datatype DataType()
Definition: mpi_message.h:109
static MPI_Datatype DataType()
Definition: mpi_message.h:45
Definition: mpi_message.h:33