From 2c6fc15734aa1976afa0250f977dbf905b4dd70e Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Mon, 20 Feb 2023 14:52:25 +0100
Subject: [PATCH] BUG: globalIndex gather/scatter fails with multi-world (fixes
 #2706)

- was using UPstream::procIDs(), which returns the sub-ranks with
  respect to the parent communicator. This is normally just an
  identity list (single-world) but with multi-world the indexing
  is incorrect.  Use UPstream::allProcs() instead.
---
 .../meshes/polyMesh/globalMeshData/globalIndex.H   |  6 +++---
 .../polyMesh/globalMeshData/globalIndexTemplates.C | 14 +++++++-------
 .../distributedTriSurfaceMesh.C                    |  8 ++++----
 3 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.H b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.H
index c5ad93b81ef..de4417b30d0 100644
--- a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.H
+++ b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2017 OpenFOAM Foundation
-    Copyright (C) 2018-2021 OpenCFD Ltd.
+    Copyright (C) 2018-2023 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -395,12 +395,12 @@ public:
 
             //- Collect indirect data in processor order on master
             //  Offsets needed on master only.
-            template<class Type, class Addr>
+            template<class ProcIDsContainer, class Type, class Addr>
             static void gather
             (
                 const labelUList& offsets,
                 const label comm,       //!< communicator
-                const UList<int>& procIDs,
+                const ProcIDsContainer& procIDs,
                 const IndirectListBase<Type, Addr>& fld,
                 List<Type>& allFld,
                 const int tag = UPstream::msgType(),
diff --git a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndexTemplates.C b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndexTemplates.C
index f8676ace535..2e5a92ed983 100644
--- a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndexTemplates.C
+++ b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndexTemplates.C
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2013-2017 OpenFOAM Foundation
-    Copyright (C) 2019-2021 OpenCFD Ltd.
+    Copyright (C) 2019-2023 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -225,12 +225,12 @@ void Foam::globalIndex::gather
 }
 
 
-template<class Type, class Addr>
+template<class ProcIDsContainer, class Type, class Addr>
 void Foam::globalIndex::gather
 (
     const labelUList& off,
     const label comm,
-    const UList<int>& procIDs,
+    const ProcIDsContainer& procIDs,
     const IndirectListBase<Type, Addr>& fld,
     List<Type>& allFld,
     const int tag,
@@ -302,7 +302,7 @@ void Foam::globalIndex::gather
     gather
     (
         comm,
-        UPstream::procID(comm),
+        UPstream::allProcs(comm),   // All communicator ranks
         fld,
         allFld,
         tag,
@@ -325,7 +325,7 @@ void Foam::globalIndex::gather
     (
         offsets_,
         comm,
-        UPstream::procID(comm),
+        UPstream::allProcs(comm),   // All communicator ranks
         fld,
         allFld,
         tag,
@@ -370,7 +370,7 @@ void Foam::globalIndex::gather
     gather
     (
         comm,
-        UPstream::procID(comm),
+        UPstream::allProcs(comm),   // All communicator ranks
         fld,
         allFld,
         tag,
@@ -658,7 +658,7 @@ void Foam::globalIndex::scatter
     (
         offsets_,
         comm,
-        UPstream::procID(comm),
+        UPstream::allProcs(comm),   // All communicator ranks
         allFld,
         fld,
         tag,
diff --git a/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C b/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C
index 0f2ab38b604..661a7bcdd56 100644
--- a/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C
+++ b/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C
@@ -1845,7 +1845,7 @@ Foam::distributedTriSurfaceMesh::independentlyDistributedBbs
 //        //globalBorderTris.gather
 //        //(
 //        //    UPstream::worldComm,
-//        //    UPstream::procID(Pstream::worldComm),
+//        //    UPstream::allProcs(UPstream::worldComm),
 //        //    globalBorderCentres
 //        //);
 //        pointField globalBorderCentres(allCentres);
@@ -1893,7 +1893,7 @@ Foam::distributedTriSurfaceMesh::independentlyDistributedBbs
 //        //globalBorderTris.scatter
 //        //(
 //        //    UPstream::worldComm,
-//        //    UPstream::procID(Pstream::worldComm),
+//        //    UPstream::allProcs(UPstream::worldComm),
 //        //    isMasterPoint
 //        //);
 //        //boolList isMasterBorder(s.size(), false);
@@ -1990,7 +1990,7 @@ Foam::distributedTriSurfaceMesh::independentlyDistributedBbs
             globalTris().gather
             (
                 UPstream::worldComm,
-                UPstream::procID(Pstream::worldComm),
+                UPstream::allProcs(UPstream::worldComm),
                 allCentres
             );
         }
@@ -2040,7 +2040,7 @@ Foam::distributedTriSurfaceMesh::independentlyDistributedBbs
             globalTris().scatter
             (
                 UPstream::worldComm,
-                UPstream::procID(Pstream::worldComm),
+                UPstream::allProcs(UPstream::worldComm),
                 allDistribution,
                 distribution
             );
-- 
GitLab