UPstreamWrapping.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2012-2016 OpenFOAM Foundation
9  Copyright (C) 2022-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 InNamespace
28  Foam::PstreamDetail
29 
30 Description
31  Functions to wrap MPI_Bcast, MPI_Allreduce, MPI_Iallreduce etc.
32 
33 SourceFiles
34  UPstreamWrappingTemplates.C
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef Foam_UPstreamWrapping_H
39 #define Foam_UPstreamWrapping_H
40 
41 #include "UPstream.H"
42 #include <mpi.h>
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 namespace PstreamDetail
49 {
50 
51 // Helper for casting to MPI_Request
52 struct Request
53 {
54  // To pointer
55  template<typename Type = MPI_Request>
56  static typename std::enable_if<std::is_pointer<Type>::value, Type>::type
57  get(const UPstream::Request& req) noexcept
58  {
59  return reinterpret_cast<Type>(req.value());
60  }
61 
62  // To integer
63  template<typename Type = MPI_Request>
64  static typename std::enable_if<std::is_integral<Type>::value, Type>::type
65  get(const UPstream::Request& req) noexcept
66  {
67  return static_cast<Type>(req.value());
68  }
69 };
70 
71 
72 // MPI_Bcast, using root=0
73 template<class Type>
74 void broadcast0
75 (
76  Type* values,
77  int count,
78  MPI_Datatype datatype,
79  const label comm
80 );
81 
82 // MPI_Reduce, using root=0
83 template<class Type>
84 void reduce0
85 (
86  Type* values,
87  int count,
88  MPI_Datatype datatype,
89  MPI_Op optype,
90  const label comm
91 );
92 
93 // MPI_Allreduce or MPI_Iallreduce
94 template<class Type>
95 void allReduce
96 (
97  Type* values,
98  int count,
99  MPI_Datatype datatype,
100  MPI_Op optype,
101  const label comm, // Communicator
102  UPstream::Request* req = nullptr, // Non-null for non-blocking
103  label* requestID = nullptr // (alternative to UPstream::Request)
104 );
105 
106 
107 // MPI_Alltoall or MPI_Ialltoall with one element per rank
108 template<class Type>
109 void allToAll
110 (
111  const UList<Type>& sendData,
112  UList<Type>& recvData,
113  MPI_Datatype datatype,
114  const label comm, // Communicator
115  UPstream::Request* req = nullptr, // Non-null for non-blocking
116  label* requestID = nullptr // (alternative to UPstream::Request)
117 );
118 
119 
120 // MPI_Alltoallv or MPI_Ialltoallv
121 template<class Type>
122 void allToAllv
123 (
124  const Type* sendData,
125  const UList<int>& sendCounts,
126  const UList<int>& sendOffsets,
127 
128  Type* recvData,
129  const UList<int>& recvCounts,
130  const UList<int>& recvOffsets,
131 
132  MPI_Datatype datatype,
133  const label comm, // Communicator
134  UPstream::Request* req = nullptr, // Non-null for non-blocking
135  label* requestID = nullptr // (alternative to UPstream::Request)
136 );
137 
138 
139 // Non-blocking consensual integer (size) exchange
140 template<class Type>
142 (
143  const UList<Type>& sendData,
144  UList<Type>& recvData,
145  MPI_Datatype datatype,
146  const int tag, // Message tag
147  const label comm // Communicator
148 );
149 
150 
151 // Non-blocking consensual integer (size) exchange
152 template<class Type>
154 (
155  const Map<Type>& sendData,
156  Map<Type>& recvData,
157  MPI_Datatype datatype,
158  const int tag, // Message tag
159  const label comm // Communicator
160 );
161 
162 
163 // MPI_Gather or MPI_Igather
164 template<class Type>
165 void gather
166 (
167  const Type* sendData, // Local send value
168  Type* recvData, // On master: recv buffer. Ignored elsewhere
169  int count, // Per rank send/recv count. Globally consistent!
170  MPI_Datatype datatype, // The send/recv data type
171  const label comm, // Communicator
172  UPstream::Request* req = nullptr, // Non-null for non-blocking
173  label* requestID = nullptr // (alternative to UPstream::Request)
174 );
175 
176 
177 // MPI_Scatter or MPI_Iscatter
178 template<class Type>
179 void scatter
180 (
181  const Type* sendData, // On master: send buffer. Ignored elsewhere
182  Type* recvData, // Local recv value
183  int count, // Per rank send/recv count. Globally consistent!
184  MPI_Datatype datatype, // The send/recv data type
185  const label comm, // Communicator
186  UPstream::Request* req = nullptr, // Non-null for non-blocking
187  label* requestID = nullptr // (alternative to UPstream::Request)
188 );
189 
190 
191 // MPI_Gatherv or MPI_Igatherv
192 template<class Type>
193 void gatherv
194 (
195  const Type* sendData,
196  int sendCount, // Ignored on master if recvCounts[0] == 0
197 
198  Type* recvData, // Ignored on non-root rank
199  const UList<int>& recvCounts, // Ignored on non-root rank
200  const UList<int>& recvOffsets, // Ignored on non-root rank
201 
202  MPI_Datatype datatype, // The send/recv data type
203  const label comm, // Communicator
204  UPstream::Request* req = nullptr, // Non-null for non-blocking
205  label* requestID = nullptr // (alternative to UPstream::Request)
206 );
207 
208 
209 // MPI_Scatterv or MPI_Iscatterv
210 template<class Type>
211 void scatterv
212 (
213  const Type* sendData, // Ignored on non-root rank
214  const UList<int>& sendCounts, // Ignored on non-root rank
215  const UList<int>& sendOffsets, // Ignored on non-root rank
216 
217  Type* recvData,
218  int recvCount,
219 
220  MPI_Datatype datatype, // The send/recv data type
221  const label comm, // Communicator
222  UPstream::Request* req = nullptr, // Non-null for non-blocking
223  label* requestID = nullptr // (alternative to UPstream::Request)
224 );
225 
226 
227 // MPI_Allgather or MPI_Iallgather
228 template<class Type>
229 void allGather
230 (
231  Type* allData, // The send/recv data
232  int count, // The send/recv count per element
233 
234  MPI_Datatype datatype, // The send/recv data type
235  const label comm, // Communicator
236  UPstream::Request* req = nullptr, // Non-null for non-blocking
237  label* requestID = nullptr // (alternative to UPstream::Request)
238 );
239 
240 
241 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
242 
243 } // End namespace PstreamDetail
244 } // End namespace Foam
245 
246 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
247 
248 #ifdef NoRepository
249  #include "UPstreamWrappingTemplates.C"
250 #endif
251 
252 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
253 
254 #endif
255 
256 // ************************************************************************* //
void broadcast0(Type *values, int count, MPI_Datatype datatype, const label comm)
void allReduce(Type *values, int count, MPI_Datatype datatype, MPI_Op optype, const label comm, UPstream::Request *req=nullptr, label *requestID=nullptr)
void gather(const Type *sendData, Type *recvData, int count, MPI_Datatype datatype, const label comm, UPstream::Request *req=nullptr, label *requestID=nullptr)
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
void allToAllConsensus(const UList< Type > &sendData, UList< Type > &recvData, MPI_Datatype datatype, const int tag, const label comm)
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
An opaque wrapper for MPI_Request with a vendor-independent representation independent of any <mpi...
Definition: UPstream.H:1571
void scatter(const Type *sendData, Type *recvData, int count, MPI_Datatype datatype, const label comm, UPstream::Request *req=nullptr, label *requestID=nullptr)
const direction noexcept
Definition: Scalar.H:258
void allGather(Type *allData, int count, MPI_Datatype datatype, const label comm, UPstream::Request *req=nullptr, label *requestID=nullptr)
void allToAllv(const Type *sendData, const UList< int > &sendCounts, const UList< int > &sendOffsets, Type *recvData, const UList< int > &recvCounts, const UList< int > &recvOffsets, MPI_Datatype datatype, const label comm, UPstream::Request *req=nullptr, label *requestID=nullptr)
void gatherv(const Type *sendData, int sendCount, Type *recvData, const UList< int > &recvCounts, const UList< int > &recvOffsets, MPI_Datatype datatype, const label comm, UPstream::Request *req=nullptr, label *requestID=nullptr)
void allToAll(const UList< Type > &sendData, UList< Type > &recvData, MPI_Datatype datatype, const label comm, UPstream::Request *req=nullptr, label *requestID=nullptr)
void reduce0(Type *values, int count, MPI_Datatype datatype, MPI_Op optype, const label comm)
Namespace for OpenFOAM.
A HashTable to objects of type <T> with a label key.
void scatterv(const Type *sendData, const UList< int > &sendCounts, const UList< int > &sendOffsets, Type *recvData, int recvCount, MPI_Datatype datatype, const label comm, UPstream::Request *req=nullptr, label *requestID=nullptr)