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-2022 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 UIPstreamBase.C
37
38\*---------------------------------------------------------------------------*/
39
40#include "Pstream.H"
41
42#ifndef Foam_UIPstream_H
43#define Foam_UIPstream_H
44
45#include "UPstream.H"
46#include "Istream.H"
47#include "DynamicList.H"
48#include "PstreamBuffers.H"
49
50// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51
52namespace Foam
53{
54
55/*---------------------------------------------------------------------------*\
56 Class UIPstreamBase Declaration
57\*---------------------------------------------------------------------------*/
59class UIPstreamBase
60:
61 public UPstream,
62 public Istream
63{
64 // Private Member Functions
65
66 //- Check buffer position against messageSize_ for EOF
67 inline void checkEof();
68
69 //- Prepare receive buffer by adjusting alignment
70 inline void prepareBuffer(const size_t align);
71
72 //- Read a T from the receive buffer
73 template<class T>
74 inline void readFromBuffer(T& val);
75
76 //- Read count bytes of data from the receive buffer.
77 // Prior data alignment is done by prepareBuffer
78 inline void readFromBuffer(void* data, const size_t count);
79
80 //- Read string length and string content
81 inline Istream& readString(std::string& str);
82
83
84protected:
85
86 // Protected Data
88 int fromProcNo_;
92 label& recvBufPos_;
94 const int tag_;
96 const label comm_;
98 const bool clearAtEnd_;
100 int messageSize_;
101
102
103 // Protected Constructors
104
105 //- Construct given process index to read from using the given
106 //- attached receive buffer, optional communication characteristics
107 //- and IO format
109 (
110 const commsTypes commsType,
111 const int fromProcNo,
112 DynamicList<char>& receiveBuf,
113 label& receiveBufPosition,
114 const int tag = UPstream::msgType(),
115 const label comm = UPstream::worldComm,
116 const bool clearAtEnd = false, // destroy receiveBuf if at end
118 );
119
120 //- Construct given buffers
121 UIPstreamBase(const int fromProcNo, PstreamBuffers& buffers);
122
123
124public:
125
126
127 //- Destructor. Optionally clears external receive buffer.
128 virtual ~UIPstreamBase();
129
130
131 // Member Functions
132
133 // Inquiry
134
135 //- Return flags of output stream
136 virtual ios_base::fmtflags flags() const
137 {
138 return ios_base::fmtflags(0);
139 }
140
141
142 // Read Functions
143
144 //- Return next token from stream
145 Istream& read(token& t);
146
147 //- Read a character
148 Istream& read(char& c);
149
150 //- Read a word
151 Istream& read(word& str);
152
153 // Read a string
154 Istream& read(string& str);
155
156 //- Read a label
157 Istream& read(label& val);
158
159 //- Read a floatScalar
160 Istream& read(floatScalar& val);
161
162 //- Read a doubleScalar
164
165 //- Read binary block with 8-byte alignment.
166 Istream& read(char* data, std::streamsize count);
167
168 //- Low-level raw binary read
169 Istream& readRaw(char* data, std::streamsize count);
170
171 //- Start of low-level raw binary read
172 bool beginRawRead();
173
174 //- End of low-level raw binary read
175 bool endRawRead()
176 {
177 return true;
178 }
179
180
181 // Positioning
182
183 //- Rewind the receive stream position so that it may be read again
184 virtual void rewind();
185
186
187 // Edit
188
189 //- Set flags of stream
190 virtual ios_base::fmtflags flags(const ios_base::fmtflags)
191 {
192 return ios_base::fmtflags(0);
193 }
194
195
196 // Print
197
198 //- Print stream description to Ostream
199 void print(Ostream& os) const;
200};
201
202
203/*---------------------------------------------------------------------------*\
204 Class UIPstream Declaration
205\*---------------------------------------------------------------------------*/
206
207//- Input inter-processor communications stream
208//- using MPI send/recv etc. - operating on external buffer.
209class UIPstream
210:
211 public UIPstreamBase
212{
213 // Private Member Functions
214
215 //- Initial buffer recv, called by constructor (blocking | scheduled)
216 void bufferIPCrecv();
217
218
219public:
220
221 // Constructors
222
223 //- Construct given process index to read from using the given
224 //- attached receive buffer, optional communication characteristics
225 //- and IO format
227 (
228 const commsTypes commsType,
229 const int fromProcNo,
230 DynamicList<char>& receiveBuf,
231 label& receiveBufPosition,
232 const int tag = UPstream::msgType(),
233 const label comm = UPstream::worldComm,
234 const bool clearAtEnd = false, // destroy receiveBuf if at end
236 );
237
238 //- Construct given buffers
239 UIPstream(const int fromProcNo, PstreamBuffers& buffers);
240
241
242 //- Destructor
243 virtual ~UIPstream() = default;
244
245
246 // Member Functions
247
248 //- Use all read methods from base
250
251
252 // Static Functions
253
254 //- Read buffer contents from given processor
255 // \return the message size
256 static label read
257 (
258 const commsTypes commsType,
259 const int fromProcNo,
260 char* buf,
261 const std::streamsize bufSize,
262 const int tag = UPstream::msgType(),
263 const label comm = UPstream::worldComm
264 );
265};
266
267
268/*---------------------------------------------------------------------------*\
269 Class UIPBstream Declaration
270\*---------------------------------------------------------------------------*/
271
272//- Input inter-processor communications stream
273//- using MPI broadcast - operating on external buffer.
274class UIPBstream
275:
276 public UIPstreamBase
277{
278 // Private Member Functions
279
280 //- Initial buffer recv via broadcast, called by constructor
281 void bufferIPCrecv();
282
283
284public:
285
286 // Constructors
287
288 //- Construct given process index to read from using the given
289 //- attached receive buffer, optional communication characteristics
290 //- and IO format
292 (
293 const commsTypes commsType,
294 const int rootProcNo,
295 DynamicList<char>& receiveBuf,
296 label& receiveBufPosition,
297 const int tag = UPstream::msgType(),
298 const label comm = UPstream::worldComm,
299 const bool clearAtEnd = false,
301 );
302
303
304 //- Destructor
305 virtual ~UIPBstream() = default;
306
307
308 // Member Functions
309
310 //- Use all read methods from base
312
313
314 // Static Functions
315
316 //- Wrapped version of UPstream::broadcast
317 // \return the message size
318 static label read
319 (
320 const commsTypes commsTypes,
321 const int rootProcNo,
322 char* buf,
323 const std::streamsize bufSize,
324 const int tag = UPstream::msgType(),
325 const label comm = UPstream::worldComm
326 );
327};
328
329
330// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
331
332} // End namespace Foam
333
334// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
335
336#endif
337
338// ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
streamFormat
Data format (ascii | binary)
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
Buffers for inter-processor communications streams (UOPstream, UIPstream).
virtual bool read()
Re-read model coefficients if they have changed.
static label read(const commsTypes commsTypes, const int rootProcNo, char *buf, const std::streamsize bufSize, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Wrapped version of UPstream::broadcast.
virtual ~UIPBstream()=default
Destructor.
Base class for input inter-processor communications stream (ie, parallel streams)....
Definition: UIPstream.H:62
virtual ios_base::fmtflags flags(const ios_base::fmtflags)
Set flags of stream.
Definition: UIPstream.H:189
bool endRawRead()
End of low-level raw binary read.
Definition: UIPstream.H:174
const int tag_
Definition: UIPstream.H:93
const label comm_
Definition: UIPstream.H:95
void print(Ostream &os) const
Print stream description to Ostream.
const bool clearAtEnd_
Definition: UIPstream.H:97
Istream & read(token &t)
Return next token from stream.
virtual void rewind()
Rewind the receive stream position so that it may be read again.
virtual ~UIPstreamBase()
Destructor. Optionally clears external receive buffer.
DynamicList< char > & recvBuf_
Definition: UIPstream.H:89
bool beginRawRead()
Start of low-level raw binary read.
Istream & readRaw(char *data, std::streamsize count)
Low-level raw binary read.
virtual ios_base::fmtflags flags() const
Return flags of output stream.
Definition: UIPstream.H:135
label & recvBufPos_
Definition: UIPstream.H:91
virtual ~UIPstream()=default
Destructor.
static label read(const commsTypes commsType, const int fromProcNo, char *buf, const std::streamsize bufSize, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Read buffer contents from given processor.
Definition: UIPstreamRead.C:42
Inter-processor communications stream.
Definition: UPstream.H:59
commsTypes
Types of communications.
Definition: UPstream.H:67
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:556
commsTypes commsType() const noexcept
Get the communications type of the stream.
Definition: UPstream.H:562
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:293
Database for solution data, solver performance and other reduced data.
Definition: data.H:58
A token holds an item read from Istream.
Definition: token.H:69
A class for handling words, derived from Foam::string.
Definition: word.H:68
const volScalarField & T
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
double doubleScalar
A typedef for double.
Definition: scalarFwd.H:48
float floatScalar
A typedef for float.
Definition: scalarFwd.H:45