UOPstream.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-2014 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::UOPstreamBase
29
30Description
31 Base class for output inter-processor communications stream
32 (ie, parallel streams).
33 Not to be used directly, thus contructors are protected.
34
35SourceFiles
36 UOPstreamBase.C
37
38\*---------------------------------------------------------------------------*/
39
40#include "Pstream.H"
41
42#ifndef Foam_UOPstream_H
43#define Foam_UOPstream_H
44
45#include "UPstream.H"
46#include "Ostream.H"
47#include "DynamicList.H"
48#include "PstreamBuffers.H"
49
50// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51
52namespace Foam
53{
54
55/*---------------------------------------------------------------------------*\
56 Class UOPstreamBase Declaration
57\*---------------------------------------------------------------------------*/
59class UOPstreamBase
60:
61 public UPstream,
62 public Ostream
63{
64 // Private Member Functions
65
66 //- Prepare send buffer for count bytes of output,
67 //- with specified alignment.
68 inline void prepareBuffer(const size_t count, const size_t align);
69
70 //- Generic write data to the send buffer, aligned by sizeof(T)
71 template<class T>
72 inline void writeToBuffer(const T& val);
73
74 //- Write count bytes of data to the send buffer,
75 //- using align byte alignment
76 inline void writeToBuffer
77 (
78 const void* data,
79 const size_t count,
80 const size_t align
81 );
82
83 //- Add a single char to the send buffer. No alignment needed
84 inline void putChar(const char c);
85
86 //- Write string length and string content.
87 // The content includes the trailing nul char.
88 inline void putString(const std::string& str);
89
90
91protected:
92
93 // Protected Data
95 int toProcNo_;
99 const int tag_;
101 const label comm_;
103 const bool sendAtDestruct_;
104
105
106 // Protected Constructors
107
108 //- Construct given process index to write to using the given
109 //- attached send buffer, optional communication characteristics
110 //- and IO format
112 (
113 const commsTypes commsType,
114 const int toProcNo,
115 DynamicList<char>& sendBuf,
116 const int tag = UPstream::msgType(),
117 const label comm = UPstream::worldComm,
118 const bool sendAtDestruct = true,
120 );
121
122 //- Construct given buffers
123 UOPstreamBase(const int toProcNo, PstreamBuffers& buffers);
124
125
126public:
127
128 //- Destructor.
129 virtual ~UOPstreamBase();
130
131
132 // Member Functions
133
134 // Inquiry
135
136 //- Return flags of output stream
137 virtual ios_base::fmtflags flags() const
138 {
139 return ios_base::fmtflags(0);
140 }
141
142
143 // Write Functions
144
145 //- Write token to stream or otherwise handle it.
146 // \return false if the token type was not handled by this method
147 virtual bool write(const token& tok);
148
149 //- Write single character. Whitespace is suppressed.
150 virtual Ostream& write(const char c);
151
152 //- Write the word-characters of a character string.
153 // Sends as a single char, or as word.
154 virtual Ostream& write(const char* str);
155
156 //- Write word
157 virtual Ostream& write(const word& str);
158
159 //- Write string
160 virtual Ostream& write(const string& str);
161
162 //- Write std::string surrounded by quotes.
163 // Optional write without quotes.
164 virtual Ostream& writeQuoted
165 (
166 const std::string& str,
167 const bool quoted=true
168 );
169
170 //- Write int32_t as a label
171 virtual Ostream& write(const int32_t val);
172
173 //- Write int64_t as a label
174 virtual Ostream& write(const int64_t val);
175
176 //- Write floatScalar
177 virtual Ostream& write(const floatScalar val);
178
179 //- Write doubleScalar
180 virtual Ostream& write(const doubleScalar val);
181
182 //- Write binary block with 8-byte alignment.
183 virtual Ostream& write(const char* data, std::streamsize count);
184
185 //- Low-level raw binary output.
186 virtual Ostream& writeRaw(const char* data, std::streamsize count);
187
188 //- Begin marker for low-level raw binary output.
189 // The count indicates the number of bytes for subsequent
190 // writeRaw calls.
191 virtual bool beginRawWrite(std::streamsize count);
192
193 //- End marker for low-level raw binary output.
194 virtual bool endRawWrite()
195 {
196 return true;
197 }
198
199 //- Add indentation characters
200 virtual void indent()
201 {}
202
203
204 // Stream state functions
205
206 //- Flush stream
207 virtual void flush()
208 {}
209
210 //- Add newline and flush stream
211 virtual void endl()
212 {}
213
214 //- Get the current padding character
215 // \return previous padding character
216 virtual char fill() const
217 {
218 return 0;
219 }
220
221 //- Set padding character for formatted field up to field width
222 virtual char fill(const char)
223 {
224 return 0;
225 }
226
227 //- Get width of output field
228 virtual int width() const
229 {
230 return 0;
231 }
232
233 //- Set width of output field
234 // \return previous width
235 virtual int width(const int)
236 {
237 return 0;
238 }
239
240 //- Get precision of output field
241 virtual int precision() const
242 {
243 return 0;
244 }
245
246 //- Set precision of output field
247 // \return old precision
248 virtual int precision(const int)
249 {
250 return 0;
251 }
252
253
254 // Positioning
255
256 //- Rewind the send buffer for overwriting
257 virtual void rewind();
258
259
260 // Edit
261
262 //- Set flags of stream
263 virtual ios_base::fmtflags flags(const ios_base::fmtflags)
264 {
265 return ios_base::fmtflags(0);
266 }
267
268
269 // Print
270
271 //- Print stream description to Ostream
272 void print(Ostream& os) const;
273};
274
275
276/*---------------------------------------------------------------------------*\
277 Class UOPstream Declaration
278\*---------------------------------------------------------------------------*/
279
280//- Output inter-processor communications stream
281//- using MPI send/recv etc. - operating on external buffer.
282class UOPstream
283:
284 public UOPstreamBase
285{
286 // Private Member Functions
287
288 //- Final buffer send, called by destructor
289 bool bufferIPCsend();
290
291
292public:
293
294 // Constructors
295
296 //- Construct given process index to write to using the given
297 //- attached send buffer, optional communication characteristics
298 //- and IO format
300 (
301 const commsTypes commsType,
302 const int toProcNo,
303 DynamicList<char>& sendBuf,
304 const int tag = UPstream::msgType(),
305 const label comm = UPstream::worldComm,
306 const bool sendAtDestruct = true,
308 );
309
310 //- Construct given buffers
311 UOPstream(const int toProcNo, PstreamBuffers& buffers);
312
313
314 //- Destructor, usually sends buffer on destruct.
315 virtual ~UOPstream();
316
317
318 // Member Functions
319
320 //- Use all write methods from base class
322
323
324 // Static Functions
325
326 //- Write buffer contents to given processor
327 // \return True on success
328 static bool write
329 (
330 const commsTypes commsType,
331 const int toProcNo,
332 const char* buf,
333 const std::streamsize bufSize,
334 const int tag = UPstream::msgType(),
335 const label comm = UPstream::worldComm
336 );
337};
338
339
340/*---------------------------------------------------------------------------*\
341 Class UOPBstream Declaration
342\*---------------------------------------------------------------------------*/
343
344//- Output inter-processor communications stream
345//- using MPI broadcast - operating on external buffer.
346//
347// \note does not use commsType, tag etc.
348class UOPBstream
349:
350 public UOPstreamBase
351{
352 // Private Member Functions
353
354 //- Final buffer send, called by destructor
355 bool bufferIPCsend();
356
357
358public:
359
360 // Constructors
361
362 //- Construct given process index to write to using the given
363 //- attached send buffer, optional communication characteristics
364 //- and IO format
366 (
367 const commsTypes commsType,
368 const int toProcNo,
369 DynamicList<char>& sendBuf,
370 const int tag = UPstream::msgType(),
371 const label comm = UPstream::worldComm,
372 const bool sendAtDestruct = true,
374 );
375
376
377 //- Destructor, usually sends buffer on destruct.
378 virtual ~UOPBstream();
379
380
381 // Member Functions
382
383 //- Use all write methods from base
385
386
387 // Static Functions
388
389 //- Wrapped version of UPstream::broadcast with const-cast
390 // \return True on success
391 static bool write
392 (
393 const commsTypes commsType,
394 const int rootProcNo,
395 const char* buf,
396 const std::streamsize bufSize,
397 const int tag = UPstream::msgType(),
398 const label comm = UPstream::worldComm
399 );
400};
401
402
403// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
404
405} // End namespace Foam
406
407// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
408
409#endif
410
411// ************************************************************************* //
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 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 ~UOPBstream()
Destructor, usually sends buffer on destruct.
Definition: OPBstreams.C:94
Base class for output inter-processor communications stream (ie, parallel streams)....
Definition: UOPstream.H:62
DynamicList< char > & sendBuf_
Definition: UOPstream.H:96
virtual ios_base::fmtflags flags(const ios_base::fmtflags)
Set flags of stream.
Definition: UOPstream.H:262
virtual ~UOPstreamBase()
Destructor.
virtual Ostream & writeRaw(const char *data, std::streamsize count)
Low-level raw binary output.
virtual char fill() const
Get the current padding character.
Definition: UOPstream.H:215
virtual void indent()
Add indentation characters.
Definition: UOPstream.H:199
virtual char fill(const char)
Set padding character for formatted field up to field width.
Definition: UOPstream.H:221
virtual void endl()
Add newline and flush stream.
Definition: UOPstream.H:210
const bool sendAtDestruct_
Definition: UOPstream.H:102
virtual bool beginRawWrite(std::streamsize count)
Begin marker for low-level raw binary output.
const int tag_
Definition: UOPstream.H:98
const label comm_
Definition: UOPstream.H:100
void print(Ostream &os) const
Print stream description to Ostream.
virtual int width() const
Get width of output field.
Definition: UOPstream.H:227
virtual void rewind()
Rewind the send buffer for overwriting.
virtual void flush()
Flush stream.
Definition: UOPstream.H:206
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)
Write std::string surrounded by quotes.
virtual bool endRawWrite()
End marker for low-level raw binary output.
Definition: UOPstream.H:193
virtual ios_base::fmtflags flags() const
Return flags of output stream.
Definition: UOPstream.H:136
virtual int precision() const
Get precision of output field.
Definition: UOPstream.H:240
virtual ~UOPstream()
Destructor, usually sends buffer on destruct.
Definition: OPstreams.C:81
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
virtual bool write()
Write the output fields.
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
runTime write()