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

combineGatherScatter.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     Variant of gather, scatter.
00027     Normal gather uses:
00028     - construct null and read (>>) from Istream
00029     - binary operator and assignment operator to combine values
00030 
00031     combineGather uses:
00032     - construct from Istream
00033     - modify operator which modifies its lhs
00034 
00035 \*---------------------------------------------------------------------------*/
00036 
00037 #include "OPstream.H"
00038 #include "IPstream.H"
00039 #include "IOstreams.H"
00040 #include "contiguous.H"
00041 
00042 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00043 
00044 namespace Foam
00045 {
00046 
00047 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00048 
00049 template <class T, class CombineOp>
00050 void Pstream::combineGather
00051 (
00052     const List<Pstream::commsStruct>& comms,
00053     T& Value,
00054     const CombineOp& cop
00055 )
00056 {
00057     if (Pstream::parRun())
00058     {
00059         // Get my communication order
00060         const commsStruct& myComm = comms[Pstream::myProcNo()];
00061 
00062         // Receive from my downstairs neighbours
00063         forAll(myComm.below(), belowI)
00064         {
00065             label belowID = myComm.below()[belowI];
00066 
00067             if (contiguous<T>())
00068             {
00069                 T value;
00070                 IPstream::read
00071                 (
00072                     Pstream::scheduled,
00073                     belowID,
00074                     reinterpret_cast<char*>(&value),
00075                     sizeof(T)
00076                 );
00077 
00078                 if (debug & 2)
00079                 {
00080                     Pout<< " received from "
00081                         << belowID << " data:" << value << endl;
00082                 }
00083 
00084                 cop(Value, value);
00085             }
00086             else
00087             {
00088                 IPstream fromBelow(Pstream::scheduled, belowID);
00089                 T value(fromBelow);
00090 
00091                 if (debug & 2)
00092                 {
00093                     Pout<< " received from "
00094                         << belowID << " data:" << value << endl;
00095                 }
00096 
00097                 cop(Value, value);
00098             }
00099         }
00100 
00101         // Send up Value
00102         if (myComm.above() != -1)
00103         {
00104             if (debug & 2)
00105             {
00106                 Pout<< " sending to " << myComm.above()
00107                     << " data:" << Value << endl;
00108             }
00109 
00110             if (contiguous<T>())
00111             {
00112                 OPstream::write
00113                 (
00114                     Pstream::scheduled,
00115                     myComm.above(),
00116                     reinterpret_cast<const char*>(&Value),
00117                     sizeof(T)
00118                 );
00119             }
00120             else
00121             {
00122                 OPstream toAbove(Pstream::scheduled, myComm.above());
00123                 toAbove << Value;
00124             }
00125         }
00126     }
00127 }
00128 
00129 
00130 template <class T, class CombineOp>
00131 void Pstream::combineGather(T& Value, const CombineOp& cop)
00132 {
00133     if (Pstream::nProcs() < Pstream::nProcsSimpleSum)
00134     {
00135         combineGather(Pstream::linearCommunication(), Value, cop);
00136     }
00137     else
00138     {
00139         combineGather(Pstream::treeCommunication(), Value, cop);
00140     }
00141 }
00142 
00143 
00144 template <class T>
00145 void Pstream::combineScatter(const List<Pstream::commsStruct>& comms, T& Value)
00146 {
00147     if (Pstream::parRun())
00148     {
00149         // Get my communication order
00150         const Pstream::commsStruct& myComm = comms[Pstream::myProcNo()];
00151 
00152         // Reveive from up
00153         if (myComm.above() != -1)
00154         {
00155             if (contiguous<T>())
00156             {
00157                 IPstream::read
00158                 (
00159                     Pstream::scheduled,
00160                     myComm.above(),
00161                     reinterpret_cast<char*>(&Value),
00162                     sizeof(T)
00163                 );
00164             }
00165             else
00166             {
00167                 IPstream fromAbove(Pstream::scheduled, myComm.above());
00168                 Value = T(fromAbove);
00169             }
00170 
00171             if (debug & 2)
00172             {
00173                 Pout<< " received from "
00174                     << myComm.above() << " data:" << Value << endl;
00175             }
00176         }
00177 
00178         // Send to my downstairs neighbours
00179         forAll(myComm.below(), belowI)
00180         {
00181             label belowID = myComm.below()[belowI];
00182 
00183             if (debug & 2)
00184             {
00185                 Pout<< " sending to " << belowID << " data:" << Value << endl;
00186             }
00187 
00188             if (contiguous<T>())
00189             {
00190                 OPstream::write
00191                 (
00192                     Pstream::scheduled,
00193                     belowID,
00194                     reinterpret_cast<const char*>(&Value),
00195                     sizeof(T)
00196                 );
00197             }
00198             else
00199             {
00200                 OPstream toBelow(Pstream::scheduled, belowID);
00201                 toBelow << Value;
00202             }
00203         }
00204     }
00205 }
00206 
00207 
00208 template <class T>
00209 void Pstream::combineScatter(T& Value)
00210 {
00211     if (Pstream::nProcs() < Pstream::nProcsSimpleSum)
00212     {
00213         combineScatter(Pstream::linearCommunication(), Value);
00214     }
00215     else
00216     {
00217         combineScatter(Pstream::treeCommunication(), Value);
00218     }
00219 }
00220 
00221 
00222 // Same thing but for whole list at a time
00223 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00224 
00225 
00226 template <class T, class CombineOp>
00227 void Pstream::listCombineGather
00228 (
00229     const List<Pstream::commsStruct>& comms,
00230     List<T>& Values,
00231     const CombineOp& cop
00232 )
00233 {
00234     if (Pstream::parRun())
00235     {
00236         // Get my communication order
00237         const commsStruct& myComm = comms[Pstream::myProcNo()];
00238 
00239         // Receive from my downstairs neighbours
00240         forAll(myComm.below(), belowI)
00241         {
00242             label belowID = myComm.below()[belowI];
00243 
00244             if (contiguous<T>())
00245             {
00246                 List<T> receivedValues(Values.size());
00247 
00248                 IPstream::read
00249                 (
00250                     Pstream::scheduled,
00251                     belowID,
00252                     reinterpret_cast<char*>(receivedValues.begin()),
00253                     receivedValues.byteSize()
00254                 );
00255 
00256                 if (debug & 2)
00257                 {
00258                     Pout<< " received from "
00259                         << belowID << " data:" << receivedValues << endl;
00260                 }
00261 
00262                 forAll(Values, i)
00263                 {
00264                     cop(Values[i], receivedValues[i]);
00265                 }
00266             }
00267             else
00268             {
00269                 IPstream fromBelow(Pstream::scheduled, belowID);
00270                 List<T> receivedValues(fromBelow);
00271 
00272                 if (debug & 2)
00273                 {
00274                     Pout<< " received from "
00275                         << belowID << " data:" << receivedValues << endl;
00276                 }
00277 
00278                 forAll(Values, i)
00279                 {
00280                     cop(Values[i], receivedValues[i]);
00281                 }
00282             }
00283         }
00284 
00285         // Send up Value
00286         if (myComm.above() != -1)
00287         {
00288             if (debug & 2)
00289             {
00290                 Pout<< " sending to " << myComm.above()
00291                     << " data:" << Values << endl;
00292             }
00293 
00294             if (contiguous<T>())
00295             {
00296                 OPstream::write
00297                 (
00298                     Pstream::scheduled,
00299                     myComm.above(),
00300                     reinterpret_cast<const char*>(Values.begin()),
00301                     Values.byteSize()
00302                 );
00303             }
00304             else
00305             {
00306                 OPstream toAbove(Pstream::scheduled, myComm.above());
00307                 toAbove << Values;
00308             }
00309         }
00310     }
00311 }
00312 
00313 
00314 template <class T, class CombineOp>
00315 void Pstream::listCombineGather(List<T>& Values, const CombineOp& cop)
00316 {
00317     if (Pstream::nProcs() < Pstream::nProcsSimpleSum)
00318     {
00319         listCombineGather(Pstream::linearCommunication(), Values, cop);
00320     }
00321     else
00322     {
00323         listCombineGather(Pstream::treeCommunication(), Values, cop);
00324     }
00325 }
00326 
00327 
00328 template <class T>
00329 void Pstream::listCombineScatter
00330 (
00331     const List<Pstream::commsStruct>& comms,
00332     List<T>& Values
00333 )
00334 {
00335     if (Pstream::parRun())
00336     {
00337         // Get my communication order
00338         const Pstream::commsStruct& myComm = comms[Pstream::myProcNo()];
00339 
00340         // Reveive from up
00341         if (myComm.above() != -1)
00342         {
00343             if (contiguous<T>())
00344             {
00345                 IPstream::read
00346                 (
00347                     Pstream::scheduled,
00348                     myComm.above(),
00349                     reinterpret_cast<char*>(Values.begin()),
00350                     Values.byteSize()
00351                 );
00352             }
00353             else
00354             {
00355                 IPstream fromAbove(Pstream::scheduled, myComm.above());
00356                 fromAbove >> Values;
00357             }
00358 
00359             if (debug & 2)
00360             {
00361                 Pout<< " received from "
00362                     << myComm.above() << " data:" << Values << endl;
00363             }
00364         }
00365 
00366         // Send to my downstairs neighbours
00367         forAll(myComm.below(), belowI)
00368         {
00369             label belowID = myComm.below()[belowI];
00370 
00371             if (debug & 2)
00372             {
00373                 Pout<< " sending to " << belowID << " data:" << Values << endl;
00374             }
00375 
00376             if (contiguous<T>())
00377             {
00378                 OPstream::write
00379                 (
00380                     Pstream::scheduled,
00381                     belowID,
00382                     reinterpret_cast<const char*>(Values.begin()),
00383                     Values.byteSize()
00384                 );
00385             }
00386             else
00387             {
00388                 OPstream toBelow(Pstream::scheduled, belowID);
00389                 toBelow << Values;
00390             }
00391         }
00392     }
00393 }
00394 
00395 
00396 template <class T>
00397 void Pstream::listCombineScatter(List<T>& Values)
00398 {
00399     if (Pstream::nProcs() < Pstream::nProcsSimpleSum)
00400     {
00401         listCombineScatter(Pstream::linearCommunication(), Values);
00402     }
00403     else
00404     {
00405         listCombineScatter(Pstream::treeCommunication(), Values);
00406     }
00407 }
00408 
00409 
00410 
00411 
00412 // Same thing but for sparse list (map)
00413 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00414 
00415 
00416 template <class Container, class CombineOp>
00417 void Pstream::mapCombineGather
00418 (
00419     const List<Pstream::commsStruct>& comms,
00420     Container& Values,
00421     const CombineOp& cop
00422 )
00423 {
00424     if (Pstream::parRun())
00425     {
00426         // Get my communication order
00427         const commsStruct& myComm = comms[Pstream::myProcNo()];
00428 
00429         // Receive from my downstairs neighbours
00430         forAll(myComm.below(), belowI)
00431         {
00432             label belowID = myComm.below()[belowI];
00433 
00434             IPstream fromBelow(Pstream::scheduled, belowID);
00435             Container receivedValues(fromBelow);
00436 
00437             if (debug & 2)
00438             {
00439                 Pout<< " received from "
00440                     << belowID << " data:" << receivedValues << endl;
00441             }
00442 
00443             for
00444             (
00445                 typename Container::const_iterator slaveIter =
00446                     receivedValues.begin();
00447                 slaveIter != receivedValues.end();
00448                 ++slaveIter
00449             )
00450             {
00451                 typename Container::iterator
00452                     masterIter = Values.find(slaveIter.key());
00453 
00454                 if (masterIter != Values.end())
00455                 {
00456                     cop(masterIter(), slaveIter());
00457                 }
00458                 else
00459                 {
00460                     Values.insert(slaveIter.key(), slaveIter());
00461                 }
00462             }
00463         }
00464 
00465         // Send up Value
00466         if (myComm.above() != -1)
00467         {
00468             if (debug & 2)
00469             {
00470                 Pout<< " sending to " << myComm.above()
00471                     << " data:" << Values << endl;
00472             }
00473 
00474             OPstream toAbove(Pstream::scheduled, myComm.above());
00475             toAbove << Values;
00476         }
00477     }
00478 }
00479 
00480 
00481 template <class Container, class CombineOp>
00482 void Pstream::mapCombineGather(Container& Values, const CombineOp& cop)
00483 {
00484     if (Pstream::nProcs() < Pstream::nProcsSimpleSum)
00485     {
00486         mapCombineGather(Pstream::linearCommunication(), Values, cop);
00487     }
00488     else
00489     {
00490         mapCombineGather(Pstream::treeCommunication(), Values, cop);
00491     }
00492 }
00493 
00494 
00495 template <class Container>
00496 void Pstream::mapCombineScatter
00497 (
00498     const List<Pstream::commsStruct>& comms,
00499     Container& Values
00500 )
00501 {
00502     if (Pstream::parRun())
00503     {
00504         // Get my communication order
00505         const Pstream::commsStruct& myComm = comms[Pstream::myProcNo()];
00506 
00507         // Reveive from up
00508         if (myComm.above() != -1)
00509         {
00510             IPstream fromAbove(Pstream::scheduled, myComm.above());
00511             fromAbove >> Values;
00512 
00513             if (debug & 2)
00514             {
00515                 Pout<< " received from "
00516                     << myComm.above() << " data:" << Values << endl;
00517             }
00518         }
00519 
00520         // Send to my downstairs neighbours
00521         forAll(myComm.below(), belowI)
00522         {
00523             label belowID = myComm.below()[belowI];
00524 
00525             if (debug & 2)
00526             {
00527                 Pout<< " sending to " << belowID << " data:" << Values << endl;
00528             }
00529 
00530             OPstream toBelow(Pstream::scheduled, belowID);
00531             toBelow << Values;
00532         }
00533     }
00534 }
00535 
00536 
00537 template <class Container>
00538 void Pstream::mapCombineScatter(Container& Values)
00539 {
00540     if (Pstream::nProcs() < Pstream::nProcsSimpleSum)
00541     {
00542         mapCombineScatter(Pstream::linearCommunication(), Values);
00543     }
00544     else
00545     {
00546         mapCombineScatter(Pstream::treeCommunication(), Values);
00547     }
00548 }
00549 
00550 
00551 
00552 
00553 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00554 
00555 } // End namespace Foam
00556 
00557 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd