UOPstream.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2014 OpenFOAM Foundation
9  Copyright (C) 2017-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::UOPstreamBase
29 
30 Description
31  Base class for output inter-processor communications stream
32  (ie, parallel streams).
33  Not to be used directly, thus contructors are protected.
34 
35 SourceFiles
36  UOPstreamBase.C
37 
38 \*---------------------------------------------------------------------------*/
39 
40 #include "Pstream.H"
41 
42 #ifndef Foam_UOPstream_H
43 #define Foam_UOPstream_H
44 
45 #include "UPstream.H"
46 #include "Ostream.H"
47 #include "DynamicList.H"
48 #include "PstreamBuffers.H"
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 /*---------------------------------------------------------------------------*\
56  Class UOPstreamBase Declaration
57 \*---------------------------------------------------------------------------*/
58 
59 class UOPstreamBase
60 :
61  public UPstream,
62  public Ostream
63 {
64  // Private Member Functions
65 
66  //- Prepare send buffer for count bytes of output,
67  //- with specified alignment.
68  inline void prepareBuffer(const size_t count, const size_t align);
69 
70  //- Generic write data to the send buffer, aligned by sizeof(T)
71  template<class T>
72  inline void writeToBuffer(const T& val);
73 
74  //- Write count bytes of data to the send buffer,
75  //- using align byte alignment
76  inline void writeToBuffer
77  (
78  const void* data,
79  const size_t count,
80  const size_t align
81  );
82 
83  //- Add a single char to the send buffer. No alignment needed
84  inline void putChar(const char c);
85 
86  //- Write string length and string content.
87  // The content includes the trailing nul char.
88  inline void putString(const std::string& str);
89 
90 
91 protected:
92 
93  // Protected Data
94 
95  int toProcNo_;
96 
98 
99  const int tag_;
100 
101  const label comm_;
102 
103  const bool sendAtDestruct_;
104 
106  // Protected Constructors
108  //- Construct given process index to write to using the given
109  //- attached send buffer, optional communication characteristics
110  //- and IO format
112  (
113  const commsTypes commsType,
114  const int toProcNo,
115  DynamicList<char>& sendBuf,
116  const int tag = UPstream::msgType(),
117  const label comm = UPstream::worldComm,
118  const bool sendAtDestruct = true,
120  );
121 
122  //- Construct given buffers
123  UOPstreamBase(const int toProcNo, PstreamBuffers& buffers);
124 
125 
126 public:
127 
128  //- Destructor.
129  virtual ~UOPstreamBase();
130 
131 
132  // Member Functions
133 
134  // Inquiry
135 
136  //- Return flags of output stream
137  virtual ios_base::fmtflags flags() const
138  {
139  return ios_base::fmtflags(0);
140  }
141 
142 
143  // Write Functions
144 
145  //- Write token to stream or otherwise handle it.
146  // \return false if the token type was not handled by this method
147  virtual bool write(const token& tok);
148 
149  //- Write single character. Whitespace is suppressed.
150  virtual Ostream& write(const char c);
152  //- Write the word-characters of a character string.
153  // Sends as a single char, or as word.
154  virtual Ostream& write(const char* str);
155 
156  //- Write word
157  virtual Ostream& write(const word& str);
158 
159  //- Write string
160  virtual Ostream& write(const string& str);
161 
162  //- Write std::string surrounded by quotes.
163  // Optional write without quotes.
164  virtual Ostream& writeQuoted
165  (
166  const std::string& str,
167  const bool quoted=true
168  );
169 
170  //- Write int32_t as a label
171  virtual Ostream& write(const int32_t val);
172 
173  //- Write int64_t as a label
174  virtual Ostream& write(const int64_t val);
175 
176  //- Write float
177  virtual Ostream& write(const float val);
178 
179  //- Write double
180  virtual Ostream& write(const double val);
181 
182  //- Write binary block with 8-byte alignment.
183  virtual Ostream& write(const char* data, std::streamsize count);
184 
185  //- Low-level raw binary output.
186  virtual Ostream& writeRaw(const char* data, std::streamsize count);
187 
188  //- Begin marker for low-level raw binary output.
189  // The count indicates the number of bytes for subsequent
190  // writeRaw calls.
191  virtual bool beginRawWrite(std::streamsize count);
192 
193  //- End marker for low-level raw binary output.
194  virtual bool endRawWrite()
195  {
196  return true;
197  }
198 
199  //- Add indentation characters
200  virtual void indent()
201  {}
202 
203 
204  // Stream state functions
205 
206  //- Flush stream
207  virtual void flush()
208  {}
209 
210  //- Add newline and flush stream
211  virtual void endl()
212  {}
213 
214  //- Get the current padding character
215  // \return previous padding character
216  virtual char fill() const
217  {
218  return 0;
219  }
220 
221  //- Set padding character for formatted field up to field width
222  virtual char fill(const char)
223  {
224  return 0;
225  }
226 
227  //- Get width of output field
228  virtual int width() const
229  {
230  return 0;
231  }
232 
233  //- Set width of output field
234  // \return previous width
235  virtual int width(const int)
236  {
237  return 0;
238  }
239 
240  //- Get precision of output field
241  virtual int precision() const
242  {
243  return 0;
244  }
245 
246  //- Set precision of output field
247  // \return old precision
248  virtual int precision(const int)
249  {
250  return 0;
251  }
252 
253 
254  // Positioning
255 
256  //- Rewind the send buffer for overwriting
257  virtual void rewind();
258 
259 
260  // Edit
261 
262  //- Set flags of stream
263  virtual ios_base::fmtflags flags(const ios_base::fmtflags)
264  {
265  return ios_base::fmtflags(0);
266  }
267 
268 
269  // Print
270 
271  //- Print stream description to Ostream
272  void print(Ostream& os) const;
273 };
274 
275 
276 /*---------------------------------------------------------------------------*\
277  Class UOPstream Declaration
278 \*---------------------------------------------------------------------------*/
280 //- Output inter-processor communications stream
281 //- using MPI send/recv etc. - operating on external buffer.
282 class UOPstream
283 :
284  public UOPstreamBase
285 {
286  // Private Member Functions
288  //- Final buffer send, called by destructor
289  bool bufferIPCsend();
290 
291 
292 public:
293 
294  // Constructors
295 
296  //- Construct given process index to write to using the given
297  //- attached send buffer, optional communication characteristics
298  //- and IO format
299  UOPstream
300  (
301  const commsTypes commsType,
302  const int toProcNo,
303  DynamicList<char>& sendBuf,
304  const int tag = UPstream::msgType(),
305  const label comm = UPstream::worldComm,
306  const bool sendAtDestruct = true,
308  );
309 
310  //- Construct given buffers
311  UOPstream(const int toProcNo, PstreamBuffers& buffers);
312 
313 
314  //- Destructor, usually sends buffer on destruct.
315  virtual ~UOPstream();
316 
317 
318  // Member Functions
319 
320  //- Use all write methods from base class
321  using UOPstreamBase::write;
322 
323 
324  // Static Functions
325 
326  //- Write buffer contents to given processor
327  // \return True on success
328  static bool write
329  (
331  const int toProcNo,
332  const char* buf,
333  const std::streamsize bufSize,
334  const int tag = UPstream::msgType(),
335  const label comm = UPstream::worldComm
336  );
337 };
338 
339 
340 /*---------------------------------------------------------------------------*\
341  Class UOPBstream Declaration
342 \*---------------------------------------------------------------------------*/
343 
344 //- Output inter-processor communications stream
345 //- using MPI broadcast - operating on external buffer.
346 //
347 // \note does not use commsType, tag etc.
348 class UOPBstream
349 :
350  public UOPstreamBase
351 {
352  // Private Member Functions
353 
354  //- Final buffer send, called by destructor
355  bool bufferIPCsend();
356 
358 public:
359 
360  // Constructors
361 
362  //- Construct given process index to write to using the given
363  //- attached send buffer, optional communication characteristics
364  //- and IO format
365  UOPBstream
366  (
367  const commsTypes commsType,
368  const int toProcNo,
369  DynamicList<char>& sendBuf,
370  const int tag = UPstream::msgType(),
371  const label comm = UPstream::worldComm,
372  const bool sendAtDestruct = true,
374  );
375 
376 
377  //- Destructor, usually sends buffer on destruct.
378  virtual ~UOPBstream();
379 
380 
381  // Member Functions
382 
383  //- Use all write methods from base
384  using UOPstreamBase::write;
385 
386 
387  // Static Functions
388 
389  //- Wrapped version of UPstream::broadcast with const-cast
390  // \return True on success
391  static bool write
392  (
393  const commsTypes commsType,
394  const int rootProcNo,
395  const char* buf,
396  const std::streamsize bufSize,
397  const int tag = UPstream::msgType(),
398  const label comm = UPstream::worldComm
399  );
400 };
401 
402 
403 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
404 
405 } // End namespace Foam
406 
407 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
408 
409 #endif
410 
411 // ************************************************************************* //
static bool write(const UPstream::commsTypes commsType, const int toProcNo, const char *buf, const std::streamsize bufSize, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Write buffer contents to given processor.
const bool sendAtDestruct_
Definition: UOPstream.H:109
virtual bool write(const token &tok)
Write token to stream or otherwise handle it.
virtual void flush()
Flush stream.
Definition: UOPstream.H:257
commsTypes
Types of communications.
Definition: UPstream.H:66
virtual void indent()
Add indentation characters.
Definition: UOPstream.H:248
A token holds an item read from Istream.
Definition: token.H:64
UOPBstream(const commsTypes commsType, const int toProcNo, DynamicList< char > &sendBuf, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm, const bool sendAtDestruct=true, IOstreamOption::streamFormat fmt=IOstreamOption::BINARY)
Construct given process index to write to using the given attached send buffer, optional communicatio...
Definition: OPBstreams.C:28
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:806
static label worldComm
Default world communicator (all processors). May differ from globalComm if local worlds are in use...
Definition: UPstream.H:361
UOPstreamBase(const commsTypes commsType, const int toProcNo, DynamicList< char > &sendBuf, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm, const bool sendAtDestruct=true, IOstreamOption::streamFormat fmt=IOstreamOption::BINARY)
Construct given process index to write to using the given attached send buffer, optional communicatio...
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
virtual ios_base::fmtflags flags() const
Return flags of output stream.
Definition: UOPstream.H:151
virtual bool beginRawWrite(std::streamsize count)
Begin marker for low-level raw binary output.
virtual int width() const
Get width of output field.
Definition: UOPstream.H:287
A class for handling words, derived from Foam::string.
Definition: word.H:63
void print(Ostream &os) const
Print stream description to Ostream.
const label comm_
Definition: UOPstream.H:107
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)
Write std::string surrounded by quotes.
virtual ~UOPBstream()
Destructor, usually sends buffer on destruct.
Definition: OPBstreams.C:87
DynamicList< char > & sendBuf_
Definition: UOPstream.H:103
Output inter-processor communications stream using MPI broadcast - operating on external buffer...
Definition: UOPstream.H:439
virtual void endl()
Add newline and flush stream.
Definition: UOPstream.H:263
virtual void rewind()
Rewind the send buffer for overwriting.
virtual int precision() const
Get precision of output field.
Definition: UOPstream.H:305
commsTypes commsType() const noexcept
Get the communications type of the stream.
Definition: UPstream.H:814
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
Output inter-processor communications stream using MPI send/recv etc. - operating on external buffer...
Definition: UOPstream.H:357
virtual ~UOPstream()
Destructor, usually sends buffer on destruct.
Definition: OPstreams.C:74
OBJstream os(runTime.globalPath()/outputName)
Database for solution data, solver performance and other reduced data.
Definition: data.H:51
Buffers for inter-processor communications streams (UOPstream, UIPstream).
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
static bool write(const commsTypes commsType, const int rootProcNo, const char *buf, const std::streamsize bufSize, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Wrapped version of UPstream::broadcast with const-cast.
virtual ~UOPstreamBase()
Destructor.
virtual Ostream & writeRaw(const char *data, std::streamsize count)
Low-level raw binary output.
UOPstream(const commsTypes commsType, const int toProcNo, DynamicList< char > &sendBuf, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm, const bool sendAtDestruct=true, IOstreamOption::streamFormat fmt=IOstreamOption::BINARY)
Construct given process index to write to using the given attached send buffer, optional communicatio...
Definition: OPstreams.C:28
const dimensionedScalar c
Speed of light in a vacuum.
streamFormat
Data format (ascii | binary)
virtual bool endRawWrite()
End marker for low-level raw binary output.
Definition: UOPstream.H:240
Base class for output inter-processor communications stream (ie, parallel streams). Not to be used directly, thus contructors are protected.
Definition: UOPstream.H:54
virtual char fill() const
Get the current padding character.
Definition: UOPstream.H:271
Inter-processor communications stream.
Definition: UPstream.H:54
Namespace for OpenFOAM.