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-2018 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,
91  streamFormat format,
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  // Raw token output only supported for verbatim strings for now
156  if (t.type() == token::VERBATIMSTRING)
157  {
158  writeToBuffer(char(token::VERBATIMSTRING));
159  write(t.stringToken());
160  }
161  else if (t.type() == token::VARIABLE)
162  {
163  writeToBuffer(char(token::VARIABLE));
164  write(t.stringToken());
165  }
166  else
167  {
169  setBad();
170  }
171  return *this;
172 }
173 
174 
176 {
177  if (!isspace(c))
178  {
179  writeToBuffer(c);
180  }
181 
182  return *this;
183 }
184 
185 
187 {
188  word nonWhiteChars(string::validate<word>(str));
189 
190  if (nonWhiteChars.size() == 1)
191  {
192  return write(nonWhiteChars[0]);
193  }
194  else if (nonWhiteChars.size())
195  {
196  return write(nonWhiteChars);
197  }
198  else
199  {
200  return *this;
201  }
202 }
203 
204 
206 {
207  writeToBuffer(char(token::WORD));
208 
209  size_t len = str.size();
210  writeToBuffer(len);
211  writeToBuffer(str.c_str(), len + 1, 1);
212 
213  return *this;
214 }
215 
216 
218 {
219  writeToBuffer(char(token::STRING));
220 
221  size_t len = str.size();
222  writeToBuffer(len);
223  writeToBuffer(str.c_str(), len + 1, 1);
224 
225  return *this;
226 }
227 
228 
230 (
231  const std::string& str,
232  const bool quoted
233 )
234 {
235  if (quoted)
236  {
237  writeToBuffer(char(token::STRING));
238  }
239  else
240  {
241  writeToBuffer(char(token::WORD));
242  }
243 
244  size_t len = str.size();
245  writeToBuffer(len);
246  writeToBuffer(str.c_str(), len + 1, 1);
247 
248  return *this;
249 }
250 
251 
253 {
254  writeToBuffer(char(token::LABEL));
255  writeToBuffer(val);
256  return *this;
257 }
258 
259 
261 {
262  writeToBuffer(char(token::LABEL));
263  writeToBuffer(val);
264  return *this;
265 }
266 
267 
269 {
270  writeToBuffer(char(token::FLOAT_SCALAR));
271  writeToBuffer(val);
272  return *this;
273 }
274 
275 
277 {
278  writeToBuffer(char(token::DOUBLE_SCALAR));
279  writeToBuffer(val);
280  return *this;
281 }
282 
283 
285 {
286  writeToBuffer(char(token::LONG_DOUBLE_SCALAR));
287  writeToBuffer(val);
288  return *this;
289 }
290 
291 
292 Foam::Ostream& Foam::UOPstream::write(const char* data, std::streamsize count)
293 {
294  if (format() != BINARY)
295  {
297  << "stream format not binary"
299  }
300 
301  writeToBuffer(data, count, 8);
302 
303  return *this;
304 }
305 
306 
308 {
309  os << "Writing from processor " << toProcNo_
310  << " to processor " << myProcNo() << " in communicator " << comm_
311  << " and tag " << tag_ << Foam::endl;
312 }
313 
314 
315 // ************************************************************************* //
System integer.
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
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
commsTypes
Types of communications.
Definition: UPstream.H:64
Ostream(streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Set stream status.
Definition: Ostream.H:78
void setGood()
Set stream to be good.
Definition: IOstream.H:257
commsTypes commsType_
Communications type of this stream.
Definition: UPstream.H:252
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
A token holds items read from Istream.
Definition: token.H:69
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:427
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
Ostream & writeQuoted(const std::string &, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: UOPstream.C:230
void setBad()
Set stream to be bad.
Definition: IOstream.H:487
void setSize(const label)
Alter the addressed list size.
Definition: DynamicListI.H:163
A class for handling words, derived from string.
Definition: word.H:59
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:52
void setCapacity(const label)
Alter the size of the underlying storage.
Definition: DynamicListI.H:118
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:216
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:292
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:52
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
errorManip< error > abort(error &err)
Definition: errorManip.H:131
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
~UOPstream()
Destructor.
Definition: UOPstream.C:125
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
Buffers for inter-processor communications streams (UOPstream, UIPstream).
const string & stringToken() const
Definition: tokenI.H:258
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const volScalarField & T
long double longDoubleScalar
Lang double precision floating point scalar type.
bool isspace(char c)
Definition: char.H:53
void setOpened()
Set stream opened.
Definition: IOstream.H:239
UPstream(const commsTypes commsType)
Construct given optional buffer size.
Definition: UPstream.H:287
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
tokenType type() const
Definition: tokenI.H:187
Version number type.
Definition: IOstream.H:96
label capacity() const
Size of the underlying storage.
Definition: DynamicListI.H:109
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:366
void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: UOPstream.C:307
Inter-processor communications stream.
Definition: UPstream.H:58