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 
56 inline void Foam::UOPstream::writeToBuffer
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,
96  streamFormat format,
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  {
147  FatalErrorIn("UOPstream::~UOPstream()")
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  {
173  notImplemented("Ostream& UOPstream::write(const token&)");
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  {
293  FatalErrorIn("Ostream::write(const char*, std::streamsize)")
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 // ************************************************************************* //
System integer.
Inter-processor communications stream.
Definition: UPstream.H:58
Buffers for inter-processor communications streams (UOPstream, UIPstream).
void setOpened()
Set stream opened.
Definition: IOstream.H:239
void setCapacity(const label)
Alter the size of the underlying storage.
Definition: DynamicListI.H:109
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:49
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
label capacity() const
Size of the underlying storage.
Definition: DynamicListI.H:100
A class for handling words, derived from string.
Definition: word.H:59
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
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:310
Ostream & writeQuoted(const std::string &, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: UOPstream.C:235
void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: UOPstream.C:304
const volScalarField & T
Definition: createFields.H:25
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:216
tokenType type() const
Definition: tokenI.H:178
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
commsTypes
Types of communications.
Definition: UPstream.H:64
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
void setSize(const label)
Alter the addressed list size.
Definition: DynamicListI.H:167
A token holds items read from Istream.
Definition: token.H:67
void setBad()
Set stream to be bad.
Definition: IOstream.H:487
errorManip< error > abort(error &err)
Definition: errorManip.H:131
const string & stringToken() const
Definition: tokenI.H:249
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:404
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
~UOPstream()
Destructor.
Definition: UOPstream.C:130
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
UPstream(const commsTypes commsType)
Construct given optional buffer size.
Definition: UPstream.H:270
error FatalError
Ostream(streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Set stream status.
Definition: Ostream.H:78
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:49
commsTypes commsType_
Communications type of this stream.
Definition: UPstream.H:235
Version number type.
Definition: IOstream.H:96
#define notImplemented(functionName)
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:356
bool isspace(char c)
Definition: char.H:53
void setGood()
Set stream to be good.
Definition: IOstream.H:257