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 | Copyright (C) 2011-2013 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #ifndef PstreamReduceOps_H
27 #define PstreamReduceOps_H
28 
29 #include "Pstream.H"
30 #include "ops.H"
31 #include "vector2D.H"
32 
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 
38 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
39 
40 // Reduce operation with user specified communication schedule
41 template<class T, class BinaryOp>
42 void reduce
43 (
44  const List<UPstream::commsStruct>& comms,
45  T& Value,
46  const BinaryOp& bop,
47  const int tag,
48  const label comm
49 )
50 {
51  if (UPstream::warnComm != -1 && comm != UPstream::warnComm)
52  {
53  Pout<< "** reducing:" << Value << " with comm:" << comm
54  << endl;
56  }
57  Pstream::gather(comms, Value, bop, tag, comm);
58  Pstream::scatter(comms, Value, tag, comm);
59 }
60 
61 
62 // Reduce using either linear or tree communication schedule
63 template<class T, class BinaryOp>
64 void reduce
65 (
66  T& Value,
67  const BinaryOp& bop,
68  const int tag = Pstream::msgType(),
69  const label comm = UPstream::worldComm
70 )
71 {
73  {
74  reduce(UPstream::linearCommunication(comm), Value, bop, tag, comm);
75  }
76  else
77  {
78  reduce(UPstream::treeCommunication(comm), Value, bop, tag, comm);
79  }
80 }
81 
82 
83 // Reduce using either linear or tree communication schedule
84 template<class T, class BinaryOp>
86 (
87  const T& Value,
88  const BinaryOp& bop,
89  const int tag = Pstream::msgType(),
90  const label comm = UPstream::worldComm
91 )
92 {
93  T WorkValue(Value);
94 
96  {
97  reduce
98  (
100  WorkValue,
101  bop,
102  tag,
103  comm
104  );
105  }
106  else
107  {
108  reduce
109  (
111  WorkValue,
112  bop,
113  tag,
114  comm
115  );
116  }
117 
118  return WorkValue;
119 }
120 
121 
122 // Reduce with sum of both value and count (for averaging)
123 template<class T>
124 void sumReduce
125 (
126  T& Value,
127  label& Count,
128  const int tag = Pstream::msgType(),
129  const label comm = UPstream::worldComm
130 )
131 {
132  reduce(Value, sumOp<T>(), tag, comm);
133  reduce(Count, sumOp<label>(), tag, comm);
134 }
135 
136 
137 // Non-blocking version of reduce. Sets request.
138 template<class T, class BinaryOp>
139 void reduce
140 (
141  T& Value,
142  const BinaryOp& bop,
143  const int tag,
144  const label comm,
145  label& request
146 )
147 {
149  (
150  "reduce(T&, const BinaryOp&, const int, const label, label&"
151  );
152 }
153 
154 
155 // Insist there are specialisations for the common reductions of scalar(s)
156 void reduce
157 (
158  scalar& Value,
159  const sumOp<scalar>& bop,
160  const int tag = Pstream::msgType(),
161  const label comm = UPstream::worldComm
162 );
163 
164 void reduce
165 (
166  scalar& Value,
167  const minOp<scalar>& bop,
168  const int tag = Pstream::msgType(),
169  const label comm = UPstream::worldComm
170 );
171 
172 void reduce
173 (
174  vector2D& Value,
175  const sumOp<vector2D>& bop,
176  const int tag = Pstream::msgType(),
177  const label comm = UPstream::worldComm
178 );
179 
180 void sumReduce
181 (
182  scalar& Value,
183  label& Count,
184  const int tag = Pstream::msgType(),
185  const label comm = UPstream::worldComm
186 );
187 
188 void reduce
189 (
190  scalar& Value,
191  const sumOp<scalar>& bop,
192  const int tag,
193  const label comm,
194  label& request
195 );
196 
197 
198 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
199 
200 } // End namespace Foam
201 
202 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
203 
204 #endif
205 
206 // ************************************************************************* //
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
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:59
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:47
static void printStack(Ostream &)
Helper function to print a stack.
Namespace for OpenFOAM.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:386
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
static const List< commsStruct > & linearCommunication(const label communicator=0)
Communication schedule for linear all-to-master (proc 0)
Definition: UPstream.H:434
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:264
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:451
static const List< commsStruct > & treeCommunication(const label communicator=0)
Communication schedule for tree all-to-master (proc 0)
Definition: UPstream.H:443
Combination-Reduction operation for a parallel run.
#define notImplemented(functionName)
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:356
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:261
void sumReduce(T &Value, label &Count, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
prefixOSstream Pout(cout,"Pout")
Definition: IOstreams.H:53
static int nProcsSimpleSum
Number of processors at which the sum algorithm changes from linear.
Definition: UPstream.H:252