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-2019 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  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::LABEL));
245  writeToBuffer(val);
246  return *this;
247 }
248 
249 
251 {
252  writeToBuffer(char(token::LABEL));
253  writeToBuffer(val);
254  return *this;
255 }
256 
257 
259 {
260  writeToBuffer(char(token::FLOAT_SCALAR));
261  writeToBuffer(val);
262  return *this;
263 }
264 
265 
267 {
268  writeToBuffer(char(token::DOUBLE_SCALAR));
269  writeToBuffer(val);
270  return *this;
271 }
272 
273 
275 {
276  writeToBuffer(char(token::LONG_DOUBLE_SCALAR));
277  writeToBuffer(val);
278  return *this;
279 }
280 
281 
282 Foam::Ostream& Foam::UOPstream::write(const char* data, std::streamsize count)
283 {
284  if (format() != BINARY)
285  {
287  << "stream format not binary"
289  }
290 
291  writeToBuffer(data, count, 8);
292 
293  return *this;
294 }
295 
296 
298 {
299  os << "Writing from processor " << toProcNo_
300  << " to processor " << myProcNo() << " in communicator " << comm_
301  << " and tag " << tag_ << Foam::endl;
302 }
303 
304 
305 // ************************************************************************* //
A class for handling verbatimStrings, derived from string.
System integer.
FvWallInfoData< WallInfo, label > label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
commsTypes
Types of communications.
Definition: UPstream.H:64
Ostream(streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Set stream status.
Definition: Ostream.H:76
void setGood()
Set stream to be good.
Definition: IOstream.H:254
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:164
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:429
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
Ostream & writeQuoted(const std::string &, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: UOPstream.C:220
void setSize(const label)
Alter the addressed list size.
Definition: DynamicListI.H:175
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:130
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:296
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:52
streamFormat format() const
Return current stream format.
Definition: IOstream.H:374
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:54
~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).
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
long double longDoubleScalar
Lang double precision floating point scalar type.
const volScalarField & T
bool isspace(char c)
Definition: char.H:53
void setOpened()
Set stream opened.
Definition: IOstream.H:236
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
Version number type.
Definition: IOstream.H:96
label capacity() const
Size of the underlying storage.
Definition: DynamicListI.H:121
void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: UOPstream.C:297
Inter-processor communications stream.
Definition: UPstream.H:58