From 83243cf01e98cb491833ba34ba35189834ab357c Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Thu, 16 Dec 2021 14:38:57 +0100
Subject: [PATCH] COMP: non-group-local communicator for MS-MPI (mingw)

- partial revert for 13740de42770 (#2158)

  MS-MPI does not currently have a MPI_Comm_create_group(),
  so keep using MPI_Comm_create() there.

  Only affects multi-world simulations.

CONFIG: retain dummy version of libPstream.dll

- retain as libPstream.dll-dummy so that it is available for
  manual replacement of the regular libPstream.dll (#2290)

  Keep extra copy of libPstream.dll as libPstream.dll-msmpi
  (for example) for manual replacement.
---
 bin/tools/createMingwRuntime | 71 ++++++++++++++++++++++++++----------
 src/Pstream/mpi/UPstream.C   | 10 +++++
 2 files changed, 62 insertions(+), 19 deletions(-)

diff --git a/bin/tools/createMingwRuntime b/bin/tools/createMingwRuntime
index b02e466f8a2..b534c7a27ed 100755
--- a/bin/tools/createMingwRuntime
+++ b/bin/tools/createMingwRuntime
@@ -6,7 +6,7 @@
 #   \\  /    A nd           | www.openfoam.com
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
-#     Copyright (C) 2020 OpenCFD Ltd.
+#     Copyright (C) 2020-2021 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@@ -256,16 +256,25 @@ echo "Third-party libraries in platforms/$targetPlatform$compOptions" >| "$tmpTa
 echo "Copy common files" 1>&2
 rsync -a --exclude .gitignore COPYING META-INFO README.md bin etc "$tmpTarget"
 
-if [ -n "$FOAM_CONFIG_ETC" ]
+# Remove branch info (probably spurious)
+if [ -f "$tmpTarget/META-INFO/build-info" ]
 then
-    etcDir="${FOAM_CONFIG_ETC##*/}"
-    if [ "$WM_PROJECT_DIR/$etcDir" = "$FOAM_CONFIG_ETC" ]
-    then
-        echo "Copy additional config dir: $etcDir" 1>&2
-        rsync -a "$etcDir" "$tmpTarget"
-    else
+    sed -i -e '/^branch=/d' "$tmpTarget/META-INFO/build-info"
+fi
+
+# One-level only?
+configEtcDir="${FOAM_CONFIG_ETC##*/}"
+if [ -n "$configEtcDir" ]
+then
+    case "$FOAM_CONFIG_ETC" in
+    ("$configEtcDir" | "$WM_PROJECT_DIR/$configEtcDir")
+        echo "Copy config dir: $configEtcDir" 1>&2
+        rsync -a "$configEtcDir" "$tmpTarget"
+        ;;
+    (*)
         echo "Do not know how to copy additional config dir: $FOAM_CONFIG_ETC" 1>&2
-    fi
+        ;;
+    esac
 else
     echo "No additional config dir" 1>&2
 fi
@@ -295,12 +304,27 @@ rsync -a "$root"/bin/*.exe "$root"/lib/*.dll "$binDir"
 
 # Pstream .dll into bin directory
 other="$root/lib/$FOAM_MPI"
-if [ -d "$other" ] && [ -n "$FOAM_MPI" ]
+if [ -d "$other" ] && [ -n "$FOAM_MPI" ] && [ "$FOAM_MPI" != dummy ]
 then
-    echo "Copy ($FOAM_MPI) libPstream.dll -> platforms bin/" 1>&2
+    echo "Copy ($FOAM_MPI) *.dll -> platforms bin/" 1>&2
     rsync -a "$other"/*.dll "$binDir"
+
+    ending="$(echo "$FOAM_MPI" | tr '[:upper:]' '[:lower:]' | sed -e 's/[^A-Za-z].*//')"
+    [ -n "$ending" ] || continue
+
+    for name in libPstream.dll
+    do
+        if [ -f "$other/$name" ]
+        then
+            # Keep duplicate for manual replacement if needed
+            newName="$name-$ending"
+            echo "    [copy] $FOAM_MPI/$name -> $newName" 1>&2
+            cp -p "$other/$name" "$binDir/$newName"
+        fi
+   done
 fi
 
+
 # Various dummy (stub) libraries
 other="$root/lib/dummy"
 if [ -d "$other" ]
@@ -308,16 +332,25 @@ then
     echo "Copy (dummy) *.dll -> platforms bin/" 1>&2
     for fullpath in "$other"/*.dll
     do
+        [ -f "$fullpath" ] || continue;
         name="${fullpath##*/}"
-        if [ -f "$fullpath" ]
+
+        if [ -f "$binDir/$name" ]
         then
-            if [ -f "$binDir/$name" ]
-            then
-                echo "    [skip dummy/$name]" 1>&2
-            else
-                echo "    dummy/$name" 1>&2
-                cp -p "$fullpath" "$binDir"
-            fi
+            case "$name" in
+            (libPstream*)
+                # Keep for manual replacement if needed
+                newName="$name-dummy"
+                echo "    [copy] dummy/$name -> $newName" 1>&2
+                cp -p "$fullpath" "$binDir/$newName"
+                ;;
+            (*)
+                echo "    [skip] dummy/$name" 1>&2
+                ;;
+            esac
+        else
+            echo "    dummy/$name" 1>&2
+            cp -p "$fullpath" "$binDir"
         fi
     done
 fi
diff --git a/src/Pstream/mpi/UPstream.C b/src/Pstream/mpi/UPstream.C
index f36485840bd..eb9061a8426 100644
--- a/src/Pstream/mpi/UPstream.C
+++ b/src/Pstream/mpi/UPstream.C
@@ -1216,6 +1216,15 @@ void Foam::UPstream::allocatePstreamCommunicator
            &PstreamGlobals::MPIGroups_[index]
         );
 
+        #if defined(MSMPI_VER)
+        // ms-mpi (10.0 and others?) does not have MPI_Comm_create_group
+        MPI_Comm_create
+        (
+            PstreamGlobals::MPICommunicators_[parentIndex],
+            PstreamGlobals::MPIGroups_[index],
+            &PstreamGlobals::MPICommunicators_[index]
+        );
+        #else
         // Create new communicator for this group
         MPI_Comm_create_group
         (
@@ -1224,6 +1233,7 @@ void Foam::UPstream::allocatePstreamCommunicator
             Pstream::msgType(),
            &PstreamGlobals::MPICommunicators_[index]
         );
+        #endif
 
         if (PstreamGlobals::MPICommunicators_[index] == MPI_COMM_NULL)
         {
-- 
GitLab