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

PstreamGlobals.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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "PstreamGlobals.H"
00028 #include "IOstreams.H"
00029 #include "Pstream.H"
00030 
00031 extern "C" {
00032 
00033 #include <linux/gamma/libgamma.h>
00034 
00035 }
00036 
00037 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00038 
00039 namespace Foam
00040 {
00041 
00042 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00043 
00044 // Receive buffers
00045 FixedList<List<List<char> >, 4> PstreamGlobals::recvBuf;
00046 
00047 // Length of receive buffers
00048 FixedList<labelList, 4> PstreamGlobals::recvBufLen;
00049 
00050 labelList PstreamGlobals::recvIndex;
00051 labelList PstreamGlobals::consumeIndex;
00052 
00053 // These are all signalling nans and probably different from the ones that
00054 // the fpu might ever generate.
00055 uint64_t PstreamGlobals::resizeMessage[PstreamGlobals::resizeMessageLen] =
00056 {
00057     0x7ff7ffffffffffABllu,
00058     0x7ff7ffffffffffCDllu,
00059     0x7ff7ffffffffff12llu,
00060     0x7ff7ffffffffff30llu,
00061     0x7ff7ffffffffff19llu,
00062     0x0000000000000000llu       // this word gets overwritten with the length.
00063 };
00064 
00065 
00066 // Wrapper around gamma_wait
00067 void PstreamGlobals::gammaWait(const label procNo)
00068 {
00069     // Last request. Block.
00070     gamma_wait(procNo, 1);
00071 
00072     // Currently unconsumed received message
00073     label ready = PstreamGlobals::consumeIndex[procNo];
00074 
00075     // Check received length
00076     if (PstreamGlobals::recvBufLen[ready][procNo] == -2)
00077     {
00078         // Was resize message. Consume and rewait (is always followed by
00079         // real message)
00080 
00081         if (Pstream::debug)
00082         {
00083             Pout<< "PstreamGlobals::gammaWait : "
00084                 << "Resize event. consumeIndex:" << ready
00085                 << " Restarting receive from " << procNo << endl;
00086         }
00087         // Consume resize message
00088         PstreamGlobals::recvBufLen[ready][procNo] = -1;
00089         PstreamGlobals::consumeIndex[procNo] =
00090             PstreamGlobals::recvBuf.fcIndex(ready);
00091         // And rewait
00092         gamma_wait(procNo, 1);
00093     }
00094 }
00095 
00096 
00097 // Copies data from global receive buffer into buf.
00098 label PstreamGlobals::copyReceive
00099 (
00100     const label procNo,
00101     char* buf,
00102     const label bufSize
00103 )
00104 {
00105     // Get the ready buffer
00106     label ready = consumeIndex[procNo];
00107 
00108     // Actually received
00109     label receivedLen = recvBufLen[ready][procNo];
00110 
00111     if (Pstream::debug)
00112     {
00113         Pout<< "copyReceive : for proc " << procNo
00114             << " copying " << receivedLen << " bytes out of buffer " << ready
00115             << endl;
00116     }
00117 
00118     if (receivedLen < 0)
00119     {
00120         FatalErrorIn
00121         (
00122             "Pstream::copyReceive(const label, char*, const label)"
00123         )   << "Illegal message length "
00124             << receivedLen
00125             << " received from proc " << procNo << " into buffer " << ready
00126             << endl
00127             << "This is probably caused by receiving more than is actually"
00128             << " sent (e.g. gather without scatter)." << endl
00129             << abort(FatalError);
00130     }
00131 
00132     if (receivedLen > bufSize)
00133     {
00134         FatalErrorIn
00135         (
00136             "Pstream::copyReceive(const label, char*, const label)"
00137         )   << "buffer ("
00138             << bufSize
00139             << ") not large enough for incomming message ("
00140             << receivedLen << ')'
00141             << " received from proc " << procNo << " into buffer " << ready
00142             << abort(FatalError);
00143     }
00144 
00145     // Copy out of receive buffer
00146     memcpy
00147     (
00148         buf,
00149         recvBuf[ready][procNo].begin(),
00150         receivedLen
00151     );
00152     // Release receive buffer
00153     recvBufLen[ready][procNo] = -1;
00154     // Go to next buffer to consume
00155     consumeIndex[procNo] = recvBuf.fcIndex(ready);
00156 
00157     return receivedLen;
00158 }
00159 
00160 
00161 // Checks whether an incoming message is a resize message. If not returns -1,
00162 // otherwise returns size read from header.
00163 label PstreamGlobals::getSizeFromHeader(const char* buf, const label len)
00164 {
00165     if (len != resizeMessageLen*sizeof(uint64_t))
00166     {
00167         return -1;
00168     }
00169 
00170     const uint64_t* dPtr = reinterpret_cast<const uint64_t*>(buf);
00171 
00172     // Check all but the last word
00173     for (label i = 0; i < resizeMessageLen-1; i++)
00174     {
00175         if (*dPtr++ != resizeMessage[i])
00176         {
00177             return -1;
00178         }
00179     }
00180 
00181     return *reinterpret_cast<const label*>(dPtr);
00182 }
00183 
00184 
00185 void PstreamGlobals::setResizeMessage(const label len)
00186 {
00187     reinterpret_cast<label&>(resizeMessage[resizeMessageLen-1]) = len;
00188 }
00189 
00190 
00191 label PstreamGlobals::getMaxBufSize(const int procNo)
00192 {
00193     label maxSz = 0;
00194 
00195     forAll(recvBuf, i)
00196     {
00197         maxSz = max(maxSz, recvBuf[i][procNo].size());
00198     }
00199     return maxSz;
00200 }
00201 
00202 
00203 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00204 
00205 } // End namespace Foam
00206 
00207 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd