PstreamReduceOps.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 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 InNamespace
25  Foam
26 
27 Description
28  Inter-processor communication reduction functions.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #ifndef PstreamReduceOps_H
33 #define PstreamReduceOps_H
34 
35 #include "Pstream.H"
36 #include "ops.H"
37 #include "vector2D.H"
38 
39 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
40 
41 namespace Foam
42 {
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 // Reduce operation with user specified communication schedule
47 template<class T, class BinaryOp>
48 void reduce
49 (
50  const List<UPstream::commsStruct>& comms,
51  T& Value,
52  const BinaryOp& bop,
53  const int tag,
54  const label comm
55 )
56 {
57  if (UPstream::warnComm != -1 && comm != UPstream::warnComm)
58  {
59  Pout<< "** reducing:" << Value << " with comm:" << comm
60  << endl;
62  }
63  Pstream::gather(comms, Value, bop, tag, comm);
64  Pstream::scatter(comms, Value, tag, comm);
65 }
66 
67 
68 // Reduce using either linear or tree communication schedule
69 template<class T, class BinaryOp>
70 void reduce
71 (
72  T& Value,
73  const BinaryOp& bop,
74  const int tag = Pstream::msgType(),
75  const label comm = UPstream::worldComm
76 )
77 {
79  {
80  reduce(UPstream::linearCommunication(comm), Value, bop, tag, comm);
81  }
82  else
83  {
84  reduce(UPstream::treeCommunication(comm), Value, bop, tag, comm);
85  }
86 }
87 
88 
89 // Reduce using either linear or tree communication schedule
90 template<class T, class BinaryOp>
92 (
93  const T& Value,
94  const BinaryOp& bop,
95  const int tag = Pstream::msgType(),
96  const label comm = UPstream::worldComm
97 )
98 {
99  T WorkValue(Value);
100 
102  {
103  reduce
104  (
106  WorkValue,
107  bop,
108  tag,
109  comm
110  );
111  }
112  else
113  {
114  reduce
115  (
117  WorkValue,
118  bop,
119  tag,
120  comm
121  );
122  }
123 
124  return WorkValue;
125 }
126 
127 
128 // Reduce with sum of both value and count (for averaging)
129 template<class T>
130 void sumReduce
131 (
132  T& Value,
133  label& Count,
134  const int tag = Pstream::msgType(),
135  const label comm = UPstream::worldComm
136 )
137 {
138  reduce(Value, sumOp<T>(), tag, comm);
139  reduce(Count, sumOp<label>(), tag, comm);
140 }
141 
142 
143 // Non-blocking version of reduce. Sets request.
144 template<class T, class BinaryOp>
145 void reduce
146 (
147  T& Value,
148  const BinaryOp& bop,
149  const int tag,
150  const label comm,
151  label& request
152 )
153 {
155 }
156 
157 
158 // Insist there are specialisations for the common reductions of scalar(s)
159 void reduce
160 (
161  scalar& Value,
162  const sumOp<scalar>& bop,
163  const int tag = Pstream::msgType(),
164  const label comm = UPstream::worldComm
165 );
166 
167 void reduce
168 (
169  scalar& Value,
170  const minOp<scalar>& bop,
171  const int tag = Pstream::msgType(),
172  const label comm = UPstream::worldComm
173 );
174 
175 void reduce
176 (
177  vector2D& Value,
178  const sumOp<vector2D>& bop,
179  const int tag = Pstream::msgType(),
180  const label comm = UPstream::worldComm
181 );
182 
183 void sumReduce
184 (
185  scalar& Value,
186  label& Count,
187  const int tag = Pstream::msgType(),
188  const label comm = UPstream::worldComm
189 );
190 
191 void reduce
192 (
193  scalar& Value,
194  const sumOp<scalar>& bop,
195  const int tag,
196  const label comm,
197  label& request
198 );
199 
200 
201 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
202 
203 } // End namespace Foam
204 
205 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
206 
207 #endif
208 
209 // ************************************************************************* //
static void printStack(Ostream &)
Helper function to print a stack.
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
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 int nProcsSimpleSum
Number of processors at which the sum algorithm changes from linear.
Definition: UPstream.H:269
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
Combination-Reduction operation for a parallel run.
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:476
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:278
static const List< commsStruct > & linearCommunication(const label communicator=0)
Communication schedule for linear all-to-master (proc 0)
Definition: UPstream.H:459
void sumReduce(T &Value, label &Count, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
static const List< commsStruct > & treeCommunication(const label communicator=0)
Communication schedule for tree all-to-master (proc 0)
Definition: UPstream.H:468
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:281
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:411
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:366
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
Namespace for OpenFOAM.