From a7d77391faa8c2d5da9ea505211a369474c4c0ca Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Mon, 20 Feb 2023 16:00:45 +0100
Subject: [PATCH] BUG: selfComm had 0 instead of parent rank for
 UPstream::procID (#2706)

---
 src/Pstream/mpi/UPstream.C | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/Pstream/mpi/UPstream.C b/src/Pstream/mpi/UPstream.C
index 61667ce2b0c..f56d5631e97 100644
--- a/src/Pstream/mpi/UPstream.C
+++ b/src/Pstream/mpi/UPstream.C
@@ -348,6 +348,12 @@ bool Foam::UPstream::init(int& argc, char**& argv, const bool needsThread)
         // For testing: warn use of non-worldComm
         UPstream::warnComm = UPstream::worldComm;
 
+        // For selfComm : the processor number wrt the new world communicator
+        if (procIDs_[UPstream::selfComm].size())
+        {
+            procIDs_[UPstream::selfComm].front() = UPstream::myProcNo(subComm);
+        }
+
         if (debug)
         {
             // Check
@@ -565,7 +571,7 @@ void Foam::UPstream::allocatePstreamCommunicator
     }
     else if (parentIndex == -2)
     {
-        // Self communicator
+        // Self communicator (selfComm)
 
         PstreamGlobals::pendingMPIFree_[index] = PstreamGlobals::NonePending;
         PstreamGlobals::MPICommunicators_[index] = MPI_COMM_SELF;
@@ -588,8 +594,12 @@ void Foam::UPstream::allocatePstreamCommunicator
         }
         #endif
 
+        // For selfComm : the process IDs within the world communicator.
+        // Uses MPI_COMM_WORLD in case called before UPstream::globalComm
+        // was initialized
+
         procIDs_[index].resize_nocopy(1);
-        procIDs_[index] = 0;
+        MPI_Comm_rank(MPI_COMM_WORLD, &procIDs_[index].front());
     }
     else
     {
-- 
GitLab