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 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  gatherScatter.C
36  combineGatherScatter.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  //- Transfer buffer
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
111  (
112  const commsTypes commsType,
113  const label bufSize = 0
114  )
115  :
117  buf_(0)
118  {
119  if (bufSize)
120  {
121  buf_.setCapacity(bufSize + 2*sizeof(scalar) + 1);
122  }
123  }
124 
125 
126  // Gather and scatter
127 
128  //- Gather data. Apply bop to combine Value
129  // from different processors
130  template<class T, class BinaryOp>
131  static void gather
132  (
133  const List<commsStruct>& comms,
134  T& Value,
135  const BinaryOp& bop,
136  const int tag,
137  const label comm
138  );
139 
140  //- Like above but switches between linear/tree communication
141  template<class T, class BinaryOp>
142  static void gather
143  (
144  T& Value,
145  const BinaryOp& bop,
146  const int tag = Pstream::msgType(),
147  const label comm = Pstream::worldComm
148  );
149 
150  //- Scatter data. Distribute without modification. Reverse of gather
151  template<class T>
152  static void scatter
153  (
154  const List<commsStruct>& comms,
155  T& Value,
156  const int tag,
157  const label comm
158  );
159 
160  //- Like above but switches between linear/tree communication
161  template<class T>
162  static void scatter
163  (
164  T& Value,
165  const int tag = Pstream::msgType(),
166  const label comm = Pstream::worldComm
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  // Combine variants working on whole List at a time.
212 
213  template<class T, class CombineOp>
214  static void listCombineGather
215  (
216  const List<commsStruct>& comms,
217  List<T>& Value,
218  const CombineOp& cop,
219  const int tag,
220  const label comm
221  );
222 
223  //- Like above but switches between linear/tree communication
224  template<class T, class CombineOp>
225  static void listCombineGather
226  (
227  List<T>& Value,
228  const CombineOp& cop,
229  const int tag = Pstream::msgType(),
230  const label comm = Pstream::worldComm
231  );
232 
233  //- Scatter data. Reverse of combineGather
234  template<class T>
235  static void listCombineScatter
236  (
237  const List<commsStruct>& comms,
238  List<T>& Value,
239  const int tag,
240  const label comm
241  );
242 
243  //- Like above but switches between linear/tree communication
244  template<class T>
245  static void listCombineScatter
246  (
247  List<T>& Value,
248  const int tag = Pstream::msgType(),
249  const label comm = Pstream::worldComm
250  );
251 
252  // Combine variants working on whole map at a time. Container needs to
253  // have iterators and find() defined.
254 
255  template<class Container, class CombineOp>
256  static void mapCombineGather
257  (
258  const List<commsStruct>& comms,
259  Container& Values,
260  const CombineOp& cop,
261  const int tag,
262  const label comm
263  );
264 
265  //- Like above but switches between linear/tree communication
266  template<class Container, class CombineOp>
267  static void mapCombineGather
268  (
269  Container& Values,
270  const CombineOp& cop,
271  const int tag = Pstream::msgType(),
272  const label comm = UPstream::worldComm
273  );
274 
275  //- Scatter data. Reverse of combineGather
276  template<class Container>
277  static void mapCombineScatter
278  (
279  const List<commsStruct>& comms,
280  Container& Values,
281  const int tag,
282  const label comm
283  );
284 
285  //- Like above but switches between linear/tree communication
286  template<class Container>
287  static void mapCombineScatter
288  (
289  Container& Values,
290  const int tag = Pstream::msgType(),
291  const label comm = UPstream::worldComm
292  );
293 
294 
295 
296  // Gather/scatter keeping the individual processor data separate.
297  // Values is a List of size UPstream::nProcs() where
298  // Values[UPstream::myProcNo()] is the data for the current processor.
299 
300  //- Gather data but keep individual values separate
301  template<class T>
302  static void gatherList
303  (
304  const List<commsStruct>& comms,
305  List<T>& Values,
306  const int tag,
307  const label comm
308  );
309 
310  //- Like above but switches between linear/tree communication
311  template<class T>
312  static void gatherList
313  (
314  List<T>& Values,
315  const int tag = Pstream::msgType(),
316  const label comm = UPstream::worldComm
317  );
318 
319  //- Scatter data. Reverse of gatherList
320  template<class T>
321  static void scatterList
322  (
323  const List<commsStruct>& comms,
324  List<T>& Values,
325  const int tag,
326  const label comm
327  );
328 
329  //- Like above but switches between linear/tree communication
330  template<class T>
331  static void scatterList
332  (
333  List<T>& Values,
334  const int tag = Pstream::msgType(),
335  const label comm = UPstream::worldComm
336  );
337 
338 
339  // Exchange
340 
341  //- Helper: exchange contiguous data. Sends sendData, receives into
342  // recvData. If block=true will wait for all transfers to finish.
343  template<class Container, class T>
344  static void exchange
345  (
346  const UList<Container>& sendData,
347  const labelUList& recvSizes,
348  List<Container>& recvData,
349  const int tag = UPstream::msgType(),
350  const label comm = UPstream::worldComm,
351  const bool block = true
352  );
353 
354  //- Helper: exchange sizes of sendData. sendData is the data per
355  // processor (in the communicator). Returns sizes of sendData
356  // on the sending processor.
357  template<class Container>
358  static void exchangeSizes
359  (
360  const Container& sendData,
361  labelList& sizes,
362  const label comm = UPstream::worldComm
363  );
364 
365  //- Exchange contiguous data. Sends sendData, receives into
366  // recvData. Determines sizes to receive.
367  // If block=true will wait for all transfers to finish.
368  template<class Container, class T>
369  static void exchange
370  (
371  const UList<Container>& sendData,
372  List<Container>& recvData,
373  const int tag = UPstream::msgType(),
374  const label comm = UPstream::worldComm,
375  const bool block = true
376  );
377 };
378 
379 
380 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
381 
382 } // End namespace Foam
383 
384 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
385 
386 #ifdef NoRepository
387  #include "gatherScatter.C"
388  #include "combineGatherScatter.C"
389  #include "gatherScatterList.C"
390  #include "exchange.C"
391 #endif
392 
393 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
394 
395 #endif
396 
397 // ************************************************************************* //
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
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 given optional buffer size.
Definition: UPstream.H:294
Foam::Pstream::gather
static void gather(const List< commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Gather data. Apply bop to combine Value.
Definition: gatherScatter.C:50
Foam::Pstream::buf_
DynamicList< char > buf_
Transfer buffer.
Definition: Pstream.H:97
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
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::UPstream::commsType
commsTypes commsType() const
Get the communications type of the stream.
Definition: UPstream.H:498
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::DynamicList::setCapacity
void setCapacity(const label nElem)
Alter the size of the underlying storage.
Definition: DynamicListI.H:232
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::msgType
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:491
Foam::UPstream::commsTypes
commsTypes
Types of communications.
Definition: UPstream.H:66
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::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:285
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
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::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