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

OPwrite.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     Write primitive and binary block from OPstream
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "mpi.h"
00031 
00032 #include "OPstream.H"
00033 
00034 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00035 
00036 // Outstanding non-blocking operations.
00038 Foam::DynamicList<MPI_Request> OPstream_outstandingRequests_;
00040 
00041 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00042 
00043 Foam::OPstream::~OPstream()
00044 {
00045     if
00046     (
00047        !write
00048         (
00049             commsType_,
00050             toProcNo_,
00051             buf_.begin(),
00052             bufPosition_
00053         )
00054     )
00055     {
00056         FatalErrorIn("OPstream::~OPstream()")
00057             << "MPI_Bsend cannot send outgoing message"
00058             << Foam::abort(FatalError);
00059     }
00060 }
00061 
00062 
00063 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00064 
00065 bool Foam::OPstream::write
00066 (
00067     const commsTypes commsType,
00068     const int toProcNo,
00069     const char* buf,
00070     const std::streamsize bufSize
00071 )
00072 {
00073     bool transferFailed = true;
00074 
00075     if (commsType == blocking)
00076     {
00077         transferFailed = MPI_Bsend
00078         (
00079             const_cast<char*>(buf),
00080             bufSize,
00081             MPI_PACKED,
00082             procID(toProcNo),
00083             msgType(),
00084             MPI_COMM_WORLD
00085         );
00086     }
00087     else if (commsType == scheduled)
00088     {
00089         transferFailed = MPI_Send
00090         (
00091             const_cast<char*>(buf),
00092             bufSize,
00093             MPI_PACKED,
00094             procID(toProcNo),
00095             msgType(),
00096             MPI_COMM_WORLD
00097         );
00098     }
00099     else if (commsType == nonBlocking)
00100     {
00101         MPI_Request request;
00102 
00103         transferFailed = MPI_Isend
00104         (
00105             const_cast<char*>(buf),
00106             bufSize,
00107             MPI_PACKED,
00108             procID(toProcNo),
00109             msgType(),
00110             MPI_COMM_WORLD,
00111             &request
00112         );
00113 
00114         OPstream_outstandingRequests_.append(request);
00115     }
00116     else
00117     {
00118         FatalErrorIn
00119         (
00120             "OPstream::write"
00121             "(const int fromProcNo, char* buf, std::streamsize bufSize)"
00122         )   << "Unsupported communications type " << commsType
00123             << Foam::abort(FatalError);
00124     }
00125 
00126     return !transferFailed;
00127 }
00128 
00129 
00130 void Foam::OPstream::waitRequests()
00131 {
00132     if (OPstream_outstandingRequests_.size())
00133     {
00134         if
00135         (
00136             MPI_Waitall
00137             (
00138                 OPstream_outstandingRequests_.size(),
00139                 OPstream_outstandingRequests_.begin(),
00140                 MPI_STATUSES_IGNORE
00141             )
00142         )
00143         {
00144             FatalErrorIn
00145             (
00146                 "OPstream::waitRequests()"
00147             )   << "MPI_Waitall returned with error" << Foam::endl;
00148         }
00149 
00150         OPstream_outstandingRequests_.clear();
00151     }
00152 }
00153 
00154 
00155 bool Foam::OPstream::finishedRequest(const label i)
00156 {
00157     if (i >= OPstream_outstandingRequests_.size())
00158     {
00159         FatalErrorIn
00160         (
00161             "OPstream::finishedRequest(const label)"
00162         )   << "There are " << OPstream_outstandingRequests_.size()
00163             << " outstanding send requests and you are asking for i=" << i
00164             << nl
00165             << "Maybe you are mixing blocking/non-blocking comms?"
00166             << Foam::abort(FatalError);
00167     }
00168 
00169     int flag;
00170     MPI_Test(&OPstream_outstandingRequests_[i], &flag, MPI_STATUS_IGNORE);
00171 
00172     return flag != 0;
00173 }
00174 
00175 
00176 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00177 
00178 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd