PstreamBuffers.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-2017 OpenFOAM Foundation
9  Copyright (C) 2021 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::PstreamBuffers
29 
30 Description
31  Buffers for inter-processor communications streams (UOPstream, UIPstream).
32 
33  Use UOPstream to stream data into buffers, call finishedSends() to
34  notify that data is in buffers and then use IUPstream to get data out
35  of received buffers. Works with both blocking and nonBlocking. Does
36  not make much sense with scheduled since there you would not need these
37  explicit buffers.
38 
39  Example usage:
40  \code
41  PstreamBuffers pBufs(Pstream::commsTypes::nonBlocking);
42 
43  for (const int proci : Pstream::allProcs())
44  {
45  if (proci != Pstream::myProcNo())
46  {
47  someObject vals;
48 
49  UOPstream send(proci, pBufs);
50  send << vals;
51  }
52  }
53 
54  pBufs.finishedSends(); // no-op for blocking
55 
56  for (const int proci : Pstream::allProcs())
57  {
58  if (proci != Pstream::myProcNo())
59  {
60  UIPstream recv(proci, pBufs);
61  someObject vals(recv);
62  }
63  }
64  \endcode
65 
66 SourceFiles
67  PstreamBuffers.C
68 
69 \*---------------------------------------------------------------------------*/
70 
71 #include "Pstream.H"
72 
73 #ifndef PstreamBuffers_H
74 #define PstreamBuffers_H
75 
76 #include "DynamicList.H"
77 #include "UPstream.H"
78 #include "IOstream.H"
79 
80 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
81 
82 namespace Foam
83 {
84 
85 /*---------------------------------------------------------------------------*\
86  Class PstreamBuffers Declaration
87 \*---------------------------------------------------------------------------*/
88 
89 class PstreamBuffers
90 {
91  friend class UOPstream;
92  friend class UIPstream;
93 
94  // Private Data
95 
96  //- Communications type of this stream
97  const UPstream::commsTypes commsType_;
98 
99  //- The transfer message type
100  const int tag_;
101 
102  //- Communicator
103  const label comm_;
104 
105  //- Buffer format (ascii | binary)
106  const IOstreamOption::streamFormat format_;
107 
108  //- Send buffer
109  List<DynamicList<char>> sendBuf_;
110 
111  //- Receive buffer
112  List<DynamicList<char>> recvBuf_;
113 
114  //- Current read positions within recvBuf_
115  labelList recvBufPos_;
116 
117  //- Track if sends are complete
118  bool finishedSendsCalled_;
119 
120 
121 public:
122 
123  // Constructors
124 
125  //- Construct given comms type, communication options, IO format
126  explicit PstreamBuffers
127  (
128  const UPstream::commsTypes commsType,
129  const int tag = UPstream::msgType(),
130  const label comm = UPstream::worldComm,
132  );
133 
134 
135  //- Destructor - checks that all data have been consumed
136  ~PstreamBuffers();
137 
138 
139  // Member Functions
140 
141  //- The transfer message type
142  int tag() const noexcept
143  {
144  return tag_;
145  }
146 
147  //- Communicator
148  label comm() const noexcept
149  {
150  return comm_;
151  }
152 
153  //- Mark all sends as having been done.
154  // This will start receives in non-blocking mode.
155  // If block will wait for all transfers to finish
156  // (only relevant for nonBlocking mode)
157  void finishedSends(const bool block = true);
158 
159  //- Mark all sends as having been done.
160  // Same as above but also returns sizes (bytes) received.
161  // \note currently only valid for non-blocking.
162  void finishedSends(labelList& recvSizes, const bool block = true);
163 
164  //- Reset (clear) individual buffers and reset state.
165  // Does not clear buffer storage
166  void clear();
167 };
168 
169 
170 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
171 
172 } // End namespace Foam
173 
174 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
175 
176 #endif
177 
178 // ************************************************************************* //
Foam::block
Creates a single block of cells from point coordinates, numbers of cells in each direction and an exp...
Definition: block.H:58
Foam::UOPstream
Output inter-processor communications stream operating on external buffer.
Definition: UOPstream.H:57
Foam::PstreamBuffers::~PstreamBuffers
~PstreamBuffers()
Destructor - checks that all data have been consumed.
Definition: PstreamBuffers.C:54
UPstream.H
Foam::PstreamBuffers
Buffers for inter-processor communications streams (UOPstream, UIPstream).
Definition: PstreamBuffers.H:88
Foam::PstreamBuffers::clear
void clear()
Reset (clear) individual buffers and reset state.
Definition: PstreamBuffers.C:125
IOstream.H
Foam::PstreamBuffers::finishedSends
void finishedSends(const bool block=true)
Mark all sends as having been done.
Definition: PstreamBuffers.C:73
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
Foam::PstreamBuffers::tag
int tag() const noexcept
The transfer message type.
Definition: PstreamBuffers.H:141
Pstream.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::UPstream::commsTypes
commsTypes
Types of communications.
Definition: UPstream.H:69
Foam::PstreamBuffers::PstreamBuffers
PstreamBuffers(const UPstream::commsTypes commsType, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm, IOstreamOption::streamFormat fmt=IOstreamOption::BINARY)
Construct given comms type, communication options, IO format.
Definition: PstreamBuffers.C:34
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:73
Foam::UPstream::msgType
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:540
Foam::UPstream::worldComm
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:293
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::PstreamBuffers::comm
label comm() const noexcept
Communicator.
Definition: PstreamBuffers.H:147
DynamicList.H
Foam::UIPstream
Input inter-processor communications stream operating on external buffer.
Definition: UIPstream.H:56