UOPstream.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 OpenFOAM Foundation
9  Copyright (C) 2016-2020 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 \*---------------------------------------------------------------------------*/
28 
29 #include "UOPstream.H"
30 #include "int.H"
31 #include "token.H"
32 #include <cctype>
33 
34 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
35 
36 inline void Foam::UOPstream::prepareBuffer
37 (
38  const size_t count,
39  const size_t align
40 )
41 {
42  if (!count)
43  {
44  return;
45  }
46 
47  // The current output position
48  label pos = sendBuf_.size();
49 
50  if (align > 1)
51  {
52  // Align output position. Pads sendBuf_.size() - oldPos characters.
53  pos = align + ((pos - 1) & ~(align - 1));
54  }
55 
56  // Extend buffer (as required)
57  sendBuf_.reserve(max(1000, label(pos + count)));
58 
59  // Move to the aligned output position. Fill any gap with nul char.
60  sendBuf_.resize(pos, '\0');
61 }
62 
63 
64 template<class T>
65 inline void Foam::UOPstream::writeToBuffer(const T& val)
66 {
67  writeToBuffer(&val, sizeof(T), sizeof(T));
68 }
69 
70 
71 inline void Foam::UOPstream::writeToBuffer(const char& c)
72 {
73  if (!sendBuf_.capacity())
74  {
75  sendBuf_.setCapacity(1000);
76  }
77  sendBuf_.append(c);
78 }
79 
80 
81 inline void Foam::UOPstream::writeToBuffer
82 (
83  const void* data,
84  const size_t count,
85  const size_t align
86 )
87 {
88  if (!count)
89  {
90  return;
91  }
92 
93  prepareBuffer(count, align);
94 
95  // The aligned output position
96  const label pos = sendBuf_.size();
97 
98  // Extend the addressable range for direct pointer access
99  sendBuf_.resize(pos + count);
100 
101  char* const __restrict__ buf = (sendBuf_.begin() + pos);
102  const char* const __restrict__ input = reinterpret_cast<const char*>(data);
103 
104  for (size_t i = 0; i < count; ++i)
105  {
106  buf[i] = input[i];
107  }
108 }
109 
110 
111 inline void Foam::UOPstream::writeStringToBuffer(const std::string& str)
112 {
113  const size_t len = str.size();
114  writeToBuffer(len);
115  writeToBuffer(str.data(), len, 1);
116 }
117 
118 
119 // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
120 
122 (
123  const commsTypes commsType,
124  const int toProcNo,
125  DynamicList<char>& sendBuf,
126  const int tag,
127  const label comm,
128  const bool sendAtDestruct,
131 )
132 :
133  UPstream(commsType),
135  toProcNo_(toProcNo),
136  sendBuf_(sendBuf),
137  tag_(tag),
138  comm_(comm),
139  sendAtDestruct_(sendAtDestruct)
140 {
141  setOpened();
142  setGood();
143 }
144 
145 
146 Foam::UOPstream::UOPstream(const int toProcNo, PstreamBuffers& buffers)
147 :
148  UPstream(buffers.commsType_),
149  Ostream(buffers.format_, buffers.version_),
150  toProcNo_(toProcNo),
151  sendBuf_(buffers.sendBuf_[toProcNo]),
152  tag_(buffers.tag_),
153  comm_(buffers.comm_),
154  sendAtDestruct_(buffers.commsType_ != UPstream::commsTypes::nonBlocking)
155 {
156  setOpened();
157  setGood();
158 }
159 
160 
161 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
162 
164 {
165  if (sendAtDestruct_)
166  {
167  if
168  (
170  (
171  commsType_,
172  toProcNo_,
173  sendBuf_.begin(),
174  sendBuf_.size(),
175  tag_,
176  comm_
177  )
178  )
179  {
181  << "Failed sending outgoing message of size " << sendBuf_.size()
182  << " to processor " << toProcNo_
184  }
185  }
186 }
187 
188 
189 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
190 
192 {
193  // Direct token handling only for some types
194 
195  switch (tok.type())
196  {
197  case token::tokenType::FLAG :
198  {
199  writeToBuffer(char(token::tokenType::FLAG));
200  writeToBuffer(char(tok.flagToken()));
201 
202  return true;
203  }
204 
205  case token::tokenType::DIRECTIVE :
206  {
207  writeToBuffer(char(token::tokenType::DIRECTIVE));
208  writeStringToBuffer(tok.wordToken());
209 
210  return true;
211  }
212 
213  case token::tokenType::VARIABLE :
214  case token::tokenType::VERBATIM :
215  {
216  writeToBuffer(char(tok.type()));
217  writeStringToBuffer(tok.stringToken());
218 
219  return true;
220  }
221 
222  default:
223  break;
224  }
225 
226  return false;
227 }
228 
229 
231 {
232  if (!isspace(c))
233  {
234  writeToBuffer(c);
235  }
236 
237  return *this;
238 }
239 
240 
242 {
243  const word nonWhiteChars(string::validate<word>(str));
244 
245  if (nonWhiteChars.size() == 1)
246  {
247  return write(nonWhiteChars[0]);
248  }
249  else if (nonWhiteChars.size())
250  {
251  return write(nonWhiteChars);
252  }
253 
254  return *this;
255 }
256 
257 
259 {
260  writeToBuffer(char(token::tokenType::WORD));
261  writeStringToBuffer(str);
262 
263  return *this;
264 }
265 
266 
268 {
269  writeToBuffer(char(token::tokenType::STRING));
270  writeStringToBuffer(str);
271 
272  return *this;
273 }
274 
275 
277 (
278  const std::string& str,
279  const bool quoted
280 )
281 {
282  if (quoted)
283  {
284  writeToBuffer(char(token::tokenType::STRING));
285  }
286  else
287  {
288  writeToBuffer(char(token::tokenType::WORD));
289  }
290  writeStringToBuffer(str);
291 
292  return *this;
293 }
294 
295 
297 {
298  writeToBuffer(char(token::tokenType::LABEL));
299  writeToBuffer(val);
300  return *this;
301 }
302 
303 
305 {
306  writeToBuffer(char(token::tokenType::LABEL));
307  writeToBuffer(val);
308  return *this;
309 }
310 
311 
313 {
314  writeToBuffer(char(token::tokenType::FLOAT));
315  writeToBuffer(val);
316  return *this;
317 }
318 
319 
321 {
322  writeToBuffer(char(token::tokenType::DOUBLE));
323  writeToBuffer(val);
324  return *this;
325 }
326 
327 
328 Foam::Ostream& Foam::UOPstream::write(const char* data, std::streamsize count)
329 {
330  if (format() != BINARY)
331  {
333  << "stream format not binary"
335  }
336 
337  writeToBuffer(data, count, 8);
338 
339  return *this;
340 }
341 
342 
344 (
345  const char* data,
346  std::streamsize count
347 )
348 {
349  // No check for format() == BINARY since this is either done in the
350  // beginRawWrite() method, or the caller knows what they are doing.
351 
352  // Previously aligned and sizes reserved via beginRawWrite()
353  writeToBuffer(data, count, 1);
354 
355  return *this;
356 }
357 
358 
360 {
361  if (format() != BINARY)
362  {
364  << "stream format not binary"
366  }
367 
368  // Alignment = 8, as per write(const char*, streamsize)
369  prepareBuffer(count, 8);
370 
371  return true;
372 }
373 
374 
376 {
377  os << "Writing from processor " << toProcNo_
378  << " to processor " << myProcNo() << " in communicator " << comm_
379  << " and tag " << tag_ << Foam::endl;
380 }
381 
382 
383 // ************************************************************************* //
token.H
Foam::UOPstream::UOPstream
UOPstream(const commsTypes commsType, const int toProcNo, DynamicList< char > &sendBuf, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm, const bool sendAtDestruct=true, streamFormat format=BINARY, versionNumber version=currentVersion)
Construct given process index to send to and optional buffer size,.
Definition: UOPstream.C:122
Foam::doubleScalar
double doubleScalar
A typedef for double.
Definition: scalarFwd.H:48
int.H
System signed integer.
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::DynamicList< char >
Foam::token::stringToken
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:644
Foam::floatScalar
float floatScalar
A typedef for float.
Definition: scalarFwd.H:45
Foam::UOPstream::print
void print(Ostream &os) const
Print stream description to Ostream.
Definition: UOPstream.C:375
Foam::PstreamBuffers
Buffers for inter-processor communications streams (UOPstream, UIPstream).
Definition: PstreamBuffers.H:87
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::IOstream::setOpened
void setOpened()
Set stream opened.
Definition: IOstream.H:123
Foam::DynamicList::reserve
void reserve(const label nElem)
Reserve allocation space for at least this size.
Definition: DynamicListI.H:254
format
word format(conversionProperties.get< word >("format"))
Foam::UOPstream::write
static bool write(const commsTypes commsType, const int toProcNo, const char *buf, const std::streamsize bufSize, const int tag=UPstream::msgType(), const label communicator=0)
Write given buffer to given processor.
Definition: UOPwrite.C:36
Foam::UPstream
Inter-processor communications stream.
Definition: UPstream.H:61
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::UOPstream::beginRawWrite
virtual bool beginRawWrite(std::streamsize count)
Begin marker for low-level raw binary output.
Definition: UOPstream.C:359
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
Foam::FatalError
error FatalError
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:599
Foam::foamVersion::version
const std::string version
OpenFOAM version (name or stringified number) as a std::string.
Foam::DynamicList::resize
void resize(const label nElem)
Alter addressable list size.
Definition: DynamicListI.H:328
Foam::token::type
tokenType type() const noexcept
Return the token type.
Definition: tokenI.H:309
Foam::UPstream::commsTypes
commsTypes
Types of communications.
Definition: UPstream.H:69
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
UOPstream.H
Foam::IOstream::setGood
void setGood()
Set stream to be good.
Definition: IOstream.H:141
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::UOPstream::~UOPstream
~UOPstream()
Destructor.
Definition: UOPstream.C:163
Foam::input
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:35
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::UOPstream::writeQuoted
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: UOPstream.C:277
Foam::token::flagToken
int flagToken() const
Return flag bitmask value.
Definition: tokenI.H:441
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::UOPstream::writeRaw
virtual Ostream & writeRaw(const char *data, std::streamsize count)
Low-level raw binary output.
Definition: UOPstream.C:344
Foam::isspace
bool isspace(char c)
Test for horizontal whitespace.
Definition: char.H:63
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:55
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177