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

Pstream.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 "Pstream.H"
00028 #include "debug.H"
00029 #include "dictionary.H"
00030 
00031 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00032 
00033 defineTypeNameAndDebug(Foam::Pstream, 0);
00034 
00035 template<>
00036 const char* Foam::NamedEnum<Foam::Pstream::commsTypes, 3>::names[] =
00037 {
00038     "blocking",
00039     "scheduled",
00040     "nonBlocking"
00041 };
00042 
00043 const Foam::NamedEnum<Foam::Pstream::commsTypes, 3>
00044     Foam::Pstream::commsTypeNames;
00045 
00046 
00047 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00048 
00049 void Foam::Pstream::setParRun()
00050 {
00051     parRun_ = true;
00052 
00053     Pout.prefix() = '[' +  name(myProcNo()) + "] ";
00054     Perr.prefix() = '[' +  name(myProcNo()) + "] ";
00055 }
00056 
00057 
00058 void Foam::Pstream::calcLinearComm(const label nProcs)
00059 {
00060     linearCommunication_.setSize(nProcs);
00061 
00062     // Master
00063     labelList belowIDs(nProcs - 1);
00064     forAll(belowIDs, i)
00065     {
00066         belowIDs[i] = i + 1;
00067     }
00068 
00069     linearCommunication_[0] = commsStruct
00070     (
00071         nProcs,
00072         0,
00073         -1,
00074         belowIDs,
00075         labelList(0)
00076     );
00077 
00078     // Slaves. Have no below processors, only communicate up to master
00079     for (label procID = 1; procID < nProcs; procID++)
00080     {
00081         linearCommunication_[procID] = commsStruct
00082         (
00083             nProcs,
00084             procID,
00085             0,
00086             labelList(0),
00087             labelList(0)
00088         );
00089     }
00090 }
00091 
00092 
00093 // Append my children (and my children children etc.) to allReceives.
00094 void Foam::Pstream::collectReceives
00095 (
00096     const label procID,
00097     const List<DynamicList<label> >& receives,
00098     DynamicList<label>& allReceives
00099 )
00100 {
00101     const DynamicList<label>& myChildren = receives[procID];
00102 
00103     forAll(myChildren, childI)
00104     {
00105         allReceives.append(myChildren[childI]);
00106         collectReceives(myChildren[childI], receives, allReceives);
00107     }
00108 }
00109 
00110 
00111 // Tree like schedule. For 8 procs:
00112 // (level 0)
00113 //      0 receives from 1
00114 //      2 receives from 3
00115 //      4 receives from 5
00116 //      6 receives from 7
00117 // (level 1)
00118 //      0 receives from 2
00119 //      4 receives from 6
00120 // (level 2)
00121 //      0 receives from 4
00122 //
00123 // The sends/receives for all levels are collected per processor (one send per
00124 // processor; multiple receives possible) creating a table:
00125 //
00126 // So per processor:
00127 // proc     receives from   sends to
00128 // ----     -------------   --------
00129 //  0       1,2,4           -
00130 //  1       -               0
00131 //  2       3               0
00132 //  3       -               2
00133 //  4       5               0
00134 //  5       -               4
00135 //  6       7               4
00136 //  7       -               6
00137 void Foam::Pstream::calcTreeComm(label nProcs)
00138 {
00139     label nLevels = 1;
00140     while ((1 << nLevels) < nProcs)
00141     {
00142         nLevels++;
00143     }
00144 
00145     List<DynamicList<label> > receives(nProcs);
00146     labelList sends(nProcs, -1);
00147 
00148     // Info<< "Using " << nLevels << " communication levels" << endl;
00149 
00150     label offset = 2;
00151     label childOffset = offset/2;
00152 
00153     for (label level = 0; level < nLevels; level++)
00154     {
00155         label receiveID = 0;
00156         while (receiveID < nProcs)
00157         {
00158             // Determine processor that sends and we receive from
00159             label sendID = receiveID + childOffset;
00160 
00161             if (sendID < nProcs)
00162             {
00163                 receives[receiveID].append(sendID);
00164                 sends[sendID] = receiveID;
00165             }
00166 
00167             receiveID += offset;
00168         }
00169 
00170         offset <<= 1;
00171         childOffset <<= 1;
00172     }
00173 
00174     // For all processors find the processors it receives data from
00175     // (and the processors they receive data from etc.)
00176     List<DynamicList<label> > allReceives(nProcs);
00177     for (label procID = 0; procID < nProcs; procID++)
00178     {
00179         collectReceives(procID, receives, allReceives[procID]);
00180     }
00181 
00182 
00183     treeCommunication_.setSize(nProcs);
00184 
00185     for (label procID = 0; procID < nProcs; procID++)
00186     {
00187         treeCommunication_[procID] = commsStruct
00188         (
00189             nProcs,
00190             procID,
00191             sends[procID],
00192             receives[procID].shrink(),
00193             allReceives[procID].shrink()
00194         );
00195     }
00196 }
00197 
00198 
00199 // Callback from Pstream::init() : initialize linear and tree communication
00200 // schedules now that nProcs is known.
00201 void Foam::Pstream::initCommunicationSchedule()
00202 {
00203     calcLinearComm(nProcs());
00204     calcTreeComm(nProcs());
00205 }
00206 
00207 
00208 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00209 
00210 // Initialise my process number to 0 (the master)
00211 int Foam::Pstream::myProcNo_(0);
00212 
00213 // By default this is not a parallel run
00214 bool Foam::Pstream::parRun_(false);
00215 
00216 // List of process IDs
00217 Foam::List<int> Foam::Pstream::procIDs_(1, 0);
00218 
00219 // Standard transfer message type
00220 const int Foam::Pstream::msgType_(1);
00221 
00222 // Linear communication schedule
00223 Foam::List<Foam::Pstream::commsStruct> Foam::Pstream::linearCommunication_(0);
00224 
00225 // Multi level communication schedule
00226 Foam::List<Foam::Pstream::commsStruct> Foam::Pstream::treeCommunication_(0);
00227 
00228 // Should compact transfer be used in which floats replace doubles
00229 // reducing the bandwidth requirement at the expense of some loss
00230 // in accuracy
00231 bool Foam::Pstream::floatTransfer
00232 (
00233     debug::optimisationSwitch("floatTransfer", 0)
00234 );
00235 
00236 // Number of processors at which the reduce algorithm changes from linear to
00237 // tree
00238 int Foam::Pstream::nProcsSimpleSum
00239 (
00240     debug::optimisationSwitch("nProcsSimpleSum", 16)
00241 );
00242 
00243 // Default commsType
00244 Foam::Pstream::commsTypes Foam::Pstream::defaultCommsType
00245 (
00246     commsTypeNames.read(debug::optimisationSwitches().lookup("commsType"))
00247 );
00248 
00249 
00250 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd