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 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 <mpi.h>
33 #include <cinttypes>
34 
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 
37 // Special reductions for bool
38 
39 void Foam::UPstream::reduceAnd(bool& value, const label comm)
40 {
41  PstreamDetail::allReduce(&value, 1, MPI_C_BOOL, MPI_LAND, comm);
42 }
43 
44 
45 void Foam::UPstream::reduceOr(bool& value, const label comm)
46 {
47  PstreamDetail::allReduce(&value, 1, MPI_C_BOOL, MPI_LOR, comm);
48 }
49 
50 
51 void Foam::reduce
52 (
53  bool& value,
54  const andOp<bool>&,
55  const int tag, /* (unused) */
56  const label comm
57 )
58 {
59  PstreamDetail::allReduce(&value, 1, MPI_C_BOOL, MPI_LAND, comm);
60 }
61 
62 
63 void Foam::reduce
64 (
65  bool& value,
66  const orOp<bool>&,
67  const int tag, /* (unused) */
68  const label comm
69 )
70 {
71  PstreamDetail::allReduce(&value, 1, MPI_C_BOOL, MPI_LOR, comm);
72 }
73 
74 
75 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
76 
77 // Common reductions
78 
79 #undef Pstream_CommonReductions
80 #define Pstream_CommonReductions(Native, TaggedType) \
81  \
82 void Foam::reduce \
83 ( \
84  Native values[], \
85  const int size, \
86  const minOp<Native>&, \
87  const int tag, /* (unused) */ \
88  const label comm \
89 ) \
90 { \
91  PstreamDetail::allReduce<Native> \
92  ( \
93  values, size, TaggedType, MPI_MIN, comm \
94  ); \
95 } \
96  \
97 void Foam::reduce \
98 ( \
99  Native values[], \
100  const int size, \
101  const maxOp<Native>&, \
102  const int tag, /* (unused) */ \
103  const label comm \
104 ) \
105 { \
106  PstreamDetail::allReduce<Native> \
107  ( \
108  values, size, TaggedType, MPI_MAX, comm \
109  ); \
110 } \
111  \
112 void Foam::reduce \
113 ( \
114  Native values[], \
115  const int size, \
116  const sumOp<Native>&, \
117  const int tag, /* (unused) */ \
118  const label comm \
119 ) \
120 { \
121  PstreamDetail::allReduce<Native> \
122  ( \
123  values, size, TaggedType, MPI_SUM, comm \
124  ); \
125 } \
126  \
127 void Foam::reduce \
128 ( \
129  Native& value, \
130  const minOp<Native>&, \
131  const int tag, /* (unused) */ \
132  const label comm \
133 ) \
134 { \
135  PstreamDetail::allReduce<Native> \
136  ( \
137  &value, 1, TaggedType, MPI_MIN, comm \
138  ); \
139 } \
140  \
141 void Foam::reduce \
142 ( \
143  Native& value, \
144  const maxOp<Native>&, \
145  const int tag, /* (unused) */ \
146  const label comm \
147 ) \
148 { \
149  PstreamDetail::allReduce<Native> \
150  ( \
151  &value, 1, TaggedType, MPI_MAX, comm \
152  ); \
153 } \
154  \
155 void Foam::reduce \
156 ( \
157  Native& value, \
158  const sumOp<Native>&, \
159  const int tag, /* (unused) */ \
160  const label comm \
161 ) \
162 { \
163  PstreamDetail::allReduce<Native> \
164  ( \
165  &value, 1, TaggedType, MPI_SUM, comm \
166  ); \
167 }
169 
170 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
171 
172 // Floating-point reductions
173 
174 #undef Pstream_FloatReductions
175 #define Pstream_FloatReductions(Native, TaggedType) \
176  \
177 Pstream_CommonReductions(Native, TaggedType); \
178  \
179 void Foam::reduce \
180 ( \
181  Native values[], \
182  const int size, \
183  const sumOp<Native>&, \
184  const int tag, /* (unused) */ \
185  const label comm, \
186  label& requestID \
187 ) \
188 { \
189  PstreamDetail::allReduce<Native> \
190  ( \
191  values, size, TaggedType, MPI_SUM, comm, &requestID \
192  ); \
193 } \
194  \
195 void Foam::reduce \
196 ( \
197  Native& value, \
198  const sumOp<Native>&, \
199  const int tag, /* (unused) */ \
200  const label comm, \
201  label& requestID \
202 ) \
203 { \
204  PstreamDetail::allReduce<Native> \
205  ( \
206  &value, 1, TaggedType, MPI_SUM, comm, &requestID \
207  ); \
208 } \
209  \
210 void Foam::sumReduce \
211 ( \
212  Native& value, \
213  label& count, \
214  const int tag, /* (unused) */ \
215  const label comm \
216 ) \
217 { \
218  if (UPstream::parRun() && UPstream::nProcs(comm) > 1) \
219  { \
220  Native values[2]; \
221  values[0] = static_cast<Native>(count); \
222  values[1] = value; \
223  \
224  PstreamDetail::allReduce<Native> \
225  ( \
226  values, 2, TaggedType, MPI_SUM, comm \
227  ); \
228  \
229  count = static_cast<label>(values[0]); \
230  value = values[1]; \
231  } \
232 }
233 
234 
235 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
236 
237 Pstream_CommonReductions(int32_t, MPI_INT32_T);
238 Pstream_CommonReductions(int64_t, MPI_INT64_T);
239 Pstream_CommonReductions(uint32_t, MPI_UINT32_T);
240 Pstream_CommonReductions(uint64_t, MPI_UINT64_T);
241 
242 Pstream_FloatReductions(float, MPI_FLOAT);
243 Pstream_FloatReductions(double, MPI_DOUBLE);
244 
245 
246 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
247 
248 #undef Pstream_CommonReductions
249 #undef Pstream_FloatReductions
250 
251 
252 // ************************************************************************* //
#define Pstream_CommonReductions(Native, TaggedType)
Inter-processor communication reduction functions.
#define Pstream_FloatReductions(Native, TaggedType)
static void reduceOr(bool &value, const label communicator=worldComm)
Logical (or) reduction (cf. MPI AllReduce)
void allReduce(Type *values, int count, MPI_Datatype datatype, MPI_Op optype, const label comm, label *requestID=nullptr)
static void reduceAnd(bool &value, const label communicator=worldComm)
Logical (and) reduction (cf. 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.