Commit 5dc5ea92 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: add UPstream::subProcs() static method

- returns a range of `int` values that can be iterated across.
  For example,

      for (const int proci : Pstream::subProcs()) { ... }

  instead of

      for
      (
          int proci = Pstream::firstSlave();
          proci <= Pstream::lastSlave();
          ++proci
      )
      {
          ...
      }
parent e18ff114
......@@ -297,12 +297,7 @@ int main(int argc, char *argv[])
{
if (Pstream::master())
{
for
(
int proci = Pstream::firstSlave();
proci <= Pstream::lastSlave();
++proci
)
for (const int proci : Pstream::subProcs())
{
IPstream fromSlave(Pstream::commsTypes::blocking, proci);
FixedList<label, 2> list3(fromSlave);
......
......@@ -148,12 +148,7 @@ int main(int argc, char *argv[])
Pout<< "full: " << flatOutput(idl3.values()) << nl
<< "send: " << flatOutput(idl3) << endl;
for
(
int proci = Pstream::firstSlave();
proci <= Pstream::lastSlave();
++proci
)
for (const int proci : Pstream::subProcs())
{
OPstream toSlave(Pstream::commsTypes::scheduled, proci);
toSlave << idl3;
......
......@@ -57,12 +57,7 @@ scalar sumReduce
// Add master value and all slaves
sum = localValue;
for
(
int slave=Pstream::firstSlave();
slave<=Pstream::lastSlave(comm);
slave++
)
for (const int slave : UPstream::subProcs(comm))
{
scalar slaveValue;
UIPstream::read
......@@ -80,12 +75,7 @@ scalar sumReduce
// Send back to slaves
for
(
int slave=UPstream::firstSlave();
slave<=UPstream::lastSlave(comm);
slave++
)
for (const int slave : UPstream::subProcs(comm))
{
UOPstream::write
(
......
......@@ -87,12 +87,7 @@ int main(int argc, char *argv[])
// Collect my own data
allData.append(data);
for
(
int slave=Pstream::firstSlave();
slave<=Pstream::lastSlave();
slave++
)
for (const int slave : Pstream::subProcs())
{
Perr << "master receiving from slave " << slave << endl;
UIPstream fromSlave(slave, pBufs);
......@@ -105,12 +100,7 @@ int main(int argc, char *argv[])
PstreamBuffers pBufs2(Pstream::commsTypes::nonBlocking);
if (Pstream::master())
{
for
(
int slave=Pstream::firstSlave();
slave<=Pstream::lastSlave();
slave++
)
for (const int slave : Pstream::subProcs())
{
Perr << "master sending to slave " << slave << endl;
UOPstream toSlave(slave, pBufs2);
......
......@@ -154,12 +154,7 @@ void testTransfer(const T& input)
if (Pstream::master())
{
for
(
int slave = Pstream::firstSlave();
slave <= Pstream::lastSlave();
++slave
)
for (const int slave : Pstream::subProcs())
{
Perr<< "master receiving from slave " << slave << endl;
IPstream fromSlave(Pstream::commsTypes::blocking, slave);
......@@ -167,12 +162,7 @@ void testTransfer(const T& input)
perrInfo(data) << endl;
}
for
(
int slave = Pstream::firstSlave();
slave <= Pstream::lastSlave();
++slave
)
for (const int slave : Pstream::subProcs())
{
Perr<< "master sending to slave " << slave << endl;
OPstream toSlave(Pstream::commsTypes::blocking, slave);
......@@ -207,12 +197,7 @@ void testTokenized(const T& data)
if (Pstream::master())
{
for
(
int slave = Pstream::firstSlave();
slave <= Pstream::lastSlave();
++slave
)
for (const int slave : Pstream::subProcs())
{
Perr<< "master receiving from slave " << slave << endl;
IPstream fromSlave(Pstream::commsTypes::blocking, slave);
......@@ -220,12 +205,7 @@ void testTokenized(const T& data)
Perr<< tok.info() << endl;
}
for
(
int slave = Pstream::firstSlave();
slave <= Pstream::lastSlave();
++slave
)
for (const int slave : Pstream::subProcs())
{
Perr<< "master sending to slave " << slave << endl;
OPstream toSlave(Pstream::commsTypes::blocking, slave);
......
......@@ -58,24 +58,14 @@ Gather<T0>::Gather(const T0& localData, const bool redistribute)
*outIter = localData;
// Receive data
for
(
int proci = Pstream::firstSlave();
proci <= Pstream::lastSlave();
++proci
)
for (const int proci : Pstream::subProcs())
{
IPstream fromSlave(Pstream::commsTypes::scheduled, proci);
fromSlave >> *(++outIter);
}
// Send data
for
(
int proci = Pstream::firstSlave();
proci <= Pstream::lastSlave();
++proci
)
for (const int proci : Pstream::subProcs())
{
OPstream toSlave(Pstream::commsTypes::scheduled, proci);
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2015-2017 OpenFOAM Foundation
Copyright (C) 2015-2019 OpenCFD Ltd.
Copyright (C) 2015-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -445,7 +445,7 @@ void Foam::mergeAndWrite
pOffset += myPoints.size();
// Receive slave ones
for (int slave=1; slave<Pstream::nProcs(); slave++)
for (const int slave : Pstream::subProcs())
{
IPstream fromSlave(Pstream::commsTypes::scheduled, slave);
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2015-2019 OpenCFD Ltd.
Copyright (C) 2015-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -387,12 +387,7 @@ void getInterfaceSizes
if (Pstream::master())
{
// Receive and add to my sizes
for
(
int slave=Pstream::firstSlave();
slave<=Pstream::lastSlave();
slave++
)
for (const int slave : Pstream::subProcs())
{
IPstream fromSlave(Pstream::commsTypes::blocking, slave);
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2017 OpenFOAM Foundation
Copyright (C) 2015-2019 OpenCFD Ltd.
Copyright (C) 2015-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -124,12 +124,7 @@ Foam::autoPtr<Foam::fvMesh> Foam::loadOrCreateMesh
);
// Send patches
for
(
int slave=Pstream::firstSlave();
slave<=Pstream::lastSlave();
slave++
)
for (const int slave : Pstream::subProcs())
{
OPstream toSlave(Pstream::commsTypes::scheduled, slave);
toSlave << patchEntries;
......
......@@ -677,7 +677,7 @@ void readFields
tmp<GeoField> tsubfld = subsetterPtr->interpolate(fields[i]);
// Send to all processors that don't have a mesh
for (label procI = 1; procI < Pstream::nProcs(); ++procI)
for (const int procI : Pstream::subProcs())
{
if (!haveMesh[procI])
{
......
......@@ -340,12 +340,7 @@ bool Foam::decomposedBlockData::readBlocks
}
// Read slave data
for
(
label proci = 1;
proci < UPstream::nProcs(comm);
++proci
)
for (const int proci : UPstream::subProcs(comm))
{
List<char> elems(is);
is.fatalCheck("read(Istream&) : reading entry");
......@@ -397,12 +392,7 @@ bool Foam::decomposedBlockData::readBlocks
}
// Read slave data
for
(
label proci = 1;
proci < UPstream::nProcs(comm);
++proci
)
for (const int proci : UPstream::subProcs(comm))
{
List<char> elems(is);
is.fatalCheck("read(Istream&) : reading entry");
......@@ -474,12 +464,7 @@ Foam::autoPtr<Foam::ISstream> Foam::decomposedBlockData::readBlocks
}
// Read slave data
for
(
label proci = 1;
proci < UPstream::nProcs(comm);
++proci
)
for (const int proci : UPstream::subProcs(comm))
{
is >> data;
is.fatalCheck("read(Istream&) : reading entry");
......@@ -545,12 +530,7 @@ Foam::autoPtr<Foam::ISstream> Foam::decomposedBlockData::readBlocks
}
// Read slave data
for
(
label proci = 1;
proci < UPstream::nProcs(comm);
++proci
)
for (const int proci : UPstream::subProcs(comm))
{
List<char> elems(is);
is.fatalCheck("read(Istream&) : reading entry");
......
......@@ -154,7 +154,7 @@ void Foam::masterOFstream::commit()
*std::max_element(recvSizes.cbegin(), recvSizes.cend())
);
for (label proci = 1; proci < Pstream::nProcs(); ++proci)
for (const int proci : Pstream::subProcs())
{
UIPstream is(proci, pBufs);
......
......@@ -374,7 +374,7 @@ public:
static void addValidParOptions(HashTable<string>& validParOptions);
//- Initialisation function called from main
// Spawns slave processes and initialises inter-communication
// Spawns sub-processes and initialises inter-communication
static bool init(int& argc, char**& argv, const bool needsThread);
//- Special purpose initialisation function.
......@@ -459,18 +459,6 @@ public:
return procIDs_[communicator];
}
//- Process index of first slave
static constexpr int firstSlave() noexcept
{
return 1;
}
//- Process index of last slave
static int lastSlave(const label communicator = 0)
{
return nProcs(communicator) - 1;
}
//- Range of process indices for all processes
static IntRange<int> allProcs(const label communicator = 0)
{
......@@ -478,6 +466,13 @@ public:
return IntRange<int>(static_cast<int>(nProcs(communicator)));
}
//- Range of process indices for sub-processes
static IntRange<int> subProcs(const label communicator = 0)
{
// Proc 1 -> nProcs
return IntRange<int>(1, static_cast<int>(nProcs(communicator)-1));
}
//- Communication schedule for linear all-to-master (proc 0)
static const List<commsStruct>& linearCommunication
(
......@@ -578,6 +573,23 @@ public:
int recvSize,
const label communicator = 0
);
// Housekeeping
//- Process index of first sub-process
// \deprecated(2020-09) use subProcs() method instead
static constexpr int firstSlave() noexcept
{
return 1;
}
//- Process index of last sub-process
// \deprecated(2020-09) use subProcs() method instead
static int lastSlave(const label communicator = 0)
{
return nProcs(communicator) - 1;
}
};
......
......@@ -1105,12 +1105,7 @@ void Foam::argList::parse
slaveProcs.resize(Pstream::nProcs()-1);
slaveMachine.resize(Pstream::nProcs()-1);
label proci = 0;
for
(
int slave = Pstream::firstSlave();
slave <= Pstream::lastSlave();
slave++
)
for (const int slave : Pstream::subProcs())
{
IPstream fromSlave(Pstream::commsTypes::scheduled, slave);
......@@ -1119,7 +1114,7 @@ void Foam::argList::parse
fromSlave >> slaveBuild >> slaveMachine[proci] >> slavePid;
slaveProcs[proci] = slaveMachine[proci] + "." + name(slavePid);
proci++;
++proci;
// Verify that all processors are running the same build
if (slaveBuild != foamVersion::build)
......@@ -1339,12 +1334,7 @@ void Foam::argList::parse
// Distribute the master's argument list (with new root)
const bool hadCaseOpt = options_.found("case");
for
(
int slave = Pstream::firstSlave();
slave <= Pstream::lastSlave();
slave++
)
for (const int slave : Pstream::subProcs())
{
options_.set("case", roots[slave-1]/globalCase_);
......@@ -1392,12 +1382,7 @@ void Foam::argList::parse
}
// Distribute the master's argument list (unaltered)
for
(
int slave = Pstream::firstSlave();
slave <= Pstream::lastSlave();
slave++
)
for (const int slave : Pstream::subProcs())
{
OPstream toSlave(Pstream::commsTypes::scheduled, slave);
toSlave << args_ << options_ << roots.size();
......
......@@ -642,12 +642,7 @@ Foam::fileOperations::masterUncollatedFileOperation::read
}
// Read slave files
for
(
label proci = 1;
proci < Pstream::nProcs(comm);
proci++
)
for (const int proci : Pstream::subProcs(comm))
{
if (debug)
{
......@@ -2391,12 +2386,7 @@ Foam::fileOperations::masterUncollatedFileOperation::NewIFstream
}
else
{
for
(
label proci = 1;
proci < Pstream::nProcs(Pstream::worldComm);
proci++
)
for (const int proci : Pstream::subProcs(Pstream::worldComm))
{
readAndSend
(
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2018 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -42,7 +43,7 @@ Type Foam::fileOperations::masterUncollatedFileOperation::scatterList
PstreamBuffers pBufs(UPstream::commsTypes::nonBlocking, tag, comm);
if (Pstream::master(comm))
{
for (label proci = 1; proci < Pstream::nProcs(comm); proci++)
for (const int proci : Pstream::subProcs(comm))
{
UOPstream os(proci, pBufs);
os << masterLst[proci];
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -85,12 +86,7 @@ Foam::LUscalarMatrix::LUscalarMatrix
if (Pstream::master(comm_))
{
for
(
int slave=Pstream::firstSlave();
slave<=Pstream::lastSlave(comm_);
slave++
)
for (const int slave : Pstream::subProcs(comm_))
{
lduMatrices.set
(
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -54,12 +54,7 @@ void Foam::LUscalarMatrix::solve
SubList<Type>(X, x.size()) = x;
for
(
int slave=Pstream::firstSlave();
slave<=Pstream::lastSlave(comm_);
slave++
)
for (const int slave : Pstream::subProcs(comm_))
{
IPstream::read
(
......@@ -94,12 +89,7 @@ void Foam::LUscalarMatrix::solve
x = SubList<Type>(X, x.size());
for
(
int slave=Pstream::firstSlave();
slave<=Pstream::lastSlave(comm_);
slave++
)
for (const int slave : Pstream::subProcs(comm_))
{
OPstream::write
(
......
......@@ -391,12 +391,7 @@ void Foam::globalMeshData::calcSharedEdges() const
// Receive data from slaves and insert
if (Pstream::parRun())
{
for
(
int slave=Pstream::firstSlave();
slave<=Pstream::lastSlave();
slave++
)
for (const int slave : Pstream::subProcs())
{
// Receive the edges using shared points from the slave.
IPstream fromSlave(Pstream::commsTypes::blocking, slave);
......@@ -440,12 +435,7 @@ void Foam::globalMeshData::calcSharedEdges() const
// Send back to slaves.
if (Pstream::parRun())
{
for
(
int slave=Pstream::firstSlave();
slave<=Pstream::lastSlave();
slave++
)
for (const int slave : Pstream::subProcs())
{
// Receive the edges using shared points from the slave.
OPstream toSlave(Pstream::commsTypes::blocking, slave);
......@@ -1907,12 +1897,7 @@ Foam::pointField Foam::globalMeshData::sharedPoints() const
}
// Receive data from slaves and insert
for
(
int slave=Pstream::firstSlave();
slave<=Pstream::lastSlave();
slave++
)
for (const int slave : Pstream::subProcs())
{
IPstream fromSlave(Pstream::commsTypes::blocking, slave);
......@@ -1929,12 +1914,7 @@ Foam::pointField Foam::globalMeshData::sharedPoints() const
}
// Send back
for
(
int slave=Pstream::firstSlave();
slave<=Pstream::lastSlave();
slave++
)
for (const int slave : Pstream::subProcs())
{
OPstream toSlave
(
......
......@@ -80,12 +80,7 @@ Foam::List<Foam::labelPair> Foam::mapDistributeBase::schedu