1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
5 \\ / A nd | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
25 Read token and binary block from IPstream
27 \*---------------------------------------------------------------------------*/
32 #include "PstreamGlobals.H"
34 // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
36 Foam::IPstream::IPstream
38 const commsTypes commsType,
45 Pstream(commsType, bufSize),
46 Istream(format, version),
47 fromProcNo_(fromProcNo),
55 // If the buffer size is not specified, probe the incomming message
59 MPI_Probe(procID(fromProcNo_), msgType(), MPI_COMM_WORLD, &status);
60 MPI_Get_count(&status, MPI_BYTE, &messageSize_);
62 buf_.setSize(messageSize_);
65 messageSize_ = read(commsType, fromProcNo_, buf_.begin(), buf_.size());
71 "IPstream::IPstream(const int fromProcNo, "
72 "const label bufSize, streamFormat format, versionNumber version)"
74 << Foam::abort(FatalError);
79 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
81 Foam::label Foam::IPstream::read
83 const commsTypes commsType,
86 const std::streamsize bufSize
89 if (commsType == blocking || commsType == scheduled)
110 "(const int fromProcNo, char* buf, std::streamsize bufSize)"
111 ) << "MPI_Recv cannot receive incomming message"
112 << Foam::abort(FatalError);
118 // Check size of message read
121 MPI_Get_count(&status, MPI_BYTE, &messageSize);
123 if (messageSize > bufSize)
128 "(const int fromProcNo, char* buf, std::streamsize bufSize)"
129 ) << "buffer (" << label(bufSize)
130 << ") not large enough for incomming message ("
131 << messageSize << ')'
132 << Foam::abort(FatalError);
137 else if (commsType == nonBlocking)
158 "(const int fromProcNo, char* buf, std::streamsize bufSize)"
159 ) << "MPI_Recv cannot start non-blocking receive"
160 << Foam::abort(FatalError);
165 PstreamGlobals::IPstream_outstandingRequests_.append(request);
174 "(const int fromProcNo, char* buf, std::streamsize bufSize)"
175 ) << "Unsupported communications type " << commsType
176 << Foam::abort(FatalError);
183 void Foam::IPstream::waitRequests()
185 if (PstreamGlobals::IPstream_outstandingRequests_.size())
191 PstreamGlobals::IPstream_outstandingRequests_.size(),
192 PstreamGlobals::IPstream_outstandingRequests_.begin(),
199 "IPstream::waitRequests()"
200 ) << "MPI_Waitall returned with error" << endl;
203 PstreamGlobals::IPstream_outstandingRequests_.clear();
208 bool Foam::IPstream::finishedRequest(const label i)
210 if (i >= PstreamGlobals::IPstream_outstandingRequests_.size())
214 "IPstream::finishedRequest(const label)"
216 << PstreamGlobals::IPstream_outstandingRequests_.size()
217 << " outstanding send requests and you are asking for i=" << i
219 << "Maybe you are mixing blocking/non-blocking comms?"
220 << Foam::abort(FatalError);
226 &PstreamGlobals::IPstream_outstandingRequests_[i],
235 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
237 // ************************************************************************* //