commSchedule.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00036
00037 defineTypeNameAndDebug(Foam::commSchedule, 0);
00038
00039
00040
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
00062
00063
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
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
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
00112
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
00134
00135
00136 label nScheduled = 0;
00137
00138 label iter = 0;
00139
00140
00141 labelList commToSchedule(comms.size(), -1);
00142
00143 while (nScheduled < comms.size())
00144 {
00145 label oldNScheduled = nScheduled;
00146
00147
00148
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
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
00185 break;
00186 }
00187
00188
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
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
00243 schedule_ = SortableList<label>(commToSchedule).indices();
00244
00245
00246
00247 labelList nProcScheduled(nProcs, 0);
00248
00249
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
00259 forAll(procSchedule_, procI)
00260 {
00261 procSchedule_[procI].setSize(nProcScheduled[procI]);
00262 }
00263 nProcScheduled = 0;
00264
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