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-2024 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)
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  // Stream State Functions
148 
149  //- Return current stream flags.
150  //- Dummy for parallel stream, returns 0.
151  virtual std::ios_base::fmtflags flags() const override
152  {
153  return std::ios_base::fmtflags(0);
154  }
155 
156  //- Set stream flags, return old stream flags.
157  //- Dummy for parallel stream, returns 0.
158  virtual std::ios_base::fmtflags flags(std::ios_base::fmtflags) override
159  {
160  return std::ios_base::fmtflags(0);
161  }
162 
163 
164  // Write Functions
165 
166  //- Inherit write methods from Ostream
167  using Ostream::writeQuoted;
168 
169  //- Write token to stream or otherwise handle it.
170  // \return false if the token type was not handled by this method
171  virtual bool write(const token& tok) override;
172 
173  //- Write single character. Whitespace is suppressed.
174  virtual Ostream& write(const char c) override;
175 
176  //- Write character/string content, with/without surrounding quotes
177  virtual Ostream& writeQuoted
178  (
179  const char* str,
180  std::streamsize len,
181  const bool quoted=true
182  ) override;
183 
184  //- Write the word-characters of a character string.
185  // Sends as a single char, or as word.
186  virtual Ostream& write(const char* str) override;
188  //- Write word
189  virtual Ostream& write(const word& str) override;
190 
191  //- Write string
192  virtual Ostream& write(const std::string& str) override;
193 
194  //- Write int32_t as a label
195  virtual Ostream& write(const int32_t val) override;
196 
197  //- Write int64_t as a label
198  virtual Ostream& write(const int64_t val) override;
199 
200  //- Write float
201  virtual Ostream& write(const float val) override;
202 
203  //- Write double
204  virtual Ostream& write(const double val) override;
205 
206  //- Write binary block with 8-byte alignment.
207  virtual Ostream& write
208  (
209  const char* data,
210  std::streamsize count
211  ) override;
212 
213  //- Low-level raw binary output.
214  virtual Ostream& writeRaw
215  (
216  const char* data,
217  std::streamsize count
218  ) override;
219 
220  //- Begin marker for low-level raw binary output.
221  // The count indicates the number of bytes for subsequent
222  // writeRaw calls.
223  virtual bool beginRawWrite(std::streamsize count) override;
224 
225  //- End marker for low-level raw binary output.
226  virtual bool endRawWrite() override
227  {
228  return true;
229  }
230 
231  //- Add indentation characters
232  virtual void indent() override
233  {}
234 
235 
236  // Stream state functions
237 
238  //- Flush stream
239  virtual void flush() override
240  {}
241 
242  //- Add newline and flush stream
243  virtual void endl() override
244  {}
245 
246  //- Get the current padding character
247  // \return previous padding character
248  virtual char fill() const override
249  {
250  return 0;
251  }
252 
253  //- Set padding character for formatted field up to field width
254  virtual char fill(const char) override
255  {
256  return 0;
257  }
258 
259  //- Get width of output field
260  virtual int width() const override
261  {
262  return 0;
263  }
264 
265  //- Set width of output field
266  // \return previous width
267  virtual int width(const int) override
268  {
269  return 0;
270  }
271 
272  //- Get precision of output field
273  virtual int precision() const override
274  {
275  return 0;
276  }
277 
278  //- Set precision of output field
279  // \return old precision
280  virtual int precision(const int) override
281  {
282  return 0;
283  }
284 
285 
286  // Positioning
287 
288  //- Rewind the send buffer for overwriting
289  virtual void rewind();
290 
291 
292  // Print
293 
294  //- Print stream description to Ostream
295  void print(Ostream& os) const override;
296 };
297 
298 
299 /*---------------------------------------------------------------------------*\
300  Class UOPstream Declaration
301 \*---------------------------------------------------------------------------*/
302 
303 //- Output inter-processor communications stream
304 //- using MPI send/recv etc. - operating on external buffer.
306 :
307  public UOPstreamBase
308 {
309  // Private Member Functions
310 
311  //- Buffer send, usually called by destructor
312  bool bufferIPCsend();
313 
314 
315 public:
316 
317  // Constructors
318 
319  //- Construct given process index to write to using the given
320  //- attached send buffer, optional communication characteristics
321  //- and IO format
322  UOPstream
323  (
325  const int toProcNo,
326  DynamicList<char>& sendBuf,
327  const int tag = UPstream::msgType(),
328  const label comm = UPstream::worldComm,
329  const bool sendAtDestruct = true,
331  );
332 
333  //- Construct given buffers
334  UOPstream(const int toProcNo, PstreamBuffers& buffers);
336  //- Construct for writing into a standalone buffer.
337  //- Data transfer is handled externally by the caller.
338  explicit UOPstream
339  (
340  DynamicList<char>& sendBuf,
342  );
343 
344 
345  //- Destructor, usually sends buffer on destruct.
346  virtual ~UOPstream();
347 
348 
349  // Member Functions
350 
351  //- Send buffer contents now and not in destructor [advanced usage].
352  //- Returns true on success
353  bool send();
354 
355  //- Use all write methods from base class
356  using UOPstreamBase::write;
357 
358 
359  // Static Functions
360 
361  //- Write buffer contents to given processor
362  // \return True on success
363  static bool write
364  (
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  UPstream::Request* req = nullptr,
374  );
375 
376  //- Write buffer contents (non-blocking) to given processor
377  // \return True on success
378  inline static bool write
379  (
381  UPstream::Request& req,
382  const int toProcNo,
383  const char* buf,
384  const std::streamsize bufSize,
385  const int tag = UPstream::msgType(),
386  const label comm = UPstream::worldComm,
388  )
389  {
390  return UOPstream::write
391  (
393  toProcNo,
394  buf,
395  bufSize,
396  tag,
397  comm,
398  &req,
399  sendMode
400  );
401  }
402 
403  //- Write UList contents to given processor.
404  // Only valid for contiguous data types.
405  // \return True on success
406  template<class Type>
407  inline static bool write
408  (
410  const int toProcNo,
411  const UList<Type>& buffer,
412  const int tag = UPstream::msgType(),
413  const label comm = UPstream::worldComm,
415  UPstream::Request* req = nullptr,
417  )
418  {
419  return UOPstream::write
420  (
421  commsType,
422  toProcNo,
423  buffer.cdata_bytes(),
424  buffer.size_bytes(),
425  tag,
426  comm,
427  req,
428  sendMode
429  );
430  }
431 
432  //- Write UList contents (non-blocking) to given processor.
433  // Only valid for contiguous data types.
434  // \return True on success
435  template<class Type>
436  inline static bool write
437  (
439  UPstream::Request& req,
440  const int toProcNo,
441  const UList<Type>& buffer,
442  const int tag = UPstream::msgType(),
443  const label comm = UPstream::worldComm,
445  )
446  {
447  return UOPstream::write
448  (
450  toProcNo,
451  buffer.cdata_bytes(),
452  buffer.size_bytes(),
453  tag,
454  comm,
455  &req,
456  sendMode
457  );
458  }
459 };
460 
461 
462 /*---------------------------------------------------------------------------*\
463  Class UOPBstream Declaration
464 \*---------------------------------------------------------------------------*/
465 
466 //- Output inter-processor communications stream
467 //- using MPI broadcast - operating on external buffer.
468 //
469 // \note does not use commsType, tag etc.
470 class UOPBstream
471 :
472  public UOPstreamBase
473 {
474  // Private Member Functions
475 
476  //- Buffer send, usually called by destructor
477  bool bufferIPCsend();
478 
479 
480 public:
481 
482  // Constructors
483 
484  //- Construct given process index to write to using the given
485  //- attached send buffer, optional communication characteristics
486  //- and IO format
487  UOPBstream
488  (
489  const UPstream::commsTypes,
490  const int toProcNo,
491  DynamicList<char>& sendBuf,
492  const int tag = UPstream::msgType(),
493  const label comm = UPstream::worldComm,
494  const bool sendAtDestruct = true,
496  );
497 
498 
499  //- Destructor, usually sends buffer on destruct.
500  virtual ~UOPBstream();
501 
502 
503  // Member Functions
504 
505  //- Use all write methods from base
506  using UOPstreamBase::write;
507 
508 
509  // Static Functions
510 
511  //- Wrapped version of UPstream::broadcast with const-cast
512  // \return True on success
513  static bool write
514  (
515  const int rootProcNo,
516  const char* buf,
517  const std::streamsize bufSize,
518  const label comm = UPstream::worldComm
519  );
520 };
521 
522 
523 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
524 
525 } // End namespace Foam
526 
527 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
528 
529 #endif
530 
531 // ************************************************************************* //
virtual void endl() override
Add newline and flush stream.
Definition: UOPstream.H:311
sendModes
Different MPI-send modes (ignored for commsTypes::buffered)
Definition: UPstream.H:94
commsTypes
Communications types.
Definition: UPstream.H:77
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.
virtual char fill() const override
Get the current padding character.
Definition: UOPstream.H:319
virtual bool endRawWrite() override
End marker for low-level raw binary output.
Definition: UOPstream.H:288
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:1252
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:421
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:353
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:272
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 without any <mpi...
Definition: UPstream.H:1741
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:305
(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:114
DynamicList< char > & sendBuf_
Reference to the send buffer data.
Definition: UOPstream.H:127
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:1284
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:394
virtual ~UOPstream()
Destructor, usually sends buffer on destruct.
Definition: OPstreams.C:93
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.
bool sendAtDestruct_
Call bufferIPCsend on termination (in the destructor)
Definition: UOPstream.H:122
virtual void indent() override
Add indentation characters.
Definition: UOPstream.H:296
void print(Ostream &os) const override
Print stream description to Ostream.
virtual int width() const override
Get width of output field.
Definition: UOPstream.H:335
virtual std::ios_base::fmtflags flags() const override
Return current stream flags. Dummy for parallel stream, returns 0.
Definition: UOPstream.H:178
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" (immediate) : (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
bool send()
Send buffer contents now and not in destructor [advanced usage]. Returns true on success.
Definition: OPstreams.C:84
Ostream(const Ostream &)=default
Copy construct.
virtual bool write(const token &tok) override
Write token to stream or otherwise handle it.
Inter-processor communications stream.
Definition: UPstream.H:65
std::streamsize size_bytes() const noexcept
Number of contiguous bytes for the List data.
Definition: UListI.H:286
Namespace for OpenFOAM.