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-2015 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 
151 
152 // Insist there are specialisations for the common reductions of scalar(s)
153 void reduce
154 (
155  scalar& Value,
156  const sumOp<scalar>& bop,
157  const int tag = Pstream::msgType(),
158  const label comm = UPstream::worldComm
159 );
160 
161 void reduce
162 (
163  scalar& Value,
164  const minOp<scalar>& bop,
165  const int tag = Pstream::msgType(),
166  const label comm = UPstream::worldComm
167 );
168 
169 void reduce
170 (
171  vector2D& Value,
172  const sumOp<vector2D>& bop,
173  const int tag = Pstream::msgType(),
174  const label comm = UPstream::worldComm
175 );
176 
177 void sumReduce
178 (
179  scalar& Value,
180  label& Count,
181  const int tag = Pstream::msgType(),
182  const label comm = UPstream::worldComm
183 );
184 
185 void reduce
186 (
187  scalar& Value,
188  const sumOp<scalar>& bop,
189  const int tag,
190  const label comm,
191  label& request
192 );
193 
194 
195 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
196 
197 } // End namespace Foam
198 
199 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
200 
201 #endif
202 
203 // ************************************************************************* //
Foam::UPstream::warnComm
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:261
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:86
Foam::UPstream::nProcs
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:387
ops.H
Combination-Reduction operation for a parallel run.
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:147
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
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:47
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:365
Foam::reduce
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Definition: PstreamReduceOps.H:43
Foam::error::printStack
static void printStack(Ostream &)
Helper function to print a stack.
Definition: dummyPrintStack.C:30
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:59
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:55
vector2D.H
Pstream.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::UPstream::nProcsSimpleSum
static int nProcsSimpleSum
Number of processors at which the sum algorithm changes from linear.
Definition: UPstream.H:249
Foam::UPstream::msgType
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:452
Foam::sumOp
Definition: ops.H:162
Foam::Pout
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Foam::vector2D
Vector2D< scalar > vector2D
vector2D obtained from generic Vector2D
Definition: vector2D.H:49
Foam::UPstream::worldComm
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:258
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:59
Foam::sumReduce
void sumReduce(T &Value, label &Count, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:125
Foam::UPstream::linearCommunication
static const List< commsStruct > & linearCommunication(const label communicator=0)
Communication schedule for linear all-to-master (proc 0)
Definition: UPstream.H:435
Foam::UPstream::treeCommunication
static const List< commsStruct > & treeCommunication(const label communicator=0)
Communication schedule for tree all-to-master (proc 0)
Definition: UPstream.H:444