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

mapDistribute.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 "mapDistribute.H"
00028 #include "commSchedule.H"
00029 #include "HashSet.H"
00030 #include "ListOps.H"
00031 
00032 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00033 
00034 Foam::List<Foam::labelPair> Foam::mapDistribute::schedule
00035 (
00036     const labelListList& subMap,
00037     const labelListList& constructMap
00038 )
00039 {
00040     // Communications: send and receive processor
00041     List<labelPair> allComms;
00042 
00043     {
00044         HashSet<labelPair, labelPair::Hash<> > commsSet(Pstream::nProcs());
00045 
00046         // Find what communication is required
00047         forAll(subMap, procI)
00048         {
00049             if (procI != Pstream::myProcNo())
00050             {
00051                 if (subMap[procI].size())
00052                 {
00053                     // I need to send to procI
00054                     commsSet.insert(labelPair(Pstream::myProcNo(), procI));
00055                 }
00056                 if (constructMap[procI].size())
00057                 {
00058                     // I need to receive from procI
00059                     commsSet.insert(labelPair(procI, Pstream::myProcNo()));
00060                 }
00061             }
00062         }
00063         allComms = commsSet.toc();
00064     }
00065 
00066 
00067     // Reduce
00068     if (Pstream::master())
00069     {
00070         // Receive and merge
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         // Send back
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     // Determine my schedule.
00117     labelList mySchedule
00118     (
00119         commSchedule
00120         (
00121             Pstream::nProcs(),
00122             allComms
00123         ).procSchedule()[Pstream::myProcNo()]
00124     );
00125 
00126     // Processors involved in my schedule
00127     return UIndirectList<labelPair>(allComms, mySchedule);
00128 
00129 
00130     //if (debug)
00131     //{
00132     //    Pout<< "I need to:" << endl;
00133     //    const List<labelPair>& comms = schedule();
00134     //    forAll(comms, i)
00135     //    {
00136     //        const labelPair& twoProcs = comms[i];
00137     //        label sendProc = twoProcs[0];
00138     //        label recvProc = twoProcs[1];
00139     //
00140     //        if (recvProc == Pstream::myProcNo())
00141     //        {
00142     //            Pout<< "    receive from " << sendProc << endl;
00143     //        }
00144     //        else
00145     //        {
00146     //            Pout<< "    send to " << recvProc << endl;
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 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00170 
00171 //- Construct from components
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 //- (optionally destructively) construct from components
00187 Foam::mapDistribute::mapDistribute
00188 (
00189     const label constructSize,
00190     labelListList& subMap,
00191     labelListList& constructMap,
00192     const bool reUse                // clone or 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     // Per processor the number of samples we have to send/receive.
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         // Note that also need to include local communication (both
00231         // RecvProc and sendProc on local processor)
00232 
00233         if (Pstream::myProcNo() == sendProc)
00234         {
00235             // I am the sender. Count destination processor.
00236             nSend[recvProc]++;
00237         }
00238         if (Pstream::myProcNo() == recvProc)
00239         {
00240             // I am the receiver.
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             // I am the sender. Store index I need to send.
00263             subMap_[recvProc][nSend[recvProc]++] = sampleI;
00264         }
00265         if (Pstream::myProcNo() == recvProc)
00266         {
00267             // I am the receiver.
00268             constructMap_[sendProc][nRecv[sendProc]++] = sampleI;
00269             // Largest entry inside constructMap
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 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00286 
00287 void Foam::mapDistribute::compact(const boolList& elemIsUsed)
00288 {
00289     // 1. send back to sender. Have him delete the corresponding element
00290     //    from the submap and do the same to the constructMap locally
00291     //    (and in same order).
00292 
00293     // Send elemIsUsed field to neighbour. Use nonblocking code from
00294     // mapDistribute but in reverse order.
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         // Set up receives from neighbours
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         // Set up 'send' to myself - write directly into recvFields
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         // Wait for all to finish
00357 
00358         OPstream::waitRequests();
00359         IPstream::waitRequests();
00360 
00361 
00362         // Compact out all submap entries that are referring to unused elements
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                     // So element is used on destination side
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     // 2. remove from construct map - since end-result (element in elemIsUsed)
00388     //    not used.
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             // Is element is used on destination side
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     // Clear the schedule (note:not necessary if nothing changed)
00421     schedulePtr_.clear();
00422 }
00423 
00424 
00425 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00426 
00427 void Foam::mapDistribute::operator=(const mapDistribute& rhs)
00428 {
00429     // Check for assignment to self
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 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd