/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org \\ / A nd | Copyright (C) 2015-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License This file is part of OpenFOAM. OpenFOAM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OpenFOAM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenFOAM. If not, see . \*---------------------------------------------------------------------------*/ #include "distributionMapBase.H" #include "commSchedule.H" #include "HashSet.H" #include "globalIndex.H" #include "ListOps.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // namespace Foam { defineTypeNameAndDebug(distributionMapBase, 0); } // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // Foam::List Foam::distributionMapBase::schedule ( const labelListList& subMap, const labelListList& constructMap, const int tag ) { // Communications: send and receive processor List allComms; { HashSet> commsSet(Pstream::nProcs()); // Find what communication is required forAll(subMap, proci) { if (proci != Pstream::myProcNo()) { if (subMap[proci].size()) { // I need to send to proci commsSet.insert(labelPair(Pstream::myProcNo(), proci)); } if (constructMap[proci].size()) { // I need to receive from proci commsSet.insert(labelPair(proci, Pstream::myProcNo())); } } } allComms = commsSet.toc(); } // Reduce if (Pstream::master()) { // Receive and merge for ( int slave=Pstream::firstSlave(); slave<=Pstream::lastSlave(); slave++ ) { IPstream fromSlave(Pstream::commsTypes::scheduled, slave, 0, tag); List nbrData(fromSlave); forAll(nbrData, i) { if (findIndex(allComms, nbrData[i]) == -1) { label sz = allComms.size(); allComms.setSize(sz+1); allComms[sz] = nbrData[i]; } } } // Send back for ( int slave=Pstream::firstSlave(); slave<=Pstream::lastSlave(); slave++ ) { OPstream toSlave(Pstream::commsTypes::scheduled, slave, 0, tag); toSlave << allComms; } } else { { OPstream toMaster ( Pstream::commsTypes::scheduled, Pstream::masterNo(), 0, tag ); toMaster << allComms; } { IPstream fromMaster ( Pstream::commsTypes::scheduled, Pstream::masterNo(), 0, tag ); fromMaster >> allComms; } } // Determine my schedule. labelList mySchedule ( commSchedule ( Pstream::nProcs(), allComms ).procSchedule()[Pstream::myProcNo()] ); // Processors involved in my schedule return List(UIndirectList(allComms, mySchedule)); // if (debug) //{ // Pout<< "I need to:" << endl; // const List& comms = schedule(); // forAll(comms, i) // { // const labelPair& twoProcs = comms[i]; // label sendProc = twoProcs[0]; // label recvProc = twoProcs[1]; // // if (recvProc == Pstream::myProcNo()) // { // Pout<< " receive from " << sendProc << endl; // } // else // { // Pout<< " send to " << recvProc << endl; // } // } //} } const Foam::List& Foam::distributionMapBase::schedule() const { if (schedulePtr_.empty()) { schedulePtr_.reset ( new List ( schedule(subMap_, constructMap_, Pstream::msgType()) ) ); } return schedulePtr_(); } void Foam::distributionMapBase::checkReceivedSize ( const label proci, const label expectedSize, const label receivedSize ) { if (receivedSize != expectedSize) { FatalErrorInFunction << "Expected from processor " << proci << " " << expectedSize << " but received " << receivedSize << " elements." << abort(FatalError); } } void Foam::distributionMapBase::printLayout(Ostream& os) const { // Determine offsets of remote data. labelList minIndex(Pstream::nProcs(), labelMax); labelList maxIndex(Pstream::nProcs(), labelMin); forAll(constructMap_, proci) { const labelList& construct = constructMap_[proci]; if (constructHasFlip_) { forAll(construct, i) { label index = mag(construct[i])-1; minIndex[proci] = min(minIndex[proci], index); maxIndex[proci] = max(maxIndex[proci], index); } } else { forAll(construct, i) { label index = construct[i]; minIndex[proci] = min(minIndex[proci], index); maxIndex[proci] = max(maxIndex[proci], index); } } } label localSize; if (maxIndex[Pstream::myProcNo()] == labelMin) { localSize = 0; } else { localSize = maxIndex[Pstream::myProcNo()]+1; } os << "Layout: (constructSize:" << constructSize_ << " subHasFlip:" << subHasFlip_ << " constructHasFlip:" << constructHasFlip_ << ")" << endl << "local (processor " << Pstream::myProcNo() << "):" << endl << " start : 0" << endl << " size : " << localSize << endl; label offset = localSize; forAll(minIndex, proci) { if (proci != Pstream::myProcNo()) { if (constructMap_[proci].size() > 0) { if (minIndex[proci] != offset) { FatalErrorInFunction << "offset:" << offset << " proci:" << proci << " minIndex:" << minIndex[proci] << abort(FatalError); } label size = maxIndex[proci]-minIndex[proci]+1; os << "processor " << proci << ':' << endl << " start : " << offset << endl << " size : " << size << endl; offset += size; } } } } void Foam::distributionMapBase::calcCompactAddressing ( const globalIndex& globalNumbering, const labelList& elements, List>& compactMap ) const { compactMap.setSize(Pstream::nProcs()); // Count all (non-local) elements needed. Just for presizing map. labelList nNonLocal(Pstream::nProcs(), 0); forAll(elements, i) { label globalIndex = elements[i]; if (globalIndex != -1 && !globalNumbering.isLocal(globalIndex)) { label proci = globalNumbering.whichProcID(globalIndex); nNonLocal[proci]++; } } forAll(compactMap, proci) { compactMap[proci].clear(); if (proci != Pstream::myProcNo()) { compactMap[proci].resize(2*nNonLocal[proci]); } } // Collect all (non-local) elements needed. forAll(elements, i) { label globalIndex = elements[i]; if (globalIndex != -1 && !globalNumbering.isLocal(globalIndex)) { label proci = globalNumbering.whichProcID(globalIndex); label index = globalNumbering.toLocal(proci, globalIndex); label nCompact = compactMap[proci].size(); compactMap[proci].insert(index, nCompact); } } } void Foam::distributionMapBase::calcCompactAddressing ( const globalIndex& globalNumbering, const labelListList& cellCells, List>& compactMap ) const { compactMap.setSize(Pstream::nProcs()); // Count all (non-local) elements needed. Just for presizing map. labelList nNonLocal(Pstream::nProcs(), 0); forAll(cellCells, cellI) { const labelList& cCells = cellCells[cellI]; forAll(cCells, i) { label globalIndex = cCells[i]; if (globalIndex != -1 && !globalNumbering.isLocal(globalIndex)) { label proci = globalNumbering.whichProcID(globalIndex); nNonLocal[proci]++; } } } forAll(compactMap, proci) { compactMap[proci].clear(); if (proci != Pstream::myProcNo()) { compactMap[proci].resize(2*nNonLocal[proci]); } } // Collect all (non-local) elements needed. forAll(cellCells, cellI) { const labelList& cCells = cellCells[cellI]; forAll(cCells, i) { label globalIndex = cCells[i]; if (globalIndex != -1 && !globalNumbering.isLocal(globalIndex)) { label proci = globalNumbering.whichProcID(globalIndex); label index = globalNumbering.toLocal(proci, globalIndex); label nCompact = compactMap[proci].size(); compactMap[proci].insert(index, nCompact); } } } } void Foam::distributionMapBase::exchangeAddressing ( const int tag, const globalIndex& globalNumbering, labelList& elements, List>& compactMap, labelList& compactStart ) { // The overall compact addressing is // - myProcNo data first (uncompacted) // - all other processors consecutively compactStart.setSize(Pstream::nProcs()); compactStart[Pstream::myProcNo()] = 0; constructSize_ = globalNumbering.localSize(); forAll(compactStart, proci) { if (proci != Pstream::myProcNo()) { compactStart[proci] = constructSize_; constructSize_ += compactMap[proci].size(); } } // Find out what to receive/send in compact addressing. // What I want to receive is what others have to send labelListList wantedRemoteElements(Pstream::nProcs()); // Compact addressing for received data constructMap_.setSize(Pstream::nProcs()); forAll(compactMap, proci) { if (proci == Pstream::myProcNo()) { // All my own elements are used label nLocal = globalNumbering.localSize(); wantedRemoteElements[proci] = identityMap(nLocal); constructMap_[proci] = identityMap(nLocal); } else { // Remote elements wanted from processor proci labelList& remoteElem = wantedRemoteElements[proci]; labelList& localElem = constructMap_[proci]; remoteElem.setSize(compactMap[proci].size()); localElem.setSize(compactMap[proci].size()); label i = 0; forAllIter(Map