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-------------------------------------------------------------------------------
11License
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
27InNamespace
28 Foam
29
30Description
31 Inter-processor communication reduction functions.
32
33\*---------------------------------------------------------------------------*/
34
35#ifndef Foam_PstreamReduceOps_H
36#define Foam_PstreamReduceOps_H
37
38#include "ops.H"
39#include "FixedList.H"
40
41// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42
43namespace Foam
44{
45
46// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47
48//- Reduce inplace (cf. MPI Allreduce)
49//- using specified communication schedule.
50template<class T, class BinaryOp>
52(
53 const List<UPstream::commsStruct>& comms,
54 T& value,
55 const BinaryOp& bop,
56 const int tag,
57 const label comm
58)
59{
60 if (UPstream::warnComm != -1 && comm != UPstream::warnComm)
61 {
62 Pout<< "** reducing:" << value << " with comm:" << comm << endl;
64 }
65 Pstream::gather(comms, value, bop, tag, comm);
66 Pstream::broadcast(value, comm);
67}
68
69
70//- Reduce inplace (cf. MPI Allreduce)
71//- using linear/tree communication schedule
72template<class T, class BinaryOp>
74(
75 T& value,
76 const BinaryOp& bop,
77 const int tag = UPstream::msgType(),
78 const label comm = UPstream::worldComm
79)
80{
81 if (UPstream::parRun())
82 {
83 reduce(UPstream::whichCommunication(comm), value, bop, tag, comm);
84 }
85}
86
87
88//- Reduce (copy) and return value
89template<class T, class BinaryOp>
91(
92 const T& value,
93 const BinaryOp& bop,
94 const int tag = UPstream::msgType(),
95 const label comm = UPstream::worldComm
96)
97{
98 T work(value);
99 reduce(work, bop, tag, comm);
100 return work;
101}
102
103
104//- Reduce inplace (cf. MPI Allreduce)
105//- the sum of both value and count (for averaging)
106template<class T>
108(
109 T& value,
110 label& count,
111 const int tag = UPstream::msgType(),
112 const label comm = UPstream::worldComm
113)
114{
115 if (UPstream::parRun())
116 {
117 reduce(value, sumOp<T>(), tag, comm);
118 reduce(count, sumOp<label>(), tag, comm);
119 }
120}
121
122
123//- Reduce inplace (cf. MPI Allreduce)
124//- multiple values (identical size on all processes!)
125template<class T, class BinaryOp>
127(
128 T values[],
129 const int size,
130 const BinaryOp&,
131 const int tag,
132 const label comm
133)
134{
136}
137
138//- Non-blocking reduce inplace (cf. MPI Iallreduce)
139//- single value. Sets request.
140template<class T, class BinaryOp>
142(
143 T& Value,
144 const BinaryOp&,
145 const int tag,
146 const label comm,
147 label& request
148)
149{
151}
152
153//- Non-blocking reduce inplace (cf. MPI Iallreduce)
154//- of multiple values (same size on all processes!)
155// Sets request.
156template<class T, class BinaryOp>
158(
159 T values[],
160 const int size,
161 const BinaryOp&,
162 const int tag,
163 const label comm,
164 label& request
165)
166{
168}
169
170
171// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
172
173// Specialisations for bool
174
175//- Logical (and) reduction (cf. MPI AllReduce)
176void reduce
177(
178 bool& value,
179 const andOp<bool>&,
180 const int tag = UPstream::msgType(),
181 const label comm = UPstream::worldComm
182);
183
184//- Logical (or) reduction (cf. MPI AllReduce)
185void reduce
186(
187 bool& value,
188 const orOp<bool>&,
189 const int tag = UPstream::msgType(),
190 const label comm = UPstream::worldComm
191);
192
193
194// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
195
196// Specialisations for common reduction types
197
198#undef Pstream_CommonReductions
199#define Pstream_CommonReductions(Native) \
200 \
201 \
202void reduce \
203( \
204 Native& value, \
205 const minOp<Native>&, \
206 const int tag = UPstream::msgType(), \
207 const label comm = UPstream::worldComm \
208); \
209 \
210 \
211void reduce \
212( \
213 Native& value, \
214 const maxOp<Native>&, \
215 const int tag = UPstream::msgType(), \
216 const label comm = UPstream::worldComm \
217); \
218 \
219 \
220void reduce \
221( \
222 Native& value, \
223 const sumOp<Native>&, \
224 const int tag = UPstream::msgType(), \
225 const label comm = UPstream::worldComm \
226); \
227 \
228 \
229void reduce \
230( \
231 Native values[], \
232 const int size, \
233 const sumOp<Native>&, \
234 const int tag, \
235 const label comm \
236); \
237 \
238 \
239template<unsigned N> \
240inline void reduce \
241( \
242 FixedList<Native, N>& values, \
243 const sumOp<Native>&, \
244 const int tag = UPstream::msgType(), \
245 const label comm = UPstream::worldComm \
246) \
247{ \
248 reduce(values.data(), int(values.size()), sumOp<Native>(), tag, comm); \
249}
250
251
258
259#undef Pstream_CommonReductions
260
261
262// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
263
264// Specialisations for floating-point types
265
266#undef Pstream_FloatReductions
267#define Pstream_FloatReductions(Native) \
268 \
269 \
270void sumReduce \
271( \
272 Native& value, \
273 label& count, \
274 const int tag = UPstream::msgType(), \
275 const label comm = UPstream::worldComm \
276); \
277 \
278 \
279void reduce \
280( \
281 Native& value, \
282 const sumOp<Native>&, \
283 const int tag, \
284 const label comm, \
285 label& requestID \
286); \
287 \
288 \
289void reduce \
290( \
291 Native values[], \
292 const int size, \
293 const sumOp<Native>&, \
294 const int tag, \
295 const label comm, \
296 label& requestID \
297);
298
299
302
303#undef Pstream_FloatReductions
304
305
306// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
307
308} // End namespace Foam
309
310// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
311
312#endif
313
314// ************************************************************************* //
#define Pstream_FloatReductions(Native)
#define Pstream_CommonReductions(Native)
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
static void gather(const List< commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
Definition: PstreamGather.C:43
static void broadcast(Type &value, const label comm=UPstream::worldComm)
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:296
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:556
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:293
static const List< commsStruct > & whichCommunication(const label communicator=worldComm)
Definition: UPstream.H:542
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
static void printStack(Ostream &os)
Helper function to print a stack.
const volScalarField & T
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:517
Namespace for OpenFOAM.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
void reduce(const List< UPstream::commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
void sumReduce(T &value, label &count, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
T returnReduce(const T &value, const BinaryOp &bop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Reduce (copy) and return value.
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Various functors for unary and binary operations. Can be used for parallel combine-reduce operations ...