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

gatherScatterList.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 list with element procID the data from processor
00029     procID. Before calling every processor should insert its value into
00030     Values[Pstream::myProcNo()].
00031     Note: after gather every processor only knows its own data and that of the
00032     processors below it. Only the 'master' of the communication schedule holds
00033     a fully filled List. Use scatter to distribute the data.
00034 
00035 \*---------------------------------------------------------------------------*/
00036 
00037 #include "IPstream.H"
00038 #include "OPstream.H"
00039 #include "contiguous.H"
00040 
00041 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00042 
00043 namespace Foam
00044 {
00045 
00046 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00047 
00048 template <class T>
00049 void Pstream::gatherList
00050 (
00051     const List<Pstream::commsStruct>& comms,
00052     List<T>& Values
00053 )
00054 {
00055     if (Pstream::parRun())
00056     {
00057         if (Values.size() != Pstream::nProcs())
00058         {
00059             FatalErrorIn
00060             (
00061                 "Pstream::gatherList(const List<Pstream::commsStruct>&"
00062                 ", List<T>)"
00063             )   << "Size of list:" << Values.size()
00064                 << " does not equal the number of processors:"
00065                 << Pstream::nProcs()
00066                 << Foam::abort(FatalError);
00067         }
00068 
00069         // Get my communication order
00070         const commsStruct& myComm = comms[Pstream::myProcNo()];
00071 
00072         // Receive from my downstairs neighbours
00073         forAll(myComm.below(), belowI)
00074         {
00075             label belowID = myComm.below()[belowI];
00076             const labelList& belowLeaves = comms[belowID].allBelow();
00077 
00078             if (contiguous<T>())
00079             {
00080                 List<T> receivedValues(belowLeaves.size() + 1);
00081 
00082                 IPstream::read
00083                 (
00084                     Pstream::scheduled,
00085                     belowID,
00086                     reinterpret_cast<char*>(receivedValues.begin()),
00087                     receivedValues.byteSize()
00088                 );
00089 
00090                 Values[belowID] = receivedValues[0];
00091 
00092                 forAll(belowLeaves, leafI)
00093                 {
00094                     Values[belowLeaves[leafI]] = receivedValues[leafI + 1];
00095                 }
00096             }
00097             else
00098             {
00099                 IPstream fromBelow(Pstream::scheduled, belowID);
00100                 fromBelow >> Values[belowID];
00101 
00102                 if (debug & 2)
00103                 {
00104                     Pout<< " received through "
00105                         << belowID << " data from:" << belowID
00106                         << " data:" << Values[belowID] << endl;
00107                 }
00108 
00109                 // Receive from all other processors below belowID
00110                 forAll(belowLeaves, leafI)
00111                 {
00112                     label leafID = belowLeaves[leafI];
00113                     fromBelow >> Values[leafID];
00114 
00115                     if (debug & 2)
00116                     {
00117                         Pout<< " received through "
00118                             << belowID << " data from:" << leafID
00119                             << " data:" << Values[leafID] << endl;
00120                     }
00121                 }
00122             }
00123         }
00124 
00125         // Send up from Values:
00126         // - my own value first
00127         // - all belowLeaves next
00128         if (myComm.above() != -1)
00129         {
00130             const labelList& belowLeaves = myComm.allBelow();
00131 
00132             if (debug & 2)
00133             {
00134                 Pout<< " sending to " << myComm.above()
00135                     << " data from me:" << Pstream::myProcNo()
00136                     << " data:" << Values[Pstream::myProcNo()] << endl;
00137             }
00138 
00139             if (contiguous<T>())
00140             {
00141                 List<T> sendingValues(belowLeaves.size() + 1);
00142                 sendingValues[0] = Values[Pstream::myProcNo()];
00143 
00144                 forAll(belowLeaves, leafI)
00145                 {
00146                     sendingValues[leafI + 1] = Values[belowLeaves[leafI]];
00147                 }
00148 
00149                 OPstream::write
00150                 (
00151                     Pstream::scheduled,
00152                     myComm.above(),
00153                     reinterpret_cast<const char*>(sendingValues.begin()),
00154                     sendingValues.byteSize()
00155                 );
00156             }
00157             else
00158             {
00159                 OPstream toAbove(Pstream::scheduled, myComm.above());
00160                 toAbove << Values[Pstream::myProcNo()];
00161 
00162                 forAll(belowLeaves, leafI)
00163                 {
00164                     label leafID = belowLeaves[leafI];
00165 
00166                     if (debug & 2)
00167                     {
00168                         Pout<< " sending to "
00169                             << myComm.above() << " data from:" << leafID
00170                             << " data:" << Values[leafID] << endl;
00171                     }
00172                     toAbove << Values[leafID];
00173                 }
00174             }
00175         }
00176     }
00177 }
00178 
00179 
00180 template <class T>
00181 void Pstream::gatherList(List<T>& Values)
00182 {
00183     if (Pstream::nProcs() < Pstream::nProcsSimpleSum)
00184     {
00185         gatherList(Pstream::linearCommunication(), Values);
00186     }
00187     else
00188     {
00189         gatherList(Pstream::treeCommunication(), Values);
00190     }
00191 }
00192 
00193 
00194 template <class T>
00195 void Pstream::scatterList
00196 (
00197     const List<Pstream::commsStruct>& comms,
00198     List<T>& Values
00199 )
00200 {
00201     if (Pstream::parRun())
00202     {
00203         if (Values.size() != Pstream::nProcs())
00204         {
00205             FatalErrorIn
00206             (
00207                 "Pstream::scatterList(const List<Pstream::commsStruct>&"
00208                 ", List<T>)"
00209             )   << "Size of list:" << Values.size()
00210                 << " does not equal the number of processors:"
00211                 << Pstream::nProcs()
00212                 << Foam::abort(FatalError);
00213         }
00214 
00215         // Get my communication order
00216         const commsStruct& myComm = comms[Pstream::myProcNo()];
00217 
00218         // Reveive from up
00219         if (myComm.above() != -1)
00220         {
00221             const labelList& notBelowLeaves = myComm.allNotBelow();
00222 
00223             if (contiguous<T>())
00224             {
00225                 List<T> receivedValues(notBelowLeaves.size());
00226 
00227                 IPstream::read
00228                 (
00229                     Pstream::scheduled,
00230                     myComm.above(),
00231                     reinterpret_cast<char*>(receivedValues.begin()),
00232                     receivedValues.byteSize()
00233                 );
00234 
00235                 forAll(notBelowLeaves, leafI)
00236                 {
00237                     Values[notBelowLeaves[leafI]] = receivedValues[leafI];
00238                 }
00239             }
00240             else
00241             {
00242                 IPstream fromAbove(Pstream::scheduled, myComm.above());
00243 
00244                 forAll(notBelowLeaves, leafI)
00245                 {
00246                     label leafID = notBelowLeaves[leafI];
00247                     fromAbove >> Values[leafID];
00248 
00249                     if (debug)
00250                     {
00251                         Pout<< " received through "
00252                             << myComm.above() << " data for:" << leafID
00253                             << " data:" << Values[leafID] << endl;
00254                     }
00255                 }
00256             }
00257         }
00258 
00259         // Send to my downstairs neighbours
00260         forAll(myComm.below(), belowI)
00261         {
00262             label belowID = myComm.below()[belowI];
00263             const labelList& notBelowLeaves = comms[belowID].allNotBelow();
00264 
00265             if (contiguous<T>())
00266             {
00267                 List<T> sendingValues(notBelowLeaves.size());
00268 
00269                 forAll(notBelowLeaves, leafI)
00270                 {
00271                     sendingValues[leafI] = Values[notBelowLeaves[leafI]];
00272                 }
00273 
00274                 OPstream::write
00275                 (
00276                     Pstream::scheduled,
00277                     belowID,
00278                     reinterpret_cast<const char*>(sendingValues.begin()),
00279                     sendingValues.byteSize()
00280                 );
00281             }
00282             else
00283             {
00284                 OPstream toBelow(Pstream::scheduled, belowID);
00285 
00286                 // Send data destined for all other processors below belowID
00287                 forAll(notBelowLeaves, leafI)
00288                 {
00289                     label leafID = notBelowLeaves[leafI];
00290                     toBelow << Values[leafID];
00291 
00292                     if (debug)
00293                     {
00294                         Pout<< " sent through "
00295                             << belowID << " data for:" << leafID
00296                             << " data:" << Values[leafID] << endl;
00297                     }
00298                 }
00299             }
00300         }
00301     }
00302 }
00303 
00304 
00305 template <class T>
00306 void Pstream::scatterList(List<T>& Values)
00307 {
00308     if (Pstream::nProcs() < Pstream::nProcsSimpleSum)
00309     {
00310         scatterList(Pstream::linearCommunication(), Values);
00311     }
00312     else
00313     {
00314         scatterList(Pstream::treeCommunication(), Values);
00315     }
00316 }
00317 
00318 
00319 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00320 
00321 } // End namespace Foam
00322 
00323 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd