From 6de8df506a3025910752f7e973a88b168e581d34 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Mon, 23 Oct 2017 09:38:43 +0200
Subject: [PATCH] ENH: provide openmp compile and link flags.

- the USE_OMP preprocessor symbol is also defined with the openmp
  compile flag to allow conditional compilation of openmp-specific
  code.
---
 applications/test/openmp/Make/files           |  3 +
 applications/test/openmp/Make/options         |  4 ++
 applications/test/openmp/Test-openmp.C        | 69 +++++++++++++++++++
 etc/config.sh/kahip                           |  7 --
 src/parallel/decompose/Allwmake               |  1 -
 .../decompose/kahipDecomp/Make/options        | 10 +--
 wmake/rules/linux64Clang/general              |  1 +
 wmake/rules/linux64Clang/openmp               |  4 ++
 wmake/rules/linux64Gcc/general                |  1 +
 wmake/rules/linux64Gcc/openmp                 |  4 ++
 wmake/rules/linux64GccKNL/general             |  1 +
 wmake/rules/linux64GccKNL/openmp              |  4 ++
 wmake/rules/linux64Icc/general                |  1 +
 wmake/rules/linux64Icc/openmp                 |  4 ++
 wmake/rules/linux64IccKNL/openmp              |  4 ++
 wmake/rules/linuxARM7Gcc/general              |  1 +
 wmake/rules/linuxARM7Gcc/openmp               |  4 ++
 wmake/rules/linuxClang/general                |  1 +
 wmake/rules/linuxClang/openmp                 |  4 ++
 wmake/rules/linuxGcc/general                  |  1 +
 wmake/rules/linuxGcc/openmp                   |  4 ++
 wmake/rules/linuxIA64Gcc/general              |  1 +
 wmake/rules/linuxIA64Gcc/openmp               |  4 ++
 wmake/rules/linuxIA64Icc/general              |  1 +
 wmake/rules/linuxIA64Icc/openmp               |  4 ++
 wmake/rules/linuxIcc/general                  |  1 +
 wmake/rules/linuxPPC64Gcc/general             |  1 +
 wmake/rules/linuxPPC64Gcc/openmp              |  4 ++
 wmake/rules/linuxPPC64leGcc/general           |  1 +
 wmake/rules/linuxPPC64leGcc/openmp            |  4 ++
 30 files changed, 142 insertions(+), 12 deletions(-)
 create mode 100644 applications/test/openmp/Make/files
 create mode 100644 applications/test/openmp/Make/options
 create mode 100644 applications/test/openmp/Test-openmp.C
 create mode 100644 wmake/rules/linux64Clang/openmp
 create mode 100644 wmake/rules/linux64Gcc/openmp
 create mode 100644 wmake/rules/linux64GccKNL/openmp
 create mode 100644 wmake/rules/linux64Icc/openmp
 create mode 100644 wmake/rules/linux64IccKNL/openmp
 create mode 100644 wmake/rules/linuxARM7Gcc/openmp
 create mode 100644 wmake/rules/linuxClang/openmp
 create mode 100644 wmake/rules/linuxGcc/openmp
 create mode 100644 wmake/rules/linuxIA64Gcc/openmp
 create mode 100644 wmake/rules/linuxIA64Icc/openmp
 create mode 100644 wmake/rules/linuxPPC64Gcc/openmp
 create mode 100644 wmake/rules/linuxPPC64leGcc/openmp

diff --git a/applications/test/openmp/Make/files b/applications/test/openmp/Make/files
new file mode 100644
index 0000000000..c2749a9419
--- /dev/null
+++ b/applications/test/openmp/Make/files
@@ -0,0 +1,3 @@
+Test-openmp.C
+
+EXE = $(FOAM_USER_APPBIN)/Test-openmp
diff --git a/applications/test/openmp/Make/options b/applications/test/openmp/Make/options
new file mode 100644
index 0000000000..54eed51fb9
--- /dev/null
+++ b/applications/test/openmp/Make/options
@@ -0,0 +1,4 @@
+EXE_INC = ${COMP_OPENMP}
+
+/* Mostly do not need to explicitly link openmp libraries */
+/* EXE_LIBS = ${LINK_OPENMP} */
diff --git a/applications/test/openmp/Test-openmp.C b/applications/test/openmp/Test-openmp.C
new file mode 100644
index 0000000000..acb68e9ed8
--- /dev/null
+++ b/applications/test/openmp/Test-openmp.C
@@ -0,0 +1,69 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2017 OpenCFD Ltd.
+     \\/     M anipulation  |
+-------------------------------------------------------------------------------
+License
+    This file is part of OpenFOAM.
+
+    OpenFOAM is free software: you can redistribute it and/or modify it
+    under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
+    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+    for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
+
+Description
+    Simple test program for compiling/running openmp
+
+\*---------------------------------------------------------------------------*/
+
+#include <cstdio>
+#include <cstdlib>
+#include <iostream>
+
+#ifdef USE_OMP
+#include <omp.h>
+#endif
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+// Main program:
+
+int main(int argc, char *argv[])
+{
+    int nThreads, threadId;
+
+// Fork threads with their own copies of variables
+#ifdef USE_OMP
+#pragma omp parallel private(nThreads, threadId)
+    {
+        threadId = omp_get_thread_num();
+        nThreads = omp_get_num_threads();
+
+        // Printf rather than cout to ensure that it emits in one go
+        printf("Called from thread = %d\n", threadId);
+
+        // Master thread
+        if (threadId == 0)
+        {
+            // Printf rather than cout to ensure that it emits in one go
+            printf("Number of threads = %d\n", nThreads);
+        }
+    }
+#else
+    std::cout << "Compiled without openmp!\n";
+#endif
+
+    return 0;
+}
+
+
+// ************************************************************************* //
diff --git a/etc/config.sh/kahip b/etc/config.sh/kahip
index 21d7a8a84a..42557831c3 100644
--- a/etc/config.sh/kahip
+++ b/etc/config.sh/kahip
@@ -42,18 +42,11 @@
 #     An Int64 OpenFOAM version can use it, but the mesh size is limited
 #     accordingly.
 #
-#     If KaHIP was compiled with openmp, you may need additional
-#     compile or link flags in KAHIP_COMP_FLAGS KAHIP_LINK_FLAGS
-#
 #------------------------------------------------------------------------------
 # USER EDITABLE PART: Changes made here may be lost with the next upgrade
 
 KAHIP_VERSION=kahip-2.00
 export KAHIP_ARCH_PATH=$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER/$KAHIP_VERSION
 
-# Adjust as required
-# export KAHIP_COMP_FLAGS="-fopenmp"
-export KAHIP_LINK_FLAGS="-lgomp"
-
 # END OF (NORMAL) USER EDITABLE PART
 #------------------------------------------------------------------------------
diff --git a/src/parallel/decompose/Allwmake b/src/parallel/decompose/Allwmake
index 39c8f109a5..f371133348 100755
--- a/src/parallel/decompose/Allwmake
+++ b/src/parallel/decompose/Allwmake
@@ -33,7 +33,6 @@ hasKahip()
     local header label settings
 
     unset KAHIP_ARCH_PATH KAHIP_VERSION
-    unset KAHIP_COMP_FLAGS KAHIP_LINK_FLAGS
     settings=$($WM_PROJECT_DIR/bin/foamEtcFile config.sh/kahip) || {
         echo "$warning (no config.sh/kahip settings)"
         return 1
diff --git a/src/parallel/decompose/kahipDecomp/Make/options b/src/parallel/decompose/kahipDecomp/Make/options
index 9c746b309c..fcd1c5d268 100644
--- a/src/parallel/decompose/kahipDecomp/Make/options
+++ b/src/parallel/decompose/kahipDecomp/Make/options
@@ -1,13 +1,15 @@
 EXE_INC = \
     -I$(KAHIP_ARCH_PATH)/include \
-    -I../decompositionMethods/lnInclude \
-    $(KAHIP_COMP_FLAGS)
+    -I../decompositionMethods/lnInclude
 
 /*
  * The $(KAHIP_ARCH_PATH)/lib$WM_COMPILER_LIB_ARCH path is provided
- * to support central, non-thirdparty installations
+ * to support central, non-thirdparty installations.
+ *
+ * KaHIP is often compiled with openmp, but may be missing the
+ * link dependency for openmp.
  */
 LIB_LIBS = \
     -L$(KAHIP_ARCH_PATH)/lib \
     -L$(KAHIP_ARCH_PATH)/lib$(WM_COMPILER_LIB_ARCH) \
-    -L$(FOAM_EXT_LIBBIN) $(KAHIP_LINK_FLAGS) -lkahip
+    -L$(FOAM_EXT_LIBBIN) $(LINK_OPENMP) -lkahip
diff --git a/wmake/rules/linux64Clang/general b/wmake/rules/linux64Clang/general
index 1002cb169b..d83a18b000 100644
--- a/wmake/rules/linux64Clang/general
+++ b/wmake/rules/linux64Clang/general
@@ -4,5 +4,6 @@ PROJECT_LIBS = -l$(WM_PROJECT) -ldl
 
 include $(GENERAL_RULES)/standard
 
+include $(DEFAULT_RULES)/openmp
 include $(DEFAULT_RULES)/c
 include $(DEFAULT_RULES)/c++
diff --git a/wmake/rules/linux64Clang/openmp b/wmake/rules/linux64Clang/openmp
new file mode 100644
index 0000000000..bcb805f57c
--- /dev/null
+++ b/wmake/rules/linux64Clang/openmp
@@ -0,0 +1,4 @@
+# Flags for compiling/linking openmp
+
+COMP_OPENMP = -DUSE_OMP -fopenmp
+LINK_OPENMP = -lgomp
diff --git a/wmake/rules/linux64Gcc/general b/wmake/rules/linux64Gcc/general
index 1002cb169b..d83a18b000 100644
--- a/wmake/rules/linux64Gcc/general
+++ b/wmake/rules/linux64Gcc/general
@@ -4,5 +4,6 @@ PROJECT_LIBS = -l$(WM_PROJECT) -ldl
 
 include $(GENERAL_RULES)/standard
 
+include $(DEFAULT_RULES)/openmp
 include $(DEFAULT_RULES)/c
 include $(DEFAULT_RULES)/c++
diff --git a/wmake/rules/linux64Gcc/openmp b/wmake/rules/linux64Gcc/openmp
new file mode 100644
index 0000000000..bcb805f57c
--- /dev/null
+++ b/wmake/rules/linux64Gcc/openmp
@@ -0,0 +1,4 @@
+# Flags for compiling/linking openmp
+
+COMP_OPENMP = -DUSE_OMP -fopenmp
+LINK_OPENMP = -lgomp
diff --git a/wmake/rules/linux64GccKNL/general b/wmake/rules/linux64GccKNL/general
index 1002cb169b..d83a18b000 100644
--- a/wmake/rules/linux64GccKNL/general
+++ b/wmake/rules/linux64GccKNL/general
@@ -4,5 +4,6 @@ PROJECT_LIBS = -l$(WM_PROJECT) -ldl
 
 include $(GENERAL_RULES)/standard
 
+include $(DEFAULT_RULES)/openmp
 include $(DEFAULT_RULES)/c
 include $(DEFAULT_RULES)/c++
diff --git a/wmake/rules/linux64GccKNL/openmp b/wmake/rules/linux64GccKNL/openmp
new file mode 100644
index 0000000000..bcb805f57c
--- /dev/null
+++ b/wmake/rules/linux64GccKNL/openmp
@@ -0,0 +1,4 @@
+# Flags for compiling/linking openmp
+
+COMP_OPENMP = -DUSE_OMP -fopenmp
+LINK_OPENMP = -lgomp
diff --git a/wmake/rules/linux64Icc/general b/wmake/rules/linux64Icc/general
index 52b0e177d7..eb3898f709 100644
--- a/wmake/rules/linux64Icc/general
+++ b/wmake/rules/linux64Icc/general
@@ -4,5 +4,6 @@ PROJECT_LIBS = -l$(WM_PROJECT) -ldl
 
 include $(GENERAL_RULES)/standard
 
+include $(DEFAULT_RULES)/openmp
 include $(DEFAULT_RULES)/c
 include $(DEFAULT_RULES)/c++
diff --git a/wmake/rules/linux64Icc/openmp b/wmake/rules/linux64Icc/openmp
new file mode 100644
index 0000000000..154c9e5743
--- /dev/null
+++ b/wmake/rules/linux64Icc/openmp
@@ -0,0 +1,4 @@
+# Flags for compiling/linking openmp
+
+COMP_OPENMP = -DUSE_OMP -qopenmp
+LINK_OPENMP = -liomp5
diff --git a/wmake/rules/linux64IccKNL/openmp b/wmake/rules/linux64IccKNL/openmp
new file mode 100644
index 0000000000..154c9e5743
--- /dev/null
+++ b/wmake/rules/linux64IccKNL/openmp
@@ -0,0 +1,4 @@
+# Flags for compiling/linking openmp
+
+COMP_OPENMP = -DUSE_OMP -qopenmp
+LINK_OPENMP = -liomp5
diff --git a/wmake/rules/linuxARM7Gcc/general b/wmake/rules/linuxARM7Gcc/general
index 1e51dcc403..4fc50267ed 100644
--- a/wmake/rules/linuxARM7Gcc/general
+++ b/wmake/rules/linuxARM7Gcc/general
@@ -5,5 +5,6 @@ PROJECT_LIBS = -l$(WM_PROJECT) -ldl
 
 include $(GENERAL_RULES)/standard
 
+include $(DEFAULT_RULES)/openmp
 include $(DEFAULT_RULES)/c
 include $(DEFAULT_RULES)/c++
diff --git a/wmake/rules/linuxARM7Gcc/openmp b/wmake/rules/linuxARM7Gcc/openmp
new file mode 100644
index 0000000000..bcb805f57c
--- /dev/null
+++ b/wmake/rules/linuxARM7Gcc/openmp
@@ -0,0 +1,4 @@
+# Flags for compiling/linking openmp
+
+COMP_OPENMP = -DUSE_OMP -fopenmp
+LINK_OPENMP = -lgomp
diff --git a/wmake/rules/linuxClang/general b/wmake/rules/linuxClang/general
index e3427c3181..eae23e305b 100644
--- a/wmake/rules/linuxClang/general
+++ b/wmake/rules/linuxClang/general
@@ -5,5 +5,6 @@ PROJECT_LIBS = -l$(WM_PROJECT) -ldl
 
 include $(GENERAL_RULES)/standard
 
+include $(DEFAULT_RULES)/openmp
 include $(DEFAULT_RULES)/c
 include $(DEFAULT_RULES)/c++
diff --git a/wmake/rules/linuxClang/openmp b/wmake/rules/linuxClang/openmp
new file mode 100644
index 0000000000..bcb805f57c
--- /dev/null
+++ b/wmake/rules/linuxClang/openmp
@@ -0,0 +1,4 @@
+# Flags for compiling/linking openmp
+
+COMP_OPENMP = -DUSE_OMP -fopenmp
+LINK_OPENMP = -lgomp
diff --git a/wmake/rules/linuxGcc/general b/wmake/rules/linuxGcc/general
index e3427c3181..eae23e305b 100644
--- a/wmake/rules/linuxGcc/general
+++ b/wmake/rules/linuxGcc/general
@@ -5,5 +5,6 @@ PROJECT_LIBS = -l$(WM_PROJECT) -ldl
 
 include $(GENERAL_RULES)/standard
 
+include $(DEFAULT_RULES)/openmp
 include $(DEFAULT_RULES)/c
 include $(DEFAULT_RULES)/c++
diff --git a/wmake/rules/linuxGcc/openmp b/wmake/rules/linuxGcc/openmp
new file mode 100644
index 0000000000..bcb805f57c
--- /dev/null
+++ b/wmake/rules/linuxGcc/openmp
@@ -0,0 +1,4 @@
+# Flags for compiling/linking openmp
+
+COMP_OPENMP = -DUSE_OMP -fopenmp
+LINK_OPENMP = -lgomp
diff --git a/wmake/rules/linuxIA64Gcc/general b/wmake/rules/linuxIA64Gcc/general
index 182ee7f901..d8fb470836 100644
--- a/wmake/rules/linuxIA64Gcc/general
+++ b/wmake/rules/linuxIA64Gcc/general
@@ -4,6 +4,7 @@ PROJECT_LIBS = -l$(WM_PROJECT) -ldl
 
 include $(GENERAL_RULES)/standard
 
+include $(DEFAULT_RULES)/openmp
 include $(DEFAULT_RULES)/X
 include $(DEFAULT_RULES)/c
 include $(DEFAULT_RULES)/c++
diff --git a/wmake/rules/linuxIA64Gcc/openmp b/wmake/rules/linuxIA64Gcc/openmp
new file mode 100644
index 0000000000..bcb805f57c
--- /dev/null
+++ b/wmake/rules/linuxIA64Gcc/openmp
@@ -0,0 +1,4 @@
+# Flags for compiling/linking openmp
+
+COMP_OPENMP = -DUSE_OMP -fopenmp
+LINK_OPENMP = -lgomp
diff --git a/wmake/rules/linuxIA64Icc/general b/wmake/rules/linuxIA64Icc/general
index 09a3896656..c3269860fb 100644
--- a/wmake/rules/linuxIA64Icc/general
+++ b/wmake/rules/linuxIA64Icc/general
@@ -4,6 +4,7 @@ GLIBS      =
 
 include $(GENERAL_RULES)/standard
 
+include $(DEFAULT_RULES)/openmp
 include $(DEFAULT_RULES)/X
 include $(DEFAULT_RULES)/c
 include $(DEFAULT_RULES)/c++
diff --git a/wmake/rules/linuxIA64Icc/openmp b/wmake/rules/linuxIA64Icc/openmp
new file mode 100644
index 0000000000..154c9e5743
--- /dev/null
+++ b/wmake/rules/linuxIA64Icc/openmp
@@ -0,0 +1,4 @@
+# Flags for compiling/linking openmp
+
+COMP_OPENMP = -DUSE_OMP -qopenmp
+LINK_OPENMP = -liomp5
diff --git a/wmake/rules/linuxIcc/general b/wmake/rules/linuxIcc/general
index 5c94cad44a..85f5be51c0 100644
--- a/wmake/rules/linuxIcc/general
+++ b/wmake/rules/linuxIcc/general
@@ -5,5 +5,6 @@ PROJECT_LIBS = -l$(WM_PROJECT) -ldl
 
 include $(GENERAL_RULES)/standard
 
+include $(DEFAULT_RULES)/openmp
 include $(DEFAULT_RULES)/c
 include $(DEFAULT_RULES)/c++
diff --git a/wmake/rules/linuxPPC64Gcc/general b/wmake/rules/linuxPPC64Gcc/general
index a590cc29c8..712516c268 100644
--- a/wmake/rules/linuxPPC64Gcc/general
+++ b/wmake/rules/linuxPPC64Gcc/general
@@ -5,6 +5,7 @@ PROJECT_LIBS = -l$(WM_PROJECT) -ldl
 
 include $(GENERAL_RULES)/standard
 
+include $(DEFAULT_RULES)/openmp
 include $(DEFAULT_RULES)/X
 include $(DEFAULT_RULES)/c
 include $(DEFAULT_RULES)/c++
diff --git a/wmake/rules/linuxPPC64Gcc/openmp b/wmake/rules/linuxPPC64Gcc/openmp
new file mode 100644
index 0000000000..bcb805f57c
--- /dev/null
+++ b/wmake/rules/linuxPPC64Gcc/openmp
@@ -0,0 +1,4 @@
+# Flags for compiling/linking openmp
+
+COMP_OPENMP = -DUSE_OMP -fopenmp
+LINK_OPENMP = -lgomp
diff --git a/wmake/rules/linuxPPC64leGcc/general b/wmake/rules/linuxPPC64leGcc/general
index 7867cbde8f..575cadaeee 100644
--- a/wmake/rules/linuxPPC64leGcc/general
+++ b/wmake/rules/linuxPPC64leGcc/general
@@ -5,6 +5,7 @@ PROJECT_LIBS = -l$(WM_PROJECT) -ldl
 
 include $(GENERAL_RULES)/standard
 
+include $(DEFAULT_RULES)/openmp
 include $(DEFAULT_RULES)/X
 include $(DEFAULT_RULES)/c
 include $(DEFAULT_RULES)/c++
diff --git a/wmake/rules/linuxPPC64leGcc/openmp b/wmake/rules/linuxPPC64leGcc/openmp
new file mode 100644
index 0000000000..bcb805f57c
--- /dev/null
+++ b/wmake/rules/linuxPPC64leGcc/openmp
@@ -0,0 +1,4 @@
+# Flags for compiling/linking openmp
+
+COMP_OPENMP = -DUSE_OMP -fopenmp
+LINK_OPENMP = -lgomp
-- 
GitLab