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 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 PstreamReduceOps_H
36 #define PstreamReduceOps_H
37 
38 #include "ops.H"
39 #include "vector2D.H"
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 // Reduce operation with user specified communication schedule
49 template<class T, class BinaryOp>
50 void reduce
51 (
52  const List<UPstream::commsStruct>& comms,
53  T& Value,
54  const BinaryOp& bop,
55  const int tag,
56  const label comm
57 )
58 {
59  if (UPstream::warnComm != -1 && comm != UPstream::warnComm)
60  {
61  Pout<< "** reducing:" << Value << " with comm:" << comm
62  << endl;
64  }
65  Pstream::gather(comms, Value, bop, tag, comm);
66  Pstream::scatter(comms, Value, tag, comm);
67 }
68 
69 
70 // Reduce using either linear or tree communication schedule
71 template<class T, class BinaryOp>
72 void reduce
73 (
74  T& Value,
75  const BinaryOp& bop,
76  const int tag = Pstream::msgType(),
77  const label comm = UPstream::worldComm
78 )
79 {
81  {
82  reduce(UPstream::linearCommunication(comm), Value, bop, tag, comm);
83  }
84  else
85  {
86  reduce(UPstream::treeCommunication(comm), Value, bop, tag, comm);
87  }
88 }
89 
90 
91 // Reduce using either linear or tree communication schedule
92 template<class T, class BinaryOp>
94 (
95  const T& Value,
96  const BinaryOp& bop,
97  const int tag = Pstream::msgType(),
98  const label comm = UPstream::worldComm
99 )
100 {
101  T WorkValue(Value);
102 
104  {
105  reduce
106  (
108  WorkValue,
109  bop,
110  tag,
111  comm
112  );
113  }
114  else
115  {
116  reduce
117  (
119  WorkValue,
120  bop,
121  tag,
122  comm
123  );
124  }
125 
126  return WorkValue;
127 }
128 
129 
130 // Reduce with sum of both value and count (for averaging)
131 template<class T>
132 void sumReduce
133 (
134  T& Value,
135  label& Count,
136  const int tag = Pstream::msgType(),
137  const label comm = UPstream::worldComm
138 )
139 {
140  reduce(Value, sumOp<T>(), tag, comm);
141  reduce(Count, sumOp<label>(), tag, comm);
142 }
143 
144 
145 // Non-blocking version of reduce. Sets request.
146 template<class T, class BinaryOp>
147 void reduce
148 (
149  T& Value,
150  const BinaryOp& bop,
151  const int tag,
152  const label comm,
153  label& request
154 )
155 {
157 }
158 
159 // Non-blocking version of reduce. Sets request.
160 template<class T, class BinaryOp>
161 void reduce
162 (
163  T Value[],
164  const int size,
165  const BinaryOp& bop,
166  const int tag,
167  const label comm,
168  label& request
169 )
170 {
172 }
173 
174 
175 // Insist there are specialisations for the common reductions of scalar(s)
176 void reduce
177 (
178  scalar& Value,
179  const sumOp<scalar>& bop,
180  const int tag = Pstream::msgType(),
181  const label comm = UPstream::worldComm
182 );
183 
184 void reduce
185 (
186  scalar& Value,
187  const minOp<scalar>& bop,
188  const int tag = Pstream::msgType(),
189  const label comm = UPstream::worldComm
190 );
191 
192 void reduce
193 (
194  vector2D& Value,
195  const sumOp<vector2D>& bop,
196  const int tag = Pstream::msgType(),
197  const label comm = UPstream::worldComm
198 );
199 
200 void sumReduce
201 (
202  scalar& Value,
203  label& Count,
204  const int tag = Pstream::msgType(),
205  const label comm = UPstream::worldComm
206 );
207 
208 void reduce
209 (
210  scalar& Value,
211  const sumOp<scalar>& bop,
212  const int tag,
213  const label comm,
214  label& request
215 );
216 
217 void reduce
218 (
219  scalar Value[],
220  const int size,
221  const sumOp<scalar>& bop,
222  const int tag,
223  const label comm,
224  label& request
225 );
226 
227 
228 #if defined(WM_SPDP)
229 void reduce
230 (
231  solveScalar& Value,
232  const sumOp<solveScalar>& bop,
233  const int tag = Pstream::msgType(),
234  const label comm = UPstream::worldComm
235 );
236 
237 void reduce
238 (
239  solveScalar& Value,
240  const minOp<solveScalar>& bop,
241  const int tag = Pstream::msgType(),
242  const label comm = UPstream::worldComm
243 );
244 
245 void reduce
246 (
247  Vector2D<solveScalar>& Value,
248  const sumOp<Vector2D<solveScalar>>& bop,
249  const int tag = Pstream::msgType(),
250  const label comm = UPstream::worldComm
251 );
252 
253 void sumReduce
254 (
255  solveScalar& Value,
256  label& Count,
257  const int tag = Pstream::msgType(),
258  const label comm = UPstream::worldComm
259 );
260 
261 void reduce
262 (
263  solveScalar& Value,
264  const sumOp<solveScalar>& bop,
265  const int tag,
266  const label comm,
267  label& request
268 );
269 
270 void reduce
271 (
272  solveScalar Value[],
273  const int size,
274  const sumOp<solveScalar>& bop,
275  const int tag,
276  const label comm,
277  label& request
278 );
279 #endif
280 
281 
282 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
283 
284 } // End namespace Foam
285 
286 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
287 
288 #endif
289 
290 // ************************************************************************* //
Foam::UPstream::warnComm
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:296
Foam::UPstream::linearCommunication
static const List< commsStruct > & linearCommunication(const label communicator=worldComm)
Communication schedule for linear all-to-master (proc 0)
Definition: UPstream.H:523
Foam::error::printStack
static void printStack(Ostream &os)
Helper function to print a stack.
Definition: dummyPrintStack.C:36
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:94
ops.H
Various functors for unary and binary operations. Can be used for parallel combine-reduce operations ...
Foam::Pstream::scatter
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
Definition: gatherScatter.C:150
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Foam::Pstream::gather
static void gather(const List< commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Definition: gatherScatter.C:50
Foam::sumOp
Definition: ops.H:213
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:517
Foam::reduce
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Definition: PstreamReduceOps.H:51
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
vector2D.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::UPstream::nProcsSimpleSum
static int nProcsSimpleSum
Definition: UPstream.H:278
Foam::UPstream::msgType
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:540
Foam::vector2D
Vector2D< scalar > vector2D
A 2D vector of scalars obtained from the generic Vector2D.
Definition: vector2D.H:51
Foam::UPstream::worldComm
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:293
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::UPstream::treeCommunication
static const List< commsStruct > & treeCommunication(const label communicator=worldComm)
Communication schedule for tree all-to-master (proc 0)
Definition: UPstream.H:532
Foam::sumReduce
void sumReduce(T &Value, label &Count, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:133
Foam::UPstream::nProcs
static label nProcs(const label communicator=worldComm)
Number of processes in parallel run, and 1 for serial run.
Definition: UPstream.H:445