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 
160 // Insist there are specialisations for the common reductions of scalar(s)
161 void reduce
162 (
163  scalar& Value,
164  const sumOp<scalar>& bop,
165  const int tag = Pstream::msgType(),
166  const label comm = UPstream::worldComm
167 );
168 
169 void reduce
170 (
171  scalar& Value,
172  const minOp<scalar>& bop,
173  const int tag = Pstream::msgType(),
174  const label comm = UPstream::worldComm
175 );
176 
177 void reduce
178 (
179  vector2D& Value,
180  const sumOp<vector2D>& bop,
181  const int tag = Pstream::msgType(),
182  const label comm = UPstream::worldComm
183 );
184 
185 void sumReduce
186 (
187  scalar& Value,
188  label& Count,
189  const int tag = Pstream::msgType(),
190  const label comm = UPstream::worldComm
191 );
192 
193 void reduce
194 (
195  scalar& Value,
196  const sumOp<scalar>& bop,
197  const int tag,
198  const label comm,
199  label& request
200 );
201 
202 
203 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
204 
205 } // End namespace Foam
206 
207 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
208 
209 #endif
210 
211 // ************************************************************************* //
Foam::UPstream::warnComm
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:288
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
Foam::UPstream::nProcs
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:426
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:337
Foam::Pout
prefixOSstream Pout
An Ostream wrapper for parallel output to std::cout.
Foam::Pstream::gather
static void gather(const List< commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Gather data. Apply bop to combine Value.
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:419
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::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
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:270
Foam::UPstream::msgType
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:491
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:285
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
Foam::sumReduce
void sumReduce(T &Value, label &Count, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:133
Foam::UPstream::linearCommunication
static const List< commsStruct > & linearCommunication(const label communicator=0)
Communication schedule for linear all-to-master (proc 0)
Definition: UPstream.H:474
Foam::UPstream::treeCommunication
static const List< commsStruct > & treeCommunication(const label communicator=0)
Communication schedule for tree all-to-master (proc 0)
Definition: UPstream.H:483