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

processorLduInterfaceTemplates.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 "processorLduInterface.H"
00028 #include "IPstream.H"
00029 #include "OPstream.H"
00030 
00031 // * * * * * * * * * * * * * * * Member Functions * * *  * * * * * * * * * * //
00032 
00033 template<class Type>
00034 void Foam::processorLduInterface::send
00035 (
00036     const Pstream::commsTypes commsType,
00037     const UList<Type>& f
00038 ) const
00039 {
00040     if (commsType == Pstream::blocking || commsType == Pstream::scheduled)
00041     {
00042         OPstream::write
00043         (
00044             commsType,
00045             neighbProcNo(),
00046             reinterpret_cast<const char*>(f.begin()),
00047             f.byteSize()
00048         );
00049     }
00050     else if (commsType == Pstream::nonBlocking)
00051     {
00052         resizeBuf(receiveBuf_, f.size()*sizeof(Type));
00053 
00054         IPstream::read
00055         (
00056             commsType,
00057             neighbProcNo(),
00058             receiveBuf_.begin(),
00059             receiveBuf_.size()
00060         );
00061 
00062         resizeBuf(sendBuf_, f.byteSize());
00063         memcpy(sendBuf_.begin(), f.begin(), f.byteSize());
00064 
00065         OPstream::write
00066         (
00067             commsType,
00068             neighbProcNo(),
00069             sendBuf_.begin(),
00070             f.byteSize()
00071         );
00072     }
00073     else
00074     {
00075         FatalErrorIn("processorLduInterface::send")
00076             << "Unsupported communications type " << commsType
00077             << exit(FatalError);
00078     }
00079 }
00080 
00081 
00082 template<class Type>
00083 void Foam::processorLduInterface::receive
00084 (
00085     const Pstream::commsTypes commsType,
00086     UList<Type>& f
00087 ) const
00088 {
00089     if (commsType == Pstream::blocking || commsType == Pstream::scheduled)
00090     {
00091         IPstream::read
00092         (
00093             commsType,
00094             neighbProcNo(),
00095             reinterpret_cast<char*>(f.begin()),
00096             f.byteSize()
00097         );
00098     }
00099     else if (commsType == Pstream::nonBlocking)
00100     {
00101         memcpy(f.begin(), receiveBuf_.begin(), f.byteSize());
00102     }
00103     else
00104     {
00105         FatalErrorIn("processorLduInterface::receive")
00106             << "Unsupported communications type " << commsType
00107             << exit(FatalError);
00108     }
00109 }
00110 
00111 
00112 template<class Type>
00113 Foam::tmp<Foam::Field<Type> > Foam::processorLduInterface::receive
00114 (
00115     const Pstream::commsTypes commsType,
00116     const label size
00117 ) const
00118 {
00119     tmp<Field<Type> > tf(new Field<Type>(size));
00120     receive(commsType, tf());
00121     return tf;
00122 }
00123 
00124 
00125 template<class Type>
00126 void Foam::processorLduInterface::compressedSend
00127 (
00128     const Pstream::commsTypes commsType,
00129     const UList<Type>& f
00130 ) const
00131 {
00132     if (sizeof(scalar) != sizeof(float) && Pstream::floatTransfer && f.size())
00133     {
00134         static const label nCmpts = sizeof(Type)/sizeof(scalar);
00135         label nm1 = (f.size() - 1)*nCmpts;
00136         label nlast = sizeof(Type)/sizeof(float);
00137         label nFloats = nm1 + nlast;
00138         label nBytes = nFloats*sizeof(float);
00139 
00140         const scalar *sArray = reinterpret_cast<const scalar*>(f.begin());
00141         const scalar *slast = &sArray[nm1];
00142         resizeBuf(sendBuf_, nBytes);
00143         float *fArray = reinterpret_cast<float*>(sendBuf_.begin());
00144 
00145         for (register label i=0; i<nm1; i++)
00146         {
00147             fArray[i] = sArray[i] - slast[i%nCmpts];
00148         }
00149 
00150         reinterpret_cast<Type&>(fArray[nm1]) = f[f.size() - 1];
00151 
00152         if (commsType == Pstream::blocking || commsType == Pstream::scheduled)
00153         {
00154             OPstream::write
00155             (
00156                 commsType,
00157                 neighbProcNo(),
00158                 sendBuf_.begin(),
00159                 nBytes
00160             );
00161         }
00162         else if (commsType == Pstream::nonBlocking)
00163         {
00164             resizeBuf(receiveBuf_, nBytes);
00165 
00166             IPstream::read
00167             (
00168                 commsType,
00169                 neighbProcNo(),
00170                 receiveBuf_.begin(),
00171                 receiveBuf_.size()
00172             );
00173 
00174             OPstream::write
00175             (
00176                 commsType,
00177                 neighbProcNo(),
00178                 sendBuf_.begin(),
00179                 nBytes
00180             );
00181         }
00182         else
00183         {
00184             FatalErrorIn("processorLduInterface::compressedSend")
00185                 << "Unsupported communications type " << commsType
00186                 << exit(FatalError);
00187         }
00188     }
00189     else
00190     {
00191         this->send(commsType, f);
00192     }
00193 }
00194 
00195 template<class Type>
00196 void Foam::processorLduInterface::compressedReceive
00197 (
00198     const Pstream::commsTypes commsType,
00199     UList<Type>& f
00200 ) const
00201 {
00202     if (sizeof(scalar) != sizeof(float) && Pstream::floatTransfer && f.size())
00203     {
00204         static const label nCmpts = sizeof(Type)/sizeof(scalar);
00205         label nm1 = (f.size() - 1)*nCmpts;
00206         label nlast = sizeof(Type)/sizeof(float);
00207         label nFloats = nm1 + nlast;
00208         label nBytes = nFloats*sizeof(float);
00209 
00210         if (commsType == Pstream::blocking || commsType == Pstream::scheduled)
00211         {
00212             resizeBuf(receiveBuf_, nBytes);
00213 
00214             IPstream::read
00215             (
00216                 commsType,
00217                 neighbProcNo(),
00218                 receiveBuf_.begin(),
00219                 nBytes
00220             );
00221         }
00222         else if (commsType != Pstream::nonBlocking)
00223         {
00224             FatalErrorIn("processorLduInterface::compressedReceive")
00225                 << "Unsupported communications type " << commsType
00226                 << exit(FatalError);
00227         }
00228 
00229         const float *fArray =
00230             reinterpret_cast<const float*>(receiveBuf_.begin());
00231         f[f.size() - 1] = reinterpret_cast<const Type&>(fArray[nm1]);
00232         scalar *sArray = reinterpret_cast<scalar*>(f.begin());
00233         const scalar *slast = &sArray[nm1];
00234 
00235         for (register label i=0; i<nm1; i++)
00236         {
00237             sArray[i] = fArray[i] + slast[i%nCmpts];
00238         }
00239     }
00240     else
00241     {
00242         this->receive<Type>(commsType, f);
00243     }
00244 }
00245 
00246 template<class Type>
00247 Foam::tmp<Foam::Field<Type> > Foam::processorLduInterface::compressedReceive
00248 (
00249     const Pstream::commsTypes commsType,
00250     const label size
00251 ) const
00252 {
00253     tmp<Field<Type> > tf(new Field<Type>(size));
00254     compressedReceive(commsType, tf());
00255     return tf;
00256 }
00257 
00258 
00259 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd