UPstreamReduce.C
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) 2022-2023 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "Pstream.H"
29 #include "PstreamReduceOps.H"
30 #include "UPstreamWrapping.H"
31 
32 #include <cinttypes>
33 
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 
36 // Special reductions for bool
37 
38 void Foam::UPstream::reduceAnd(bool& value, const label comm)
39 {
40  PstreamDetail::allReduce(&value, 1, MPI_C_BOOL, MPI_LAND, comm);
41 }
42 
43 
44 void Foam::UPstream::reduceOr(bool& value, const label comm)
45 {
46  PstreamDetail::allReduce(&value, 1, MPI_C_BOOL, MPI_LOR, comm);
47 }
48 
49 
50 void Foam::reduce
51 (
52  bool& value,
53  const andOp<bool>&,
54  const int tag, /* (unused) */
55  const label comm
56 )
57 {
58  PstreamDetail::allReduce(&value, 1, MPI_C_BOOL, MPI_LAND, comm);
59 }
60 
61 
62 void Foam::reduce
63 (
64  bool& value,
65  const orOp<bool>&,
66  const int tag, /* (unused) */
67  const label comm
68 )
69 {
70  PstreamDetail::allReduce(&value, 1, MPI_C_BOOL, MPI_LOR, comm);
71 }
72 
73 
74 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
75 
76 // Common reductions
77 
78 #undef Pstream_CommonReductions
79 #define Pstream_CommonReductions(Native, TaggedType) \
80  \
81 void Foam::reduce \
82 ( \
83  Native values[], \
84  const int size, \
85  const minOp<Native>&, \
86  const int tag, /* (unused) */ \
87  const label comm \
88 ) \
89 { \
90  PstreamDetail::allReduce<Native> \
91  ( \
92  values, size, TaggedType, MPI_MIN, comm \
93  ); \
94 } \
95  \
96 void Foam::reduce \
97 ( \
98  Native values[], \
99  const int size, \
100  const maxOp<Native>&, \
101  const int tag, /* (unused) */ \
102  const label comm \
103 ) \
104 { \
105  PstreamDetail::allReduce<Native> \
106  ( \
107  values, size, TaggedType, MPI_MAX, comm \
108  ); \
109 } \
110  \
111 void Foam::reduce \
112 ( \
113  Native values[], \
114  const int size, \
115  const sumOp<Native>&, \
116  const int tag, /* (unused) */ \
117  const label comm \
118 ) \
119 { \
120  PstreamDetail::allReduce<Native> \
121  ( \
122  values, size, TaggedType, MPI_SUM, comm \
123  ); \
124 } \
125  \
126 void Foam::reduce \
127 ( \
128  Native& value, \
129  const minOp<Native>&, \
130  const int tag, /* (unused) */ \
131  const label comm \
132 ) \
133 { \
134  PstreamDetail::allReduce<Native> \
135  ( \
136  &value, 1, TaggedType, MPI_MIN, comm \
137  ); \
138 } \
139  \
140 void Foam::reduce \
141 ( \
142  Native& value, \
143  const maxOp<Native>&, \
144  const int tag, /* (unused) */ \
145  const label comm \
146 ) \
147 { \
148  PstreamDetail::allReduce<Native> \
149  ( \
150  &value, 1, TaggedType, MPI_MAX, comm \
151  ); \
152 } \
153  \
154 void Foam::reduce \
155 ( \
156  Native& value, \
157  const sumOp<Native>&, \
158  const int tag, /* (unused) */ \
159  const label comm \
160 ) \
161 { \
162  PstreamDetail::allReduce<Native> \
163  ( \
164  &value, 1, TaggedType, MPI_SUM, comm \
165  ); \
166 }
168 
169 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
170 
171 // Floating-point reductions
172 
173 #undef Pstream_FloatReductions
174 #define Pstream_FloatReductions(Native, TaggedType) \
175  \
176 Pstream_CommonReductions(Native, TaggedType); \
177  \
178 void Foam::reduce \
179 ( \
180  Native values[], \
181  const int size, \
182  const sumOp<Native>&, \
183  const int tag, /* (unused) */ \
184  const label comm, \
185  UPstream::Request& req \
186 ) \
187 { \
188  PstreamDetail::allReduce<Native> \
189  ( \
190  values, size, TaggedType, MPI_SUM, comm, &req, nullptr \
191  ); \
192 } \
193  \
194 /* Deprecated: prefer version with UPstream::Request */ \
195 void Foam::reduce \
196 ( \
197  Native values[], \
198  const int size, \
199  const sumOp<Native>&, \
200  const int tag, /* (unused) */ \
201  const label comm, \
202  label& requestID \
203 ) \
204 { \
205  PstreamDetail::allReduce<Native> \
206  ( \
207  values, size, TaggedType, MPI_SUM, comm, nullptr, &requestID \
208  ); \
209 } \
210  \
211 void Foam::reduce \
212 ( \
213  Native& value, \
214  const sumOp<Native>&, \
215  const int tag, /* (unused) */ \
216  const label comm, \
217  UPstream::Request& req \
218 ) \
219 { \
220  PstreamDetail::allReduce<Native> \
221  ( \
222  &value, 1, TaggedType, MPI_SUM, comm, &req, nullptr \
223  ); \
224 } \
225  \
226 /* Deprecated: prefer version with UPstream::Request */ \
227 void Foam::reduce \
228 ( \
229  Native& value, \
230  const sumOp<Native>&, \
231  const int tag, /* (unused) */ \
232  const label comm, \
233  label& requestID \
234 ) \
235 { \
236  PstreamDetail::allReduce<Native> \
237  ( \
238  &value, 1, TaggedType, MPI_SUM, comm, nullptr, &requestID \
239  ); \
240 } \
241  \
242 void Foam::sumReduce \
243 ( \
244  Native& value, \
245  label& count, \
246  const int tag, /* (unused) */ \
247  const label comm \
248 ) \
249 { \
250  if (UPstream::is_parallel(comm)) \
251  { \
252  Native values[2]; \
253  values[0] = static_cast<Native>(count); \
254  values[1] = value; \
255  \
256  PstreamDetail::allReduce<Native> \
257  ( \
258  values, 2, TaggedType, MPI_SUM, comm \
259  ); \
260  \
261  count = static_cast<label>(values[0]); \
262  value = values[1]; \
263  } \
264 }
265 
266 
267 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
268 
269 Pstream_CommonReductions(int32_t, MPI_INT32_T);
270 Pstream_CommonReductions(int64_t, MPI_INT64_T);
271 Pstream_CommonReductions(uint32_t, MPI_UINT32_T);
272 Pstream_CommonReductions(uint64_t, MPI_UINT64_T);
273 
274 Pstream_FloatReductions(float, MPI_FLOAT);
275 Pstream_FloatReductions(double, MPI_DOUBLE);
276 
277 
278 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
279 
280 #undef Pstream_CommonReductions
281 #undef Pstream_FloatReductions
282 
283 
284 // ************************************************************************* //
#define Pstream_CommonReductions(Native, TaggedType)
Inter-processor communication reduction functions.
#define Pstream_FloatReductions(Native, TaggedType)
Functions to wrap MPI_Bcast, MPI_Allreduce, MPI_Iallreduce etc.
static void reduceOr(bool &value, const label communicator=worldComm)
Logical (or) reduction (MPI_AllReduce)
void allReduce(Type *values, int count, MPI_Datatype datatype, MPI_Op optype, const label comm, UPstream::Request *req=nullptr, label *requestID=nullptr)
static void reduceAnd(bool &value, const label communicator=worldComm)
Logical (and) reduction (MPI_AllReduce)
void reduce(const List< UPstream::commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
Reduce inplace (cf. MPI Allreduce) using specified communication schedule.