/*---------------------------------------------------------------------------*\
========= |
\\ / 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