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

gatherScatter.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     Gather data from all processors onto single processor according to some
00027     communication schedule (usually linear-to-master or tree-to-master).
00028     The gathered data will be a single value constructed from the values
00029     on individual processors using a user-specified operator.
00030 
00031 \*---------------------------------------------------------------------------*/
00032 
00033 #include "OPstream.H"
00034 #include "IPstream.H"
00035 #include "contiguous.H"
00036 
00037 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00038 
00039 namespace Foam
00040 {
00041 
00042 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00043 
00044 template <class T, class BinaryOp>
00045 void Pstream::gather
00046 (
00047     const List<Pstream::commsStruct>& comms,
00048     T& Value,
00049     const BinaryOp& bop
00050 )
00051 {
00052     if (Pstream::parRun())
00053     {
00054         // Get my communication order
00055         const commsStruct& myComm = comms[Pstream::myProcNo()];
00056 
00057         // Receive from my downstairs neighbours
00058         forAll(myComm.below(), belowI)
00059         {
00060             T value;
00061 
00062             if (contiguous<T>())
00063             {
00064                 IPstream::read
00065                 (
00066                     Pstream::scheduled,
00067                     myComm.below()[belowI],
00068                     reinterpret_cast<char*>(&value),
00069                     sizeof(T)
00070                 );
00071             }
00072             else
00073             {
00074                 IPstream fromBelow(Pstream::scheduled, myComm.below()[belowI]);
00075                 fromBelow >> value;
00076             }
00077 
00078             Value = bop(Value, value);
00079         }
00080 
00081         // Send up Value
00082         if (myComm.above() != -1)
00083         {
00084             if (contiguous<T>())
00085             {
00086                 OPstream::write
00087                 (
00088                     Pstream::scheduled,
00089                     myComm.above(),
00090                     reinterpret_cast<const char*>(&Value),
00091                     sizeof(T)
00092                 );
00093             }
00094             else
00095             {
00096                 OPstream toAbove(Pstream::scheduled, myComm.above());
00097                 toAbove << Value;
00098             }
00099         }
00100     }
00101 }
00102 
00103 
00104 template <class T, class BinaryOp>
00105 void Pstream::gather(T& Value, const BinaryOp& bop)
00106 {
00107     if (Pstream::nProcs() < Pstream::nProcsSimpleSum)
00108     {
00109         gather(Pstream::linearCommunication(), Value, bop);
00110     }
00111     else
00112     {
00113         gather(Pstream::treeCommunication(), Value, bop);
00114     }
00115 }
00116 
00117 
00118 template <class T>
00119 void Pstream::scatter(const List<Pstream::commsStruct>& comms, T& Value)
00120 {
00121     if (Pstream::parRun())
00122     {
00123         // Get my communication order
00124         const commsStruct& myComm = comms[Pstream::myProcNo()];
00125 
00126         // Reveive from up
00127         if (myComm.above() != -1)
00128         {
00129             if (contiguous<T>())
00130             {
00131                 IPstream::read
00132                 (
00133                     Pstream::scheduled,
00134                     myComm.above(),
00135                     reinterpret_cast<char*>(&Value),
00136                     sizeof(T)
00137                 );
00138             }
00139             else
00140             {
00141                 IPstream fromAbove(Pstream::scheduled, myComm.above());
00142                 fromAbove >> Value;
00143             }
00144         }
00145 
00146         // Send to my downstairs neighbours
00147         forAll(myComm.below(), belowI)
00148         {
00149             if (contiguous<T>())
00150             {
00151                 OPstream::write
00152                 (
00153                     Pstream::scheduled,
00154                     myComm.below()[belowI],
00155                     reinterpret_cast<const char*>(&Value),
00156                     sizeof(T)
00157                 );
00158             }
00159             else
00160             {
00161                 OPstream toBelow(Pstream::scheduled,myComm.below()[belowI]);
00162                 toBelow << Value;
00163             }
00164         }
00165     }
00166 }
00167 
00168 
00169 template <class T>
00170 void Pstream::scatter(T& Value)
00171 {
00172     if (Pstream::nProcs() < Pstream::nProcsSimpleSum)
00173     {
00174         scatter(Pstream::linearCommunication(), Value);
00175     }
00176     else
00177     {
00178         scatter(Pstream::treeCommunication(), Value);
00179     }
00180 }
00181 
00182 
00183 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00184 
00185 } // End namespace Foam
00186 
00187 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd