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 | Copyright (C) 2011-2015 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 Description
25  Write primitive and binary block from UOPstream
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "error.H"
30 
31 #include "UOPstream.H"
32 #include "int.H"
33 #include "token.H"
34 
35 #include <cctype>
36 
37 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
38 
39 template<class T>
40 inline void Foam::UOPstream::writeToBuffer(const T& t)
41 {
42  writeToBuffer(&t, sizeof(T), sizeof(T));
43 }
44 
45 
46 inline void Foam::UOPstream::writeToBuffer(const char& c)
47 {
48  if (!sendBuf_.capacity())
49  {
50  sendBuf_.setCapacity(1000);
51  }
52  sendBuf_.append(c);
53 }
54 
55 
57 (
58  const void* data,
59  size_t count,
60  size_t align
61 )
62 {
63  if (!sendBuf_.capacity())
64  {
65  sendBuf_.setCapacity(1000);
66  }
67 
68  label alignedPos = sendBuf_.size();
69 
70  if (align > 1)
71  {
72  // Align bufPosition. Pads sendBuf_.size() - oldPos characters.
73  alignedPos = align + ((sendBuf_.size() - 1) & ~(align - 1));
74  }
75 
76  // Extend if necessary
77  sendBuf_.setSize(alignedPos + count);
78 
79  const char* dataPtr = reinterpret_cast<const char*>(data);
80  size_t i = count;
81  while (i--) sendBuf_[alignedPos++] = *dataPtr++;
82 }
83 
84 
85 
86 // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
87 
89 (
90  const commsTypes commsType,
91  const int toProcNo,
92  DynamicList<char>& sendBuf,
93  const int tag,
94  const label comm,
95  const bool sendAtDestruct,
97  versionNumber version
98 )
99 :
100  UPstream(commsType),
101  Ostream(format, version),
102  toProcNo_(toProcNo),
103  sendBuf_(sendBuf),
104  tag_(tag),
105  comm_(comm),
106  sendAtDestruct_(sendAtDestruct)
107 {
108  setOpened();
109  setGood();
110 }
111 
112 
113 Foam::UOPstream::UOPstream(const int toProcNo, PstreamBuffers& buffers)
114 :
115  UPstream(buffers.commsType_),
116  Ostream(buffers.format_, buffers.version_),
117  toProcNo_(toProcNo),
118  sendBuf_(buffers.sendBuf_[toProcNo]),
119  tag_(buffers.tag_),
120  comm_(buffers.comm_),
121  sendAtDestruct_(buffers.commsType_ != UPstream::nonBlocking)
122 {
123  setOpened();
124  setGood();
125 }
126 
127 
128 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
129 
131 {
132  if (sendAtDestruct_)
133  {
134  if
135  (
137  (
138  commsType_,
139  toProcNo_,
140  sendBuf_.begin(),
141  sendBuf_.size(),
142  tag_,
143  comm_
144  )
145  )
146  {
148  << "Failed sending outgoing message of size " << sendBuf_.size()
149  << " to processor " << toProcNo_
151  }
152  }
153 }
154 
155 
156 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
157 
159 {
160  // Raw token output only supported for verbatim strings for now
161  if (t.type() == token::VERBATIMSTRING)
162  {
164  write(t.stringToken());
165  }
166  else if (t.type() == token::VARIABLE)
167  {
168  write(char(token::VARIABLE));
169  write(t.stringToken());
170  }
171  else
172  {
174  setBad();
175  }
176  return *this;
177 }
178 
179 
181 {
182  if (!isspace(c))
183  {
184  writeToBuffer(c);
185  }
186 
187  return *this;
188 }
189 
190 
192 {
193  word nonWhiteChars(string::validate<word>(str));
194 
195  if (nonWhiteChars.size() == 1)
196  {
197  return write(nonWhiteChars.c_str()[1]);
198  }
199  else if (nonWhiteChars.size())
200  {
201  return write(nonWhiteChars);
202  }
203  else
204  {
205  return *this;
206  }
207 }
208 
209 
211 {
212  write(char(token::WORD));
213 
214  size_t len = str.size();
215  writeToBuffer(len);
216  writeToBuffer(str.c_str(), len + 1, 1);
217 
218  return *this;
219 }
220 
221 
223 {
224  write(char(token::STRING));
225 
226  size_t len = str.size();
227  writeToBuffer(len);
228  writeToBuffer(str.c_str(), len + 1, 1);
229 
230  return *this;
231 }
232 
233 
235 (
236  const std::string& str,
237  const bool quoted
238 )
239 {
240  if (quoted)
241  {
242  write(char(token::STRING));
243  }
244  else
245  {
246  write(char(token::WORD));
247  }
248 
249  size_t len = str.size();
250  writeToBuffer(len);
251  writeToBuffer(str.c_str(), len + 1, 1);
252 
253  return *this;
254 }
255 
256 
258 {
259  write(char(token::LABEL));
260  writeToBuffer(val);
261  return *this;
262 }
263 
264 
266 {
267  write(char(token::LABEL));
268  writeToBuffer(val);
269  return *this;
270 }
271 
272 
274 {
275  write(char(token::FLOAT_SCALAR));
276  writeToBuffer(val);
277  return *this;
278 }
279 
280 
282 {
284  writeToBuffer(val);
285  return *this;
286 }
287 
288 
289 Foam::Ostream& Foam::UOPstream::write(const char* data, std::streamsize count)
290 {
291  if (format() != BINARY)
292  {
294  << "stream format not binary"
296  }
297 
298  writeToBuffer(data, count, 8);
299 
300  return *this;
301 }
302 
303 
305 {
306  os << "Writing from processor " << toProcNo_
307  << " to processor " << myProcNo() << " in communicator " << comm_
308  << " and tag " << tag_ << Foam::endl;
309 }
310 
311 
312 // ************************************************************************* //
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:89
format
word format(conversionProperties.lookup("format"))
Foam::doubleScalar
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:49
int.H
System integer.
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::DynamicList< char >
Foam::token::LABEL
@ LABEL
Definition: token.H:82
Foam::floatScalar
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:49
Foam::PstreamBuffers
Buffers for inter-processor communications streams (UOPstream, UIPstream).
Definition: PstreamBuffers.H:85
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::token
A token holds items read from Istream.
Definition: token.H:67
Foam::UOPstream::writeToBuffer
void writeToBuffer(const T &)
Write a T to the transfer buffer.
Definition: UOPstream.C:40
Foam::IOstream::setOpened
void setOpened()
Set stream opened.
Definition: IOstream.H:239
Foam::token::stringToken
const string & stringToken() const
Definition: tokenI.H:249
Foam::token::FLOAT_SCALAR
@ FLOAT_SCALAR
Definition: token.H:83
Foam::IOstream::versionNumber
Version number type.
Definition: IOstream.H:96
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:365
Foam::token::type
tokenType type() const
Definition: tokenI.H:178
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:34
Foam::label
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
Foam::UPstream
Inter-processor communications stream.
Definition: UPstream.H:58
error.H
Foam::UOPstream::writeQuoted
Ostream & writeQuoted(const std::string &, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: UOPstream.C:235
Foam::token::VARIABLE
@ VARIABLE
Definition: token.H:79
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:55
Foam::FatalError
error FatalError
Foam::token::STRING
@ STRING
Definition: token.H:80
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::UPstream::commsTypes
commsTypes
Types of communications.
Definition: UPstream.H:64
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
T
const volScalarField & T
Definition: createFields.H:25
UOPstream.H
Foam::IOstream::setGood
void setGood()
Set stream to be good.
Definition: IOstream.H:257
Foam::UOPstream::print
void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: UOPstream.C:304
Foam::UOPstream::~UOPstream
~UOPstream()
Destructor.
Definition: UOPstream.C:130
Foam::token::WORD
@ WORD
Definition: token.H:78
Foam::token::DOUBLE_SCALAR
@ DOUBLE_SCALAR
Definition: token.H:84
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::isspace
bool isspace(char c)
Definition: char.H:53
Foam::token::VERBATIMSTRING
@ VERBATIMSTRING
Definition: token.H:81
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:52
write
Tcoeff write()
Foam::IOstream::streamFormat
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86