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 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::PstreamBuffers
28 
29 Description
30  Buffers for inter-processor communications streams (UOPstream, UIPstream).
31 
32  Use UOPstream to stream data into buffers, call finishedSends() to
33  notify that data is in buffers and then use IUPstream to get data out
34  of received buffers. Works with both blocking and nonBlocking. Does
35  not make much sense with scheduled since there you would not need these
36  explicit buffers.
37 
38  Example usage:
39  \code
40  PstreamBuffers pBufs(Pstream::commsTypes::nonBlocking);
41 
42  for (const int proci : Pstream::allProcs())
43  {
44  if (proci != Pstream::myProcNo())
45  {
46  someObject vals;
47 
48  UOPstream send(proci, pBufs);
49  send << vals;
50  }
51  }
52 
53  pBufs.finishedSends(); // no-op for blocking
54 
55  for (const int proci : Pstream::allProcs())
56  {
57  if (proci != Pstream::myProcNo())
58  {
59  UIPstream recv(proci, pBufs);
60  someObject vals(recv);
61  }
62  }
63  \endcode
64 
65 SourceFiles
66  PstreamBuffers.C
67 
68 \*---------------------------------------------------------------------------*/
69 
70 #include "Pstream.H"
71 
72 #ifndef PstreamBuffers_H
73 #define PstreamBuffers_H
74 
75 #include "DynamicList.H"
76 #include "UPstream.H"
77 #include "IOstream.H"
78 
79 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
80 
81 namespace Foam
82 {
83 
84 /*---------------------------------------------------------------------------*\
85  Class PstreamBuffers Declaration
86 \*---------------------------------------------------------------------------*/
87 
88 class PstreamBuffers
89 {
90  friend class UOPstream;
91  friend class UIPstream;
92 
93  // Private data
94 
95  //- Communications type of this stream
96  const UPstream::commsTypes commsType_;
97 
98  const int tag_;
99 
100  const label comm_;
101 
102  const IOstream::streamFormat format_;
103 
104  const IOstream::versionNumber version_;
105 
106  //- Send buffer
107  List<DynamicList<char>> sendBuf_;
108 
109  //- Receive buffer
110  List<DynamicList<char>> recvBuf_;
111 
112  //- Read position in recvBuf_
113  labelList recvBufPos_;
114 
115  bool finishedSendsCalled_;
116 
117 public:
118 
119  // Static data
120 
121  static DynamicList<char> nullBuf;
122 
123 
124  // Constructors
125 
126  //- Construct given comms type,
127  // write format and IO version
129  (
130  const UPstream::commsTypes commsType,
131  const int tag = UPstream::msgType(),
132  const label comm = UPstream::worldComm,
135  );
136 
137  //- Destructor
138  ~PstreamBuffers();
139 
140 
141  // Member functions
142 
143  int tag() const
144  {
145  return tag_;
146  }
147 
148  label comm() const
149  {
150  return comm_;
151  }
152 
153  //- Mark all sends as having been done. This will start receives
154  // in non-blocking mode. If block will wait for all transfers to
155  // finish (only relevant for nonBlocking mode)
156  void finishedSends(const bool block = true);
157 
158  //- Mark all sends as having been done. Same as above but also returns
159  // sizes (bytes) received. Note:currently only valid for
160  // non-blocking.
161  void finishedSends(labelList& recvSizes, const bool block = true);
162 
163  //- Clear storage and reset
164  void clear();
165 
166 };
167 
168 
169 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
170 
171 } // End namespace Foam
172 
173 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
174 
175 #endif
176 
177 // ************************************************************************* //
Foam::PstreamBuffers::tag
int tag() const
Definition: PstreamBuffers.H:142
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.
Definition: PstreamBuffers.C:60
UPstream.H
Foam::DynamicList< char >
Foam::PstreamBuffers
Buffers for inter-processor communications streams (UOPstream, UIPstream).
Definition: PstreamBuffers.H:87
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:168
Foam::PstreamBuffers::clear
void clear()
Clear storage and reset.
Definition: PstreamBuffers.C:130
format
word format(conversionProperties.get< word >("format"))
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
IOstream.H
Foam::PstreamBuffers::finishedSends
void finishedSends(const bool block=true)
Mark all sends as having been done. This will start receives.
Definition: PstreamBuffers.C:80
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
Pstream.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::foamVersion::version
const std::string version
OpenFOAM version (name or stringified number) as a std::string.
Foam::PstreamBuffers::PstreamBuffers
PstreamBuffers(const UPstream::commsTypes commsType, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm, IOstream::streamFormat format=IOstream::BINARY, IOstream::versionNumber version=IOstream::currentVersion)
Construct given comms type,.
Definition: PstreamBuffers.C:38
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:73
Foam::UPstream::msgType
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:541
Foam::UPstream::commsTypes
commsTypes
Types of communications.
Definition: UPstream.H:69
Foam::UPstream::worldComm
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:295
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
DynamicList.H
Foam::UIPstream
Input inter-processor communications stream operating on external buffer.
Definition: UIPstream.H:56
Foam::PstreamBuffers::nullBuf
static DynamicList< char > nullBuf
Definition: PstreamBuffers.H:120
Foam::PstreamBuffers::comm
label comm() const
Definition: PstreamBuffers.H:147