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-2023 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  //- Destination rank for the data
96  const int toProcNo_;
97 
98  //- Message tag for communication
99  const int tag_;
100 
101  //- The communicator index
102  const int comm_;
103 
104  //- Call bufferIPCsend on termination (in the destructor)
105  const bool sendAtDestruct_;
106 
107  //- Reference to the send buffer data
110 
111  // Protected Constructors
112 
113  //- Construct given process index to write to using the given
114  //- attached send buffer, optional communication characteristics
115  //- and IO format
117  (
119  const int toProcNo,
120  DynamicList<char>& sendBuf,
121  const int tag = UPstream::msgType(),
122  const label comm = UPstream::worldComm,
123  const bool sendAtDestruct = true,
125  );
126 
127  //- Construct given buffers
128  UOPstreamBase(const int toProcNo, PstreamBuffers& buffers);
129 
130  //- Construct for externally obtained buffers
132  (
133  DynamicList<char>& sendBuf,
135  );
136 
137 public:
138 
139  //- Destructor.
140  virtual ~UOPstreamBase();
141 
142 
143  // Member Functions
144 
145  // Inquiry
146 
147  //- Return flags of output stream
148  virtual ios_base::fmtflags flags() const
149  {
150  return ios_base::fmtflags(0);
151  }
152 
153 
154  // Write Functions
155 
156  //- Write token to stream or otherwise handle it.
157  // \return false if the token type was not handled by this method
158  virtual bool write(const token& tok);
159 
160  //- Write single character. Whitespace is suppressed.
161  virtual Ostream& write(const char c);
162 
163  //- Write the word-characters of a character string.
164  // Sends as a single char, or as word.
165  virtual Ostream& write(const char* str);
166 
167  //- Write word
168  virtual Ostream& write(const word& str);
169 
170  //- Write string
171  virtual Ostream& write(const string& str);
172 
173  //- Write std::string surrounded by quotes.
174  // Optional write without quotes.
175  virtual Ostream& writeQuoted
176  (
177  const std::string& str,
178  const bool quoted=true
179  );
180 
181  //- Write int32_t as a label
182  virtual Ostream& write(const int32_t val);
183 
184  //- Write int64_t as a label
185  virtual Ostream& write(const int64_t val);
186 
187  //- Write float
188  virtual Ostream& write(const float val);
189 
190  //- Write double
191  virtual Ostream& write(const double val);
192 
193  //- Write binary block with 8-byte alignment.
194  virtual Ostream& write(const char* data, std::streamsize count);
195 
196  //- Low-level raw binary output.
197  virtual Ostream& writeRaw(const char* data, std::streamsize count);
198 
199  //- Begin marker for low-level raw binary output.
200  // The count indicates the number of bytes for subsequent
201  // writeRaw calls.
202  virtual bool beginRawWrite(std::streamsize count);
203 
204  //- End marker for low-level raw binary output.
205  virtual bool endRawWrite()
206  {
207  return true;
208  }
209 
210  //- Add indentation characters
211  virtual void indent()
212  {}
213 
214 
215  // Stream state functions
216 
217  //- Flush stream
218  virtual void flush()
219  {}
220 
221  //- Add newline and flush stream
222  virtual void endl()
223  {}
224 
225  //- Get the current padding character
226  // \return previous padding character
227  virtual char fill() const
228  {
229  return 0;
230  }
231 
232  //- Set padding character for formatted field up to field width
233  virtual char fill(const char)
234  {
235  return 0;
236  }
237 
238  //- Get width of output field
239  virtual int width() const
240  {
241  return 0;
242  }
243 
244  //- Set width of output field
245  // \return previous width
246  virtual int width(const int)
247  {
248  return 0;
249  }
250 
251  //- Get precision of output field
252  virtual int precision() const
253  {
254  return 0;
255  }
256 
257  //- Set precision of output field
258  // \return old precision
259  virtual int precision(const int)
260  {
261  return 0;
262  }
264 
265  // Positioning
266 
267  //- Rewind the send buffer for overwriting
268  virtual void rewind();
269 
270 
271  // Edit
272 
273  //- Set flags of stream
274  virtual ios_base::fmtflags flags(const ios_base::fmtflags)
275  {
276  return ios_base::fmtflags(0);
277  }
278 
279 
280  // Print
281 
282  //- Print stream description to Ostream
283  void print(Ostream& os) const;
284 };
285 
287 /*---------------------------------------------------------------------------*\
288  Class UOPstream Declaration
289 \*---------------------------------------------------------------------------*/
290 
291 //- Output inter-processor communications stream
292 //- using MPI send/recv etc. - operating on external buffer.
293 class UOPstream
294 :
295  public UOPstreamBase
296 {
297  // Private Member Functions
298 
299  //- Final buffer send, called by destructor
300  bool bufferIPCsend();
301 
303 public:
304 
305  // Constructors
306 
307  //- Construct given process index to write to using the given
308  //- attached send buffer, optional communication characteristics
309  //- and IO format
311  (
313  const int toProcNo,
314  DynamicList<char>& sendBuf,
315  const int tag = UPstream::msgType(),
316  const label comm = UPstream::worldComm,
317  const bool sendAtDestruct = true,
319  );
321  //- Construct given buffers
322  UOPstream(const int toProcNo, PstreamBuffers& buffers);
323 
324  //- Construct for writing into a standalone buffer.
325  //- Data transfer is handled externally by the caller.
326  explicit UOPstream
327  (
330  );
331 
332 
333  //- Destructor, usually sends buffer on destruct.
334  virtual ~UOPstream();
335 
336 
337  // Member Functions
339  //- Use all write methods from base class
340  using UOPstreamBase::write;
341 
342 
343  // Static Functions
344 
345  //- Write buffer contents to given processor
346  // \return True on success
347  static bool write
348  (
350  const int toProcNo,
351  const char* buf,
352  const std::streamsize bufSize,
353  const int tag = UPstream::msgType(),
354  const label comm = UPstream::worldComm,
356  UPstream::Request* req = nullptr,
358  );
359 
360  //- Write buffer contents (non-blocking) to given processor
361  // \return True on success
362  inline static bool write
363  (
365  UPstream::Request& req,
366  const int toProcNo,
367  const char* buf,
368  const std::streamsize bufSize,
369  const int tag = UPstream::msgType(),
370  const label comm = UPstream::worldComm,
372  )
373  {
374  return UOPstream::write
375  (
377  toProcNo,
378  buf,
379  bufSize,
380  tag,
381  comm,
382  &req,
383  sendMode
384  );
385  }
386 
387  //- Write UList contents to given processor.
388  // Only valid for contiguous data types.
389  // \return True on success
390  template<class Type>
391  inline static bool write
392  (
394  const int toProcNo,
395  const UList<Type>& buffer,
396  const int tag = UPstream::msgType(),
397  const label comm = UPstream::worldComm,
399  UPstream::Request* req = nullptr,
401  )
402  {
403  return UOPstream::write
404  (
405  commsType,
406  toProcNo,
407  buffer.cdata_bytes(),
408  buffer.size_bytes(),
409  tag,
410  comm,
411  req,
412  sendMode
413  );
414  }
415 
416  //- Write UList contents (non-blocking) to given processor.
417  // Only valid for contiguous data types.
418  // \return True on success
419  template<class Type>
420  inline static bool write
421  (
423  UPstream::Request& req,
424  const int toProcNo,
425  const UList<Type>& buffer,
426  const int tag = UPstream::msgType(),
427  const label comm = UPstream::worldComm,
429  )
430  {
431  return UOPstream::write
432  (
434  toProcNo,
435  buffer.cdata_bytes(),
436  buffer.size_bytes(),
437  tag,
438  comm,
439  &req,
440  sendMode
441  );
442  }
443 };
444 
445 
446 /*---------------------------------------------------------------------------*\
447  Class UOPBstream Declaration
448 \*---------------------------------------------------------------------------*/
449 
450 //- Output inter-processor communications stream
451 //- using MPI broadcast - operating on external buffer.
452 //
453 // \note does not use commsType, tag etc.
454 class UOPBstream
455 :
456  public UOPstreamBase
457 {
458  // Private Member Functions
459 
460  //- Final buffer send, called by destructor
461  bool bufferIPCsend();
462 
463 
464 public:
465 
466  // Constructors
467 
468  //- Construct given process index to write to using the given
469  //- attached send buffer, optional communication characteristics
470  //- and IO format
471  UOPBstream
472  (
473  const UPstream::commsTypes,
474  const int toProcNo,
475  DynamicList<char>& sendBuf,
476  const int tag = UPstream::msgType(),
477  const label comm = UPstream::worldComm,
478  const bool sendAtDestruct = true,
480  );
481 
482 
483  //- Destructor, usually sends buffer on destruct.
484  virtual ~UOPBstream();
485 
486 
487  // Member Functions
488 
489  //- Use all write methods from base
490  using UOPstreamBase::write;
491 
492 
493  // Static Functions
494 
495  //- Wrapped version of UPstream::broadcast with const-cast
496  // \return True on success
497  static bool write
498  (
499  const int rootProcNo,
500  const char* buf,
501  const std::streamsize bufSize,
502  const label comm = UPstream::worldComm
503  );
504 };
505 
506 
507 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
508 
509 } // End namespace Foam
510 
511 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
512 
513 #endif
514 
515 // ************************************************************************* //
sendModes
Different MPI-send modes (ignored for commsTypes::blocking)
Definition: UPstream.H:89
const bool sendAtDestruct_
Call bufferIPCsend on termination (in the destructor)
Definition: UOPstream.H:119
virtual bool write(const token &tok)
Write token to stream or otherwise handle it.
virtual void flush()
Flush stream.
Definition: UOPstream.H:280
commsTypes
Communications types.
Definition: UPstream.H:74
virtual void indent()
Add indentation characters.
Definition: UOPstream.H:271
const int comm_
The communicator index.
Definition: UOPstream.H:114
A token holds an item read from Istream.
Definition: token.H:64
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:1184
static label worldComm
Communicator for all ranks. May differ from commGlobal() if local worlds are in use.
Definition: UPstream.H:411
static bool write(const int rootProcNo, const char *buf, const std::streamsize bufSize, const label comm=UPstream::worldComm)
Wrapped version of UPstream::broadcast with const-cast.
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:236
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:174
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:310
An opaque wrapper for MPI_Request with a vendor-independent representation independent of any <mpi...
Definition: UPstream.H:1571
A class for handling words, derived from Foam::string.
Definition: word.H:63
UOPstreamBase(const UPstream::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...
UOPstream(const UPstream::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
void print(Ostream &os) const
Print stream description to Ostream.
(MPI_Send, MPI_Isend)
const int toProcNo_
Destination rank for the data.
Definition: UOPstream.H:104
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_
Reference to the send buffer data.
Definition: UOPstream.H:124
virtual void endl()
Add newline and flush stream.
Definition: UOPstream.H:286
virtual void rewind()
Rewind the send buffer for overwriting.
UOPBstream(const UPstream::commsTypes, 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
virtual int precision() const
Get precision of output field.
Definition: UOPstream.H:328
commsTypes commsType() const noexcept
Get the communications type of the stream.
Definition: UPstream.H:1192
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:380
virtual ~UOPstream()
Destructor, usually sends buffer on destruct.
Definition: OPstreams.C:84
OBJstream os(runTime.globalPath()/outputName)
Database for solution data, solver performance and other reduced data.
Definition: data.H:52
Buffers for inter-processor communications streams (UOPstream, UIPstream).
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
virtual ~UOPstreamBase()
Destructor.
virtual Ostream & writeRaw(const char *data, std::streamsize count)
Low-level raw binary output.
const dimensionedScalar c
Speed of light in a vacuum.
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, UPstream::Request *req=nullptr, const UPstream::sendModes sendMode=UPstream::sendModes::normal)
Write buffer contents to given processor.
streamFormat
Data format (ascii | binary)
"nonBlocking" : (MPI_Isend, MPI_Irecv)
virtual bool endRawWrite()
End marker for low-level raw binary output.
Definition: UOPstream.H:263
const int tag_
Message tag for communication.
Definition: UOPstream.H:109
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:294
Inter-processor communications stream.
Definition: UPstream.H:62
std::streamsize size_bytes() const noexcept
Number of contiguous bytes for the List data.
Definition: UListI.H:250
Namespace for OpenFOAM.