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 "mapDistribute.H"
00028 #include "commSchedule.H"
00029 #include "HashSet.H"
00030 #include "ListOps.H"
00031
00032
00033
00034 Foam::List<Foam::labelPair> Foam::mapDistribute::schedule
00035 (
00036 const labelListList& subMap,
00037 const labelListList& constructMap
00038 )
00039 {
00040
00041 List<labelPair> allComms;
00042
00043 {
00044 HashSet<labelPair, labelPair::Hash<> > commsSet(Pstream::nProcs());
00045
00046
00047 forAll(subMap, procI)
00048 {
00049 if (procI != Pstream::myProcNo())
00050 {
00051 if (subMap[procI].size())
00052 {
00053
00054 commsSet.insert(labelPair(Pstream::myProcNo(), procI));
00055 }
00056 if (constructMap[procI].size())
00057 {
00058
00059 commsSet.insert(labelPair(procI, Pstream::myProcNo()));
00060 }
00061 }
00062 }
00063 allComms = commsSet.toc();
00064 }
00065
00066
00067
00068 if (Pstream::master())
00069 {
00070
00071 for
00072 (
00073 int slave=Pstream::firstSlave();
00074 slave<=Pstream::lastSlave();
00075 slave++
00076 )
00077 {
00078 IPstream fromSlave(Pstream::blocking, slave);
00079 List<labelPair> nbrData(fromSlave);
00080
00081 forAll(nbrData, i)
00082 {
00083 if (findIndex(allComms, nbrData[i]) == -1)
00084 {
00085 label sz = allComms.size();
00086 allComms.setSize(sz+1);
00087 allComms[sz] = nbrData[i];
00088 }
00089 }
00090 }
00091
00092 for
00093 (
00094 int slave=Pstream::firstSlave();
00095 slave<=Pstream::lastSlave();
00096 slave++
00097 )
00098 {
00099 OPstream toSlave(Pstream::blocking, slave);
00100 toSlave << allComms;
00101 }
00102 }
00103 else
00104 {
00105 {
00106 OPstream toMaster(Pstream::blocking, Pstream::masterNo());
00107 toMaster << allComms;
00108 }
00109 {
00110 IPstream fromMaster(Pstream::blocking, Pstream::masterNo());
00111 fromMaster >> allComms;
00112 }
00113 }
00114
00115
00116
00117 labelList mySchedule
00118 (
00119 commSchedule
00120 (
00121 Pstream::nProcs(),
00122 allComms
00123 ).procSchedule()[Pstream::myProcNo()]
00124 );
00125
00126
00127 return UIndirectList<labelPair>(allComms, mySchedule);
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 }
00151
00152
00153 const Foam::List<Foam::labelPair>& Foam::mapDistribute::schedule() const
00154 {
00155 if (schedulePtr_.empty())
00156 {
00157 schedulePtr_.reset
00158 (
00159 new List<labelPair>
00160 (
00161 schedule(subMap_, constructMap_)
00162 )
00163 );
00164 }
00165 return schedulePtr_();
00166 }
00167
00168
00169
00170
00171
00172 Foam::mapDistribute::mapDistribute
00173 (
00174 const label constructSize,
00175 const labelListList& subMap,
00176 const labelListList& constructMap
00177 )
00178 :
00179 constructSize_(constructSize),
00180 subMap_(subMap),
00181 constructMap_(constructMap),
00182 schedulePtr_()
00183 {}
00184
00185
00186
00187 Foam::mapDistribute::mapDistribute
00188 (
00189 const label constructSize,
00190 labelListList& subMap,
00191 labelListList& constructMap,
00192 const bool reUse
00193 )
00194 :
00195 constructSize_(constructSize),
00196 subMap_(subMap, reUse),
00197 constructMap_(constructMap, reUse),
00198 schedulePtr_()
00199 {}
00200
00201
00202 Foam::mapDistribute::mapDistribute
00203 (
00204 const labelList& sendProcs,
00205 const labelList& recvProcs
00206 )
00207 :
00208 constructSize_(0),
00209 schedulePtr_()
00210 {
00211 if (sendProcs.size() != recvProcs.size())
00212 {
00213 FatalErrorIn
00214 (
00215 "mapDistribute::mapDistribute(const labelList&, const labelList&)"
00216 ) << "The send and receive data is not the same length. sendProcs:"
00217 << sendProcs.size() << " recvProcs:" << recvProcs.size()
00218 << abort(FatalError);
00219 }
00220
00221
00222 labelList nSend(Pstream::nProcs(), 0);
00223 labelList nRecv(Pstream::nProcs(), 0);
00224
00225 forAll(sendProcs, sampleI)
00226 {
00227 label sendProc = sendProcs[sampleI];
00228 label recvProc = recvProcs[sampleI];
00229
00230
00231
00232
00233 if (Pstream::myProcNo() == sendProc)
00234 {
00235
00236 nSend[recvProc]++;
00237 }
00238 if (Pstream::myProcNo() == recvProc)
00239 {
00240
00241 nRecv[sendProc]++;
00242 }
00243 }
00244
00245 subMap_.setSize(Pstream::nProcs());
00246 constructMap_.setSize(Pstream::nProcs());
00247 forAll(nSend, procI)
00248 {
00249 subMap_[procI].setSize(nSend[procI]);
00250 constructMap_[procI].setSize(nRecv[procI]);
00251 }
00252 nSend = 0;
00253 nRecv = 0;
00254
00255 forAll(sendProcs, sampleI)
00256 {
00257 label sendProc = sendProcs[sampleI];
00258 label recvProc = recvProcs[sampleI];
00259
00260 if (Pstream::myProcNo() == sendProc)
00261 {
00262
00263 subMap_[recvProc][nSend[recvProc]++] = sampleI;
00264 }
00265 if (Pstream::myProcNo() == recvProc)
00266 {
00267
00268 constructMap_[sendProc][nRecv[sendProc]++] = sampleI;
00269
00270 constructSize_ = sampleI+1;
00271 }
00272 }
00273 }
00274
00275
00276 Foam::mapDistribute::mapDistribute(const mapDistribute& map)
00277 :
00278 constructSize_(map.constructSize_),
00279 subMap_(map.subMap_),
00280 constructMap_(map.constructMap_),
00281 schedulePtr_()
00282 {}
00283
00284
00285
00286
00287 void Foam::mapDistribute::compact(const boolList& elemIsUsed)
00288 {
00289
00290
00291
00292
00293
00294
00295 {
00296 List<boolList> sendFields(Pstream::nProcs());
00297
00298 for (label domain = 0; domain < Pstream::nProcs(); domain++)
00299 {
00300 const labelList& map = constructMap_[domain];
00301
00302 if (domain != Pstream::myProcNo() && map.size())
00303 {
00304 boolList& subField = sendFields[domain];
00305 subField.setSize(map.size());
00306 forAll(map, i)
00307 {
00308 subField[i] = elemIsUsed[map[i]];
00309 }
00310
00311 OPstream::write
00312 (
00313 Pstream::nonBlocking,
00314 domain,
00315 reinterpret_cast<const char*>(subField.begin()),
00316 subField.size()*sizeof(bool)
00317 );
00318 }
00319 }
00320
00321
00322
00323 List<boolList> recvFields(Pstream::nProcs());
00324
00325 for (label domain = 0; domain < Pstream::nProcs(); domain++)
00326 {
00327 const labelList& map = subMap_[domain];
00328
00329 if (domain != Pstream::myProcNo() && map.size())
00330 {
00331 recvFields[domain].setSize(map.size());
00332 IPstream::read
00333 (
00334 Pstream::nonBlocking,
00335 domain,
00336 reinterpret_cast<char*>(recvFields[domain].begin()),
00337 recvFields[domain].size()*sizeof(bool)
00338 );
00339 }
00340 }
00341
00342
00343
00344
00345 {
00346 const labelList& map = constructMap_[Pstream::myProcNo()];
00347
00348 recvFields[Pstream::myProcNo()].setSize(map.size());
00349 forAll(map, i)
00350 {
00351 recvFields[Pstream::myProcNo()][i] = elemIsUsed[map[i]];
00352 }
00353 }
00354
00355
00356
00357
00358 OPstream::waitRequests();
00359 IPstream::waitRequests();
00360
00361
00362
00363 for (label domain = 0; domain < Pstream::nProcs(); domain++)
00364 {
00365 const labelList& map = subMap_[domain];
00366
00367 labelList newMap(map.size());
00368 label newI = 0;
00369
00370 forAll(map, i)
00371 {
00372 if (recvFields[domain][i])
00373 {
00374
00375 newMap[newI++] = map[i];
00376 }
00377 }
00378 if (newI < map.size())
00379 {
00380 newMap.setSize(newI);
00381 subMap_[domain].transfer(newMap);
00382 }
00383 }
00384 }
00385
00386
00387
00388
00389
00390 label maxConstructIndex = -1;
00391
00392 for (label domain = 0; domain < Pstream::nProcs(); domain++)
00393 {
00394 const labelList& map = constructMap_[domain];
00395
00396 labelList newMap(map.size());
00397 label newI = 0;
00398
00399 forAll(map, i)
00400 {
00401 label destinationI = map[i];
00402
00403
00404 if (elemIsUsed[destinationI])
00405 {
00406 maxConstructIndex = max(maxConstructIndex, destinationI);
00407
00408 newMap[newI++] = destinationI;
00409 }
00410 }
00411 if (newI < map.size())
00412 {
00413 newMap.setSize(newI);
00414 constructMap_[domain].transfer(newMap);
00415 }
00416 }
00417
00418 constructSize_ = maxConstructIndex+1;
00419
00420
00421 schedulePtr_.clear();
00422 }
00423
00424
00425
00426
00427 void Foam::mapDistribute::operator=(const mapDistribute& rhs)
00428 {
00429
00430 if (this == &rhs)
00431 {
00432 FatalErrorIn
00433 (
00434 "Foam::mapDistribute::operator=(const Foam::mapDistribute&)"
00435 ) << "Attempted assignment to self"
00436 << abort(FatalError);
00437 }
00438 constructSize_ = rhs.constructSize_;
00439 subMap_ = rhs.subMap_;
00440 constructMap_ = rhs.constructMap_;
00441 schedulePtr_.clear();
00442 }
00443
00444
00445