Pstream.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::Pstream
26 
27 Description
28  Inter-processor communications stream.
29 
30 SourceFiles
31  Pstream.C
32  gatherScatter.C
33  combineGatherScatter.C
34  gatherScatterList.C
35  exchange.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef Pstream_H
40 #define Pstream_H
41 
42 #include "UPstream.H"
43 #include "DynamicList.H"
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 namespace Foam
48 {
49 
50 /*---------------------------------------------------------------------------*\
51  Class Pstream Declaration
52 \*---------------------------------------------------------------------------*/
53 
54 class Pstream
55 :
56  public UPstream
57 {
58 
59 protected:
60 
61  // Protected data
62 
63  //- Transfer buffer
65 
66 public:
67 
68  // Declare name of the class and its debug switch
69  ClassName("Pstream");
70 
71 
72  // Constructors
73 
74  //- Construct given optional buffer size
76  (
77  const commsTypes commsType,
78  const label bufSize = 0
79  )
80  :
81  UPstream(commsType),
82  buf_(0)
83  {
84  if (bufSize)
85  {
86  buf_.setCapacity(bufSize + 2*sizeof(scalar) + 1);
87  }
88  }
89 
90 
91  // Gather and scatter
92 
93  //- Gather data. Apply bop to combine Value
94  // from different processors
95  template<class T, class BinaryOp>
96  static void gather
97  (
98  const List<commsStruct>& comms,
99  T& Value,
100  const BinaryOp& bop,
101  const int tag,
102  const label comm
103  );
104 
105  //- Like above but switches between linear/tree communication
106  template<class T, class BinaryOp>
107  static void gather
108  (
109  T& Value,
110  const BinaryOp& bop,
111  const int tag = Pstream::msgType(),
112  const label comm = Pstream::worldComm
113  );
114 
115  //- Scatter data. Distribute without modification. Reverse of gather
116  template<class T>
117  static void scatter
118  (
119  const List<commsStruct>& comms,
120  T& Value,
121  const int tag,
122  const label comm
123  );
124 
125  //- Like above but switches between linear/tree communication
126  template<class T>
127  static void scatter
128  (
129  T& Value,
130  const int tag = Pstream::msgType(),
131  const label comm = Pstream::worldComm
132  );
133 
134  // Combine variants. Inplace combine values from processors.
135  // (Uses construct from Istream instead of <<)
136 
137  template<class T, class CombineOp>
138  static void combineGather
139  (
140  const List<commsStruct>& comms,
141  T& Value,
142  const CombineOp& cop,
143  const int tag,
144  const label comm
145  );
146 
147  //- Like above but switches between linear/tree communication
148  template<class T, class CombineOp>
149  static void combineGather
150  (
151  T& Value,
152  const CombineOp& cop,
153  const int tag = Pstream::msgType(),
154  const label comm = Pstream::worldComm
155  );
156 
157  //- Scatter data. Reverse of combineGather
158  template<class T>
159  static void combineScatter
160  (
161  const List<commsStruct>& comms,
162  T& Value,
163  const int tag,
164  const label comm
165  );
166 
167  //- Like above but switches between linear/tree communication
168  template<class T>
169  static void combineScatter
170  (
171  T& Value,
172  const int tag = Pstream::msgType(),
173  const label comm = Pstream::worldComm
174  );
175 
176  // Combine variants working on whole List at a time.
177 
178  template<class T, class CombineOp>
179  static void listCombineGather
180  (
181  const List<commsStruct>& comms,
182  List<T>& Value,
183  const CombineOp& cop,
184  const int tag,
185  const label comm
186  );
187 
188  //- Like above but switches between linear/tree communication
189  template<class T, class CombineOp>
190  static void listCombineGather
191  (
192  List<T>& Value,
193  const CombineOp& cop,
194  const int tag = Pstream::msgType(),
195  const label comm = Pstream::worldComm
196  );
197 
198  //- Scatter data. Reverse of combineGather
199  template<class T>
200  static void listCombineScatter
201  (
202  const List<commsStruct>& comms,
203  List<T>& Value,
204  const int tag,
205  const label comm
206  );
207 
208  //- Like above but switches between linear/tree communication
209  template<class T>
210  static void listCombineScatter
211  (
212  List<T>& Value,
213  const int tag = Pstream::msgType(),
214  const label comm = Pstream::worldComm
215  );
216 
217  // Combine variants working on whole map at a time. Container needs to
218  // have iterators and find() defined.
219 
220  template<class Container, class CombineOp>
221  static void mapCombineGather
222  (
223  const List<commsStruct>& comms,
224  Container& Values,
225  const CombineOp& cop,
226  const int tag,
227  const label comm
228  );
229 
230  //- Like above but switches between linear/tree communication
231  template<class Container, class CombineOp>
232  static void mapCombineGather
233  (
234  Container& Values,
235  const CombineOp& cop,
236  const int tag = Pstream::msgType(),
237  const label comm = UPstream::worldComm
238  );
239 
240  //- Scatter data. Reverse of combineGather
241  template<class Container>
242  static void mapCombineScatter
243  (
244  const List<commsStruct>& comms,
245  Container& Values,
246  const int tag,
247  const label comm
248  );
249 
250  //- Like above but switches between linear/tree communication
251  template<class Container>
252  static void mapCombineScatter
253  (
254  Container& Values,
255  const int tag = Pstream::msgType(),
256  const label comm = UPstream::worldComm
257  );
258 
259 
260 
261  // Gather/scatter keeping the individual processor data separate.
262  // Values is a List of size UPstream::nProcs() where
263  // Values[UPstream::myProcNo()] is the data for the current processor.
264 
265  //- Gather data but keep individual values separate
266  template<class T>
267  static void gatherList
268  (
269  const List<commsStruct>& comms,
270  List<T>& Values,
271  const int tag,
272  const label comm
273  );
274 
275  //- Like above but switches between linear/tree communication
276  template<class T>
277  static void gatherList
278  (
279  List<T>& Values,
280  const int tag = Pstream::msgType(),
281  const label comm = UPstream::worldComm
282  );
283 
284  //- Scatter data. Reverse of gatherList
285  template<class T>
286  static void scatterList
287  (
288  const List<commsStruct>& comms,
289  List<T>& Values,
290  const int tag,
291  const label comm
292  );
293 
294  //- Like above but switches between linear/tree communication
295  template<class T>
296  static void scatterList
297  (
298  List<T>& Values,
299  const int tag = Pstream::msgType(),
300  const label comm = UPstream::worldComm
301  );
302 
303 
304  // Exchange
305 
306  //- Helper: exchange contiguous data. Sends sendData, receives into
307  // recvData. If block=true will wait for all transfers to finish.
308  template<class Container, class T>
309  static void exchange
310  (
311  const UList<Container>& sendData,
312  const labelUList& recvSizes,
313  List<Container>& recvData,
314  const int tag = UPstream::msgType(),
315  const label comm = UPstream::worldComm,
316  const bool block = true
317  );
318 
319  //- Helper: exchange sizes of sendData. sendData is the data per
320  // processor (in the communicator). Returns sizes of sendData
321  // on the sending processor.
322  template<class Container>
323  static void exchangeSizes
324  (
325  const Container& sendData,
326  labelList& sizes,
327  const label comm = UPstream::worldComm
328  );
329 
330  //- Exchange contiguous data. Sends sendData, receives into
331  // recvData. Determines sizes to receive.
332  // If block=true will wait for all transfers to finish.
333  template<class Container, class T>
334  static void exchange
335  (
336  const UList<Container>& sendData,
337  List<Container>& recvData,
338  const int tag = UPstream::msgType(),
339  const label comm = UPstream::worldComm,
340  const bool block = true
341  );
342 };
343 
344 
345 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
346 
347 } // End namespace Foam
348 
349 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
350 
351 #ifdef NoRepository
352  #include "gatherScatter.C"
353  #include "combineGatherScatter.C"
354  #include "gatherScatterList.C"
355  #include "exchange.C"
356 #endif
357 
358 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
359 
360 #endif
361 
362 // ************************************************************************* //
static void scatterList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Scatter data. Reverse of gatherList.
commsTypes commsType() const
Get the communications type of the stream.
Definition: UPstream.H:483
static void listCombineScatter(const List< commsStruct > &comms, List< T > &Value, const int tag, const label comm)
Scatter data. Reverse of combineGather.
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:59
Gather data from all processors onto single processor according to some communication schedule (usual...
commsTypes
Types of communications.
Definition: UPstream.H:64
DynamicList< char > buf_
Transfer buffer.
Definition: Pstream.H:63
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
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:38
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:476
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:278
static void listCombineGather(const List< commsStruct > &comms, List< T > &Value, const CombineOp &cop, const int tag, const label comm)
static void mapCombineScatter(const List< commsStruct > &comms, Container &Values, const int tag, const label comm)
Scatter data. Reverse of combineGather.
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:137
Inter-processor communications stream.
Definition: Pstream.H:53
Gather data from all processors onto single processor according to some communication schedule (usual...
void setCapacity(const label)
Alter the size of the underlying storage.
Definition: DynamicListI.H:130
static void combineGather(const List< commsStruct > &comms, T &Value, const CombineOp &cop, const int tag, const label comm)
static void mapCombineGather(const List< commsStruct > &comms, Container &Values, const CombineOp &cop, const int tag, const label comm)
Variant of gather, scatter. Normal gather uses:
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:60
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
Pstream(const commsTypes commsType, const label bufSize=0)
Construct given optional buffer size.
Definition: Pstream.H:75
Creates a single block of cells from point coordinates, numbers of cells in each direction and an exp...
Definition: block.H:63
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
ClassName("Pstream")
Exchange data.
static void combineScatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Reverse of combineGather.
static void gatherList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Gather data but keep individual values separate.
Inter-processor communications stream.
Definition: UPstream.H:58
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:47
Namespace for OpenFOAM.