OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

IPread.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026     Read token and binary block from IPstream
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "mpi.h"
00031 
00032 #include "IPstream.H"
00033 
00034 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00035 
00036 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00037 
00038 // Outstanding non-blocking operations.
00040 Foam::DynamicList<MPI_Request> IPstream_outstandingRequests_;
00042 
00043 // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
00044 
00045 Foam::IPstream::IPstream
00046 (
00047     const commsTypes commsType,
00048     const int fromProcNo,
00049     const label bufSize,
00050     streamFormat format,
00051     versionNumber version
00052 )
00053 :
00054     Pstream(commsType, bufSize),
00055     Istream(format, version),
00056     fromProcNo_(fromProcNo),
00057     messageSize_(0)
00058 {
00059     setOpened();
00060     setGood();
00061 
00062     MPI_Status status;
00063 
00064     // If the buffer size is not specified, probe the incomming message
00065     // and set it
00066     if (!bufSize)
00067     {
00068         MPI_Probe(procID(fromProcNo_), msgType(), MPI_COMM_WORLD, &status);
00069         MPI_Get_count(&status, MPI_BYTE, &messageSize_);
00070 
00071         buf_.setSize(messageSize_);
00072     }
00073 
00074     messageSize_ = read(commsType, fromProcNo_, buf_.begin(), buf_.size());
00075 
00076     if (!messageSize_)
00077     {
00078         FatalErrorIn
00079         (
00080             "IPstream::IPstream(const int fromProcNo, "
00081             "const label bufSize, streamFormat format, versionNumber version)"
00082         )   << "read failed"
00083             << Foam::abort(FatalError);
00084     }
00085 }
00086 
00087 
00088 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00089 
00090 Foam::label Foam::IPstream::read
00091 (
00092     const commsTypes commsType,
00093     const int fromProcNo,
00094     char* buf,
00095     const std::streamsize bufSize
00096 )
00097 {
00098     if (commsType == blocking || commsType == scheduled)
00099     {
00100         MPI_Status status;
00101 
00102         if
00103         (
00104             MPI_Recv
00105             (
00106                 buf,
00107                 bufSize,
00108                 MPI_PACKED,
00109                 procID(fromProcNo),
00110                 msgType(),
00111                 MPI_COMM_WORLD,
00112                 &status
00113             )
00114         )
00115         {
00116             FatalErrorIn
00117             (
00118                 "IPstream::read"
00119                 "(const int fromProcNo, char* buf, std::streamsize bufSize)"
00120             )   << "MPI_Recv cannot receive incomming message"
00121                 << Foam::abort(FatalError);
00122 
00123             return 0;
00124         }
00125 
00126 
00127         // Check size of message read
00128 
00129         label messageSize;
00130         MPI_Get_count(&status, MPI_BYTE, &messageSize);
00131 
00132         if (messageSize > bufSize)
00133         {
00134             FatalErrorIn
00135             (
00136                 "IPstream::read"
00137                 "(const int fromProcNo, char* buf, std::streamsize bufSize)"
00138             )   << "buffer (" << label(bufSize)
00139                 << ") not large enough for incomming message ("
00140                 << messageSize << ')'
00141                 << Foam::abort(FatalError);
00142         }
00143 
00144         return messageSize;
00145     }
00146     else if (commsType == nonBlocking)
00147     {
00148         MPI_Request request;
00149 
00150         if
00151         (
00152             MPI_Irecv
00153             (
00154                 buf,
00155                 bufSize,
00156                 MPI_PACKED,
00157                 procID(fromProcNo),
00158                 msgType(),
00159                 MPI_COMM_WORLD,
00160                 &request
00161             )
00162         )
00163         {
00164             FatalErrorIn
00165             (
00166                 "IPstream::read"
00167                 "(const int fromProcNo, char* buf, std::streamsize bufSize)"
00168             )   << "MPI_Recv cannot start non-blocking receive"
00169                 << Foam::abort(FatalError);
00170 
00171             return 0;
00172         }
00173 
00174         IPstream_outstandingRequests_.append(request);
00175 
00176         return 1;
00177     }
00178     else
00179     {
00180         FatalErrorIn
00181         (
00182             "IPstream::read"
00183             "(const int fromProcNo, char* buf, std::streamsize bufSize)"
00184         )   << "Unsupported communications type " << commsType
00185             << Foam::abort(FatalError);
00186 
00187         return 0;
00188     }
00189 }
00190 
00191 
00192 void Foam::IPstream::waitRequests()
00193 {
00194     if (IPstream_outstandingRequests_.size())
00195     {
00196         if
00197         (
00198             MPI_Waitall
00199             (
00200                 IPstream_outstandingRequests_.size(),
00201                 IPstream_outstandingRequests_.begin(),
00202                 MPI_STATUSES_IGNORE
00203             )
00204         )
00205         {
00206             FatalErrorIn
00207             (
00208                 "IPstream::waitRequests()"
00209             )   << "MPI_Waitall returned with error" << endl;
00210         }
00211 
00212         IPstream_outstandingRequests_.clear();
00213     }
00214 }
00215 
00216 
00217 bool Foam::IPstream::finishedRequest(const label i)
00218 {
00219     if (i >= IPstream_outstandingRequests_.size())
00220     {
00221         FatalErrorIn
00222         (
00223             "IPstream::finishedRequest(const label)"
00224         )   << "There are " << IPstream_outstandingRequests_.size()
00225             << " outstanding send requests and you are asking for i=" << i
00226             << nl
00227             << "Maybe you are mixing blocking/non-blocking comms?"
00228             << Foam::abort(FatalError);
00229     }
00230 
00231     int flag;
00232     MPI_Test(&IPstream_outstandingRequests_[i], &flag, MPI_STATUS_IGNORE);
00233 
00234     return flag != 0;
00235 }
00236 
00237 
00238 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00239 
00240 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd