From 5dc5ea928ab7ea6f22af8c14fd28499308c650a4 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Mon, 28 Sep 2020 11:57:40 +0200
Subject: [PATCH] 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
      )
      {
          ...
      }
---
 applications/test/FixedList/Test-FixedList.C  |  7 +---
 .../test/IndirectList/Test-IndirectList.C     |  7 +---
 .../Test-parallel-communicators.C             | 14 +------
 .../Test-parallel-nonBlocking.C               | 14 +------
 applications/test/parallel/Test-parallel.C    | 28 ++-----------
 applications/test/router/Gather/Gather.C      | 14 +------
 .../mesh/manipulation/checkMesh/checkTools.C  |  4 +-
 .../splitMeshRegions/splitMeshRegions.C       |  9 +----
 .../redistributePar/loadOrCreateMesh.C        |  9 +----
 .../redistributePar/redistributePar.C         |  2 +-
 .../decomposedBlockData/decomposedBlockData.C | 28 ++-----------
 .../db/IOstreams/Fstreams/masterOFstream.C    |  2 +-
 src/OpenFOAM/db/IOstreams/Pstreams/UPstream.H | 38 +++++++++++-------
 src/OpenFOAM/global/argList/argList.C         | 23 ++---------
 .../masterUncollatedFileOperation.C           | 14 +------
 .../masterUncollatedFileOperationTemplates.C  |  3 +-
 .../matrices/LUscalarMatrix/LUscalarMatrix.C  |  8 +---
 .../LUscalarMatrix/LUscalarMatrixTemplates.C  | 16 ++------
 .../polyMesh/globalMeshData/globalMeshData.C  | 28 ++-----------
 .../mapDistribute/mapDistributeBase.C         | 14 +------
 .../polyBoundaryMesh/polyBoundaryMesh.C       |  2 +-
 .../polyMesh/syncTools/syncToolsTemplates.C   | 31 +++------------
 .../PatchTools/PatchToolsGatherAndMerge.C     |  2 +-
 src/Pstream/mpi/allReduceTemplates.C          | 14 +------
 .../vtk/output/foamVtkPatchWriterTemplates.C  | 21 ++--------
 .../fvMeshDistributeTemplates.C               |  4 +-
 src/dynamicMesh/fvMeshTools/fvMeshTools.C     |  9 +----
 .../ensight/output/ensightOutput.C            | 12 +++---
 .../ensight/output/ensightOutputTemplates.C   | 12 +++---
 .../ensight/part/cells/ensightCellsIO.C       | 12 +++---
 src/fileFormats/vtk/output/foamVtkOutput.C    | 17 ++------
 .../vtk/output/foamVtkOutputTemplates.C       | 39 ++++---------------
 .../vtk/write/foamVtkSurfaceWriter.C          |  9 +----
 .../externalCoupledTemplates.C                |  2 +-
 .../lagrangian/dataCloud/dataCloudTemplates.C |  6 +--
 .../conversion/ensight/ensightOutputCloud.C   |  6 +--
 .../ensight/ensightOutputCloudTemplates.C     |  4 +-
 src/meshTools/output/foamVtkIndPatchWriter.C  |  9 +----
 src/meshTools/output/foamVtkPatchMeshWriter.C | 21 ++--------
 .../metisLikeDecomp/metisLikeDecomp.C         |  6 +--
 .../simpleGeomDecomp/simpleGeomDecomp.C       | 10 ++---
 .../distributedTriSurfaceMesh.C               |  2 +-
 .../noiseModels/surfaceNoise/surfaceNoise.C   |  6 +--
 43 files changed, 140 insertions(+), 398 deletions(-)

diff --git a/applications/test/FixedList/Test-FixedList.C b/applications/test/FixedList/Test-FixedList.C
index 5287593c1db..1257c970916 100644
--- a/applications/test/FixedList/Test-FixedList.C
+++ b/applications/test/FixedList/Test-FixedList.C
@@ -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);
diff --git a/applications/test/IndirectList/Test-IndirectList.C b/applications/test/IndirectList/Test-IndirectList.C
index 6d4a26305f5..6ea616c0ba5 100644
--- a/applications/test/IndirectList/Test-IndirectList.C
+++ b/applications/test/IndirectList/Test-IndirectList.C
@@ -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;
diff --git a/applications/test/parallel-communicators/Test-parallel-communicators.C b/applications/test/parallel-communicators/Test-parallel-communicators.C
index d0dc8dd1db5..99a3158d060 100644
--- a/applications/test/parallel-communicators/Test-parallel-communicators.C
+++ b/applications/test/parallel-communicators/Test-parallel-communicators.C
@@ -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
                 (
diff --git a/applications/test/parallel-nonBlocking/Test-parallel-nonBlocking.C b/applications/test/parallel-nonBlocking/Test-parallel-nonBlocking.C
index 7fca05e3c22..8d80b9bab53 100644
--- a/applications/test/parallel-nonBlocking/Test-parallel-nonBlocking.C
+++ b/applications/test/parallel-nonBlocking/Test-parallel-nonBlocking.C
@@ -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);
diff --git a/applications/test/parallel/Test-parallel.C b/applications/test/parallel/Test-parallel.C
index f87a699be8e..b6ac4f7e288 100644
--- a/applications/test/parallel/Test-parallel.C
+++ b/applications/test/parallel/Test-parallel.C
@@ -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);
diff --git a/applications/test/router/Gather/Gather.C b/applications/test/router/Gather/Gather.C
index 176f2b7341f..cf3542edad2 100644
--- a/applications/test/router/Gather/Gather.C
+++ b/applications/test/router/Gather/Gather.C
@@ -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);
 
diff --git a/applications/utilities/mesh/manipulation/checkMesh/checkTools.C b/applications/utilities/mesh/manipulation/checkMesh/checkTools.C
index 72512b5e8cd..3b71ef1cc8c 100644
--- a/applications/utilities/mesh/manipulation/checkMesh/checkTools.C
+++ b/applications/utilities/mesh/manipulation/checkMesh/checkTools.C
@@ -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);
 
diff --git a/applications/utilities/mesh/manipulation/splitMeshRegions/splitMeshRegions.C b/applications/utilities/mesh/manipulation/splitMeshRegions/splitMeshRegions.C
index 1f429e77779..22060652a6d 100644
--- a/applications/utilities/mesh/manipulation/splitMeshRegions/splitMeshRegions.C
+++ b/applications/utilities/mesh/manipulation/splitMeshRegions/splitMeshRegions.C
@@ -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);
 
diff --git a/applications/utilities/parallelProcessing/redistributePar/loadOrCreateMesh.C b/applications/utilities/parallelProcessing/redistributePar/loadOrCreateMesh.C
index fb4245fa241..f905908adc3 100644
--- a/applications/utilities/parallelProcessing/redistributePar/loadOrCreateMesh.C
+++ b/applications/utilities/parallelProcessing/redistributePar/loadOrCreateMesh.C
@@ -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;
diff --git a/applications/utilities/parallelProcessing/redistributePar/redistributePar.C b/applications/utilities/parallelProcessing/redistributePar/redistributePar.C
index fd916b8476f..9eaae1eb1a9 100644
--- a/applications/utilities/parallelProcessing/redistributePar/redistributePar.C
+++ b/applications/utilities/parallelProcessing/redistributePar/redistributePar.C
@@ -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])
                     {
diff --git a/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockData.C b/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockData.C
index 7be8601aa9e..64b271ab78d 100644
--- a/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockData.C
+++ b/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockData.C
@@ -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");
diff --git a/src/OpenFOAM/db/IOstreams/Fstreams/masterOFstream.C b/src/OpenFOAM/db/IOstreams/Fstreams/masterOFstream.C
index dc10310c9e9..d9a9e7a9d26 100644
--- a/src/OpenFOAM/db/IOstreams/Fstreams/masterOFstream.C
+++ b/src/OpenFOAM/db/IOstreams/Fstreams/masterOFstream.C
@@ -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);
 
diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.H b/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.H
index 4628871b309..e8d18edb29d 100644
--- a/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.H
+++ b/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.H
@@ -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;
+        }
 };
 
 
diff --git a/src/OpenFOAM/global/argList/argList.C b/src/OpenFOAM/global/argList/argList.C
index e808f5993d2..fcc4c535761 100644
--- a/src/OpenFOAM/global/argList/argList.C
+++ b/src/OpenFOAM/global/argList/argList.C
@@ -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();
diff --git a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C
index e1b9401108b..ba488be83ea 100644
--- a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C
+++ b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C
@@ -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
                     (
diff --git a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperationTemplates.C b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperationTemplates.C
index 7f613e5a5f9..9b6228f3ab7 100644
--- a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperationTemplates.C
+++ b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperationTemplates.C
@@ -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];
diff --git a/src/OpenFOAM/matrices/LUscalarMatrix/LUscalarMatrix.C b/src/OpenFOAM/matrices/LUscalarMatrix/LUscalarMatrix.C
index e8ab17c740f..a8e56660606 100644
--- a/src/OpenFOAM/matrices/LUscalarMatrix/LUscalarMatrix.C
+++ b/src/OpenFOAM/matrices/LUscalarMatrix/LUscalarMatrix.C
@@ -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
                 (
diff --git a/src/OpenFOAM/matrices/LUscalarMatrix/LUscalarMatrixTemplates.C b/src/OpenFOAM/matrices/LUscalarMatrix/LUscalarMatrixTemplates.C
index fcd19ae15c9..1c8d0fe6e73 100644
--- a/src/OpenFOAM/matrices/LUscalarMatrix/LUscalarMatrixTemplates.C
+++ b/src/OpenFOAM/matrices/LUscalarMatrix/LUscalarMatrixTemplates.C
@@ -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
                 (
diff --git a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalMeshData.C b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalMeshData.C
index e063318239e..c553ca8affc 100644
--- a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalMeshData.C
+++ b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalMeshData.C
@@ -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
             (
diff --git a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistributeBase.C b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistributeBase.C
index 5b90f95a532..59c00950998 100644
--- a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistributeBase.C
+++ b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistributeBase.C
@@ -80,12 +80,7 @@ Foam::List<Foam::labelPair> Foam::mapDistributeBase::schedule
     if (Pstream::master())
     {
         // Receive and merge
-        for
-        (
-            int slave=Pstream::firstSlave();
-            slave<=Pstream::lastSlave();
-            slave++
-        )
+        for (const int slave : Pstream::subProcs())
         {
             IPstream fromSlave(Pstream::commsTypes::scheduled, slave, 0, tag);
             List<labelPair> nbrData(fromSlave);
@@ -101,12 +96,7 @@ Foam::List<Foam::labelPair> Foam::mapDistributeBase::schedule
             }
         }
         // Send back
-        for
-        (
-            int slave=Pstream::firstSlave();
-            slave<=Pstream::lastSlave();
-            slave++
-        )
+        for (const int slave : Pstream::subProcs())
         {
             OPstream toSlave(Pstream::commsTypes::scheduled, slave, 0, tag);
             toSlave << allComms;
diff --git a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C
index ec523bc956d..2cb398068ad 100644
--- a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C
+++ b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C
@@ -1007,7 +1007,7 @@ bool Foam::polyBoundaryMesh::checkParallelSync(const bool report) const
     // Have every processor check but print error on master
     // (in processor sequence).
 
-    for (label proci = 1; proci < Pstream::nProcs(); ++proci)
+    for (const int proci : Pstream::subProcs())
     {
         if
         (
diff --git a/src/OpenFOAM/meshes/polyMesh/syncTools/syncToolsTemplates.C b/src/OpenFOAM/meshes/polyMesh/syncTools/syncToolsTemplates.C
index c88115b2320..23a94cc8eba 100644
--- a/src/OpenFOAM/meshes/polyMesh/syncTools/syncToolsTemplates.C
+++ b/src/OpenFOAM/meshes/polyMesh/syncTools/syncToolsTemplates.C
@@ -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.
@@ -291,12 +291,7 @@ void Foam::syncTools::syncPointMap
             if (Pstream::master())
             {
                 // Receive the edges using shared points from the slave.
-                for
-                (
-                    int slave=Pstream::firstSlave();
-                    slave<=Pstream::lastSlave();
-                    slave++
-                )
+                for (const int slave : Pstream::subProcs())
                 {
                     IPstream fromSlave(Pstream::commsTypes::scheduled, slave);
                     Map<T> nbrValues(fromSlave);
@@ -315,12 +310,7 @@ void Foam::syncTools::syncPointMap
                 }
 
                 // Send back
-                for
-                (
-                    int slave=Pstream::firstSlave();
-                    slave<=Pstream::lastSlave();
-                    slave++
-                )
+                for (const int slave : Pstream::subProcs())
                 {
                     OPstream toSlave(Pstream::commsTypes::scheduled, slave);
                     toSlave << sharedPointValues;
@@ -656,12 +646,7 @@ void Foam::syncTools::syncEdgeMap
         if (Pstream::master())
         {
             // Receive the edges using shared points from the slave.
-            for
-            (
-                int slave=Pstream::firstSlave();
-                slave<=Pstream::lastSlave();
-                slave++
-            )
+            for (const int slave : Pstream::subProcs())
             {
                 IPstream fromSlave(Pstream::commsTypes::scheduled, slave);
                 EdgeMap<T> nbrValues(fromSlave);
@@ -680,14 +665,8 @@ void Foam::syncTools::syncEdgeMap
             }
 
             // Send back
-            for
-            (
-                int slave=Pstream::firstSlave();
-                slave<=Pstream::lastSlave();
-                slave++
-            )
+            for (const int slave : Pstream::subProcs())
             {
-
                 OPstream toSlave(Pstream::commsTypes::scheduled, slave);
                 toSlave << sharedEdgeValues;
             }
diff --git a/src/OpenFOAM/meshes/primitiveMesh/PatchTools/PatchToolsGatherAndMerge.C b/src/OpenFOAM/meshes/primitiveMesh/PatchTools/PatchToolsGatherAndMerge.C
index 8eb0020c81c..aeeb4ba59a3 100644
--- a/src/OpenFOAM/meshes/primitiveMesh/PatchTools/PatchToolsGatherAndMerge.C
+++ b/src/OpenFOAM/meshes/primitiveMesh/PatchTools/PatchToolsGatherAndMerge.C
@@ -170,7 +170,7 @@ void Foam::PatchTools::gatherAndMerge
 
 
             // Receive slave ones
-            for (int slave=1; slave<Pstream::nProcs(); slave++)
+            for (const int slave : Pstream::subProcs())
             {
                 IPstream fromSlave(Pstream::commsTypes::scheduled, slave);
 
diff --git a/src/Pstream/mpi/allReduceTemplates.C b/src/Pstream/mpi/allReduceTemplates.C
index 37c3fdc20f4..2c8e928407d 100644
--- a/src/Pstream/mpi/allReduceTemplates.C
+++ b/src/Pstream/mpi/allReduceTemplates.C
@@ -54,12 +54,7 @@ void Foam::allReduce
     {
         if (UPstream::master(communicator))
         {
-            for
-            (
-                int proci=UPstream::firstSlave();
-                proci<=UPstream::lastSlave(communicator);
-                ++proci
-            )
+            for (const int proci : UPstream::subProcs(communicator))
             {
                 Type value;
 
@@ -109,12 +104,7 @@ void Foam::allReduce
 
         if (UPstream::master(communicator))
         {
-            for
-            (
-                int proci=UPstream::firstSlave();
-                proci<=UPstream::lastSlave(communicator);
-                ++proci
-            )
+            for (const int proci : UPstream::subProcs(communicator))
             {
                 if
                 (
diff --git a/src/conversion/vtk/output/foamVtkPatchWriterTemplates.C b/src/conversion/vtk/output/foamVtkPatchWriterTemplates.C
index fe2dbcc1dcf..9d52a55f663 100644
--- a/src/conversion/vtk/output/foamVtkPatchWriterTemplates.C
+++ b/src/conversion/vtk/output/foamVtkPatchWriterTemplates.C
@@ -97,12 +97,7 @@ void Foam::vtk::patchWriter::write
             Field<Type> recv;
 
             // Receive each patch field and write
-            for
-            (
-                int slave=Pstream::firstSlave();
-                slave<=Pstream::lastSlave();
-                ++slave
-            )
+            for (const int slave : Pstream::subProcs())
             {
                 IPstream fromSlave(Pstream::commsTypes::blocking, slave);
 
@@ -214,12 +209,7 @@ void Foam::vtk::patchWriter::write
             Field<Type> recv;
 
             // Receive each patch field and write
-            for
-            (
-                int slave=Pstream::firstSlave();
-                slave<=Pstream::lastSlave();
-                ++slave
-            )
+            for (const int slave : Pstream::subProcs())
             {
                 IPstream fromSlave(Pstream::commsTypes::blocking, slave);
 
@@ -348,12 +338,7 @@ void Foam::vtk::patchWriter::write
             Field<Type> recv;
 
             // Receive each patch field and write
-            for
-            (
-                int slave=Pstream::firstSlave();
-                slave<=Pstream::lastSlave();
-                ++slave
-            )
+            for (const int slave : Pstream::subProcs())
             {
                 IPstream fromSlave(Pstream::commsTypes::blocking, slave);
 
diff --git a/src/dynamicMesh/fvMeshDistribute/fvMeshDistributeTemplates.C b/src/dynamicMesh/fvMeshDistribute/fvMeshDistributeTemplates.C
index e51b0668f25..f98e21d6437 100644
--- a/src/dynamicMesh/fvMeshDistribute/fvMeshDistributeTemplates.C
+++ b/src/dynamicMesh/fvMeshDistribute/fvMeshDistributeTemplates.C
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2016 OpenFOAM Foundation
-    Copyright (C) 2015-2019 OpenCFD Ltd.
+    Copyright (C) 2015-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -307,7 +307,7 @@ void Foam::fvMeshDistribute::getFieldNames
         Pstream::gatherList(allNames);
         Pstream::scatterList(allNames);
 
-        for (label proci = 1; proci < Pstream::nProcs(); proci++)
+        for (const int proci : Pstream::subProcs())
         {
             if (allNames[proci] != allNames[0])
             {
diff --git a/src/dynamicMesh/fvMeshTools/fvMeshTools.C b/src/dynamicMesh/fvMeshTools/fvMeshTools.C
index c508bb15b92..1fbc71b471a 100644
--- a/src/dynamicMesh/fvMeshTools/fvMeshTools.C
+++ b/src/dynamicMesh/fvMeshTools/fvMeshTools.C
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2012-2016 OpenFOAM Foundation
-    Copyright (C) 2015-2019 OpenCFD Ltd.
+    Copyright (C) 2015-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -472,12 +472,7 @@ Foam::autoPtr<Foam::fvMesh> Foam::fvMeshTools::newMesh
         );
 
         // 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;
diff --git a/src/fileFormats/ensight/output/ensightOutput.C b/src/fileFormats/ensight/output/ensightOutput.C
index da614e896ac..61045f2ef41 100644
--- a/src/fileFormats/ensight/output/ensightOutput.C
+++ b/src/fileFormats/ensight/output/ensightOutput.C
@@ -290,11 +290,11 @@ void Foam::ensightOutput::writeFaceConnectivity
 
     parallel = parallel && Pstream::parRun();
 
-    const labelRange senders =
+    const IntRange<int> senders =
     (
         parallel
-      ? labelRange(1, Pstream::nProcs()-1)
-      : labelRange()
+      ? Pstream::subProcs()
+      : IntRange<int>()
     );
 
     if (Pstream::master())
@@ -381,11 +381,11 @@ void Foam::ensightOutput::writeFaceConnectivity
 
     parallel = parallel && Pstream::parRun();
 
-    const labelRange senders =
+    const IntRange<int> senders =
     (
         parallel
-      ? labelRange(1, Pstream::nProcs()-1)
-      : labelRange()
+      ? Pstream::subProcs()
+      : IntRange<int>()
     );
 
 
diff --git a/src/fileFormats/ensight/output/ensightOutputTemplates.C b/src/fileFormats/ensight/output/ensightOutputTemplates.C
index f17cd0d5616..00f5ab4d234 100644
--- a/src/fileFormats/ensight/output/ensightOutputTemplates.C
+++ b/src/fileFormats/ensight/output/ensightOutputTemplates.C
@@ -63,11 +63,11 @@ bool Foam::ensightOutput::Detail::writeCoordinates
 {
     parallel = parallel && Pstream::parRun();
 
-    const labelRange senders =
+    const IntRange<int> senders =
     (
         parallel
-      ? labelRange(1, Pstream::nProcs()-1)
-      : labelRange()
+      ? Pstream::subProcs()
+      : IntRange<int>()
     );
 
 
@@ -130,11 +130,11 @@ bool Foam::ensightOutput::Detail::writeFieldComponents
 {
     parallel = parallel && Pstream::parRun();
 
-    const labelRange senders =
+    const IntRange<int> senders =
     (
         parallel
-      ? labelRange(1, Pstream::nProcs()-1)
-      : labelRange()
+      ? Pstream::subProcs()
+      : IntRange<int>()
     );
 
 
diff --git a/src/fileFormats/ensight/part/cells/ensightCellsIO.C b/src/fileFormats/ensight/part/cells/ensightCellsIO.C
index e5f080803b0..43c1133f825 100644
--- a/src/fileFormats/ensight/part/cells/ensightCellsIO.C
+++ b/src/fileFormats/ensight/part/cells/ensightCellsIO.C
@@ -52,11 +52,11 @@ void Foam::ensightCells::writePolysConnectivity
         return;
     }
 
-    const labelRange senders =
+    const IntRange<int> senders =
     (
         parallel
-      ? labelRange(1, Pstream::nProcs()-1)
-      : labelRange()
+      ? Pstream::subProcs()
+      : IntRange<int>()
     );
 
 
@@ -213,11 +213,11 @@ void Foam::ensightCells::writeShapeConnectivity
     }
 
 
-    const labelRange senders =
+    const IntRange<int> senders =
     (
         parallel
-      ? labelRange(1, Pstream::nProcs()-1)
-      : labelRange()
+      ? Pstream::subProcs()
+      : IntRange<int>()
     );
 
 
diff --git a/src/fileFormats/vtk/output/foamVtkOutput.C b/src/fileFormats/vtk/output/foamVtkOutput.C
index cf6351be260..7e32b87dcf0 100644
--- a/src/fileFormats/vtk/output/foamVtkOutput.C
+++ b/src/fileFormats/vtk/output/foamVtkOutput.C
@@ -5,7 +5,7 @@
     \\  /    A nd           | www.openfoam.com
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
-    Copyright (C) 2016-2019 OpenCFD Ltd.
+    Copyright (C) 2016-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -135,12 +135,7 @@ void Foam::vtk::writeListParallel
         List<uint8_t> recv;
 
         // Receive and write
-        for
-        (
-            int slave=Pstream::firstSlave();
-            slave<=Pstream::lastSlave();
-            ++slave
-        )
+        for (const int slave : Pstream::subProcs())
         {
             IPstream fromSlave(Pstream::commsTypes::blocking, slave);
 
@@ -183,12 +178,7 @@ void Foam::vtk::writeListParallel
         labelList recv;
 
         // Receive and write
-        for
-        (
-            int slave=Pstream::firstSlave();
-            slave<=Pstream::lastSlave();
-            ++slave
-        )
+        for (const int slave : Pstream::subProcs())
         {
             IPstream fromSlave(Pstream::commsTypes::blocking, slave);
 
@@ -219,7 +209,6 @@ void Foam::vtk::writeListParallel
 
 // * * * * * * * * * * * * * * Legacy Functions  * * * * * * * * * * * * * * //
 
-
 void Foam::vtk::legacy::fileHeader
 (
     std::ostream& os,
diff --git a/src/fileFormats/vtk/output/foamVtkOutputTemplates.C b/src/fileFormats/vtk/output/foamVtkOutputTemplates.C
index 7573440f087..e296a2b2b56 100644
--- a/src/fileFormats/vtk/output/foamVtkOutputTemplates.C
+++ b/src/fileFormats/vtk/output/foamVtkOutputTemplates.C
@@ -5,7 +5,7 @@
     \\  /    A nd           | www.openfoam.com
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
-    Copyright (C) 2016-2019 OpenCFD Ltd.
+    Copyright (C) 2016-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -136,12 +136,7 @@ void Foam::vtk::writeListParallel
         List<Type> recv;
 
         // Receive and write
-        for
-        (
-            int slave=Pstream::firstSlave();
-            slave<=Pstream::lastSlave();
-            ++slave
-        )
+        for (const int slave : Pstream::subProcs())
         {
             IPstream fromSlave(Pstream::commsTypes::blocking, slave);
 
@@ -179,12 +174,7 @@ void Foam::vtk::writeListParallel
         List<Type> recv;
 
         // Receive and write
-        for
-        (
-            int slave=Pstream::firstSlave();
-            slave<=Pstream::lastSlave();
-            ++slave
-        )
+        for (const int slave : Pstream::subProcs())
         {
             IPstream fromSlave(Pstream::commsTypes::blocking, slave);
 
@@ -222,12 +212,7 @@ void Foam::vtk::writeListParallel
         List<Type> recv;
 
         // Receive and write
-        for
-        (
-            int slave=Pstream::firstSlave();
-            slave<=Pstream::lastSlave();
-            ++slave
-        )
+        for (const int slave : Pstream::subProcs())
         {
             IPstream fromSlave(Pstream::commsTypes::blocking, slave);
 
@@ -266,12 +251,7 @@ void Foam::vtk::writeListsParallel
         List<Type> recv1, recv2;
 
         // Receive and write
-        for
-        (
-            int slave=Pstream::firstSlave();
-            slave<=Pstream::lastSlave();
-            ++slave
-        )
+        for (const int slave : Pstream::subProcs())
         {
             IPstream fromSlave(Pstream::commsTypes::blocking, slave);
 
@@ -310,14 +290,9 @@ void Foam::vtk::writeListsParallel
         vtk::writeList(fmt, values2, addressing);
 
         List<Type> recv1, recv2;
-;
+
         // Receive and write
-        for
-        (
-            int slave=Pstream::firstSlave();
-            slave<=Pstream::lastSlave();
-            ++slave
-        )
+        for (const int slave : Pstream::subProcs())
         {
             IPstream fromSlave(Pstream::commsTypes::blocking, slave);
 
diff --git a/src/fileFormats/vtk/write/foamVtkSurfaceWriter.C b/src/fileFormats/vtk/write/foamVtkSurfaceWriter.C
index 2b4369a9591..bb8b3fac7cc 100644
--- a/src/fileFormats/vtk/write/foamVtkSurfaceWriter.C
+++ b/src/fileFormats/vtk/write/foamVtkSurfaceWriter.C
@@ -5,7 +5,7 @@
     \\  /    A nd           | www.openfoam.com
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
-    Copyright (C) 2018-2019 OpenCFD Ltd.
+    Copyright (C) 2018-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -104,12 +104,7 @@ void Foam::vtk::surfaceWriter::writePoints()
             pointField recv;
 
             // Receive each point field and write
-            for
-            (
-                int slave=Pstream::firstSlave();
-                slave<=Pstream::lastSlave();
-                ++slave
-            )
+            for (const int slave : Pstream::subProcs())
             {
                 IPstream fromSlave(Pstream::commsTypes::blocking, slave);
 
diff --git a/src/functionObjects/field/externalCoupled/externalCoupledTemplates.C b/src/functionObjects/field/externalCoupled/externalCoupledTemplates.C
index c3a7c939755..e3e8a3a2409 100644
--- a/src/functionObjects/field/externalCoupled/externalCoupledTemplates.C
+++ b/src/functionObjects/field/externalCoupled/externalCoupledTemplates.C
@@ -409,7 +409,7 @@ bool Foam::functionObjects::externalCoupled::writeData
                     }
                     masterFilePtr() << os.str().c_str();
 
-                    for (label proci = 1; proci < Pstream::nProcs(); proci++)
+                    for (const int proci : Pstream::subProcs())
                     {
                         IPstream fromSlave
                         (
diff --git a/src/functionObjects/lagrangian/dataCloud/dataCloudTemplates.C b/src/functionObjects/lagrangian/dataCloud/dataCloudTemplates.C
index 5f5c3daeb24..c90205543d2 100644
--- a/src/functionObjects/lagrangian/dataCloud/dataCloudTemplates.C
+++ b/src/functionObjects/lagrangian/dataCloud/dataCloudTemplates.C
@@ -5,7 +5,7 @@
     \\  /    A nd           | www.openfoam.com
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
-    Copyright (C) 2018-2019 OpenCFD Ltd.
+    Copyright (C) 2018-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -85,7 +85,7 @@ void Foam::functionObjects::dataCloud::writeListParallel
         Field<Type> recvField;
 
         // Receive and write
-        for (int slave=1; slave<Pstream::nProcs(); ++slave)
+        for (const int slave : Pstream::subProcs())
         {
             IPstream fromSlave(Pstream::commsTypes::blocking, slave);
 
@@ -142,7 +142,7 @@ void Foam::functionObjects::dataCloud::writeListParallel
         Field<Type> recvField;
 
         // Receive and write
-        for (int slave=1; slave<Pstream::nProcs(); ++slave)
+        for (const int slave : Pstream::subProcs())
         {
             IPstream fromSlave(Pstream::commsTypes::blocking, slave);
 
diff --git a/src/lagrangian/intermediate/conversion/ensight/ensightOutputCloud.C b/src/lagrangian/intermediate/conversion/ensight/ensightOutputCloud.C
index afdb64d1676..28c512a8b43 100644
--- a/src/lagrangian/intermediate/conversion/ensight/ensightOutputCloud.C
+++ b/src/lagrangian/intermediate/conversion/ensight/ensightOutputCloud.C
@@ -5,7 +5,7 @@
     \\  /    A nd           | www.openfoam.com
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
-    Copyright (C) 2016-2019 OpenCFD Ltd.
+    Copyright (C) 2016-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -132,7 +132,7 @@ bool Foam::ensightOutput::writeCloudPositions
             writeMeasured(os, positions);
 
             // Slaves
-            for (int slave=1; slave<Pstream::nProcs(); ++slave)
+            for (const int slave : Pstream::subProcs())
             {
                 IPstream fromSlave(comm, slave);
                 pointField recv(fromSlave);
@@ -149,7 +149,7 @@ bool Foam::ensightOutput::writeCloudPositions
             parcelId = writeMeasured(os, parcelId, positions);
 
             // Slaves
-            for (int slave=1; slave<Pstream::nProcs(); ++slave)
+            for (const int slave : Pstream::subProcs())
             {
                 IPstream fromSlave(comm, slave);
                 pointField recv(fromSlave);
diff --git a/src/lagrangian/intermediate/conversion/ensight/ensightOutputCloudTemplates.C b/src/lagrangian/intermediate/conversion/ensight/ensightOutputCloudTemplates.C
index 497484178e9..c07cb1667f5 100644
--- a/src/lagrangian/intermediate/conversion/ensight/ensightOutputCloudTemplates.C
+++ b/src/lagrangian/intermediate/conversion/ensight/ensightOutputCloudTemplates.C
@@ -5,7 +5,7 @@
     \\  /    A nd           | www.openfoam.com
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
-    Copyright (C) 2016-2019 OpenCFD Ltd.
+    Copyright (C) 2016-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -71,7 +71,7 @@ bool Foam::ensightOutput::writeCloudField
         }
 
         // Slaves
-        for (int slave=1; slave<Pstream::nProcs(); ++slave)
+        for (const int slave : Pstream::subProcs())
         {
             IPstream fromSlave(comm, slave);
             Field<Type> recv(fromSlave);
diff --git a/src/meshTools/output/foamVtkIndPatchWriter.C b/src/meshTools/output/foamVtkIndPatchWriter.C
index 896e65fda23..2adbf7dfffe 100644
--- a/src/meshTools/output/foamVtkIndPatchWriter.C
+++ b/src/meshTools/output/foamVtkIndPatchWriter.C
@@ -5,7 +5,7 @@
     \\  /    A nd           | www.openfoam.com
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
-    Copyright (C) 2018 OpenCFD Ltd.
+    Copyright (C) 2018-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -104,12 +104,7 @@ void Foam::vtk::indirectPatchWriter::writePoints()
             pointField recv;
 
             // Receive each point field and write
-            for
-            (
-                int slave=Pstream::firstSlave();
-                slave<=Pstream::lastSlave();
-                ++slave
-            )
+            for (const int slave : Pstream::subProcs())
             {
                 IPstream fromSlave(Pstream::commsTypes::blocking, slave);
 
diff --git a/src/meshTools/output/foamVtkPatchMeshWriter.C b/src/meshTools/output/foamVtkPatchMeshWriter.C
index 6896777f7fe..fc66f32ee4d 100644
--- a/src/meshTools/output/foamVtkPatchMeshWriter.C
+++ b/src/meshTools/output/foamVtkPatchMeshWriter.C
@@ -125,12 +125,7 @@ void Foam::vtk::patchMeshWriter::writePoints()
             pointField recv;
 
             // Receive each point field and write
-            for
-            (
-                int slave=Pstream::firstSlave();
-                slave<=Pstream::lastSlave();
-                ++slave
-            )
+            for (const int slave : Pstream::subProcs())
             {
                 IPstream fromSlave(Pstream::commsTypes::blocking, slave);
 
@@ -584,12 +579,7 @@ void Foam::vtk::patchMeshWriter::writePatchIDs()
             labelList recv;
 
             // Receive each pair
-            for
-            (
-                int slave=Pstream::firstSlave();
-                slave<=Pstream::lastSlave();
-                ++slave
-            )
+            for (const int slave : Pstream::subProcs())
             {
                 IPstream fromSlave(Pstream::commsTypes::blocking, slave);
 
@@ -793,12 +783,7 @@ bool Foam::vtk::patchMeshWriter::writeNeighIDs()
             labelList recv;
 
             // Receive each pair
-            for
-            (
-                int slave=Pstream::firstSlave();
-                slave<=Pstream::lastSlave();
-                ++slave
-            )
+            for (const int slave : Pstream::subProcs())
             {
                 IPstream fromSlave(Pstream::commsTypes::blocking, slave);
 
diff --git a/src/parallel/decompose/decompositionMethods/metisLikeDecomp/metisLikeDecomp.C b/src/parallel/decompose/decompositionMethods/metisLikeDecomp/metisLikeDecomp.C
index b978a5e64b7..d4a6198c064 100644
--- a/src/parallel/decompose/decompositionMethods/metisLikeDecomp/metisLikeDecomp.C
+++ b/src/parallel/decompose/decompositionMethods/metisLikeDecomp/metisLikeDecomp.C
@@ -5,7 +5,7 @@
     \\  /    A nd           | www.openfoam.com
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
-    Copyright (C) 2017-2019 OpenCFD Ltd.
+    Copyright (C) 2017-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -78,7 +78,7 @@ Foam::label Foam::metisLikeDecomp::decomposeGeneral
             allAdjncy[nTotalConnections++] = adjncy[i];
         }
 
-        for (int slave=1; slave<Pstream::nProcs(); ++slave)
+        for (const int slave : Pstream::subProcs())
         {
             IPstream fromSlave(Pstream::commsTypes::scheduled, slave);
             List<label> nbrAdjncy(fromSlave);
@@ -110,7 +110,7 @@ Foam::label Foam::metisLikeDecomp::decomposeGeneral
 
 
         // Send allFinalDecomp back
-        for (int slave=1; slave<Pstream::nProcs(); ++slave)
+        for (const int slave : Pstream::subProcs())
         {
             OPstream toSlave(Pstream::commsTypes::scheduled, slave);
             toSlave << SubList<label>
diff --git a/src/parallel/decompose/decompositionMethods/simpleGeomDecomp/simpleGeomDecomp.C b/src/parallel/decompose/decompositionMethods/simpleGeomDecomp/simpleGeomDecomp.C
index 94b6a10f783..e5625bf2b6a 100644
--- a/src/parallel/decompose/decompositionMethods/simpleGeomDecomp/simpleGeomDecomp.C
+++ b/src/parallel/decompose/decompositionMethods/simpleGeomDecomp/simpleGeomDecomp.C
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2017 OpenFOAM Foundation
-    Copyright (C) 2017-2018 OpenCFD Ltd.
+    Copyright (C) 2017-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -350,7 +350,7 @@ Foam::labelList Foam::simpleGeomDecomp::decompose
             nTotalPoints += points.size();
 
             // Add slaves
-            for (int slave=1; slave<Pstream::nProcs(); ++slave)
+            for (const int slave : Pstream::subProcs())
             {
                 IPstream fromSlave(Pstream::commsTypes::scheduled, slave);
                 pointField nbrPoints(fromSlave);
@@ -367,7 +367,7 @@ Foam::labelList Foam::simpleGeomDecomp::decompose
             labelList finalDecomp(decomposeOneProc(allPoints));
 
             // Send back
-            for (int slave=1; slave<Pstream::nProcs(); ++slave)
+            for (const int slave : Pstream::subProcs())
             {
                 OPstream toSlave(Pstream::commsTypes::scheduled, slave);
                 toSlave << SubField<label>
@@ -435,7 +435,7 @@ Foam::labelList Foam::simpleGeomDecomp::decompose
             nTotalPoints += points.size();
 
             // Add slaves
-            for (int slave=1; slave<Pstream::nProcs(); ++slave)
+            for (const int slave : Pstream::subProcs())
             {
                 IPstream fromSlave(Pstream::commsTypes::scheduled, slave);
                 pointField nbrPoints(fromSlave);
@@ -459,7 +459,7 @@ Foam::labelList Foam::simpleGeomDecomp::decompose
             labelList finalDecomp(decomposeOneProc(allPoints, allWeights));
 
             // Send back
-            for (int slave=1; slave<Pstream::nProcs(); ++slave)
+            for (const int slave : Pstream::subProcs())
             {
                 OPstream toSlave(Pstream::commsTypes::scheduled, slave);
                 toSlave << SubField<label>
diff --git a/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C b/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C
index 3740e366153..0aa35c88d97 100644
--- a/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C
+++ b/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C
@@ -1655,7 +1655,7 @@ Foam::distributedTriSurfaceMesh::independentlyDistributedBbs
     bool masterOnly;
     {
         masterOnly = true;
-        for (label proci = 1; proci < Pstream::nProcs(); proci++)
+        for (const int proci : Pstream::subProcs())
         {
             if (triIndexer.localSize(proci) != 0)
             {
diff --git a/src/randomProcesses/noise/noiseModels/surfaceNoise/surfaceNoise.C b/src/randomProcesses/noise/noiseModels/surfaceNoise/surfaceNoise.C
index c01eaafb0d6..8c0b4bd8861 100644
--- a/src/randomProcesses/noise/noiseModels/surfaceNoise/surfaceNoise.C
+++ b/src/randomProcesses/noise/noiseModels/surfaceNoise/surfaceNoise.C
@@ -5,7 +5,7 @@
     \\  /    A nd           | www.openfoam.com
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
-    Copyright (C) 2015-2019 OpenCFD Ltd.
+    Copyright (C) 2015-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -267,7 +267,7 @@ Foam::scalar surfaceNoise::writeSurfaceData
                 allData[faceI] = data[faceI];
             }
 
-            for (label procI = 1; procI < Pstream::nProcs(); ++procI)
+            for (const int procI : Pstream::subProcs())
             {
                 UIPstream fromProc(procI, pBufs);
                 scalarList dataSlice(fromProc);
@@ -370,7 +370,7 @@ Foam::scalar surfaceNoise::surfaceAverage
                 allData[faceI] = data[faceI];
             }
 
-            for (label procI = 1; procI < Pstream::nProcs(); procI++)
+            for (const int procI : Pstream::subProcs())
             {
                 UIPstream fromProc(procI, pBufs);
                 scalarList dataSlice(fromProc);
-- 
GitLab