Loading...
Searching...
No Matches
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,2024 OpenFOAM Foundation
9 Copyright (C) 2017-2025 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::UIPstreamBase
29
30Description
31 Base class for input inter-processor communications stream
32 (ie, parallel streams).
33 Not to be used directly, thus contructors are protected.
34
35SourceFiles
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
53namespace Foam
54{
56/*---------------------------------------------------------------------------*\
57 Class UIPstreamBase Declaration
58\*---------------------------------------------------------------------------*/
59
60class 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
86protected:
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(),
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
145public:
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 int32_t
188 virtual Istream& read(int32_t& val) override;
189
190 //- Read int64_t
191 virtual Istream& read(int64_t& val) override;
192
193 //- Read uint32_t
194 virtual Istream& read(uint32_t& val) override;
195
196 //- Read uint64_t
197 virtual Istream& read(uint64_t& val) override;
198
199 //- Read a float
200 virtual Istream& read(float& val) override;
201
202 //- Read a double
203 virtual Istream& read(double& val) override;
204
205 //- Read binary block with 8-byte alignment.
206 //- Reading into a null pointer behaves like a forward seek of
207 //- count characters.
208 virtual Istream& read(char* data, std::streamsize count) override;
209
210 //- Low-level raw binary read.
211 //- Reading into a null pointer behaves like a forward seek of
212 //- count characters.
213 virtual Istream& readRaw(char* data, std::streamsize count) override;
214
215 //- Start of low-level raw binary read
216 virtual bool beginRawRead() override;
217
218 //- End of low-level raw binary read
219 virtual bool endRawRead() override { return true; }
220
221
222 // Positioning
223
224 //- Rewind the receive stream position so that it may be read again
225 virtual void rewind() override;
226
227
228 // Print
229
230 //- Print stream description to Ostream
231 void print(Ostream& os) const override;
232};
233
234
235/*---------------------------------------------------------------------------*\
236 Class UIPstream Declaration
237\*---------------------------------------------------------------------------*/
238
239//- Input inter-processor communications stream
240//- using MPI send/recv etc. - operating on external buffer.
241class UIPstream
242:
243 public UIPstreamBase
244{
245 // Private Member Functions
246
247 //- Initial buffer recv, called by constructor (blocking | scheduled)
248 void bufferIPCrecv();
249
250
251public:
252
253 // Constructors
254
255 //- Construct given process index to read from using the given
256 //- attached receive buffer, optional communication characteristics
257 //- and IO format
259 (
261 const int fromProcNo,
262 DynamicList<char>& receiveBuf,
263 label& receiveBufPosition,
264 const int tag = UPstream::msgType(),
265 const int communicator = UPstream::worldComm,
266 const bool clearAtEnd = false, // destroy receiveBuf if at end
268 );
269
270 //- Construct given buffers
271 UIPstream(const int fromProcNo, PstreamBuffers& buffers);
272
273 //- Construct for reading from a standalone buffer that has
274 //- been obtained externally by the caller.
275 // The parameter is allowed to be const (since reading will not
276 // affect it), but must reference a concrete variable.
277 explicit UIPstream
278 (
279 const DynamicList<char>& recvBuf,
281 );
283
284 //- Destructor
285 virtual ~UIPstream() = default;
286
287
288 // Member Functions
289
290 //- Use all read methods from base
292
293
294 // Static Functions
295
296 //- Receive buffer contents (contiguous types) from given processor.
297 // \return the message size (elements read). May change in the future
298 template<class Type>
299 static std::streamsize read
300 (
302 const int fromProcNo,
303 Type* buffer,
304 std::streamsize count,
305 const int tag = UPstream::msgType(),
308 UPstream::Request* req = nullptr
309 );
311 //- Read buffer contents (non-blocking) from given processor
312 // \return number of elements read. May change in the future
313 template<class Type>
314 inline static std::streamsize read
315 (
318 const int fromProcNo,
319 Type* buffer,
320 std::streamsize count,
321 const int tag = UPstream::msgType(),
323 );
324
325 //- Receive into UList storage from given processor.
326 template<class Type>
327 inline static std::streamsize read
328 (
330 const int fromProcNo,
331 UList<Type>& buffer,
332 const int tag = UPstream::msgType(),
335 UPstream::Request* req = nullptr
336 );
337
338 //- Receive into SubList storage from given processor.
339 template<class Type>
340 inline static std::streamsize read
341 (
343 const int fromProcNo,
344 SubList<Type> buffer, // passed by shallow copy
345 const int tag = UPstream::msgType(),
348 UPstream::Request* req = nullptr
349 );
350
351 //- Receive into UList storage (non-blocking) from given processor.
352 template<class Type>
353 inline static std::streamsize read
354 (
357 const int fromProcNo,
358 UList<Type>& buffer,
359 const int tag = UPstream::msgType(),
361 );
362
363 //- Receive into SubList storage (non-blocking) from given processor.
364 template<class Type>
365 inline static std::streamsize read
366 (
369 const int fromProcNo,
370 SubList<Type> buffer, // passed by shallow copy
371 const int tag = UPstream::msgType(),
373 );
374};
375
376
377/*---------------------------------------------------------------------------*\
378 Class UIPBstream Declaration
379\*---------------------------------------------------------------------------*/
380
381//- Input inter-processor communications stream
382//- using MPI broadcast - operating on external buffer.
383class UIPBstream
385 public UIPstreamBase
386{
387 // Private Member Functions
388
389 //- Initial buffer recv via broadcast, called by constructor
390 void bufferIPCrecv();
391
392
393public:
394
395 // Constructors
396
397 //- Construct using the given attached receive buffer,
398 // optional communication characteristics and IO format
400 (
401 DynamicList<char>& receiveBuf,
402 label& receiveBufPosition,
404 const bool clearAtEnd = false,
406 );
407
408
409 //- Destructor
410 virtual ~UIPBstream() = default;
411};
412
413
414// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
415
416} // End namespace Foam
418// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
419
420#ifdef NoRepository
421 #include "UIPstream.txx"
422#endif
423
424// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
425
426#endif
427
428// ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
streamFormat
Data format (ascii | binary | coherent).
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
Istream(const Istream &)=default
Copy construct.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
Buffers for inter-processor communications streams (UOPstream, UIPstream).
A non-owning sub-view of a List (allocated or unallocated storage).
Definition SubList.H:61
Input inter-processor communications stream using MPI broadcast - operating on external buffer.
Definition UIPstream.H:484
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
virtual ~UIPBstream()=default
Destructor.
virtual std::ios_base::fmtflags flags() const override
Return current stream flags. Dummy for parallel stream, returns 0.
Definition UIPstream.H:190
label storedRecvBufPos_
Receive position in buffer data, if ony If there is no external location for recvBufPos_.
Definition UIPstream.H:120
virtual void rewind() override
Rewind the receive stream position so that it may be read again.
virtual Istream & read(token &) override
Return next token from stream.
const int fromProcNo_
Source rank for the data.
Definition UIPstream.H:99
virtual bool endRawRead() override
End of low-level raw binary read.
Definition UIPstream.H:282
void print(Ostream &os) const override
Print stream description to Ostream.
label remaining() const noexcept
The number of characters remaining in the get buffer.
const int tag_
Message tag for communication.
Definition UIPstream.H:104
virtual std::ios_base::fmtflags flags(std::ios_base::fmtflags) override
Set stream flags, return old stream flags. Dummy for parallel stream, returns 0.
Definition UIPstream.H:199
const int comm_
The communicator index.
Definition UIPstream.H:109
virtual bool beginRawRead() override
Start of low-level raw binary read.
label messageSize_
The message size, read on bufferIPCrecv or set directly.
Definition UIPstream.H:114
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,...
const bool clearAtEnd_
Clear the receive buffer on termination (in the destructor).
Definition UIPstream.H:125
virtual ~UIPstreamBase()
Destructor. Optionally clears external receive buffer.
DynamicList< char > & recvBuf_
Reference to the receive buffer data.
Definition UIPstream.H:130
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...
label & recvBufPos_
Reference to the receive position in buffer data.
Definition UIPstream.H:135
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.
static std::streamsize read(const UPstream::commsTypes commsType, const int fromProcNo, SubList< Type > buffer, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm, UPstream::Request *req=nullptr)
Receive into SubList storage from given processor.
static std::streamsize read(UPstream::Request &req, const int fromProcNo, Type *buffer, std::streamsize count, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm)
Read buffer contents (non-blocking) from given processor.
static std::streamsize read(UPstream::Request &req, const int fromProcNo, SubList< Type > buffer, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm)
Receive into SubList storage (non-blocking) from given processor.
static std::streamsize read(UPstream::Request &req, const int fromProcNo, UList< Type > &buffer, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm)
Receive into UList storage (non-blocking) from given processor.
virtual ~UIPstream()=default
Destructor.
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,...
Definition IPstreams.C:28
static std::streamsize read(const UPstream::commsTypes commsType, const int fromProcNo, UList< Type > &buffer, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm, UPstream::Request *req=nullptr)
Receive into UList storage from given processor.
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:89
An opaque wrapper for MPI_Request with a vendor-independent representation without any <mpi....
Definition UPstream.H:2919
Wrapper for internally indexed communicator label. Always invokes UPstream::allocateCommunicatorCompo...
Definition UPstream.H:2546
commsTypes
Communications types.
Definition UPstream.H:81
static int & msgType() noexcept
Message tag of standard messages.
Definition UPstream.H:1926
commsTypes commsType() const noexcept
Get the communications type of the stream.
Definition UPstream.H:1958
static label worldComm
Communicator for all ranks. May differ from commGlobal() if local worlds are in use.
Definition UPstream.H:1069
UPstream(const commsTypes commsType) noexcept
Construct for given communication type.
Definition UPstream.H:1184
A token holds an item read from Istream.
Definition token.H:70
A class for handling words, derived from Foam::string.
Definition word.H:66
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition int32.H:127
const direction noexcept
Definition scalarImpl.H:265
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)