From 9c7aa9727aa5eab15aec36976ea0e4b64916105a Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Thu, 8 Oct 2020 18:33:17 +0200
Subject: [PATCH] ENH: add UPstream::parRun(const bool)

- slightly neater code when disabling/restoring parallel state

  New
      const bool oldParRun = Pstream::parRun(false);
      ...
      Pstream::parRun(oldParRun);

  Old
      const bool oldParRun = Pstream::parRun();
      Pstream::parRun() = false;
      ...
      Pstream::parRun() = oldParRun;
---
 .../manipulation/renumberMesh/renumberMesh.C    | 14 ++++++--------
 .../redistributePar/loadOrCreateMesh.C          |  5 ++---
 .../redistributePar/redistributePar.C           | 15 ++++++---------
 .../foamToEnsight/convertLagrangian.H           |  6 +++---
 .../surfaceRedistributePar.C                    |  7 +++----
 src/OpenFOAM/db/IOstreams/Pstreams/UPstream.C   |  5 +++--
 src/OpenFOAM/db/IOstreams/Pstreams/UPstream.H   |  9 +++++++++
 src/OpenFOAM/global/argList/argList.C           |  7 ++-----
 .../masterUncollatedFileOperation.C             | 17 ++++++++---------
 .../fvMeshDistribute/fvMeshDistribute.C         | 13 +++++++------
 .../inverseDistance/waveMethod.C                |  7 +++----
 .../distributedTriSurfaceMesh.C                 | 10 ++++------
 12 files changed, 56 insertions(+), 59 deletions(-)

diff --git a/applications/utilities/mesh/manipulation/renumberMesh/renumberMesh.C b/applications/utilities/mesh/manipulation/renumberMesh/renumberMesh.C
index b6bf8476a1c..5fd9762ef04 100644
--- a/applications/utilities/mesh/manipulation/renumberMesh/renumberMesh.C
+++ b/applications/utilities/mesh/manipulation/renumberMesh/renumberMesh.C
@@ -575,8 +575,7 @@ labelList regionRenumber
         Info<< "    region " << regioni << " starts at " << celli << endl;
 
         // Make sure no parallel comms
-        const bool oldParRun = UPstream::parRun();
-        UPstream::parRun() = false;
+        const bool oldParRun = UPstream::parRun(false);
 
         // Per region do a reordering.
         fvMeshSubset subsetter(mesh, regioni, cellToRegion);
@@ -589,8 +588,8 @@ labelList regionRenumber
             subMesh.cellCentres()
         );
 
-        // Restore state
-        UPstream::parRun() = oldParRun;
+        UPstream::parRun(oldParRun);  // Restore parallel state
+
 
         const labelList& cellMap = subsetter.cellMap();
 
@@ -914,8 +913,7 @@ int main(int argc, char *argv[])
         dictionary decomposeDict(renumberDictPtr().subDict("blockCoeffs"));
         decomposeDict.set("numberOfSubdomains", nBlocks);
 
-        bool oldParRun = UPstream::parRun();
-        UPstream::parRun() = false;
+        const bool oldParRun = UPstream::parRun(false);
 
         autoPtr<decompositionMethod> decomposePtr = decompositionMethod::New
         (
@@ -931,8 +929,8 @@ int main(int argc, char *argv[])
             )
         );
 
-        // Restore state
-        UPstream::parRun() = oldParRun;
+        UPstream::parRun(oldParRun);  // Restore parallel state
+
 
         // For debugging: write out region
         createScalarField
diff --git a/applications/utilities/parallelProcessing/redistributePar/loadOrCreateMesh.C b/applications/utilities/parallelProcessing/redistributePar/loadOrCreateMesh.C
index f905908adc3..dbcec2a27a4 100644
--- a/applications/utilities/parallelProcessing/redistributePar/loadOrCreateMesh.C
+++ b/applications/utilities/parallelProcessing/redistributePar/loadOrCreateMesh.C
@@ -157,8 +157,7 @@ Foam::autoPtr<Foam::fvMesh> Foam::loadOrCreateMesh
 
     if (!haveMesh)
     {
-        const bool oldParRun = Pstream::parRun();
-        Pstream::parRun() = false;
+        const bool oldParRun = Pstream::parRun(false);
 
 
         // Create dummy mesh. Only used on procs that don't have mesh.
@@ -241,7 +240,7 @@ Foam::autoPtr<Foam::fvMesh> Foam::loadOrCreateMesh
         //    << endl;
         dummyMesh.write();
 
-        Pstream::parRun() = oldParRun;
+        Pstream::parRun(oldParRun);  // Restore parallel state
     }
 
 
diff --git a/applications/utilities/parallelProcessing/redistributePar/redistributePar.C b/applications/utilities/parallelProcessing/redistributePar/redistributePar.C
index 9eaae1eb1a9..45dd9210c97 100644
--- a/applications/utilities/parallelProcessing/redistributePar/redistributePar.C
+++ b/applications/utilities/parallelProcessing/redistributePar/redistributePar.C
@@ -2425,10 +2425,9 @@ int main(int argc, char *argv[])
         instantList timeDirs;
         if (Pstream::master())
         {
-            const bool oldParRun = Pstream::parRun();
-            Pstream::parRun() = false;
+            const bool oldParRun = Pstream::parRun(false);
             timeDirs = Time::findTimes(args.path(), "constant");
-            Pstream::parRun() = oldParRun;
+            Pstream::parRun(oldParRun);  // Restore parallel state
         }
         Pstream::scatter(timeDirs);
         for (const instant& t : timeDirs)
@@ -2464,10 +2463,9 @@ int main(int argc, char *argv[])
 
         if (Pstream::master())
         {
-            const bool oldParRun = Pstream::parRun();
-            Pstream::parRun() = false;
+            const bool oldParRun = Pstream::parRun(false);
             timeDirs = Time::findTimes(basePath, "constant");
-            Pstream::parRun() = oldParRun;
+            Pstream::parRun(oldParRun);  // Restore parallel state
         }
         Pstream::scatter(timeDirs);
         for (const instant& t : timeDirs)
@@ -3038,11 +3036,10 @@ int main(int argc, char *argv[])
                 if (!Pstream::master() && !haveMesh[Pstream::myProcNo()])
                 {
                     // Remove dummy mesh created by loadOrCreateMesh
-                    const bool oldParRun = Pstream::parRun();
-                    Pstream::parRun() = false;
+                    const bool oldParRun = Pstream::parRun(false);
                     mesh.removeFiles();
                     rmDir(mesh.objectRegistry::objectPath());
-                    Pstream::parRun() = oldParRun;
+                    Pstream::parRun(oldParRun);  // Restore parallel state
                 }
                 continue;
             }
diff --git a/applications/utilities/postProcessing/dataConversion/foamToEnsight/convertLagrangian.H b/applications/utilities/postProcessing/dataConversion/foamToEnsight/convertLagrangian.H
index 143f9f226f7..92755ce449a 100644
--- a/applications/utilities/postProcessing/dataConversion/foamToEnsight/convertLagrangian.H
+++ b/applications/utilities/postProcessing/dataConversion/foamToEnsight/convertLagrangian.H
@@ -88,9 +88,9 @@ if (doLagrangian)
                 // but that combination does not work.
                 // So check the header and sync globally
 
-                fieldExists =
-                    fieldObject.typeHeaderOk<IOField<scalar>>(false);
-
+                const bool oldParRun = Pstream::parRun(false);
+                fieldExists = fieldObject.typeHeaderOk<IOField<scalar>>(false);
+                Pstream::parRun(oldParRun);  // Restore parallel state
                 reduce(fieldExists, orOp<bool>());
             }
 
diff --git a/applications/utilities/surface/surfaceRedistributePar/surfaceRedistributePar.C b/applications/utilities/surface/surfaceRedistributePar/surfaceRedistributePar.C
index 7b9824c0b0c..9ef2bbdb9fd 100644
--- a/applications/utilities/surface/surfaceRedistributePar/surfaceRedistributePar.C
+++ b/applications/utilities/surface/surfaceRedistributePar/surfaceRedistributePar.C
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2016 OpenFOAM Foundation
-    Copyright (C) 2015-2018 OpenCFD Ltd.
+    Copyright (C) 2015-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -240,10 +240,9 @@ int main(int argc, char *argv[])
         if (Pstream::master())
         {
             // Actually load the surface
-            const bool oldParRun = Pstream::parRun();
-            Pstream::parRun() = false;
+            const bool oldParRun = Pstream::parRun(false);
             triSurfaceMesh surf(io);
-            Pstream::parRun() = oldParRun;
+            Pstream::parRun(oldParRun);  // Restore parallel state
             s = surf;
             bbs = List<treeBoundBox>(1, treeBoundBox(boundBox::greatBox));
         }
diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.C b/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.C
index 2bba13bb92a..51a1dfaf946 100644
--- a/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.C
+++ b/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.C
@@ -362,6 +362,9 @@ bool Foam::UPstream::parRun_(false);
 
 bool Foam::UPstream::haveThreads_(false);
 
+int Foam::UPstream::msgType_(1);
+
+
 Foam::LIFOStack<Foam::label> Foam::UPstream::freeComms_;
 
 Foam::DynamicList<int> Foam::UPstream::myProcNo_(10);
@@ -370,8 +373,6 @@ Foam::DynamicList<Foam::List<int>> Foam::UPstream::procIDs_(10);
 
 Foam::DynamicList<Foam::label> Foam::UPstream::parentCommunicator_(10);
 
-int Foam::UPstream::msgType_(1);
-
 
 Foam::DynamicList<Foam::List<Foam::UPstream::commsStruct>>
 Foam::UPstream::linearCommunication_(10);
diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.H b/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.H
index e8d18edb29d..ede1cf9794d 100644
--- a/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.H
+++ b/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.H
@@ -412,6 +412,15 @@ public:
             static void freeTag(const word&, const int tag);
 
 
+        //- Set as parallel run on/off.
+        //  \return the previous value
+        static bool parRun(const bool on)
+        {
+            bool old(parRun_);
+            parRun_ = on;
+            return old;
+        }
+
         //- Is this a parallel run?
         static bool& parRun()
         {
diff --git a/src/OpenFOAM/global/argList/argList.C b/src/OpenFOAM/global/argList/argList.C
index fcc4c535761..735a2487c97 100644
--- a/src/OpenFOAM/global/argList/argList.C
+++ b/src/OpenFOAM/global/argList/argList.C
@@ -1249,8 +1249,7 @@ void Foam::argList::parse
                 // Disable any parallel comms happening inside the fileHandler
                 // since we are on master. This can happen e.g. inside
                 // the masterUncollated/collated handler.
-                const bool oldParRun = Pstream::parRun();
-                Pstream::parRun() = false;
+                const bool oldParRun = Pstream::parRun(false);
 
                 autoPtr<ISstream> decompDictStream
                 (
@@ -1266,9 +1265,7 @@ void Foam::argList::parse
 
                 dictionary decompDict(*decompDictStream);
 
-                // Restore parallel behaviour
-                Pstream::parRun() = oldParRun;
-
+                Pstream::parRun(oldParRun);  // Restore parallel state
 
                 decompDict.readEntry("numberOfSubdomains", dictNProcs);
 
diff --git a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C
index ba488be83ea..6adb7eada43 100644
--- a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C
+++ b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C
@@ -1686,8 +1686,7 @@ Foam::fileOperations::masterUncollatedFileOperation::readObjects
     {
         // Avoid fileOperation::readObjects from triggering parallel ops
         // (through call to filePath which triggers parallel )
-        const bool oldParRun = UPstream::parRun();
-        UPstream::parRun() = false;
+        const bool oldParRun = UPstream::parRun(false);
 
         //- Use non-time searching version
         objectNames = fileOperation::readObjects
@@ -1731,7 +1730,7 @@ Foam::fileOperations::masterUncollatedFileOperation::readObjects
             }
         }
 
-        UPstream::parRun() = oldParRun;
+        UPstream::parRun(oldParRun);  // Restore parallel state
     }
 
     Pstream::scatter(newInstance);  //, Pstream::msgType(), comm_);
@@ -2111,13 +2110,12 @@ bool Foam::fileOperations::masterUncollatedFileOperation::read
         if (Pstream::master())  // comm_))
         {
             // Do master-only reading always.
-            const bool oldParRun = UPstream::parRun();
-            UPstream::parRun() = false;
+            const bool oldParRun = UPstream::parRun(false);
 
             ok = io.readData(io.readStream(typeName));
             io.close();
 
-            UPstream::parRun() = oldParRun;
+            UPstream::parRun(oldParRun);  // Restore parallel state
         }
 
         Pstream::scatter(ok);   //, Pstream::msgType(), comm_);
@@ -2257,10 +2255,11 @@ Foam::instantList Foam::fileOperations::masterUncollatedFileOperation::findTimes
         if (Pstream::master())  // comm_))
         {
             // Do master-only reading always.
-            const bool oldParRun = UPstream::parRun();
-            UPstream::parRun() = false;
+            const bool oldParRun = UPstream::parRun(false);
+
             times = fileOperation::findTimes(directory, constantName);
-            UPstream::parRun() = oldParRun;
+
+            UPstream::parRun(oldParRun);  // Restore parallel state
         }
         Pstream::scatter(times);    //, Pstream::msgType(), comm_);
 
diff --git a/src/dynamicMesh/fvMeshDistribute/fvMeshDistribute.C b/src/dynamicMesh/fvMeshDistribute/fvMeshDistribute.C
index 15fe5ab5d00..1fe48b0fc13 100644
--- a/src/dynamicMesh/fvMeshDistribute/fvMeshDistribute.C
+++ b/src/dynamicMesh/fvMeshDistribute/fvMeshDistribute.C
@@ -1951,8 +1951,8 @@ Foam::autoPtr<Foam::mapDistributePolyMesh> Foam::fvMeshDistribute::distribute
     // What to send to neighbouring domains
     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-    bool oldParRun = UPstream::parRun();
-    UPstream::parRun() = false;
+    // Disable parallel.
+    const bool oldParRun = UPstream::parRun(false);
 
     forAll(nSendCells, recvProc)
     {
@@ -2162,7 +2162,7 @@ Foam::autoPtr<Foam::mapDistributePolyMesh> Foam::fvMeshDistribute::distribute
     }
 
 
-    UPstream::parRun() = oldParRun;
+    UPstream::parRun(oldParRun);  // Restore parallel state
 
 
     // Start sending&receiving from buffers
@@ -2279,8 +2279,8 @@ Foam::autoPtr<Foam::mapDistributePolyMesh> Foam::fvMeshDistribute::distribute
     // Receive and add what was sent
     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-    oldParRun = UPstream::parRun();
-    UPstream::parRun() = false;
+    // Disable parallel. Original state already known.
+    UPstream::parRun(false);
 
     forAll(nRevcCells, sendProc)
     {
@@ -2697,7 +2697,8 @@ Foam::autoPtr<Foam::mapDistributePolyMesh> Foam::fvMeshDistribute::distribute
         }
     }
 
-    UPstream::parRun() = oldParRun;
+    UPstream::parRun(oldParRun);  // Restore parallel state
+
 
     // Print a bit.
     if (debug)
diff --git a/src/overset/cellCellStencil/inverseDistance/waveMethod.C b/src/overset/cellCellStencil/inverseDistance/waveMethod.C
index d7d0b210aa1..ef54ca9de4f 100644
--- a/src/overset/cellCellStencil/inverseDistance/waveMethod.C
+++ b/src/overset/cellCellStencil/inverseDistance/waveMethod.C
@@ -5,7 +5,7 @@
     \\  /    A nd           | www.openfoam.com
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
-    Copyright (C) 2017 OpenCFD Ltd.
+    Copyright (C) 2017-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -55,8 +55,7 @@ void Foam::waveMethod::calculate
 
     // The actual matching is only w.r.t local cells so cannot be run in
     // parallel.
-    const bool oldParRun = Pstream::parRun();
-    Pstream::parRun() = false;
+    const bool oldParRun = Pstream::parRun(false);
 
     label nSeeds = 0;
 
@@ -144,7 +143,7 @@ void Foam::waveMethod::calculate
         }
     }
 
-    Pstream::parRun() = oldParRun;
+    Pstream::parRun(oldParRun);  // Restore parallel state
 
     if (debug)
     {
diff --git a/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C b/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C
index 0aa35c88d97..80aa0bce584 100644
--- a/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C
+++ b/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C
@@ -1691,10 +1691,9 @@ Foam::distributedTriSurfaceMesh::independentlyDistributedBbs
             calcFaceFaces(s, pointFaces, faceFaces);
 
             // Do the actual decomposition
-            const bool oldParRun = UPstream::parRun();
-            UPstream::parRun() = false;
+            const bool oldParRun = UPstream::parRun(false);
             distribution = decomposer_().decompose(faceFaces, triCentres);
-            UPstream::parRun() = oldParRun;
+            UPstream::parRun(oldParRun);  // Restore parallel state
         }
         else
         {
@@ -2023,10 +2022,9 @@ Foam::distributedTriSurfaceMesh::independentlyDistributedBbs
                 UIndirectList<point>(mergedPoints, allToMerged) = allCentres;
 
                 // Decompose merged centres
-                const bool oldParRun = UPstream::parRun();
-                UPstream::parRun() = false;
+                const bool oldParRun = UPstream::parRun(false);
                 labelList mergedDist(decomposer_().decompose(mergedPoints));
-                UPstream::parRun() = oldParRun;
+                UPstream::parRun(oldParRun);  // Restore parallel state
 
                 // Convert to all
                 allDistribution = UIndirectList<label>
-- 
GitLab