PstreamReduceOps.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 InNamespace
28  Foam
29 
30 Description
31  Inter-processor communication reduction functions.
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef Foam_PstreamReduceOps_H
36 #define Foam_PstreamReduceOps_H
37 
38 #include "Pstream.H"
39 #include "FixedList.H"
40 #include "ops.H"
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 //- Reduce inplace (cf. MPI Allreduce)
50 //- using specified communication schedule.
51 template<class T, class BinaryOp>
52 void reduce
53 (
54  const List<UPstream::commsStruct>& comms,
55  T& value,
56  const BinaryOp& bop,
57  const int tag,
58  const label comm
59 )
60 {
61  if (UPstream::warnComm != -1 && comm != UPstream::warnComm)
62  {
63  Pout<< "** reducing:" << value << " with comm:" << comm << endl;
65  }
66  Pstream::gather(comms, value, bop, tag, comm);
67  Pstream::broadcast(value, comm);
68 }
69 
70 
71 //- Reduce inplace (cf. MPI Allreduce)
72 //- using linear/tree communication schedule
73 template<class T, class BinaryOp>
74 void reduce
75 (
76  T& value,
77  const BinaryOp& bop,
78  const int tag = UPstream::msgType(),
79  const label comm = UPstream::worldComm
80 )
81 {
82  if (UPstream::parRun())
83  {
84  Foam::reduce(UPstream::whichCommunication(comm), value, bop, tag, comm);
85  }
86 }
87 
88 
89 //- Reduce inplace (cf. MPI Allreduce)
90 //- multiple values (same size on all processes!)
91 template<class T, class BinaryOp>
92 void reduce
93 (
94  T values[],
95  const int size,
96  const BinaryOp&,
97  const int tag,
98  const label comm
99 )
100 {
102 }
103 
104 //- Non-blocking reduce inplace (cf. MPI Iallreduce)
105 //- single value. Sets request.
106 template<class T, class BinaryOp>
107 void reduce
108 (
109  T& Value,
110  const BinaryOp&,
111  const int tag,
112  const label comm,
113  label& request
114 )
115 {
117 }
118 
119 //- Non-blocking reduce inplace (cf. MPI Iallreduce)
120 //- of multiple values (same size on all processes!)
121 // Sets request.
122 template<class T, class BinaryOp>
123 void reduce
124 (
125  T values[],
126  const int size,
127  const BinaryOp&,
128  const int tag,
129  const label comm,
130  label& request
131 )
132 {
134 }
135 
136 
137 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
138 
139 // Special reductions for bool
140 
141 //- Logical (and) inplace reduction. Uses UPstream::reduceAnd
142 void reduce
143 (
144  bool& value,
145  const andOp<bool>&,
146  const int tag = UPstream::msgType(),
147  const label comm = UPstream::worldComm
148 );
149 
150 //- Logical (or) inplace reduction. Uses UPstream::reduceOr
151 void reduce
152 (
153  bool& value,
154  const orOp<bool>&,
155  const int tag = UPstream::msgType(),
156  const label comm = UPstream::worldComm
157 );
158 
159 
160 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
161 
162 // Common reductions
163 
164 #undef Pstream_CommonReductions
165 #define Pstream_CommonReductions(Native) \
166  \
167  \
168 void reduce \
169 ( \
170  Native values[], \
171  const int size, \
172  const minOp<Native>&, \
173  const int tag = UPstream::msgType(), \
174  const label comm = UPstream::worldComm \
175 ); \
176  \
177  \
178 void reduce \
179 ( \
180  Native& value, \
181  const minOp<Native>&, \
182  const int tag = UPstream::msgType(), \
183  const label comm = UPstream::worldComm \
184 ); \
185  \
186  \
187 template<unsigned N> \
188 inline void reduce \
189 ( \
190  FixedList<Native, N>& values, \
191  const minOp<Native>&, \
192  const int tag = UPstream::msgType(), \
193  const label comm = UPstream::worldComm \
194 ) \
195 { \
196  reduce(values.data(), int(values.size()), minOp<Native>(), tag, comm); \
197 } \
198  \
199  \
200 void reduce \
201 ( \
202  Native values[], \
203  const int size, \
204  const maxOp<Native>&, \
205  const int tag, \
206  const label comm \
207 ); \
208  \
209  \
210 void reduce \
211 ( \
212  Native& value, \
213  const maxOp<Native>&, \
214  const int tag = UPstream::msgType(), \
215  const label comm = UPstream::worldComm \
216 ); \
217  \
218  \
219 template<unsigned N> \
220 inline void reduce \
221 ( \
222  FixedList<Native, N>& values, \
223  const maxOp<Native>&, \
224  const int tag = UPstream::msgType(), \
225  const label comm = UPstream::worldComm \
226 ) \
227 { \
228  reduce(values.data(), int(values.size()), maxOp<Native>(), tag, comm); \
229 } \
230  \
231  \
232 void reduce \
233 ( \
234  Native values[], \
235  const int size, \
236  const sumOp<Native>&, \
237  const int tag, \
238  const label comm \
239 ); \
240  \
241  \
242 void reduce \
243 ( \
244  Native& value, \
245  const sumOp<Native>&, \
246  const int tag = UPstream::msgType(), \
247  const label comm = UPstream::worldComm \
248 ); \
249  \
250  \
251 template<unsigned N> \
252 inline void reduce \
253 ( \
254  FixedList<Native, N>& values, \
255  const sumOp<Native>&, \
256  const int tag = UPstream::msgType(), \
257  const label comm = UPstream::worldComm \
258 ) \
259 { \
260  reduce(values.data(), int(values.size()), sumOp<Native>(), tag, comm); \
261 }
262 
263 
264 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
265 
266 // Floating-point reductions
267 
268 #undef Pstream_FloatReductions
269 #define Pstream_FloatReductions(Native) \
270  \
271 Pstream_CommonReductions(Native); \
272  \
273  \
274 void reduce \
275 ( \
276  Native values[], \
277  const int size, \
278  const sumOp<Native>&, \
279  const int tag, \
280  const label comm, \
281  label& requestID \
282 ); \
283  \
284  \
285 void reduce \
286 ( \
287  Native& value, \
288  const sumOp<Native>&, \
289  const int tag, \
290  const label comm, \
291  label& requestID \
292 );
293 
294 
295 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
296 
297 Pstream_CommonReductions(int32_t);
298 Pstream_CommonReductions(int64_t);
299 Pstream_CommonReductions(uint32_t);
300 Pstream_CommonReductions(uint64_t);
301 
304 
305 
306 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
308 #undef Pstream_CommonReductions
309 #undef Pstream_FloatReductions
310 
312 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
313 
314 //- Reduce inplace (cf. MPI Allreduce)
315 //- the sum of both value and count (for averaging)
316 template<class T>
317 void sumReduce
318 (
319  T& value,
320  label& count,
321  const int tag = UPstream::msgType(),
322  const label comm = UPstream::worldComm
323 )
324 {
325  if (UPstream::parRun())
326  {
327  Foam::reduce(value, sumOp<T>(), tag, comm);
328  Foam::reduce(count, sumOp<label>(), tag, comm);
329  }
330 }
331 
332 
333 // Floating-point sum-reduce
334 
335 #undef Pstream_SumReduce
336 #define Pstream_SumReduce(Native) \
337  \
338  \
339 void sumReduce \
340 ( \
341  Native& value, \
342  label& count, \
343  const int tag = UPstream::msgType(), \
344  const label comm = UPstream::worldComm \
345 );
346 
348 Pstream_SumReduce(float);
349 Pstream_SumReduce(double);
350 
351 #undef Pstream_SumReduce
352 
353 
354 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
355 
356 // Convenience wrappers for some reduction operations
357 // - defined after all specialisations are known
358 
359 //- Perform reduction on a copy, using specified binary operation
360 // \return the resulting value
361 template<class T, class BinaryOp>
363 (
364  const T& value,
365  const BinaryOp& bop,
366  const int tag = UPstream::msgType(),
367  const label comm = UPstream::worldComm
368 )
369 {
370  T work(value);
371  Foam::reduce(work, bop, tag, comm);
372  return work;
373 }
374 
375 
376 //- Perform logical (and) MPI Allreduce on a copy. Uses UPstream::reduceAnd
377 // \return the resulting value
378 inline bool returnReduceAnd
379 (
380  const bool value,
381  const label comm = UPstream::worldComm
382 )
383 {
384  bool work(value);
385  UPstream::reduceAnd(work, comm);
386  return work;
387 }
388 
389 
390 //- Perform logical (or) MPI Allreduce on a copy. Uses UPstream::reduceOr
391 // \return the resulting value
392 inline bool returnReduceOr
393 (
394  const bool value,
395  const label comm = UPstream::worldComm
396 )
397 {
398  bool work(value);
399  UPstream::reduceOr(work, comm);
400  return work;
401 }
402 
403 
404 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
405 
406 } // End namespace Foam
407 
408 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
409 
410 #endif
411 
412 // ************************************************************************* //
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
void sumReduce(T &value, label &count, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Reduce inplace (cf. MPI Allreduce) the sum of both value and count (for averaging) ...
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:639
Various functors for unary and binary operations. Can be used for parallel combine-reduce operations ...
static void reduceOr(bool &value, const label communicator=worldComm)
Logical (or) reduction (cf. MPI AllReduce)
static const List< commsStruct > & whichCommunication(const label communicator=worldComm)
Communication schedule for linear/tree all-to-master (proc 0). Chooses based on the value of UPstream...
Definition: UPstream.H:790
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:806
static label worldComm
Default world communicator (all processors). May differ from globalComm if local worlds are in use...
Definition: UPstream.H:361
T returnReduce(const T &value, const BinaryOp &bop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Perform reduction on a copy, using specified binary operation.
static void broadcast(Type &value, const label comm=UPstream::worldComm)
Broadcast content (contiguous or non-contiguous) to all processes in communicator.
#define Pstream_FloatReductions(Native)
static void gather(const List< commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
Gather (reduce) data, appyling bop to combine value from different processors. The basis for Foam::re...
Definition: PstreamGather.C:37
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
bool returnReduceAnd(const bool value, const label comm=UPstream::worldComm)
Perform logical (and) MPI Allreduce on a copy. Uses UPstream::reduceAnd.
static void reduceAnd(bool &value, const label communicator=worldComm)
Logical (and) reduction (cf. MPI AllReduce)
static void printStack(Ostream &os)
Helper function to print a stack.
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:366
#define Pstream_CommonReductions(Native)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
#define Pstream_SumReduce(Native)
void reduce(const List< UPstream::commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
Reduce inplace (cf. MPI Allreduce) using specified communication schedule.
bool returnReduceOr(const bool value, const label comm=UPstream::worldComm)
Perform logical (or) MPI Allreduce on a copy. Uses UPstream::reduceOr.
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:666
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Namespace for OpenFOAM.