PstreamBuffers.C
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 \*---------------------------------------------------------------------------*/
28 
29 #include "PstreamBuffers.H"
30 
31 // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
32 
34 (
35  const UPstream::commsTypes commsType,
36  const int tag,
37  const label comm,
39 )
40 :
41  commsType_(commsType),
42  tag_(tag),
43  comm_(comm),
44  format_(fmt),
45  sendBuf_(UPstream::nProcs(comm)),
46  recvBuf_(UPstream::nProcs(comm)),
47  recvBufPos_(UPstream::nProcs(comm), Zero),
48  finishedSendsCalled_(false)
49 {}
50 
51 
52 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
53 
55 {
56  // Check that all data has been consumed.
57  forAll(recvBufPos_, proci)
58  {
59  if (recvBufPos_[proci] < recvBuf_[proci].size())
60  {
62  << "Message from processor " << proci
63  << " Only consumed " << recvBufPos_[proci] << " of "
64  << recvBuf_[proci].size() << " bytes" << nl
66  }
67  }
68 }
69 
70 
71 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
72 
74 {
75  // Could also check that it is not called twice
76  finishedSendsCalled_ = true;
77 
78  if (commsType_ == UPstream::commsTypes::nonBlocking)
79  {
80  Pstream::exchange<DynamicList<char>, char>
81  (
82  sendBuf_,
83  recvBuf_,
84  tag_,
85  comm_,
86  block
87  );
88  }
89 }
90 
91 
93 {
94  // Could also check that it is not called twice
95  finishedSendsCalled_ = true;
96 
97  if (commsType_ == UPstream::commsTypes::nonBlocking)
98  {
99  Pstream::exchangeSizes(sendBuf_, recvSizes, comm_);
100 
101  Pstream::exchange<DynamicList<char>, char>
102  (
103  sendBuf_,
104  recvSizes,
105  recvBuf_,
106  tag_,
107  comm_,
108  block
109  );
110  }
111  else
112  {
114  << "Obtaining sizes not supported in "
115  << UPstream::commsTypeNames[commsType_] << endl
116  << " since transfers already in progress. Use non-blocking instead."
117  << exit(FatalError);
118 
119  // Note: maybe possible only if using different tag from write started
120  // by ~UOPstream. Needs some work.
121  }
122 }
123 
124 
126 {
127  for (DynamicList<char>& buf : sendBuf_)
128  {
129  buf.clear();
130  }
131  for (DynamicList<char>& buf : recvBuf_)
132  {
133  buf.clear();
134  }
135  recvBufPos_ = 0;
136  finishedSendsCalled_ = false;
137 }
138 
139 
140 // ************************************************************************* //
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::PstreamBuffers::~PstreamBuffers
~PstreamBuffers()
Destructor - checks that all data have been consumed.
Definition: PstreamBuffers.C:54
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::DynamicList< char >
Foam::Pstream::exchangeSizes
static void exchangeSizes(const Container &sendData, labelList &sizes, const label comm=UPstream::worldComm)
Helper: exchange sizes of sendData. sendData is the data per.
Definition: exchange.C:349
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::PstreamBuffers::clear
void clear()
Reset (clear) individual buffers and reset state.
Definition: PstreamBuffers.C:125
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::UPstream::commsTypeNames
static const Enum< commsTypes > commsTypeNames
Names of the communication types.
Definition: UPstream.H:77
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::FatalError
error FatalError
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
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
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::UPstream::commsTypes::nonBlocking
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::List< label >
PstreamBuffers.H