Pstream.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) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2016-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::Pstream
29
30Description
31 Inter-processor communications stream.
32
33SourceFiles
34 Pstream.C
35 PstreamBroadcast.C
36 PstreamGather.C
37 PstreamCombineGather.C
38 PstreamGatherList.C
39 PstreamExchange.C
40
41\*---------------------------------------------------------------------------*/
42
43#ifndef Foam_Pstream_H
44#define Foam_Pstream_H
45
46#include "UPstream.H"
47#include "DynamicList.H"
48
49// Legacy
50// #define Foam_Pstream_scatter_nobroadcast
51
52// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
53
54namespace Foam
55{
56
57/*---------------------------------------------------------------------------*\
58 Class Pstream Declaration
59\*---------------------------------------------------------------------------*/
61class Pstream
62:
63 public UPstream
64{
65 // Private Static Functions
66
67 //- Exchange contiguous data. Sends sendBufs, receives into recvBufs.
68 // Data provided and received as container.
69 template<class Container, class T>
70 static void exchangeContainer
71 (
72 const UList<Container>& sendBufs,
73 const labelUList& recvSizes,
74 List<Container>& recvBufs,
75 const int tag,
76 const label comm,
77 const bool wait
78 );
79
80 //- Exchange contiguous data. Sends sendBufs, receives into recvBufs.
81 // Data provided and received as pointers.
82 template<class T>
83 static void exchangeBuf
84 (
85 const labelUList& sendSizes,
86 const UList<const char*>& sendBufs,
87 const labelUList& recvSizes,
88 List<char*>& recvBufs,
89 const int tag,
90 const label comm,
91 const bool wait
92 );
93
94
95protected:
96
97 // Protected Data
98
99 //- Allocated transfer buffer (can be used for send or receive)
101
102
103public:
104
105 // Declare name of the class and its debug switch
106 ClassName("Pstream");
107
108
109 // Constructors
110
111 //- Construct for given commsTypes, with optional buffer size
112 explicit Pstream
113 (
115 const label bufSize = 0
116 )
117 :
119 {
120 if (bufSize > 0)
121 {
122 transferBuf_.setCapacity(bufSize + 2*sizeof(scalar) + 1);
123 }
124 }
125
126
127 // Static Functions
128
129 // Broadcast
130
131 //- Broadcast buffer content to all processes in communicator.
133
134 //- Broadcast content (contiguous or non-contiguous)
135 //- to all processes in communicator.
136 template<class Type>
137 static void broadcast
138 (
139 Type& value,
140 const label comm = UPstream::worldComm
141 );
142
143 //- Broadcast multiple items to all processes in communicator.
144 template<class Type, class... Args>
145 static void broadcasts(const label comm, Type& arg1, Args&&... args);
146
147
148 // Gather
149
150 //- Gather (reduce) data, appyling \c bop to combine \c value
151 //- from different processors. The basis for Foam::reduce().
152 // Uses the specified communication schedule.
153 template<class T, class BinaryOp>
154 static void gather
155 (
156 const List<commsStruct>& comms,
157 T& value,
158 const BinaryOp& bop,
159 const int tag,
160 const label comm
161 );
162
163 //- Gather (reduce) data, applying \c bop to combine \c value
164 //- from different processors. The basis for Foam::reduce().
165 // Uses linear/tree communication.
166 template<class T, class BinaryOp>
167 static void gather
168 (
169 T& value,
170 const BinaryOp& bop,
171 const int tag = UPstream::msgType(),
172 const label comm = UPstream::worldComm
173 );
174
175
176 // Gather/combine data
177 // Inplace combine values from processors.
178 // (Uses construct from Istream instead of <<)
179
180 //- Gather data, applying \c cop to inplace combine \c value
181 //- from different processors.
182 // Uses the specified communication schedule.
183 template<class T, class CombineOp>
184 static void combineGather
185 (
186 const List<commsStruct>& comms,
187 T& value,
188 const CombineOp& cop,
189 const int tag,
190 const label comm
191 );
192
193 //- Gather data, applying \c cop to inplace combine \c value
194 //- from different processors.
195 // Uses linear/tree communication.
196 template<class T, class CombineOp>
197 static void combineGather
198 (
199 T& value,
200 const CombineOp& cop,
201 const int tag = UPstream::msgType(),
202 const label comm = UPstream::worldComm
203 );
204
205 //- Gather data, applying \c cop to inplace combine \c value
206 //- from different processors.
207 //- After completion all processors have the same data.
208 // Uses the specified communication schedule.
209 // Wraps combineGather/broadcast (may change in the future).
210 template<class T, class CombineOp>
211 static void combineAllGather
212 (
213 const List<commsStruct>& comms,
214 T& value,
215 const CombineOp& cop,
216 const int tag = UPstream::msgType(),
217 const label comm = UPstream::worldComm
218 );
219
220 //- Gather data, applying \c cop to inplace combine \c value
221 //- from different processors.
222 //- After completion all processors have the same data.
223 // Uses linear/tree communication.
224 // Wraps combineGather/broadcast (may change in the future).
225 template<class T, class CombineOp>
226 static void combineAllGather
227 (
228 T& value,
229 const CombineOp& cop,
230 const int tag = UPstream::msgType(),
231 const label comm = UPstream::worldComm
232 );
233
234
235 // Combine variants working on whole List at a time.
236
237 template<class T, class CombineOp>
238 static void listCombineGather
239 (
240 const List<commsStruct>& comms,
241 List<T>& values,
242 const CombineOp& cop,
243 const int tag,
244 const label comm
245 );
246
247 //- Like above but switches between linear/tree communication
248 template<class T, class CombineOp>
249 static void listCombineGather
250 (
251 List<T>& values,
252 const CombineOp& cop,
253 const int tag = UPstream::msgType(),
254 const label comm = UPstream::worldComm
255 );
256
257 //- After completion all processors have the same data.
258 template<class T, class CombineOp>
259 static void listCombineAllGather
260 (
261 const List<commsStruct>& comms,
262 List<T>& values,
263 const CombineOp& cop,
264 const int tag,
265 const label comm
266 );
267
268 //- After completion all processors have the same data.
269 template<class T, class CombineOp>
270 static void listCombineAllGather
271 (
272 List<T>& values,
273 const CombineOp& cop,
274 const int tag = UPstream::msgType(),
275 const label comm = UPstream::worldComm
276 );
277
278
279 // Combine variants working on whole map at a time.
280 // Container needs iterators, find() and insert methods defined.
281
282 template<class Container, class CombineOp>
283 static void mapCombineGather
284 (
285 const List<commsStruct>& comms,
286 Container& values,
287 const CombineOp& cop,
288 const int tag,
289 const label comm
290 );
291
292 //- Like above but switches between linear/tree communication
293 template<class Container, class CombineOp>
294 static void mapCombineGather
295 (
296 Container& values,
297 const CombineOp& cop,
298 const int tag = UPstream::msgType(),
299 const label comm = UPstream::worldComm
300 );
301
302
303 //- After completion all processors have the same data.
304 template<class Container, class CombineOp>
305 static void mapCombineAllGather
306 (
307 const List<commsStruct>& comms,
308 Container& values,
309 const CombineOp& cop,
310 const int tag,
311 const label comm
312 );
313
314 //- After completion all processors have the same data.
315 template<class Container, class CombineOp>
316 static void mapCombineAllGather
317 (
318 Container& values,
319 const CombineOp& cop,
320 const int tag = UPstream::msgType(),
321 const label comm = UPstream::worldComm
322 );
323
324
325 // Gather/scatter keeping the individual processor data separate.
326 // The values is a List of size UPstream::nProcs() where
327 // values[UPstream::myProcNo()] is the data for the current processor.
328
329 //- Gather data, but keep individual values separate.
330 //- Uses the specified communication schedule.
331 template<class T>
332 static void gatherList
333 (
334 const List<commsStruct>& comms,
335 List<T>& values,
336 const int tag,
337 const label comm
338 );
339
340 //- Gather data, but keep individual values separate.
341 //- Uses linear/tree communication.
342 template<class T>
343 static void gatherList
344 (
345 List<T>& values,
346 const int tag = UPstream::msgType(),
347 const label comm = UPstream::worldComm
348 );
349
350 //- Gather data, but keep individual values separate.
351 //- Uses the specified communication schedule.
352 // After completion all processors have the same data.
353 // Wraps gatherList/scatterList (may change in the future).
354 template<class T>
355 static void allGatherList
356 (
357 const List<commsStruct>& comms,
358 List<T>& values,
359 const int tag,
360 const label comm
361 );
362
363 //- Gather data, but keep individual values separate.
364 //- Uses linear/tree communication.
365 // After completion all processors have the same data.
366 // Wraps gatherList/scatterList (may change in the future).
367 template<class T>
368 static void allGatherList
369 (
370 List<T>& values,
371 const int tag = UPstream::msgType(),
372 const label comm = UPstream::worldComm
373 );
374
375
376 // Scatter
377
378 //- Broadcast data: Distribute without modification.
379 // \note comms and tag parameters only used when
380 // Foam_Pstream_scatter_nobroadcast is defined
381 template<class T>
382 static void scatter
383 (
384 const List<commsStruct>& comms,
385 T& value,
386 const int tag,
387 const label comm
388 );
389
390 //- Broadcast data: Distribute without modification.
391 // \note tag parameter only used when
392 // Foam_Pstream_scatter_nobroadcast is defined
393 template<class T>
394 static void scatter
395 (
396 T& value,
397 const int tag = UPstream::msgType(),
398 const label comm = UPstream::worldComm
399 );
400
401 //- Broadcast data: Distribute without modification.
402 // \note tag parameter only used when
403 // Foam_Pstream_scatter_nobroadcast is defined
404 template<class T>
405 static void combineScatter
406 (
407 const List<commsStruct>& comms,
408 T& value,
409 const int tag,
410 const label comm
411 );
412
413 //- Broadcast data: Distribute without modification.
414 // \note tag parameter only used when
415 // Foam_Pstream_scatter_nobroadcast is defined
416 template<class T>
417 static void combineScatter
418 (
419 T& value,
420 const int tag = UPstream::msgType(),
421 const label comm = UPstream::worldComm
422 );
423
424 //- Broadcast data: Distribute without modification.
425 // \note comms and tag parameters only used when
426 // Foam_Pstream_scatter_nobroadcast is defined
427 template<class T>
428 static void listCombineScatter
429 (
430 const List<commsStruct>& comms,
431 List<T>& value,
432 const int tag,
433 const label comm
434 );
435
436 //- Broadcast data: Distribute without modification.
437 // \note comms and tag parameters only used when
438 // Foam_Pstream_scatter_nobroadcast is defined
439 template<class T>
440 static void listCombineScatter
441 (
442 List<T>& value,
443 const int tag = UPstream::msgType(),
444 const label comm = UPstream::worldComm
445 );
446
447 //- Broadcast data: Distribute without modification.
448 template<class Container>
449 static void mapCombineScatter
450 (
451 const List<commsStruct>& comms,
452 Container& values,
453 const int tag,
454 const label comm
455 );
456
457 //- Like above but switches between linear/tree communication
458 template<class Container>
459 static void mapCombineScatter
460 (
461 Container& values,
462 const int tag = UPstream::msgType(),
463 const label comm = UPstream::worldComm
464 );
465
466
467 //- Scatter data. Reverse of gatherList
468 template<class T>
469 static void scatterList
470 (
471 const List<commsStruct>& comms,
472 List<T>& values,
473 const int tag,
474 const label comm
475 );
476
477 //- Like above but switches between linear/tree communication
478 template<class T>
479 static void scatterList
480 (
481 List<T>& values,
482 const int tag = UPstream::msgType(),
483 const label comm = UPstream::worldComm
484 );
485
486
487 // Exchange
488
489 //- Helper: exchange sizes of sendData for specified
490 //- set of send/receive processes.
491 template<class Container>
492 static void exchangeSizes
493 (
494 const labelUList& sendProcs,
495 const labelUList& recvProcs,
496 const Container& sendData,
497 labelList& sizes,
498 const label tag = UPstream::msgType(),
499 const label comm = UPstream::worldComm
500 );
501
502 //- Helper: exchange sizes of sendData.
503 //- The sendData is the data per processor (in the communicator).
504 // Returns sizes of sendData on the sending processor.
505 template<class Container>
506 static void exchangeSizes
507 (
508 const Container& sendData,
509 labelList& sizes,
510 const label comm = UPstream::worldComm
511 );
512
513
514 //- Helper: exchange contiguous data.
515 //- Sends sendData, receives into recvData.
516 // If wait=true will wait for all transfers to finish.
517 template<class Container, class T>
518 static void exchange
519 (
520 const UList<Container>& sendData,
521 const labelUList& recvSizes,
522 List<Container>& recvData,
523 const int tag = UPstream::msgType(),
524 const label comm = UPstream::worldComm,
525 const bool wait = true
526 );
527
528 //- Exchange contiguous data.
529 //- Sends sendData, receives into recvData.
530 //- Determines sizes to receive.
531 // If wait=true will wait for all transfers to finish.
532 template<class Container, class T>
533 static void exchange
534 (
535 const UList<Container>& sendData,
536 List<Container>& recvData,
537 const int tag = UPstream::msgType(),
538 const label comm = UPstream::worldComm,
539 const bool wait = true
540 );
541};
542
543
544// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
545
546} // End namespace Foam
547
548// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
549
550#ifdef NoRepository
551 #include "PstreamBroadcast.C"
552 #include "PstreamGather.C"
553 #include "PstreamCombineGather.C"
554 #include "PstreamGatherList.C"
555 #include "PstreamExchange.C"
556#endif
557
558// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
559
560#endif
561
562// ************************************************************************* //
Variant of gather, scatter. Normal gather uses:
Exchange data.
Gather data from all processors onto single processor according to some communication schedule (usual...
Gather data from all processors onto single processor according to some communication schedule (usual...
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
void setCapacity(const label len)
Alter the size of the underlying storage.
Definition: DynamicListI.H:303
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
Inter-processor communications stream.
Definition: Pstream.H:63
static void allGatherList(const List< commsStruct > &comms, List< T > &values, const int tag, const label comm)
DynamicList< char > transferBuf_
Allocated transfer buffer (can be used for send or receive)
Definition: Pstream.H:99
ClassName("Pstream")
static void gather(const List< commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
Definition: PstreamGather.C:43
static void exchangeSizes(const labelUList &sendProcs, const labelUList &recvProcs, const Container &sendData, labelList &sizes, const label tag=UPstream::msgType(), const label comm=UPstream::worldComm)
static void listCombineAllGather(const List< commsStruct > &comms, List< T > &values, const CombineOp &cop, const int tag, const label comm)
After completion all processors have the same data.
static void gatherList(const List< commsStruct > &comms, List< T > &values, const int tag, const label comm)
static void combineGather(const List< commsStruct > &comms, T &value, const CombineOp &cop, const int tag, const label comm)
static void combineScatter(const List< commsStruct > &comms, T &value, const int tag, const label comm)
Broadcast data: Distribute without modification.
Pstream(const UPstream::commsTypes commsType, const label bufSize=0)
Construct for given commsTypes, with optional buffer size.
Definition: Pstream.H:112
static void combineAllGather(const List< commsStruct > &comms, T &value, const CombineOp &cop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
static void scatterList(const List< commsStruct > &comms, List< T > &values, const int tag, const label comm)
Scatter data. Reverse of gatherList.
static void listCombineScatter(const List< commsStruct > &comms, List< T > &value, const int tag, const label comm)
Broadcast data: Distribute without modification.
static void exchange(const UList< Container > &sendData, const labelUList &recvSizes, List< Container > &recvData, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm, const bool wait=true)
static void mapCombineScatter(const List< commsStruct > &comms, Container &values, const int tag, const label comm)
Broadcast data: Distribute without modification.
static void broadcast(Type &value, const label comm=UPstream::worldComm)
static void scatter(const List< commsStruct > &comms, T &value, const int tag, const label comm)
Broadcast data: Distribute without modification.
static void mapCombineGather(const List< commsStruct > &comms, Container &values, const CombineOp &cop, const int tag, const label comm)
static void listCombineGather(const List< commsStruct > &comms, List< T > &values, const CombineOp &cop, const int tag, const label comm)
static void broadcasts(const label comm, Type &arg1, Args &&... args)
Broadcast multiple items to all processes in communicator.
static void mapCombineAllGather(const List< commsStruct > &comms, Container &values, const CombineOp &cop, const int tag, const label comm)
After completion all processors have the same data.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
Inter-processor communications stream.
Definition: UPstream.H:59
commsTypes
Types of communications.
Definition: UPstream.H:67
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:556
commsTypes commsType() const noexcept
Get the communications type of the stream.
Definition: UPstream.H:562
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:293
static bool broadcast(char *buf, const std::streamsize bufSize, const label communicator=worldComm, const int rootProcNo=masterNo())
#define ClassName(TypeNameString)
Add typeName information from argument TypeNameString to a class.
Definition: className.H:67
const volScalarField & T
Namespace for OpenFOAM.
Foam::argList args(argc, argv)