UOPstream.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "UOPstream.H"
27 #include "int.H"
28 #include "token.H"
29 
30 #include <cctype>
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 template<class T>
35 inline void Foam::UOPstream::writeToBuffer(const T& t)
36 {
37  writeToBuffer(&t, sizeof(T), sizeof(T));
38 }
39 
40 
41 inline void Foam::UOPstream::writeToBuffer(const char& c)
42 {
43  if (!sendBuf_.capacity())
44  {
45  sendBuf_.setCapacity(1000);
46  }
47  sendBuf_.append(c);
48 }
49 
50 
51 inline void Foam::UOPstream::writeToBuffer
52 (
53  const void* data,
54  size_t count,
55  size_t align
56 )
57 {
58  if (!sendBuf_.capacity())
59  {
60  sendBuf_.setCapacity(1000);
61  }
62 
63  label alignedPos = sendBuf_.size();
64 
65  if (align > 1)
66  {
67  // Align bufPosition. Pads sendBuf_.size() - oldPos characters.
68  alignedPos = align + ((sendBuf_.size() - 1) & ~(align - 1));
69  }
70 
71  // Extend if necessary
72  sendBuf_.setSize(alignedPos + count);
73 
74  const char* dataPtr = reinterpret_cast<const char*>(data);
75  size_t i = count;
76  while (i--) sendBuf_[alignedPos++] = *dataPtr++;
77 }
78 
79 
80 
81 // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
82 
84 (
85  const commsTypes commsType,
86  const int toProcNo,
87  DynamicList<char>& sendBuf,
88  const int tag,
89  const label comm,
90  const bool sendAtDestruct,
92  versionNumber version
93 )
94 :
95  UPstream(commsType),
96  Ostream(format, version),
97  toProcNo_(toProcNo),
98  sendBuf_(sendBuf),
99  tag_(tag),
100  comm_(comm),
101  sendAtDestruct_(sendAtDestruct)
102 {
103  setOpened();
104  setGood();
105 }
106 
107 
108 Foam::UOPstream::UOPstream(const int toProcNo, PstreamBuffers& buffers)
109 :
110  UPstream(buffers.commsType_),
111  Ostream(buffers.format_, buffers.version_),
112  toProcNo_(toProcNo),
113  sendBuf_(buffers.sendBuf_[toProcNo]),
114  tag_(buffers.tag_),
115  comm_(buffers.comm_),
116  sendAtDestruct_(buffers.commsType_ != UPstream::commsTypes::nonBlocking)
117 {
118  setOpened();
119  setGood();
120 }
121 
122 
123 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
124 
126 {
127  if (sendAtDestruct_)
128  {
129  if
130  (
132  (
133  commsType_,
134  toProcNo_,
135  sendBuf_.begin(),
136  sendBuf_.size(),
137  tag_,
138  comm_
139  )
140  )
141  {
143  << "Failed sending outgoing message of size " << sendBuf_.size()
144  << " to processor " << toProcNo_
146  }
147  }
148 }
149 
150 
151 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
152 
154 {
155  if (!isspace(c))
156  {
157  writeToBuffer(c);
158  }
159 
160  return *this;
161 }
162 
163 
165 {
166  word nonWhiteChars(string::validate<word>(str));
167 
168  if (nonWhiteChars.size() == 1)
169  {
170  return write(nonWhiteChars[0]);
171  }
172  else if (nonWhiteChars.size())
173  {
174  return write(nonWhiteChars);
175  }
176  else
177  {
178  return *this;
179  }
180 }
181 
182 
184 {
185  writeToBuffer(char(token::WORD));
186 
187  size_t len = str.size();
188  writeToBuffer(len);
189  writeToBuffer(str.c_str(), len + 1, 1);
190 
191  return *this;
192 }
193 
194 
196 {
197  writeToBuffer(char(token::STRING));
198 
199  size_t len = str.size();
200  writeToBuffer(len);
201  writeToBuffer(str.c_str(), len + 1, 1);
202 
203  return *this;
204 }
205 
206 
208 {
209  writeToBuffer(char(token::VERBATIMSTRING));
210 
211  size_t len = vs.size();
212  writeToBuffer(len);
213  writeToBuffer(vs.c_str(), len + 1, 1);
214 
215  return *this;
216 }
217 
218 
220 (
221  const std::string& str,
222  const bool quoted
223 )
224 {
225  if (quoted)
226  {
227  writeToBuffer(char(token::STRING));
228  }
229  else
230  {
231  writeToBuffer(char(token::WORD));
232  }
233 
234  size_t len = str.size();
235  writeToBuffer(len);
236  writeToBuffer(str.c_str(), len + 1, 1);
237 
238  return *this;
239 }
240 
241 
243 {
244  writeToBuffer(char(token::INTEGER_32));
245  writeToBuffer(val);
246  return *this;
247 }
248 
249 
251 {
252  writeToBuffer(char(token::INTEGER_64));
253  writeToBuffer(val);
254  return *this;
255 }
256 
257 
259 {
260  writeToBuffer(char(token::UNSIGNED_INTEGER_32));
261  writeToBuffer(val);
262  return *this;
263 }
264 
265 
267 {
268  writeToBuffer(char(token::UNSIGNED_INTEGER_64));
269  writeToBuffer(val);
270  return *this;
271 }
272 
273 
275 {
276  writeToBuffer(char(token::FLOAT_SCALAR));
277  writeToBuffer(val);
278  return *this;
279 }
280 
281 
283 {
284  writeToBuffer(char(token::DOUBLE_SCALAR));
285  writeToBuffer(val);
286  return *this;
287 }
288 
289 
291 {
292  writeToBuffer(char(token::LONG_DOUBLE_SCALAR));
293  writeToBuffer(val);
294  return *this;
295 }
296 
297 
298 Foam::Ostream& Foam::UOPstream::write(const char* data, std::streamsize count)
299 {
300  if (format() != BINARY)
301  {
303  << "stream format not binary"
305  }
306 
307  writeToBuffer(data, count, 8);
308 
309  return *this;
310 }
311 
312 
314 {
315  os << "Writing from processor " << toProcNo_
316  << " to processor " << myProcNo() << " in communicator " << comm_
317  << " and tag " << tag_ << Foam::endl;
318 }
319 
320 
321 // ************************************************************************* //
Version number type.
Definition: IOstream.H:97
void setGood()
Set stream to be good.
Definition: IOstream.H:254
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:87
void setOpened()
Set stream opened.
Definition: IOstream.H:236
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Buffers for inter-processor communications streams (UOPstream, UIPstream).
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:34
~UOPstream()
Destructor.
Definition: UOPstream.C:125
Ostream & writeQuoted(const std::string &, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: UOPstream.C:220
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:84
void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: UOPstream.C:313
Inter-processor communications stream.
Definition: UPstream.H:59
commsTypes
Types of communications.
Definition: UPstream.H:65
@ WORD
Definition: token.H:83
@ UNSIGNED_INTEGER_32
Definition: token.H:90
@ FLOAT_SCALAR
Definition: token.H:92
@ INTEGER_64
Definition: token.H:89
@ DOUBLE_SCALAR
Definition: token.H:93
@ LONG_DOUBLE_SCALAR
Definition: token.H:94
@ VERBATIMSTRING
Definition: token.H:87
@ UNSIGNED_INTEGER_64
Definition: token.H:91
@ INTEGER_32
Definition: token.H:88
@ STRING
Definition: token.H:86
A class for handling verbatimStrings, derived from string.
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
System integer.
const dimensionedScalar c
Speed of light in a vacuum.
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:52
errorManip< error > abort(error &err)
Definition: errorManip.H:131
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:52
error FatalError
bool isspace(char c)
Definition: char.H:53
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
long double longDoubleScalar
Lang double precision floating point scalar type.
word format(conversionProperties.lookup("format"))