Loading...
Searching...
No Matches
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,2024 OpenFOAM Foundation
9 Copyright (C) 2017-2025 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::UOPstreamBase
29
30Description
31 Base class for output inter-processor communications stream
32 (ie, parallel streams).
33 Not to be used directly, thus contructors are protected.
34
35SourceFiles
36 UOPstream.txx
37 UOPstreamBase.C
38
39\*---------------------------------------------------------------------------*/
40
41#include "Pstream.H"
42
43#ifndef Foam_UOPstream_H
44#define Foam_UOPstream_H
45
46#include "UPstream.H"
47#include "Ostream.H"
48#include "DynamicList.H"
49#include "PstreamBuffers.H"
50
51// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52
53namespace Foam
54{
56/*---------------------------------------------------------------------------*\
57 Class UOPstreamBase Declaration
58\*---------------------------------------------------------------------------*/
59
60class UOPstreamBase
61:
62 public UPstream,
63 public Ostream
64{
65 // Private Member Functions
66
67 //- Prepare send buffer for count bytes of output,
68 //- with specified alignment.
69 inline void prepareBuffer(const size_t count, const size_t align);
70
71 //- Generic write data to the send buffer, aligned by sizeof(T)
72 template<class T>
73 inline void writeToBuffer(const T& val);
74
75 //- Write count bytes of data to the send buffer,
76 //- using align byte alignment
77 inline void writeToBuffer
78 (
79 const void* data,
80 const size_t count,
81 const size_t align
82 );
83
84 //- Add a single char to the send buffer. No alignment needed
85 inline void putChar(const char c);
86
87 //- Write string length and content, including embedded nul chars.
88 inline void putString(const char* str, const size_t len);
89
90 //- Write string length and content, including embedded nul chars.
91 inline void putString(const std::string& str);
92
93
94protected:
95
96 // Protected Data
97
98 //- Destination rank for the data
99 const int toProcNo_;
100
101 //- Message tag for communication
102 const int tag_;
103
104 //- The communicator index
105 const int comm_;
106
107 //- Call bufferIPCsend on termination (in the destructor)
109
110 //- Reference to the send buffer data
112
114 // Protected Constructors
115
116 //- Construct given process index to write to using the given
117 //- attached send buffer, optional communication characteristics
118 //- and IO format
120 (
122 const int toProcNo,
124 const int tag = UPstream::msgType(),
126 const bool sendAtDestruct = true,
128 );
129
130 //- Construct given buffers
131 UOPstreamBase(const int toProcNo, PstreamBuffers& buffers);
132
133 //- Construct for externally obtained buffers
135 (
136 DynamicList<char>& sendBuf,
138 );
139
140public:
141
142 //- Destructor.
143 virtual ~UOPstreamBase();
144
145
146 // Member Functions
147
148 // Stream State Functions
149
150 //- Return current stream flags.
151 //- Dummy for parallel stream, returns 0.
152 virtual std::ios_base::fmtflags flags() const override
153 {
154 return std::ios_base::fmtflags(0);
155 }
156
157 //- Set stream flags, return old stream flags.
158 //- Dummy for parallel stream, returns 0.
159 virtual std::ios_base::fmtflags flags(std::ios_base::fmtflags) override
160 {
161 return std::ios_base::fmtflags(0);
162 }
163
164
165 // Write Functions
166
167 //- Inherit write methods from Ostream
169
170 //- Write token to stream or otherwise handle it.
171 // \return false if the token type was not handled by this method
172 virtual bool write(const token& tok) override;
173
174 //- Write single character. Whitespace is suppressed.
175 virtual Ostream& write(const char c) override;
176
177 //- Write character/string content, with/without surrounding quotes
178 virtual Ostream& writeQuoted
180 const char* str,
181 std::streamsize len,
182 const bool quoted=true
183 ) override;
184
185 //- Write the word-characters of a character string.
186 // Sends as a single char, or as word.
187 virtual Ostream& write(const char* str) override;
189 //- Write word
190 virtual Ostream& write(const word& str) override;
191
192 //- Write string
193 virtual Ostream& write(const std::string& str) override;
194
195 //- Write int32_t
196 virtual Ostream& write(const int32_t val) override;
197
198 //- Write int64_t
199 virtual Ostream& write(const int64_t val) override;
200
201 //- Write uint32_t
202 virtual Ostream& write(const uint32_t val) override;
203
204 //- Write uint64_t
205 virtual Ostream& write(const uint64_t val) override;
206
207 //- Write float
208 virtual Ostream& write(const float val) override;
209
210 //- Write double
211 virtual Ostream& write(const double val) override;
212
213 //- Write binary block with 8-byte alignment.
214 virtual Ostream& write
215 (
216 const char* data,
217 std::streamsize count
218 ) override;
219
220 //- Low-level raw binary output.
221 virtual Ostream& writeRaw
222 (
223 const char* data,
224 std::streamsize count
225 ) override;
226
227 //- Begin marker for low-level raw binary output.
228 // The count indicates the number of bytes for subsequent
229 // writeRaw calls.
230 virtual bool beginRawWrite(std::streamsize count) override;
231
232 //- End marker for low-level raw binary output.
233 virtual bool endRawWrite() override
234 {
235 return true;
236 }
237
238 //- Add indentation characters
239 virtual void indent() override
240 {}
241
242
243 // Stream state functions
244
245 //- Flush stream
246 virtual void flush() override
247 {}
248
249 //- Add newline and flush stream
250 virtual void endl() override
251 {}
252
253 //- Get the current padding character
254 // \return previous padding character
255 virtual char fill() const override
256 {
257 return 0;
258 }
259
260 //- Set padding character for formatted field up to field width
261 virtual char fill(const char) override
262 {
263 return 0;
264 }
265
266 //- Get width of output field
267 virtual int width() const override
268 {
269 return 0;
270 }
271
272 //- Set width of output field
273 // \return previous width
274 virtual int width(const int) override
275 {
276 return 0;
277 }
278
279 //- Get precision of output field
280 virtual int precision() const override
281 {
282 return 0;
283 }
284
285 //- Set precision of output field
286 // \return old precision
287 virtual int precision(const int) override
288 {
289 return 0;
290 }
291
292
293 // Positioning
294
295 //- Rewind the send buffer for overwriting
296 virtual void rewind();
297
298
299 // Print
300
301 //- Print stream description to Ostream
302 void print(Ostream& os) const override;
303};
304
305
306/*---------------------------------------------------------------------------*\
307 Class UOPstream Declaration
308\*---------------------------------------------------------------------------*/
309
310//- Output inter-processor communications stream
311//- using MPI send/recv etc. - operating on external buffer.
312class UOPstream
313:
314 public UOPstreamBase
315{
316 // Private Member Functions
317
318 //- Buffer send, usually called by destructor
319 bool bufferIPCsend();
320
321
322public:
323
324 // Constructors
325
326 //- Construct given process index to write to using the given
327 //- attached send buffer, optional communication characteristics
328 //- and IO format
332 const int toProcNo,
333 DynamicList<char>& sendBuf,
334 const int tag = UPstream::msgType(),
336 const bool sendAtDestruct = true,
338 );
339
340 //- Construct given buffers
341 UOPstream(const int toProcNo, PstreamBuffers& buffers);
342
343 //- Construct for writing into a standalone buffer.
344 //- Data transfer is handled externally by the caller.
345 explicit UOPstream
350
351
352 //- Destructor, usually sends buffer on destruct.
353 virtual ~UOPstream();
354
355
356 // Member Functions
357
358 //- Send buffer contents now and not in destructor [advanced usage].
359 //- Returns true on success
360 bool send();
361
362 //- Use all write methods from base class
365
366 // Static Functions
367
368 //- Write buffer contents (contiguous types only) to given processor
369 // \return True on success
370 template<class Type>
371 static bool write
372 (
374 const int toProcNo,
375 const Type* buffer,
376 std::streamsize count,
377 const int tag = UPstream::msgType(),
380 UPstream::Request* req = nullptr,
383 );
384
385 //- Write buffer contents (contiguous types only) to given processor
386 // \return True on success
387 template<class Type>
388 inline static bool write
389 (
392 const int toProcNo,
393 const Type* buffer,
394 std::streamsize count,
395 const int tag = UPstream::msgType(),
398 );
399
400 //- Send UList contiguous contents to given processor.
401 template<class Type>
402 inline static bool write
403 (
405 const int toProcNo,
406 const UList<Type>& buffer,
407 const int tag = UPstream::msgType(),
410 UPstream::Request* req = nullptr,
412 );
413
414 //- Send SubList contiguous contents to given processor.
415 template<class Type>
416 inline static bool write
417 (
419 const int toProcNo,
420 const SubList<Type> buffer, // passed by shallow copy
421 const int tag = UPstream::msgType(),
424 UPstream::Request* req = nullptr,
426 );
427
428 //- Send UList contiguous contents (non-blocking) to given processor.
429 template<class Type>
430 inline static bool write
431 (
434 const int toProcNo,
435 const UList<Type>& buffer,
436 const int tag = UPstream::msgType(),
439 );
440
441 //- Send SubList contiguous contents (non-blocking) to given processor.
442 template<class Type>
443 inline static bool write
444 (
447 const int toProcNo,
448 const SubList<Type> buffer, // passed by shallow copy
449 const int tag = UPstream::msgType(),
452 );
453};
454
455
456/*---------------------------------------------------------------------------*\
457 Class UOPBstream Declaration
458\*---------------------------------------------------------------------------*/
459
460//- Output inter-processor communications stream
461//- using MPI broadcast - operating on external buffer.
462//
463// \note does not use commsType, tag etc.
464class UOPBstream
465:
466 public UOPstreamBase
467{
468 // Private Member Functions
469
470 //- Buffer send, usually called by destructor
471 bool bufferIPCsend();
472
473
474public:
475
476 // Constructors
477
478 //- Construct with attached send buffer,
479 //- optional communication characteristics and IO format
480 explicit UOPBstream
482 DynamicList<char>& sendBuf,
484 const bool sendAtDestruct = true,
486 );
487
488
489 //- Destructor, usually sends buffer on destruct.
490 virtual ~UOPBstream();
491
492
493 // Member Functions
494
495 //- Broadcast a zero value (buffer) size that can be matched
496 //- by the UIPBstream constructor.
497 static void send(Foam::zero, int communicator, int root=0);
498};
499
500
501// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
502
503} // End namespace Foam
504
505// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
506
507#ifdef NoRepository
508 #include "UOPstream.txx"
509#endif
510
511// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
512
513#endif
514
515// ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
streamFormat
Data format (ascii | binary | coherent).
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true)=0
Write character/string content, with/without surrounding quotes.
Ostream(const Ostream &)=default
Copy construct.
Buffers for inter-processor communications streams (UOPstream, UIPstream).
A non-owning sub-view of a List (allocated or unallocated storage).
Definition SubList.H:61
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
Output inter-processor communications stream using MPI broadcast - operating on external buffer.
Definition UOPstream.H:591
UOPBstream(DynamicList< char > &sendBuf, const int communicator=UPstream::worldComm, const bool sendAtDestruct=true, IOstreamOption::streamFormat fmt=IOstreamOption::BINARY)
Construct with attached send buffer, optional communication characteristics and IO format.
Definition OPBstreams.C:28
static void send(Foam::zero, int communicator, int root=0)
Broadcast a zero value (buffer) size that can be matched by the UIPBstream constructor.
virtual ~UOPBstream()
Destructor, usually sends buffer on destruct.
Definition OPBstreams.C:67
Base class for output inter-processor communications stream (ie, parallel streams)....
Definition UOPstream.H:59
virtual int precision() const override
Get precision of output field.
Definition UOPstream.H:364
virtual std::ios_base::fmtflags flags() const override
Return current stream flags. Dummy for parallel stream, returns 0.
Definition UOPstream.H:179
virtual int width(const int) override
Set width of output field.
Definition UOPstream.H:356
DynamicList< char > & sendBuf_
Reference to the send buffer data.
Definition UOPstream.H:128
virtual ~UOPstreamBase()
Destructor.
virtual void indent() override
Add indentation characters.
Definition UOPstream.H:307
const int toProcNo_
Destination rank for the data.
Definition UOPstream.H:108
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true) override
Write character/string content, with/without surrounding quotes.
bool sendAtDestruct_
Call bufferIPCsend on termination (in the destructor).
Definition UOPstream.H:123
void print(Ostream &os) const override
Print stream description to Ostream.
const int tag_
Message tag for communication.
Definition UOPstream.H:113
virtual bool endRawWrite() override
End marker for low-level raw binary output.
Definition UOPstream.H:299
virtual std::ios_base::fmtflags flags(std::ios_base::fmtflags) override
Set stream flags, return old stream flags. Dummy for parallel stream, returns 0.
Definition UOPstream.H:188
const int comm_
The communicator index.
Definition UOPstream.H:118
virtual int width() const override
Get width of output field.
Definition UOPstream.H:346
UOPstreamBase(const UPstream::commsTypes commsType, const int toProcNo, DynamicList< char > &sendBuf, const int tag=UPstream::msgType(), const int communicator=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...
virtual Ostream & writeRaw(const char *data, std::streamsize count) override
Low-level raw binary output.
virtual bool beginRawWrite(std::streamsize count) override
Begin marker for low-level raw binary output.
virtual char fill() const override
Get the current padding character.
Definition UOPstream.H:330
virtual void rewind()
Rewind the send buffer for overwriting.
virtual int precision(const int) override
Set precision of output field.
Definition UOPstream.H:374
virtual char fill(const char) override
Set padding character for formatted field up to field width.
Definition UOPstream.H:338
virtual bool write(const token &tok) override
Write token to stream or otherwise handle it.
virtual void flush() override
Flush stream.
Definition UOPstream.H:316
virtual void endl() override
Add newline and flush stream.
Definition UOPstream.H:322
Output inter-processor communications stream using MPI send/recv etc. - operating on external buffer.
Definition UOPstream.H:408
static bool write(const UPstream::commsTypes commsType, const int toProcNo, const Type *buffer, std::streamsize count, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm, UPstream::Request *req=nullptr, const UPstream::sendModes sendMode=UPstream::sendModes::normal)
Write buffer contents (contiguous types only) to given processor.
static bool write(const UPstream::commsTypes commsType, const int toProcNo, const UList< Type > &buffer, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm, UPstream::Request *req=nullptr, const UPstream::sendModes sendMode=UPstream::sendModes::normal)
Send UList contiguous contents to given processor.
static bool write(UPstream::Request &req, const int toProcNo, const UList< Type > &buffer, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm, const UPstream::sendModes sendMode=UPstream::sendModes::normal)
Send UList contiguous contents (non-blocking) to given processor.
static bool write(const UPstream::commsTypes commsType, const int toProcNo, const SubList< Type > buffer, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm, UPstream::Request *req=nullptr, const UPstream::sendModes sendMode=UPstream::sendModes::normal)
Send SubList contiguous contents to given processor.
virtual ~UOPstream()
Destructor, usually sends buffer on destruct.
Definition OPstreams.C:93
UOPstream(const UPstream::commsTypes commsType, const int toProcNo, DynamicList< char > &sendBuf, const int tag=UPstream::msgType(), const int communicator=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
static bool write(UPstream::Request &req, const int toProcNo, const SubList< Type > buffer, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm, const UPstream::sendModes sendMode=UPstream::sendModes::normal)
Send SubList contiguous contents (non-blocking) to given processor.
static bool write(UPstream::Request &req, const int toProcNo, const Type *buffer, std::streamsize count, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm, const UPstream::sendModes sendMode=UPstream::sendModes::normal)
Write buffer contents (contiguous types only) to given processor.
bool send()
Send buffer contents now and not in destructor [advanced usage]. Returns true on success.
Definition OPstreams.C:84
An opaque wrapper for MPI_Request with a vendor-independent representation without any <mpi....
Definition UPstream.H:2919
Wrapper for internally indexed communicator label. Always invokes UPstream::allocateCommunicatorCompo...
Definition UPstream.H:2546
commsTypes
Communications types.
Definition UPstream.H:81
static int & msgType() noexcept
Message tag of standard messages.
Definition UPstream.H:1926
sendModes
Different MPI-send modes (ignored for commsTypes::buffered).
Definition UPstream.H:98
@ normal
(MPI_Send, MPI_Isend)
Definition UPstream.H:99
commsTypes commsType() const noexcept
Get the communications type of the stream.
Definition UPstream.H:1958
static label worldComm
Communicator for all ranks. May differ from commGlobal() if local worlds are in use.
Definition UPstream.H:1069
UPstream(const commsTypes commsType) noexcept
Construct for given communication type.
Definition UPstream.H:1184
A token holds an item read from Istream.
Definition token.H:70
A class for handling words, derived from Foam::string.
Definition word.H:66
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
runTime write()