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