1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM 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 2 of the License, or (at your
14 option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 Read token and binary block from IPstream
28 \*---------------------------------------------------------------------------*/
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 // Outstanding non-blocking operations.
40 Foam::DynamicList<MPI_Request> IPstream_outstandingRequests_;
41 //! @endcond fileScope
43 // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
45 Foam::IPstream::IPstream
47 const commsTypes commsType,
54 Pstream(commsType, bufSize),
55 Istream(format, version),
56 fromProcNo_(fromProcNo),
64 // If the buffer size is not specified, probe the incomming message
68 MPI_Probe(procID(fromProcNo_), msgType(), MPI_COMM_WORLD, &status);
69 MPI_Get_count(&status, MPI_BYTE, &messageSize_);
71 buf_.setSize(messageSize_);
74 messageSize_ = read(commsType, fromProcNo_, buf_.begin(), buf_.size());
80 "IPstream::IPstream(const int fromProcNo, "
81 "const label bufSize, streamFormat format, versionNumber version)"
83 << Foam::abort(FatalError);
88 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
90 Foam::label Foam::IPstream::read
92 const commsTypes commsType,
95 const std::streamsize bufSize
98 if (commsType == blocking || commsType == scheduled)
119 "(const int fromProcNo, char* buf, std::streamsize bufSize)"
120 ) << "MPI_Recv cannot receive incomming message"
121 << Foam::abort(FatalError);
127 // Check size of message read
130 MPI_Get_count(&status, MPI_BYTE, &messageSize);
132 if (messageSize > bufSize)
137 "(const int fromProcNo, char* buf, std::streamsize bufSize)"
138 ) << "buffer (" << label(bufSize)
139 << ") not large enough for incomming message ("
140 << messageSize << ')'
141 << Foam::abort(FatalError);
146 else if (commsType == nonBlocking)
167 "(const int fromProcNo, char* buf, std::streamsize bufSize)"
168 ) << "MPI_Recv cannot start non-blocking receive"
169 << Foam::abort(FatalError);
174 IPstream_outstandingRequests_.append(request);
183 "(const int fromProcNo, char* buf, std::streamsize bufSize)"
184 ) << "Unsupported communications type " << commsType
185 << Foam::abort(FatalError);
192 void Foam::IPstream::waitRequests()
194 if (IPstream_outstandingRequests_.size())
200 IPstream_outstandingRequests_.size(),
201 IPstream_outstandingRequests_.begin(),
208 "IPstream::waitRequests()"
209 ) << "MPI_Waitall returned with error" << endl;
212 IPstream_outstandingRequests_.clear();
217 bool Foam::IPstream::finishedRequest(const label i)
219 if (i >= IPstream_outstandingRequests_.size())
223 "IPstream::finishedRequest(const label)"
224 ) << "There are " << IPstream_outstandingRequests_.size()
225 << " outstanding send requests and you are asking for i=" << i
227 << "Maybe you are mixing blocking/non-blocking comms?"
228 << Foam::abort(FatalError);
232 MPI_Test(&IPstream_outstandingRequests_[i], &flag, MPI_STATUS_IGNORE);
238 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
240 // ************************************************************************* //