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

commSchedule.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 "commSchedule.H"
00028 #include "SortableList.H"
00029 #include "boolList.H"
00030 #include "IOstreams.H"
00031 #include "IOmanip.H"
00032 #include "OStringStream.H"
00033 #include "Pstream.H"
00034 
00035 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00036 
00037 defineTypeNameAndDebug(Foam::commSchedule, 0);
00038 
00039 
00040 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00041 
00042 Foam::label Foam::commSchedule::outstandingComms
00043 (
00044     const labelList& commToSchedule,
00045     DynamicList<label>& procComms
00046 ) const
00047 {
00048     label nOutstanding = 0;
00049 
00050     forAll(procComms, i)
00051     {
00052         if (commToSchedule[procComms[i]] == -1)
00053         {
00054             nOutstanding++;
00055         }
00056     }
00057     return nOutstanding;
00058 }
00059 
00060 
00061 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00062 
00063 // Construct from separate addressing
00064 Foam::commSchedule::commSchedule
00065 (
00066     const label nProcs,
00067     const List<labelPair>& comms
00068 )
00069 :
00070     schedule_(comms.size()),
00071     procSchedule_(nProcs)
00072 {
00073     // Determine comms per processor.
00074     List<DynamicList<label> > procToComms(nProcs);
00075 
00076     forAll(comms, commI)
00077     {
00078         label proc0 = comms[commI][0];
00079         label proc1 = comms[commI][1];
00080 
00081         if (proc0 < 0 || proc0 >= nProcs || proc1 < 0 || proc1 >= nProcs)
00082         {
00083             FatalErrorIn
00084             (
00085                 "commSchedule::commSchedule"
00086                 "(const label, const List<labelPair>&)"
00087             )   << "Illegal processor " << comms[commI] << abort(FatalError);
00088         }
00089 
00090         procToComms[proc0].append(commI);
00091         procToComms[proc1].append(commI);
00092     }
00093     // Note: no need to shrink procToComms. Are small.
00094 
00095     if (debug && Pstream::master())
00096     {
00097         Pout<< "commSchedule::commSchedule : Wanted communication:" << endl;
00098 
00099         forAll(comms, i)
00100         {
00101             const labelPair& twoProcs = comms[i];
00102 
00103             Pout<< i << ": "
00104                 << twoProcs[0] << " with " << twoProcs[1] << endl;
00105         }
00106         Pout<< endl;
00107 
00108 
00109         Pout<< "commSchedule::commSchedule : Schedule:" << endl;
00110 
00111         // Print header. Use buffered output to prevent parallel output messing
00112         // up.
00113         {
00114             OStringStream os;
00115             os  << "iter|";
00116             for (int i = 0; i < nProcs; i++)
00117             {
00118                 os  << setw(3) << i;
00119             }
00120             Pout<< os.str().c_str() << endl;
00121         }
00122         {
00123             OStringStream os;
00124             os  << "----+";
00125             for (int i = 0; i < nProcs; i++)
00126             {
00127                 os  << "---";
00128             }
00129             Pout<< os.str().c_str() << endl;
00130         }
00131     }
00132 
00133     // Schedule all. Note: crap scheduler. Assumes all communication takes
00134     // equally long.
00135 
00136     label nScheduled = 0;
00137 
00138     label iter = 0;
00139 
00140     // Per index into comms the time when it was scheduled
00141     labelList commToSchedule(comms.size(), -1);
00142 
00143     while (nScheduled < comms.size())
00144     {
00145         label oldNScheduled = nScheduled;
00146 
00147         // Find unscheduled comms. This is the comms where the two processors
00148         // still have the most unscheduled comms.
00149 
00150         boolList busy(nProcs, false);
00151 
00152         while (true)
00153         {
00154             label maxCommI = -1;
00155             label maxNeed = labelMin;
00156 
00157             forAll(comms, commI)
00158             {
00159                 label proc0 = comms[commI][0];
00160                 label proc1 = comms[commI][1];
00161 
00162                 if
00163                 (
00164                     commToSchedule[commI] == -1             // unscheduled
00165                 && !busy[proc0]
00166                 && !busy[proc1]
00167                 )
00168                 {
00169                     label need =
00170                         outstandingComms(commToSchedule, procToComms[proc0])
00171                       + outstandingComms(commToSchedule, procToComms[proc1]);
00172 
00173                     if (need > maxNeed)
00174                     {
00175                         maxNeed = need;
00176                         maxCommI = commI;
00177                     }
00178                 }
00179             }
00180 
00181 
00182             if (maxCommI == -1)
00183             {
00184                 // Found no unscheduled procs.
00185                 break;
00186             }
00187 
00188             // Schedule commI in this iteration
00189             commToSchedule[maxCommI] = nScheduled++;
00190             busy[comms[maxCommI][0]] = true;
00191             busy[comms[maxCommI][1]] = true;
00192         }
00193 
00194         if (debug && Pstream::master())
00195         {
00196             label nIterComms = nScheduled-oldNScheduled;
00197 
00198             if (nIterComms > 0)
00199             {
00200                 labelList procToComm(nProcs, -1);
00201 
00202                 forAll(commToSchedule, commI)
00203                 {
00204                     label sched = commToSchedule[commI];
00205 
00206                     if (sched >= oldNScheduled && sched < nScheduled)
00207                     {
00208                         label proc0 = comms[commI][0];
00209                         procToComm[proc0] = commI;
00210                         label proc1 = comms[commI][1];
00211                         procToComm[proc1] = commI;
00212                     }
00213                 }
00214 
00215                 // Print it
00216                 OStringStream os;
00217                 os  << setw(3) << iter << " |";
00218                 forAll(procToComm, procI)
00219                 {
00220                     if (procToComm[procI] == -1)
00221                     {
00222                         os  << "   ";
00223                     }
00224                     else
00225                     {
00226                         os  << setw(3) << procToComm[procI];
00227                     }
00228                 }
00229                 Pout<< os.str().c_str() << endl;
00230             }
00231         }
00232 
00233         iter++;
00234     }
00235 
00236     if (debug && Pstream::master())
00237     {
00238         Pout<< endl;
00239     }
00240 
00241 
00242     // Sort commToSchedule and obtain order in comms
00243     schedule_ = SortableList<label>(commToSchedule).indices();
00244 
00245     // Sort schedule_ by processor
00246 
00247     labelList nProcScheduled(nProcs, 0);
00248 
00249     // Count
00250     forAll(schedule_, i)
00251     {
00252         label commI = schedule_[i];
00253         const labelPair& twoProcs = comms[commI];
00254 
00255         nProcScheduled[twoProcs[0]]++;
00256         nProcScheduled[twoProcs[1]]++;
00257     }
00258     // Allocate
00259     forAll(procSchedule_, procI)
00260     {
00261         procSchedule_[procI].setSize(nProcScheduled[procI]);
00262     }
00263     nProcScheduled = 0;
00264     // Fill
00265     forAll(schedule_, i)
00266     {
00267         label commI = schedule_[i];
00268         const labelPair& twoProcs = comms[commI];
00269 
00270         label proc0 = twoProcs[0];
00271         procSchedule_[proc0][nProcScheduled[proc0]++] = commI;
00272 
00273         label proc1 = twoProcs[1];
00274         procSchedule_[proc1][nProcScheduled[proc1]++] = commI;
00275     }
00276 
00277     if (debug && Pstream::master())
00278     {
00279         Pout<< "commSchedule::commSchedule : Per processor:" << endl;
00280 
00281         forAll(procSchedule_, procI)
00282         {
00283             const labelList& procComms = procSchedule_[procI];
00284 
00285             Pout<< "Processor " << procI << " talks to processors:" << endl;
00286 
00287             forAll(procComms, i)
00288             {
00289                 const labelPair& twoProcs = comms[procComms[i]];
00290 
00291                 label nbr = (twoProcs[1] == procI ? twoProcs[0] : twoProcs[1]);
00292 
00293                 Pout<< "    " << nbr << endl;
00294             }
00295         }
00296         Pout<< endl;
00297     }
00298 }
00299 
00300 
00301 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd