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.
reduction_utilities.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 authors: Riccardo Rossi
11 // Denis Demidov
12 // Philipp Bucher (https://github.com/philbucher)
13 //
14 
15 #pragma once
16 
17 // System includes
18 #include <tuple>
19 #include <limits>
20 #include <algorithm>
21 #include <mutex>
22 
23 // External includes
24 
25 // Project includes
26 #include "includes/define.h"
29 
30 namespace Kratos
31 {
32 
33 namespace Internals
34 {
37 template <class TObjectType>
39 {
40  static TObjectType Get()
41  {
42  return TObjectType();
43  }
44 };
45 
46 template <class TValueType, std::size_t ArraySize>
47 struct NullInitialized<array_1d<TValueType,ArraySize>>
48 {
50  {
52  std::fill_n(array.begin(), ArraySize, NullInitialized<TValueType>::Get());
53  return array;
54  }
55 };
56 } // namespace Internals
57 
59 
60 //***********************************************************************************
61 //***********************************************************************************
62 //***********************************************************************************
63 
66 template<class TDataType, class TReturnType = TDataType>
68 {
69 public:
70  typedef TDataType value_type;
71  typedef TReturnType return_type;
72 
73  TReturnType mValue = Internals::NullInitialized<TReturnType>::Get(); // deliberately making the member value public, to allow one to change it as needed
74 
76  TReturnType GetValue() const
77  {
78  return mValue;
79  }
80 
82  void LocalReduce(const TDataType value){
83  mValue += value;
84  }
85 
88  {
89  AtomicAdd(mValue, rOther.mValue);
90  }
91 };
92 
93 //***********************************************************************************
94 //***********************************************************************************
95 //***********************************************************************************
96 template<class TDataType, class TReturnType = TDataType>
98 {
99 public:
100  typedef TDataType value_type;
101  typedef TReturnType return_type;
102 
103  TReturnType mValue = Internals::NullInitialized<TReturnType>::Get(); // deliberately making the member value public, to allow one to change it as needed
104 
106  TReturnType GetValue() const
107  {
108  return mValue;
109  }
110 
112  void LocalReduce(const TDataType value){
113  mValue -= value;
114  }
115 
118  {
119  AtomicAdd(mValue, rOther.mValue);
120  }
121 };
122 
123 //***********************************************************************************
124 //***********************************************************************************
125 //***********************************************************************************
126 template<class TDataType, class TReturnType = TDataType>
128 {
129 public:
130  typedef TDataType value_type;
131  typedef TReturnType return_type;
132 
133  TReturnType mValue = std::numeric_limits<TReturnType>::lowest(); // deliberately making the member value public, to allow one to change it as needed
134 
136  TReturnType GetValue() const
137  {
138  return mValue;
139  }
140 
142  void LocalReduce(const TDataType value){
143  mValue = std::max(mValue,value);
144  }
145 
148  {
150  LocalReduce(rOther.mValue);
151  }
152 };
153 
154 //***********************************************************************************
155 //***********************************************************************************
156 //***********************************************************************************
157 template<class TDataType, class TReturnType = TDataType>
159 {
160 public:
161  typedef TDataType value_type;
162  typedef TReturnType return_type;
163 
164  TReturnType mValue = std::numeric_limits<TReturnType>::lowest(); // deliberately making the member value public, to allow one to change it as needed
165 
167  TReturnType GetValue() const
168  {
169  return mValue;
170  }
171 
173  void LocalReduce(const TDataType value){
174  mValue = (std::abs(mValue) < std::abs(value)) ? value : mValue;
175  }
176 
179  {
181  LocalReduce(rOther.mValue);
182  }
183 };
184 
185 //***********************************************************************************
186 //***********************************************************************************
187 //***********************************************************************************
188 template<class TDataType, class TReturnType = TDataType>
190 {
191 public:
192  typedef TDataType value_type;
193  typedef TReturnType return_type;
194 
195  TReturnType mValue = std::numeric_limits<TReturnType>::max(); // deliberately making the member value public, to allow one to change it as needed
196 
198  TReturnType GetValue() const
199  {
200  return mValue;
201  }
202 
204  void LocalReduce(const TDataType value){
205  mValue = std::min(mValue,value);
206  }
207 
210  {
212  LocalReduce(rOther.mValue);
213  }
214 };
215 
216 
217 //***********************************************************************************
218 //***********************************************************************************
219 //***********************************************************************************
220 
221 template<class TDataType, class TReturnType = TDataType>
223 {
224 public:
225  typedef TDataType value_type;
226  typedef TReturnType return_type;
227 
228  TReturnType mValue = std::numeric_limits<TReturnType>::max(); // deliberately making the member value public, to allow one to change it as needed
229 
231  TReturnType GetValue() const
232  {
233  return mValue;
234  }
235 
237  void LocalReduce(const TDataType value){
238  mValue = (std::abs(mValue) < std::abs(value)) ? mValue : value;
239  }
240 
243  {
245  LocalReduce(rOther.mValue);
246  }
247 };
248 
249 //***********************************************************************************
250 //***********************************************************************************
251 //***********************************************************************************
252 
253 template<class TDataType, class TReturnType = std::vector<TDataType>>
255 {
256 public:
257  typedef TDataType value_type;
258  typedef TReturnType return_type;
259 
260  TReturnType mValue = TReturnType(); // deliberately making the member value public, to allow one to change it as needed
261 
263  TReturnType GetValue() const
264  {
265  return mValue;
266  }
267 
269  void LocalReduce(const TDataType value){
270  mValue.insert(mValue.end(), value);
271  }
272 
275  {
277  std::copy(rOther.mValue.begin(), rOther.mValue.end(), std::inserter(mValue, mValue.end()));
278  }
279 };
280 
281 template<class MapType>
283 {
284 public:
285  using value_type = typename MapType::value_type;
286  using return_type = MapType;
287 
289 
292  {
293  return mValue;
294  }
295 
297  void LocalReduce(const value_type rValue){
298  mValue.emplace(rValue);
299  }
300 
303  {
305  mValue.merge(rOther.mValue);
306  }
307 };
308 
309 template <class... Reducer>
311  typedef std::tuple<typename Reducer::value_type...> value_type;
312  typedef std::tuple<typename Reducer::return_type...> return_type;
313 
314  std::tuple<Reducer...> mChild;
315 
317 
320  return_type return_value;
321  fill_value<0>(return_value);
322  return return_value;
323  }
324 
325  template <int I, class T>
326  typename std::enable_if<(I < sizeof...(Reducer)), void>::type
327  fill_value(T& v) {
328  std::get<I>(v) = std::get<I>(mChild).GetValue();
329  fill_value<I+1>(v);
330  };
331 
332  template <int I, class T>
333  typename std::enable_if<(I == sizeof...(Reducer)), void>::type
334  fill_value(T& v) {}
335 
337  template <class... T>
338  void LocalReduce(const std::tuple<T...> &&v) {
339  // Static recursive loop over tuple elements
340  reduce_local<0>(v);
341  }
342 
344  void ThreadSafeReduce(const CombinedReduction &other) {
345  reduce_global<0>(other);
346  }
347 
348  private:
349 
350  template <int I, class T>
351  typename std::enable_if<(I < sizeof...(Reducer)), void>::type
352  reduce_local(T &&v) {
353  std::get<I>(mChild).LocalReduce(std::get<I>(v));
354  reduce_local<I+1>(std::forward<T>(v));
355  };
356 
357  template <int I, class T>
358  typename std::enable_if<(I == sizeof...(Reducer)), void>::type
359  reduce_local(T &&v) {
360  // Exit static recursion
361  }
362 
363  template <int I>
364  typename std::enable_if<(I < sizeof...(Reducer)), void>::type
365  reduce_global(const CombinedReduction &other) {
366  std::get<I>(mChild).ThreadSafeReduce(std::get<I>(other.mChild));
367  reduce_global<I+1>(other);
368  }
369 
370  template <int I>
371  typename std::enable_if<(I == sizeof...(Reducer)), void>::type
372  reduce_global(const CombinedReduction &other) {
373  // Exit static recursion
374  }
375 };
376 
377 } // namespace Kratos.
Definition: reduction_utilities.h:159
void ThreadSafeReduce(const AbsMaxReduction< TDataType, TReturnType > &rOther)
THREADSAFE (needs some sort of lock guard) reduction, to be used to sync threads.
Definition: reduction_utilities.h:178
TDataType value_type
Definition: reduction_utilities.h:161
void LocalReduce(const TDataType value)
NON-THREADSAFE (fast) value of reduction, to be used within a single thread.
Definition: reduction_utilities.h:173
TReturnType return_type
Definition: reduction_utilities.h:162
TReturnType GetValue() const
access to reduced value
Definition: reduction_utilities.h:167
TReturnType mValue
Definition: reduction_utilities.h:164
Definition: reduction_utilities.h:223
void LocalReduce(const TDataType value)
NON-THREADSAFE (fast) value of reduction, to be used within a single thread.
Definition: reduction_utilities.h:237
void ThreadSafeReduce(const AbsMinReduction< TDataType, TReturnType > &rOther)
THREADSAFE (needs some sort of lock guard) reduction, to be used to sync threads.
Definition: reduction_utilities.h:242
TDataType value_type
Definition: reduction_utilities.h:225
TReturnType GetValue() const
access to reduced value
Definition: reduction_utilities.h:231
TReturnType mValue
Definition: reduction_utilities.h:228
TReturnType return_type
Definition: reduction_utilities.h:226
Definition: reduction_utilities.h:255
void LocalReduce(const TDataType value)
NON-THREADSAFE (fast) value of reduction, to be used within a single thread.
Definition: reduction_utilities.h:269
TDataType value_type
Definition: reduction_utilities.h:257
TReturnType return_type
Definition: reduction_utilities.h:258
TReturnType mValue
Definition: reduction_utilities.h:260
void ThreadSafeReduce(const AccumReduction< TDataType, TReturnType > &rOther)
THREADSAFE (needs some sort of lock guard) reduction, to be used to sync threads.
Definition: reduction_utilities.h:274
TReturnType GetValue() const
access to reduced value
Definition: reduction_utilities.h:263
Definition: reduction_utilities.h:283
typename MapType::value_type value_type
Definition: reduction_utilities.h:285
MapType return_type
Definition: reduction_utilities.h:286
return_type GetValue() const
access to reduced value
Definition: reduction_utilities.h:291
void LocalReduce(const value_type rValue)
NON-THREADSAFE (fast) value of reduction, to be used within a single thread.
Definition: reduction_utilities.h:297
void ThreadSafeReduce(MapReduction< MapType > &rOther)
THREADSAFE (needs some sort of lock guard) reduction, to be used to sync threads.
Definition: reduction_utilities.h:302
return_type mValue
Definition: reduction_utilities.h:288
Definition: reduction_utilities.h:128
void LocalReduce(const TDataType value)
NON-THREADSAFE (fast) value of reduction, to be used within a single thread.
Definition: reduction_utilities.h:142
TReturnType return_type
Definition: reduction_utilities.h:131
TReturnType GetValue() const
access to reduced value
Definition: reduction_utilities.h:136
void ThreadSafeReduce(const MaxReduction< TDataType, TReturnType > &rOther)
THREADSAFE (needs some sort of lock guard) reduction, to be used to sync threads.
Definition: reduction_utilities.h:147
TReturnType mValue
Definition: reduction_utilities.h:133
TDataType value_type
Definition: reduction_utilities.h:130
Definition: reduction_utilities.h:190
void ThreadSafeReduce(const MinReduction< TDataType, TReturnType > &rOther)
THREADSAFE (needs some sort of lock guard) reduction, to be used to sync threads.
Definition: reduction_utilities.h:209
TReturnType mValue
Definition: reduction_utilities.h:195
TReturnType GetValue() const
access to reduced value
Definition: reduction_utilities.h:198
void LocalReduce(const TDataType value)
NON-THREADSAFE (fast) value of reduction, to be used within a single thread.
Definition: reduction_utilities.h:204
TReturnType return_type
Definition: reduction_utilities.h:193
TDataType value_type
Definition: reduction_utilities.h:192
Definition: reduction_utilities.h:98
TReturnType return_type
Definition: reduction_utilities.h:101
TReturnType GetValue() const
access to reduced value
Definition: reduction_utilities.h:106
void LocalReduce(const TDataType value)
NON-THREADSAFE (fast) value of reduction, to be used within a single thread.
Definition: reduction_utilities.h:112
TReturnType mValue
Definition: reduction_utilities.h:103
void ThreadSafeReduce(const SubReduction< TDataType, TReturnType > &rOther)
THREADSAFE (needs some sort of lock guard) reduction, to be used to sync threads.
Definition: reduction_utilities.h:117
TDataType value_type
Definition: reduction_utilities.h:100
utility function to do a sum reduction
Definition: reduction_utilities.h:68
void LocalReduce(const TDataType value)
NON-THREADSAFE (fast) value of reduction, to be used within a single thread.
Definition: reduction_utilities.h:82
TReturnType mValue
Definition: reduction_utilities.h:73
void ThreadSafeReduce(const SumReduction< TDataType, TReturnType > &rOther)
THREADSAFE (needs some sort of lock guard) reduction, to be used to sync threads.
Definition: reduction_utilities.h:87
TReturnType GetValue() const
access to reduced value
Definition: reduction_utilities.h:76
TDataType value_type
Definition: reduction_utilities.h:70
TReturnType return_type
Definition: reduction_utilities.h:71
Short class definition.
Definition: array_1d.h:61
BOOST_UBLAS_INLINE const_iterator begin() const
Definition: array_1d.h:606
static double max(double a, double b)
Definition: GeometryFunctions.h:79
static double min(double a, double b)
Definition: GeometryFunctions.h:71
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
void AtomicAdd(TDataType &target, const TDataType &value)
Definition: atomic_utilities.h:55
v
Definition: generate_convection_diffusion_explicit_element.py:114
type
Definition: generate_gid_list_file.py:35
#define KRATOS_CRITICAL_SECTION
Definition: parallel_utilities.h:39
Definition: reduction_utilities.h:310
void ThreadSafeReduce(const CombinedReduction &other)
THREADSAFE (needs some sort of lock guard) reduction, to be used to sync threads.
Definition: reduction_utilities.h:344
void LocalReduce(const std::tuple< T... > &&v)
NON-THREADSAFE (fast) value of reduction, to be used within a single thread.
Definition: reduction_utilities.h:338
CombinedReduction()
Definition: reduction_utilities.h:316
std::tuple< Reducer... > mChild
Definition: reduction_utilities.h:314
std::enable_if<(I==sizeof...(Reducer)), void >::type fill_value(T &v)
Definition: reduction_utilities.h:334
std::tuple< typename Reducer::return_type... > return_type
Definition: reduction_utilities.h:312
std::tuple< typename Reducer::value_type... > value_type
Definition: reduction_utilities.h:311
std::enable_if<(I< sizeof...(Reducer)), void >::type fill_value(T &v)
Definition: reduction_utilities.h:327
return_type GetValue()
access to reduced value
Definition: reduction_utilities.h:319
static array_1d< TValueType, ArraySize > Get()
Definition: reduction_utilities.h:49
Helper class for null-initializiation.
Definition: reduction_utilities.h:39
static TObjectType Get()
Definition: reduction_utilities.h:40