UIPstream.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-2013 OpenFOAM Foundation
9  Copyright (C) 2017-2025 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::UIPstreamBase
29 
30 Description
31  Base class for input inter-processor communications stream
32  (ie, parallel streams).
33  Not to be used directly, thus contructors are protected.
34 
35 SourceFiles
36  UIPstream.txx
37  UIPstreamBase.C
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #include "Pstream.H"
42 
43 #ifndef Foam_UIPstream_H
44 #define Foam_UIPstream_H
45 
46 #include "UPstream.H"
47 #include "Istream.H"
48 #include "DynamicList.H"
49 #include "PstreamBuffers.H"
50 
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52 
53 namespace Foam
54 {
55 
56 /*---------------------------------------------------------------------------*\
57  Class UIPstreamBase Declaration
58 \*---------------------------------------------------------------------------*/
59 
60 class UIPstreamBase
61 :
62  public UPstream,
63  public Istream
64 {
65  // Private Member Functions
66 
67  //- Check buffer position against messageSize_ for EOF
68  inline void checkEof();
69 
70  //- Prepare receive buffer by adjusting alignment
71  inline void prepareBuffer(const size_t align);
72 
73  //- Read a T from the receive buffer
74  template<class T>
75  inline void readFromBuffer(T& val);
76 
77  //- Read count bytes of data from the receive buffer.
78  // Prior data alignment is done by prepareBuffer
79  // Reading into a null pointer behaves like a forward seek
80  inline void readFromBuffer(void* data, const size_t count);
81 
82  //- Read string length and string content
83  inline Istream& readString(std::string& str);
84 
85 
86 protected:
87 
88  // Protected Data
89 
90  //- Source rank for the data
91  const int fromProcNo_;
92 
93  //- Message tag for communication
94  const int tag_;
95 
96  //- The communicator index
97  const int comm_;
98 
99  //- The message size, read on bufferIPCrecv or set directly
100  label messageSize_;
101 
102  //- Receive position in buffer data, if ony
103  //- If there is no external location for recvBufPos_
105 
106  //- Clear the receive buffer on termination (in the destructor)
107  const bool clearAtEnd_;
108 
109  //- Reference to the receive buffer data
111 
112  //- Reference to the receive position in buffer data
113  label& recvBufPos_;
115 
116  // Protected Constructors
117 
118  //- Construct given process index to read from using the given
119  //- attached receive buffer, optional communication characteristics
120  //- and IO format
122  (
124  const int fromProcNo,
125  DynamicList<char>& receiveBuf,
126  label& receiveBufPosition,
127  const int tag = UPstream::msgType(),
128  const int communicator = UPstream::worldComm,
129  const bool clearAtEnd = false, // destroy receiveBuf if at end
131  );
132 
133  //- Construct given buffers
134  UIPstreamBase(const int fromProcNo, PstreamBuffers& buffers);
136  //- Construct for an externally obtained buffer.
137  // The parameter is allowed to be const (since reading will not
138  // affect it), but must reference a concrete variable.
140  (
141  const DynamicList<char>& receiveBuf,
143  );
144 
145 public:
146 
147  //- Destructor. Optionally clears external receive buffer.
148  virtual ~UIPstreamBase();
149 
150 
151  // Member Functions
152 
153  // Stream State Functions
154 
155  //- Return current stream flags.
156  //- Dummy for parallel stream, returns 0.
157  virtual std::ios_base::fmtflags flags() const override
158  {
159  return std::ios_base::fmtflags(0);
160  }
161 
162  //- Set stream flags, return old stream flags.
163  //- Dummy for parallel stream, returns 0.
164  virtual std::ios_base::fmtflags flags(std::ios_base::fmtflags) override
165  {
166  return std::ios_base::fmtflags(0);
167  }
168 
169  //- The number of characters remaining in the get buffer
170  label remaining() const noexcept;
171 
172 
173  // Read Functions
174 
175  //- Return next token from stream
176  virtual Istream& read(token&) override;
177 
178  //- Read a character
179  virtual Istream& read(char& c) override;
180 
181  //- Read a word
182  virtual Istream& read(word& str) override;
183 
184  // Read a string
185  virtual Istream& read(string& str) override;
186 
187  //- Read a label
188  virtual Istream& read(label& val) override;
189 
190  //- Read a float
191  virtual Istream& read(float& val) override;
192 
193  //- Read a double
194  virtual Istream& read(double& val) override;
195 
196  //- Read binary block with 8-byte alignment.
197  //- Reading into a null pointer behaves like a forward seek of
198  //- count characters.
199  virtual Istream& read(char* data, std::streamsize count) override;
200 
201  //- Low-level raw binary read.
202  //- Reading into a null pointer behaves like a forward seek of
203  //- count characters.
204  virtual Istream& readRaw(char* data, std::streamsize count) override;
205 
206  //- Start of low-level raw binary read
207  virtual bool beginRawRead() override;
208 
209  //- End of low-level raw binary read
210  virtual bool endRawRead() override { return true; }
211 
212 
213  // Positioning
214 
215  //- Rewind the receive stream position so that it may be read again
216  virtual void rewind() override;
217 
218 
219  // Print
220 
221  //- Print stream description to Ostream
222  void print(Ostream& os) const override;
223 };
224 
225 
226 /*---------------------------------------------------------------------------*\
227  Class UIPstream Declaration
228 \*---------------------------------------------------------------------------*/
229 
230 //- Input inter-processor communications stream
231 //- using MPI send/recv etc. - operating on external buffer.
232 class UIPstream
233 :
234  public UIPstreamBase
235 {
236  // Private Member Functions
237 
238  //- Initial buffer recv, called by constructor (blocking | scheduled)
239  void bufferIPCrecv();
240 
241 
242 public:
243 
244  // Constructors
245 
246  //- Construct given process index to read from using the given
247  //- attached receive buffer, optional communication characteristics
248  //- and IO format
249  UIPstream
250  (
252  const int fromProcNo,
253  DynamicList<char>& receiveBuf,
254  label& receiveBufPosition,
255  const int tag = UPstream::msgType(),
256  const int communicator = UPstream::worldComm,
257  const bool clearAtEnd = false, // destroy receiveBuf if at end
259  );
260 
261  //- Construct given buffers
262  UIPstream(const int fromProcNo, PstreamBuffers& buffers);
263 
264  //- Construct for reading from a standalone buffer that has
265  //- been obtained externally by the caller.
266  // The parameter is allowed to be const (since reading will not
267  // affect it), but must reference a concrete variable.
268  explicit UIPstream
269  (
270  const DynamicList<char>& recvBuf,
272  );
273 
274 
275  //- Destructor
276  virtual ~UIPstream() = default;
277 
278 
279  // Member Functions
280 
281  //- Use all read methods from base
282  using UIPstreamBase::read;
283 
284 
285  // Static Functions
286 
287  //- Receive buffer contents (contiguous types) from given processor.
288  // \return the message size (elements read). May change in the future
289  template<class Type>
290  static std::streamsize read
291  (
293  const int fromProcNo,
294  Type* buffer,
295  std::streamsize count,
296  const int tag = UPstream::msgType(),
297  const int communicator = UPstream::worldComm,
299  UPstream::Request* req = nullptr
300  );
301 
302  //- Read buffer contents (non-blocking) from given processor
303  // \return number of elements read. May change in the future
304  template<class Type>
305  inline static std::streamsize read
306  (
308  UPstream::Request& req,
309  const int fromProcNo,
310  Type* buffer,
311  std::streamsize count,
312  const int tag = UPstream::msgType(),
314  );
315 
316  //- Receive into UList storage from given processor.
317  template<class Type>
318  inline static std::streamsize read
319  (
321  const int fromProcNo,
322  UList<Type>& buffer,
323  const int tag = UPstream::msgType(),
324  const int communicator = UPstream::worldComm,
326  UPstream::Request* req = nullptr
327  );
328 
329  //- Receive into SubList storage from given processor.
330  template<class Type>
331  inline static std::streamsize read
332  (
334  const int fromProcNo,
335  SubList<Type> buffer, // passed by shallow copy
336  const int tag = UPstream::msgType(),
337  const int communicator = UPstream::worldComm,
339  UPstream::Request* req = nullptr
340  );
341 
342  //- Receive into UList storage (non-blocking) from given processor.
343  template<class Type>
344  inline static std::streamsize read
345  (
347  UPstream::Request& req,
348  const int fromProcNo,
349  UList<Type>& buffer,
350  const int tag = UPstream::msgType(),
352  );
353 
354  //- Receive into SubList storage (non-blocking) from given processor.
355  template<class Type>
356  inline static std::streamsize read
357  (
359  UPstream::Request& req,
360  const int fromProcNo,
361  SubList<Type> buffer, // passed by shallow copy
362  const int tag = UPstream::msgType(),
364  );
365 };
366 
367 
368 /*---------------------------------------------------------------------------*\
369  Class UIPBstream Declaration
370 \*---------------------------------------------------------------------------*/
371 
372 //- Input inter-processor communications stream
373 //- using MPI broadcast - operating on external buffer.
374 class UIPBstream
375 :
376  public UIPstreamBase
377 {
378  // Private Member Functions
379 
380  //- Initial buffer recv via broadcast, called by constructor
381  void bufferIPCrecv();
382 
383 
384 public:
385 
386  // Constructors
387 
388  //- Construct using the given attached receive buffer,
389  // optional communication characteristics and IO format
390  UIPBstream
391  (
392  DynamicList<char>& receiveBuf,
393  label& receiveBufPosition,
394  const int communicator = UPstream::worldComm,
395  const bool clearAtEnd = false,
397  );
398 
399 
400  //- Destructor
401  virtual ~UIPBstream() = default;
402 };
403 
404 
405 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
406 
407 } // End namespace Foam
408 
409 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
410 
411 #ifdef NoRepository
412  #include "UIPstream.txx"
413 #endif
414 
415 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
416 
417 #endif
418 
419 // ************************************************************************* //
virtual bool endRawRead() override
End of low-level raw binary read.
Definition: UIPstream.H:267
virtual ~UIPstreamBase()
Destructor. Optionally clears external receive buffer.
commsTypes
Communications types.
Definition: UPstream.H:77
Base class for input inter-processor communications stream (ie, parallel streams). Not to be used directly, thus contructors are protected.
Definition: UIPstream.H:55
virtual bool beginRawRead() override
Start of low-level raw binary read.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
UIPstreamBase(const UPstream::commsTypes commsType, const int fromProcNo, DynamicList< char > &receiveBuf, label &receiveBufPosition, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm, const bool clearAtEnd=false, IOstreamOption::streamFormat fmt=IOstreamOption::BINARY)
Construct given process index to read from using the given attached receive buffer, optional communication characteristics and IO format.
const int fromProcNo_
Source rank for the data.
Definition: UIPstream.H:99
label & recvBufPos_
Reference to the receive position in buffer data.
Definition: UIPstream.H:135
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:1787
static std::streamsize read(const UPstream::commsTypes commsType, const int fromProcNo, Type *buffer, std::streamsize count, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm, UPstream::Request *req=nullptr)
Receive buffer contents (contiguous types) from given processor.
virtual ~UIPBstream()=default
Destructor.
virtual ~UIPstream()=default
Destructor.
static label worldComm
Communicator for all ranks. May differ from commGlobal() if local worlds are in use.
Definition: UPstream.H:987
void print(Ostream &os) const override
Print stream description to Ostream.
label messageSize_
The message size, read on bufferIPCrecv or set directly.
Definition: UIPstream.H:114
label storedRecvBufPos_
Receive position in buffer data, if ony If there is no external location for recvBufPos_.
Definition: UIPstream.H:120
Input inter-processor communications stream using MPI broadcast - operating on external buffer...
Definition: UIPstream.H:466
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 without any <mpi...
Definition: UPstream.H:2599
virtual Istream & readRaw(char *data, std::streamsize count) override
Low-level raw binary read. Reading into a null pointer behaves like a forward seek of count character...
UIPstream(const UPstream::commsTypes commsType, const int fromProcNo, DynamicList< char > &receiveBuf, label &receiveBufPosition, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm, const bool clearAtEnd=false, IOstreamOption::streamFormat fmt=IOstreamOption::BINARY)
Construct given process index to read from using the given attached receive buffer, optional communication characteristics and IO format.
Definition: IPstreams.C:28
commsTypes commsType() const noexcept
Get the communications type of the stream.
Definition: UPstream.H:1819
label remaining() const noexcept
The number of characters remaining in the get buffer.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: scalarImpl.H:255
OBJstream os(runTime.globalPath()/outputName)
Buffers for inter-processor communications streams (UOPstream, UIPstream).
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
virtual std::ios_base::fmtflags flags() const override
Return current stream flags. Dummy for parallel stream, returns 0.
Definition: UIPstream.H:190
Wrapper for internally indexed communicator label. Always invokes UPstream::allocateCommunicatorCompo...
Definition: UPstream.H:2237
const bool clearAtEnd_
Clear the receive buffer on termination (in the destructor)
Definition: UIPstream.H:125
DynamicList< char > & recvBuf_
Reference to the receive buffer data.
Definition: UIPstream.H:130
virtual void rewind() override
Rewind the receive stream position so that it may be read again.
const int tag_
Message tag for communication.
Definition: UIPstream.H:104
const dimensionedScalar c
Speed of light in a vacuum.
streamFormat
Data format (ascii | binary)
UIPBstream(DynamicList< char > &receiveBuf, label &receiveBufPosition, const int communicator=UPstream::worldComm, const bool clearAtEnd=false, IOstreamOption::streamFormat fmt=IOstreamOption::BINARY)
Construct using the given attached receive buffer,.
Definition: IPBstreams.C:28
const int comm_
The communicator index.
Definition: UIPstream.H:109
Inter-processor communications stream.
Definition: UPstream.H:65
virtual Istream & read(token &) override
Return next token from stream.
Namespace for OpenFOAM.
Istream(const Istream &)=default
Copy construct.