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 (label proci = 0; proci < Pstream::nProcs(); ++proci)
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 (label proci = 0; proci < Pstream::nProcs(); ++proci)
56  {
57  if (proci != Pstream::myProcNo())
58  {
59  UIPstream recv(proci, pBufs);
60  someObject vals(recv);
61  }
62  }
63  \endcode
64 
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  const int tag_;
100 
101  const label comm_;
102 
103  const IOstream::streamFormat format_;
104 
105  const IOstream::versionNumber version_;
106 
107  //- Send buffer
108  List<DynamicList<char>> sendBuf_;
109 
110  //- Receive buffer
111  List<DynamicList<char>> recvBuf_;
112 
113  //- Read position in recvBuf_
114  labelList recvBufPos_;
115 
116  bool finishedSendsCalled_;
117 
118 public:
119 
120  // Static data
121 
122  static DynamicList<char> nullBuf;
123 
124 
125  // Constructors
126 
127  //- Construct given comms type,
128  // write format and IO version
130  (
131  const UPstream::commsTypes commsType,
132  const int tag = UPstream::msgType(),
133  const label comm = UPstream::worldComm,
136  );
137 
138  //- Destructor
139  ~PstreamBuffers();
140 
141 
142  // Member functions
143 
144  int tag() const
145  {
146  return tag_;
147  }
148 
149  label comm() const
150  {
151  return comm_;
152  }
153 
154  //- Mark all sends as having been done. This will start receives
155  // in non-blocking mode. If block will wait for all transfers to
156  // finish (only relevant for nonBlocking mode)
157  void finishedSends(const bool block = true);
158 
159  //- Mark all sends as having been done. Same as above but also returns
160  // sizes (bytes) received. Note:currently only valid for
161  // non-blocking.
162  void finishedSends(labelList& recvSizes, const bool block = true);
163 
164  //- Clear storage and reset
165  void clear();
166 
167 };
168 
169 
170 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
171 
172 } // End namespace Foam
173 
174 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
175 
176 #endif
177 
178 // ************************************************************************* //
Foam::PstreamBuffers::tag
int tag() const
Definition: PstreamBuffers.H:143
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:88
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:492
Foam::UPstream::commsTypes
commsTypes
Types of communications.
Definition: UPstream.H:66
Foam::UPstream::worldComm
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:285
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
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:121
Foam::PstreamBuffers::comm
label comm() const
Definition: PstreamBuffers.H:148