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

ProcessorTopology.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 "ProcessorTopology.H"
00028 #include "ListOps.H"
00029 #include "Pstream.H"
00030 #include "commSchedule.H"
00031 
00032 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00033 
00034 template<class Patch, class ProcPatch>
00035 Foam::labelList Foam::ProcessorTopology<Patch, ProcPatch>::procNeighbours
00036 (
00037     const PtrList<Patch>& patches
00038 )
00039 {
00040     // Determine number of processor neighbours and max neighbour id.
00041 
00042     label nNeighbours = 0;
00043 
00044     label maxNb = 0;
00045 
00046     forAll(patches, patchi)
00047     {
00048         const Patch& patch = patches[patchi];
00049 
00050         if (isType<ProcPatch>(patch))
00051         {
00052             const ProcPatch& procPatch = 
00053                 refCast<const ProcPatch>(patch);
00054 
00055             nNeighbours++;
00056 
00057             maxNb = max(maxNb, procPatch.neighbProcNo());
00058         }
00059     }
00060 
00061     labelList neighbours(nNeighbours);
00062 
00063     procPatchMap_.setSize(maxNb + 1);
00064     procPatchMap_ = -1;
00065 
00066     nNeighbours = 0;
00067 
00068     forAll(patches, patchi)
00069     {
00070         const Patch& patch = patches[patchi];
00071 
00072         if (isType<ProcPatch>(patch))
00073         {
00074             const ProcPatch& procPatch = 
00075                 refCast<const ProcPatch>(patch);
00076 
00077             neighbours[nNeighbours++] = procPatch.neighbProcNo();
00078 
00079             // Construct reverse map
00080             procPatchMap_[procPatch.neighbProcNo()] = patchi;
00081         }
00082     }
00083 
00084     return neighbours;
00085 }
00086 
00087 
00088 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00089 
00090 // Construct from components
00091 template<class Patch, class ProcPatch>
00092 Foam::ProcessorTopology<Patch, ProcPatch>::ProcessorTopology
00093 (
00094     const PtrList<Patch>& patches
00095 )
00096 :
00097     labelListList(Pstream::nProcs()),
00098     patchSchedule_(2*patches.size())
00099 {
00100     if (Pstream::parRun())
00101     {
00102         // Fill my 'slot' with my neighbours
00103         operator[](Pstream::myProcNo()) = procNeighbours(patches);
00104 
00105         // Distribute to all processors
00106         Pstream::gatherList(*this);
00107         Pstream::scatterList(*this);
00108     }
00109 
00110     if (Pstream::parRun() && Pstream::defaultCommsType == Pstream::scheduled)
00111     {
00112         label patchEvali = 0;
00113 
00114         // 1. All non-processor patches
00115         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00116 
00117         forAll(patches, patchi)
00118         {
00119             if (!isType<ProcPatch>(patches[patchi]))
00120             {
00121                 patchSchedule_[patchEvali].patch = patchi;
00122                 patchSchedule_[patchEvali++].init = true;
00123                 patchSchedule_[patchEvali].patch = patchi;
00124                 patchSchedule_[patchEvali++].init = false;
00125             }
00126         }
00127 
00128         // 2. All processor patches
00129         // ~~~~~~~~~~~~~~~~~~~~~~~~
00130 
00131         // Determine the schedule for all. Insert processor pair once
00132         // to determine the schedule. Each processor pair stands for both
00133         // send and receive.
00134         label nComms = 0;
00135         forAll(*this, procI)
00136         {
00137             nComms += operator[](procI).size();
00138         }
00139         DynamicList<labelPair> comms(nComms);
00140 
00141         forAll(*this, procI)
00142         {
00143             const labelList& nbrs = operator[](procI);
00144 
00145             forAll(nbrs, i)
00146             {
00147                 if (procI < nbrs[i])
00148                 {
00149                     comms.append(labelPair(procI, nbrs[i]));
00150                 }
00151             }
00152         }
00153         comms.shrink();
00154 
00155         // Determine a schedule.
00156         labelList mySchedule
00157         (
00158             commSchedule
00159             (
00160                 Pstream::nProcs(),
00161                 comms
00162             ).procSchedule()[Pstream::myProcNo()]
00163         );
00164 
00165         forAll(mySchedule, iter)
00166         {
00167             label commI = mySchedule[iter];
00168 
00169             // Get the other processor
00170             label nb = comms[commI][0];
00171             if (nb == Pstream::myProcNo())
00172             {
00173                 nb = comms[commI][1];
00174             }
00175             label patchi = procPatchMap_[nb];
00176 
00177             if (Pstream::myProcNo() > nb)
00178             {
00179                 patchSchedule_[patchEvali].patch = patchi;
00180                 patchSchedule_[patchEvali++].init = true;
00181                 patchSchedule_[patchEvali].patch = patchi;
00182                 patchSchedule_[patchEvali++].init = false;
00183             }
00184             else
00185             {
00186                 patchSchedule_[patchEvali].patch = patchi;
00187                 patchSchedule_[patchEvali++].init = false;
00188                 patchSchedule_[patchEvali].patch = patchi;
00189                 patchSchedule_[patchEvali++].init = true;
00190             }
00191         }
00192     }
00193     else
00194     {
00195         label patchEvali = 0;
00196 
00197         // 1. All non-processor patches
00198         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00199 
00200         // Have evaluate directly after initEvaluate. Could have them separated
00201         // as long as they're not intermingled with processor patches since
00202         // then e.g. any reduce parallel traffic would interfere with the
00203         // processor swaps.
00204 
00205         forAll(patches, patchi)
00206         {
00207             if (!isType<ProcPatch>(patches[patchi]))
00208             {
00209                 patchSchedule_[patchEvali].patch = patchi;
00210                 patchSchedule_[patchEvali++].init = true;
00211                 patchSchedule_[patchEvali].patch = patchi;
00212                 patchSchedule_[patchEvali++].init = false;
00213             }
00214         }
00215 
00216         // 2. All processor patches
00217         // ~~~~~~~~~~~~~~~~~~~~~~~~
00218 
00219         // 2a. initEvaluate
00220         forAll(patches, patchi)
00221         {
00222             if (isType<ProcPatch>(patches[patchi]))
00223             {
00224                 patchSchedule_[patchEvali].patch = patchi;
00225                 patchSchedule_[patchEvali++].init = true;
00226             }
00227         }
00228 
00229         // 2b. evaluate
00230         forAll(patches, patchi)
00231         {
00232             if (isType<ProcPatch>(patches[patchi]))
00233             {
00234                 patchSchedule_[patchEvali].patch = patchi;
00235                 patchSchedule_[patchEvali++].init = false;
00236             }
00237         }
00238     }
00239 }
00240 
00241 
00242 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd