Loading...
Searching...
No Matches
UOPstreamBase.C
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-2017,2024 OpenFOAM Foundation
9 Copyright (C) 2016-2023 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
27\*---------------------------------------------------------------------------*/
28
29#include "UOPstream.H"
30#include "int.H"
31#include "token.H"
32#include <cctype>
34// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
35
36namespace Foam
37{
38
39// Return the position with word boundary alignment
40inline static label byteAlign(const label pos, const size_t align)
41{
42 return
43 (
44 (align > 1)
45 ? (align + ((pos - 1) & ~(align - 1)))
46 : pos
47 );
48}
49
50} // End namespace Foam
51
52
53// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
54
55inline void Foam::UOPstreamBase::prepareBuffer
56(
57 const size_t count,
58 const size_t align
59)
60{
61 if (!count)
62 {
63 return;
64 }
65
66 // Align for the next output position
67 const label pos = byteAlign(sendBuf_.size(), align);
68
69 // Extend buffer (as required)
70 sendBuf_.reserve(Foam::max(label(1024), label(pos + count)));
71
72 // Move to the aligned output position. Fill any gap with nul char.
73 sendBuf_.resize(pos, '\0');
74}
75
76
77template<class T>
78inline void Foam::UOPstreamBase::writeToBuffer(const T& val)
79{
80 writeToBuffer(&val, sizeof(T), sizeof(T));
81}
82
83
84inline void Foam::UOPstreamBase::writeToBuffer
85(
86 const void* data,
87 const size_t count,
88 const size_t align
89)
90{
91 if (!count)
92 {
93 return;
94 }
95
96 prepareBuffer(count, align);
97
98 // The aligned output position
99 const label pos = sendBuf_.size();
100
101 // Extend the addressable range for direct pointer access
102 sendBuf_.resize(pos + count);
103
104 char* const __restrict__ buf = (sendBuf_.data() + pos);
105 const char* const __restrict__ input = reinterpret_cast<const char*>(data);
106
107 for (size_t i = 0; i < count; ++i)
108 {
109 buf[i] = input[i];
110 }
111}
112
113
114inline void Foam::UOPstreamBase::putChar(const char c)
115{
116 if (!sendBuf_.capacity())
117 {
118 sendBuf_.setCapacity(1024);
119 }
120 sendBuf_.push_back(c);
121}
122
123
124inline void Foam::UOPstreamBase::putString
125(
126 const char* str,
127 const size_t len
128)
129{
130 writeToBuffer(len);
131 writeToBuffer(str, len, 1); // no-op when len == 0
132}
133
134
135inline void Foam::UOPstreamBase::putString(const std::string& str)
137 putString(str.data(), str.size());
138}
139
140
141// * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
142
144(
145 const UPstream::commsTypes commsType,
146 const int toProcNo,
147 DynamicList<char>& sendBuf,
148 const int tag,
149 const int communicator,
150 const bool sendAtDestruct,
152)
153:
154 UPstream(commsType),
155 Ostream(fmt),
156 toProcNo_(toProcNo),
157 tag_(tag),
158 comm_(communicator),
159 sendAtDestruct_(sendAtDestruct),
160 sendBuf_(sendBuf)
161{
162 setOpened();
163 setGood();
164}
165
166
167Foam::UOPstreamBase::UOPstreamBase(const int toProcNo, PstreamBuffers& buffers)
168:
169 UPstream(buffers.commsType()),
170 Ostream(buffers.format()),
171 toProcNo_(toProcNo),
172 tag_(buffers.tag()),
173 comm_(buffers.comm()),
174 sendAtDestruct_(buffers.commsType() != UPstream::commsTypes::nonBlocking),
175 sendBuf_(buffers.accessSendBuffer(toProcNo))
176{
177 setOpened();
178 setGood();
179}
180
181
183(
184 DynamicList<char>& sendBuf,
186)
187:
188 UPstream(UPstream::commsTypes::nonBlocking), // placeholder
189 Ostream(fmt),
190 toProcNo_(UPstream::masterNo()), // placeholder
191 tag_(UPstream::msgType()), // placeholder
192 comm_(UPstream::commSelf()), // placeholder
193 sendAtDestruct_(false), // Never sendAtDestruct!!
194 sendBuf_(sendBuf)
195{
196 sendBuf_.clear(); // Overwrite into buffer
199}
200
201
202// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
205{}
207
208// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
209
210bool Foam::UOPstreamBase::write(const token& tok)
212 // Direct token handling only for some types
213
214 switch (tok.type())
215 {
217 {
218 putChar(token::tokenType::FLAG);
219 putChar(tok.flagToken());
220
221 return true;
222 }
223
224 // The word-variants
227 {
228 putChar(tok.type());
229 putString(tok.wordToken());
230
231 return true;
232 }
234 // The string-variants
240 {
241 putChar(tok.type());
242 putString(tok.stringToken());
244 return true;
245 }
246
247 default:
248 break;
249 }
250
251 return false;
252}
254
256{
257 if (!isspace(c))
259 putChar(c);
260 }
261
262 return *this;
264
265
267(
268 const char* str,
269 std::streamsize len,
270 const bool quoted
271)
272{
273 if (quoted)
274 {
276 }
277 else
278 {
279 putChar(token::tokenType::WORD);
281 putString(str, len);
282
283 return *this;
284}
285
286
288{
289 const word nonWhiteChars(string::validate<word>(str));
290
291 if (nonWhiteChars.size() == 1)
292 {
293 return write(nonWhiteChars[0]);
294 }
295 else if (nonWhiteChars.size())
296 {
297 return write(nonWhiteChars);
298 }
299
300 return *this;
301}
302
303
305{
307 putString(str);
308
309 return *this;
310}
311
312
313Foam::Ostream& Foam::UOPstreamBase::write(const std::string& str)
314{
316 putString(str);
317
318 return *this;
319}
320
321
325 writeToBuffer(val);
326 return *this;
327}
328
329
333 writeToBuffer(val);
334 return *this;
335}
336
337
338Foam::Ostream& Foam::UOPstreamBase::write(const uint32_t val)
341 writeToBuffer(val);
342 return *this;
343}
344
345
346Foam::Ostream& Foam::UOPstreamBase::write(const uint64_t val)
349 writeToBuffer(val);
350 return *this;
351}
352
353
357 writeToBuffer(val);
358 return *this;
359}
360
361
365 writeToBuffer(val);
366 return *this;
367}
368
369
371(
372 const char* data,
373 std::streamsize count
374)
375{
377 {
379 << "stream format not binary"
381 }
382
383 // Align on word boundary (64-bit)
384 writeToBuffer(data, count, 8);
385
386 return *this;
387}
388
389
391(
392 const char* data,
393 std::streamsize count
394)
395{
396 // No check for IOstreamOption::BINARY since this is either done in the
397 // beginRawWrite() method, or the caller knows what they are doing.
398
399 // Previously aligned and sizes reserved via beginRawWrite()
400 writeToBuffer(data, count, 1);
401
402 return *this;
403}
404
405
406bool Foam::UOPstreamBase::beginRawWrite(std::streamsize count)
407{
409 {
411 << "stream format not binary"
413 }
414
415 // Align on word boundary (64-bit)
416 // - as per write(const char*, streamsize)
417 prepareBuffer(count, 8);
418
419 return true;
420}
421
422
423// Not needed yet
427///
428/// Foam::label Foam::UOPstreamBase::pos() const
429/// {
430/// return sendBuf_.size();
431/// }
432
433
436 sendBuf_.clear(); // Overwrite into buffer
437 setOpened();
438 setGood();
439}
440
441
443{
444 os << "Writing to processor " << toProcNo_
445 << " from processor " << myProcNo() << " in communicator " << comm_
446 << " and tag " << tag_ << Foam::endl;
447}
448
449
450// ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
streamFormat format() const noexcept
Get the current stream format.
streamFormat
Data format (ascii | binary | coherent).
void setGood() noexcept
Set stream state to be good.
Definition IOstream.H:174
void setOpened() noexcept
Set stream opened.
Definition IOstream.H:150
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
Ostream(const Ostream &)=default
Copy construct.
Buffers for inter-processor communications streams (UOPstream, UIPstream).
DynamicList< char > & sendBuf_
Reference to the send buffer data.
Definition UOPstream.H:128
virtual ~UOPstreamBase()
Destructor.
const int toProcNo_
Destination rank for the data.
Definition UOPstream.H:108
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true) override
Write character/string content, with/without surrounding quotes.
bool sendAtDestruct_
Call bufferIPCsend on termination (in the destructor).
Definition UOPstream.H:123
void print(Ostream &os) const override
Print stream description to Ostream.
const int tag_
Message tag for communication.
Definition UOPstream.H:113
const int comm_
The communicator index.
Definition UOPstream.H:118
UOPstreamBase(const UPstream::commsTypes commsType, const int toProcNo, DynamicList< char > &sendBuf, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm, const bool sendAtDestruct=true, IOstreamOption::streamFormat fmt=IOstreamOption::BINARY)
Construct given process index to write to using the given attached send buffer, optional communicatio...
virtual Ostream & writeRaw(const char *data, std::streamsize count) override
Low-level raw binary output.
virtual bool beginRawWrite(std::streamsize count) override
Begin marker for low-level raw binary output.
virtual void rewind()
Rewind the send buffer for overwriting.
virtual bool write(const token &tok) override
Write token to stream or otherwise handle it.
Wrapper for internally indexed communicator label. Always invokes UPstream::allocateCommunicatorCompo...
Definition UPstream.H:2546
Inter-processor communications stream.
Definition UPstream.H:69
static constexpr int commSelf() noexcept
Communicator within the current rank only.
Definition UPstream.H:1088
static int myProcNo(const label communicator=worldComm)
Rank of this process in the communicator (starting from masterNo()). Negative if the process is not a...
Definition UPstream.H:1706
commsTypes
Communications types.
Definition UPstream.H:81
static int & msgType() noexcept
Message tag of standard messages.
Definition UPstream.H:1926
static constexpr int masterNo() noexcept
Relative rank for the master process - is always 0.
Definition UPstream.H:1691
commsTypes commsType() const noexcept
Get the communications type of the stream.
Definition UPstream.H:1958
UPstream(const commsTypes commsType) noexcept
Construct for given communication type.
Definition UPstream.H:1184
static StringType validate(const std::string &str)
Return a valid String from the given string.
Definition stringI.H:193
A token holds an item read from Istream.
Definition token.H:70
@ DOUBLE
double (double-precision) type
Definition token.H:94
@ FLAG
stream flag (1-byte bitmask)
Definition token.H:86
@ WORD
Foam::word.
Definition token.H:97
@ UNSIGNED_INTEGER_32
uint32 type
Definition token.H:91
@ EXPRESSION
Definition token.H:104
@ CHAR_DATA
String-variant: plain character content.
Definition token.H:110
@ INTEGER_64
int64 type
Definition token.H:90
@ FLOAT
float (single-precision) type
Definition token.H:93
@ DIRECTIVE
Definition token.H:101
@ UNSIGNED_INTEGER_64
uint64 type
Definition token.H:92
@ INTEGER_32
int32 type
Definition token.H:89
@ STRING
Foam::string (usually double-quoted).
Definition token.H:98
const string & stringToken() const
Return const reference to the string contents.
Definition tokenI.H:1076
int flagToken() const
Return flag bitmask value.
Definition tokenI.H:638
tokenType type() const noexcept
Return the token type.
Definition tokenI.H:482
const word & wordToken() const
Return const reference to the word contents.
Definition tokenI.H:1022
A class for handling words, derived from Foam::string.
Definition word.H:66
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
OBJstream os(runTime.globalPath()/outputName)
System signed integer.
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition BitOps.H:73
Namespace for OpenFOAM.
dimensionedScalar pos(const dimensionedScalar &ds)
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:40
constexpr bool isspace(char c) noexcept
Test for whitespace (C-locale only).
Definition char.H:77
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
static label byteAlign(const label pos, const size_t align)
errorManip< error > abort(error &err)
Definition errorManip.H:139
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
runTime write()
word format(conversionProperties.get< word >("format"))