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-2021 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 Class
28  Foam::Pstream
29 
30 Description
31  Inter-processor communications stream.
32 
33 SourceFiles
34  Pstream.C
35  combineGatherScatter.C
36  gatherScatter.C
37  gatherScatterList.C
38  exchange.C
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef Pstream_H
43 #define Pstream_H
44 
45 #include "UPstream.H"
46 #include "DynamicList.H"
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 /*---------------------------------------------------------------------------*\
54  Class Pstream Declaration
55 \*---------------------------------------------------------------------------*/
56 
57 class Pstream
58 :
59  public UPstream
60 {
61  // Private Static Functions
62 
63  //- Exchange contiguous data. Sends sendData, receives into
64  // recvData. If block=true will wait for all transfers to finish.
65  // Data provided and received as container.
66  template<class Container, class T>
67  static void exchangeContainer
68  (
69  const UList<Container>& sendBufs,
70  const labelUList& recvSizes,
71  List<Container>& recvBufs,
72  const int tag,
73  const label comm,
74  const bool block
75  );
76 
77  //- Exchange contiguous data. Sends sendData, receives into
78  // recvData. If block=true will wait for all transfers to finish.
79  // Data provided and received as pointers.
80  template<class T>
81  static void exchangeBuf
82  (
83  const labelUList& sendSizes, // number of T, not number of char
84  const UList<const char*>& sendBufs,
85  const labelUList& recvSizes, // number of T, not number of char
86  List<char*>& recvBufs,
87  const int tag,
88  const label comm,
89  const bool block
90  );
91 
92 
93 protected:
94 
95  // Protected Data
96 
97  //- Allocated transfer buffer (can be used for send or receive)
99 
100 
101 public:
102 
103  // Declare name of the class and its debug switch
104  ClassName("Pstream");
105 
106 
107  // Constructors
108 
109  //- Construct given optional buffer size
110  explicit Pstream
111  (
112  const commsTypes commsType,
113  const label bufSize = 0
114  )
115  :
117  {
118  if (bufSize)
119  {
120  transferBuf_.setCapacity(bufSize + 2*sizeof(scalar) + 1);
121  }
122  }
123 
124 
125  // Gather and scatter
126 
127  //- Gather data.
128  //- Apply bop to combine Value from different processors
129  template<class T, class BinaryOp>
130  static void gather
131  (
132  const List<commsStruct>& comms,
133  T& Value,
134  const BinaryOp& bop,
135  const int tag,
136  const label comm
137  );
138 
139  //- Like above but switches between linear/tree communication
140  template<class T, class BinaryOp>
141  static void gather
142  (
143  T& Value,
144  const BinaryOp& bop,
145  const int tag = Pstream::msgType(),
146  const label comm = Pstream::worldComm
147  );
148 
149  //- Scatter data. Distribute without modification. Reverse of gather
150  template<class T>
151  static void scatter
152  (
153  const List<commsStruct>& comms,
154  T& Value,
155  const int tag,
156  const label comm
157  );
158 
159  //- Like above but switches between linear/tree communication
160  template<class T>
161  static void scatter
162  (
163  T& Value,
164  const int tag = Pstream::msgType(),
165  const label comm = Pstream::worldComm
166  );
167 
168 
169  // Combine variants. Inplace combine values from processors.
170  // (Uses construct from Istream instead of <<)
171 
172  template<class T, class CombineOp>
173  static void combineGather
174  (
175  const List<commsStruct>& comms,
176  T& Value,
177  const CombineOp& cop,
178  const int tag,
179  const label comm
180  );
181 
182  //- Like above but switches between linear/tree communication
183  template<class T, class CombineOp>
184  static void combineGather
185  (
186  T& Value,
187  const CombineOp& cop,
188  const int tag = Pstream::msgType(),
189  const label comm = Pstream::worldComm
190  );
191 
192  //- Scatter data. Reverse of combineGather
193  template<class T>
194  static void combineScatter
195  (
196  const List<commsStruct>& comms,
197  T& Value,
198  const int tag,
199  const label comm
200  );
201 
202  //- Like above but switches between linear/tree communication
203  template<class T>
204  static void combineScatter
205  (
206  T& Value,
207  const int tag = Pstream::msgType(),
208  const label comm = Pstream::worldComm
209  );
210 
211 
212  // Combine variants working on whole List at a time.
213 
214  template<class T, class CombineOp>
215  static void listCombineGather
216  (
217  const List<commsStruct>& comms,
218  List<T>& Value,
219  const CombineOp& cop,
220  const int tag,
221  const label comm
222  );
223 
224  //- Like above but switches between linear/tree communication
225  template<class T, class CombineOp>
226  static void listCombineGather
227  (
228  List<T>& Value,
229  const CombineOp& cop,
230  const int tag = Pstream::msgType(),
231  const label comm = Pstream::worldComm
232  );
233 
234  //- Scatter data. Reverse of combineGather
235  template<class T>
236  static void listCombineScatter
237  (
238  const List<commsStruct>& comms,
239  List<T>& Value,
240  const int tag,
241  const label comm
242  );
243 
244  //- Like above but switches between linear/tree communication
245  template<class T>
246  static void listCombineScatter
247  (
248  List<T>& Value,
249  const int tag = Pstream::msgType(),
250  const label comm = Pstream::worldComm
251  );
252 
253 
254  // Combine variants working on whole map at a time. Container needs to
255  // have iterators and find() defined.
256 
257  template<class Container, class CombineOp>
258  static void mapCombineGather
259  (
260  const List<commsStruct>& comms,
261  Container& Values,
262  const CombineOp& cop,
263  const int tag,
264  const label comm
265  );
266 
267  //- Like above but switches between linear/tree communication
268  template<class Container, class CombineOp>
269  static void mapCombineGather
270  (
271  Container& Values,
272  const CombineOp& cop,
273  const int tag = Pstream::msgType(),
274  const label comm = UPstream::worldComm
275  );
276 
277  //- Scatter data. Reverse of combineGather
278  template<class Container>
279  static void mapCombineScatter
280  (
281  const List<commsStruct>& comms,
282  Container& Values,
283  const int tag,
284  const label comm
285  );
286 
287  //- Like above but switches between linear/tree communication
288  template<class Container>
289  static void mapCombineScatter
290  (
291  Container& Values,
292  const int tag = Pstream::msgType(),
293  const label comm = UPstream::worldComm
294  );
295 
296 
297 
298  // Gather/scatter keeping the individual processor data separate.
299  // Values is a List of size UPstream::nProcs() where
300  // Values[UPstream::myProcNo()] is the data for the current processor.
301 
302  //- Gather data but keep individual values separate
303  template<class T>
304  static void gatherList
305  (
306  const List<commsStruct>& comms,
307  List<T>& Values,
308  const int tag,
309  const label comm
310  );
311 
312  //- Like above but switches between linear/tree communication
313  template<class T>
314  static void gatherList
315  (
316  List<T>& Values,
317  const int tag = Pstream::msgType(),
318  const label comm = UPstream::worldComm
319  );
320 
321  //- Scatter data. Reverse of gatherList
322  template<class T>
323  static void scatterList
324  (
325  const List<commsStruct>& comms,
326  List<T>& Values,
327  const int tag,
328  const label comm
329  );
330 
331  //- Like above but switches between linear/tree communication
332  template<class T>
333  static void scatterList
334  (
335  List<T>& Values,
336  const int tag = Pstream::msgType(),
337  const label comm = UPstream::worldComm
338  );
339 
340 
341  // Exchange
342 
343  //- Helper: exchange contiguous data. Sends sendData, receives into
344  // recvData. If block=true will wait for all transfers to finish.
345  template<class Container, class T>
346  static void exchange
347  (
348  const UList<Container>& sendData,
349  const labelUList& recvSizes,
350  List<Container>& recvData,
351  const int tag = UPstream::msgType(),
352  const label comm = UPstream::worldComm,
353  const bool block = true
354  );
355 
356  //- Helper: exchange sizes of sendData. sendData is the data per
357  // processor (in the communicator). Returns sizes of sendData
358  // on the sending processor.
359  template<class Container>
360  static void exchangeSizes
361  (
362  const Container& sendData,
363  labelList& sizes,
364  const label comm = UPstream::worldComm
365  );
366 
367  //- Exchange contiguous data. Sends sendData, receives into
368  // recvData. Determines sizes to receive.
369  // If block=true will wait for all transfers to finish.
370  template<class Container, class T>
371  static void exchange
372  (
373  const UList<Container>& sendData,
374  List<Container>& recvData,
375  const int tag = UPstream::msgType(),
376  const label comm = UPstream::worldComm,
377  const bool block = true
378  );
379 };
380 
381 
382 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
383 
384 } // End namespace Foam
385 
386 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
387 
388 #ifdef NoRepository
389  #include "gatherScatter.C"
390  #include "combineGatherScatter.C"
391  #include "gatherScatterList.C"
392  #include "exchange.C"
393 #endif
394 
395 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
396 
397 #endif
398 
399 // ************************************************************************* //
Foam::Pstream::Pstream
Pstream(const commsTypes commsType, const label bufSize=0)
Construct given optional buffer size.
Definition: Pstream.H:110
Foam::Pstream::mapCombineGather
static void mapCombineGather(const List< commsStruct > &comms, Container &Values, const CombineOp &cop, const int tag, const label comm)
Definition: combineGatherScatter.C:551
gatherScatter.C
Gather data from all processors onto single processor according to some communication schedule (usual...
Foam::block
Creates a single block of cells from point coordinates, numbers of cells in each direction and an exp...
Definition: block.H:58
Foam::UPstream::commsType
commsTypes commsType() const noexcept
Get the communications type of the stream.
Definition: UPstream.H:547
UPstream.H
Foam::DynamicList< char >
Foam::Pstream::scatterList
static void scatterList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Scatter data. Reverse of gatherList.
Definition: gatherScatterList.C:215
exchange.C
Exchange data.
Foam::Pstream::exchangeSizes
static void exchangeSizes(const Container &sendData, labelList &sizes, const label comm=UPstream::worldComm)
Helper: exchange sizes of sendData. sendData is the data per.
Definition: exchange.C:349
Foam::Pstream::scatter
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
Definition: gatherScatter.C:150
gatherScatterList.C
Gather data from all processors onto single processor according to some communication schedule (usual...
Foam::UPstream::UPstream
UPstream(const commsTypes commsType)
Construct for given communication type.
Definition: UPstream.H:302
Foam::Pstream::gather
static void gather(const List< commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Definition: gatherScatter.C:50
Foam::DynamicList::setCapacity
void setCapacity(const label len)
Alter the size of the underlying storage.
Definition: DynamicListI.H:303
Foam::UPstream
Inter-processor communications stream.
Definition: UPstream.H:61
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::Pstream::ClassName
ClassName("Pstream")
Foam::Pstream::exchange
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 block=true)
Helper: exchange contiguous data. Sends sendData, receives into.
Definition: exchange.C:187
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::Pstream::combineGather
static void combineGather(const List< commsStruct > &comms, T &Value, const CombineOp &cop, const int tag, const label comm)
Definition: combineGatherScatter.C:48
Foam::Pstream::listCombineGather
static void listCombineGather(const List< commsStruct > &comms, List< T > &Value, const CombineOp &cop, const int tag, const label comm)
Definition: combineGatherScatter.C:290
Foam::UPstream::commsTypes
commsTypes
Types of communications.
Definition: UPstream.H:69
Foam::Pstream::gatherList
static void gatherList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Gather data but keep individual values separate.
Definition: gatherScatterList.C:52
Foam::UPstream::msgType
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:540
Foam::Pstream::mapCombineScatter
static void mapCombineScatter(const List< commsStruct > &comms, Container &Values, const int tag, const label comm)
Scatter data. Reverse of combineGather.
Definition: combineGatherScatter.C:666
Foam::Pstream
Inter-processor communications stream.
Definition: Pstream.H:56
Foam::UPstream::worldComm
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:293
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
combineGatherScatter.C
Variant of gather, scatter. Normal gather uses:
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
DynamicList.H
Foam::Pstream::listCombineScatter
static void listCombineScatter(const List< commsStruct > &comms, List< T > &Value, const int tag, const label comm)
Scatter data. Reverse of combineGather.
Definition: combineGatherScatter.C:432
Foam::Pstream::transferBuf_
DynamicList< char > transferBuf_
Allocated transfer buffer (can be used for send or receive)
Definition: Pstream.H:97
Foam::Pstream::combineScatter
static void combineScatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Reverse of combineGather.
Definition: combineGatherScatter.C:183