From 3a38efedd6a354f5782699032fd67911283dba58 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Riette?= <sebastien.riette@meteo.fr>
Date: Thu, 10 Nov 2022 17:00:30 +0100
Subject: [PATCH] S. Riette 10 Nov 2022: build system update
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

add -a and -A options to check_commit_testprogs to choose the architecture
add -mesonhprofile to make_fcm.sh to convert a Méso-NH rules into a fcm configuration file
add architecture files for ifort and a new one for gfortran derived from the Méso-NH rules
---
 build/with_fcm/arch/arch-X64_JEANZAY.fcm      |  7 --
 build/with_fcm/arch/arch-gnu.fcm              |  7 --
 build/with_fcm/arch/arch-intel.env            |  0
 build/with_fcm/arch/arch-intel.fcm            | 25 ++++++
 .../with_fcm/arch/arch-mesonh-LXgfortran.env  |  3 +
 .../with_fcm/arch/arch-mesonh-LXgfortran.fcm  | 28 +++++++
 build/with_fcm/make_fcm.sh                    | 80 ++++++++++++++++++-
 docs/Offline.md                               | 23 ++++--
 docs/TODO                                     |  3 -
 tools/check_commit_testprogs.sh               | 29 ++++---
 10 files changed, 167 insertions(+), 38 deletions(-)
 create mode 100644 build/with_fcm/arch/arch-intel.env
 create mode 100644 build/with_fcm/arch/arch-intel.fcm
 create mode 100644 build/with_fcm/arch/arch-mesonh-LXgfortran.env
 create mode 100644 build/with_fcm/arch/arch-mesonh-LXgfortran.fcm

diff --git a/build/with_fcm/arch/arch-X64_JEANZAY.fcm b/build/with_fcm/arch/arch-X64_JEANZAY.fcm
index b7753a4f5..8f585d67f 100644
--- a/build/with_fcm/arch/arch-X64_JEANZAY.fcm
+++ b/build/with_fcm/arch/arch-X64_JEANZAY.fcm
@@ -24,10 +24,3 @@ $LD_EXE_TO_SHARED =
 # Other
 $AR            =     ar
 
-#Kept for latter use
-#$FPP           =     mpiifort -E
-#$MAKE          =     gmake
-#$FPP_FLAGS     =     -P
-#$FPP_DEF       =     KEY_NONE CPP_NETCDF4
-#$MPI_LD
-#$MPI_FFLAGS     
diff --git a/build/with_fcm/arch/arch-gnu.fcm b/build/with_fcm/arch/arch-gnu.fcm
index 81eebf9f2..1b16787f6 100644
--- a/build/with_fcm/arch/arch-gnu.fcm
+++ b/build/with_fcm/arch/arch-gnu.fcm
@@ -24,10 +24,3 @@ $LD_EXE_TO_SHARED = -shared
 # Other
 $AR            =     ar
 
-#Kept for latter use
-#$FPP           =     gfortran -E
-#$MAKE          =     gmake
-#$FPP_FLAGS     =     -P
-#$FPP_DEF       =
-#$MPI_LD
-#$MPI_FFLAGS
diff --git a/build/with_fcm/arch/arch-intel.env b/build/with_fcm/arch/arch-intel.env
new file mode 100644
index 000000000..e69de29bb
diff --git a/build/with_fcm/arch/arch-intel.fcm b/build/with_fcm/arch/arch-intel.fcm
new file mode 100644
index 000000000..cffa8366d
--- /dev/null
+++ b/build/with_fcm/arch/arch-intel.fcm
@@ -0,0 +1,25 @@
+# Compilation
+$FCOMPILER     =     ifort
+$BASE_FFLAGS   =     -c -fpic -i4 -r8 -auto -align all -fp-model strict
+$PROD_FFLAGS   =     -g -traceback -O3 -xHost -qopt-zmm-usage=high
+$DEV_FFLAGS    =     -g -O1 -traceback
+$DEBUG_FFLAGS  =     -g -traceback -check bounds
+$CCOMPILER     =     icc
+$BASE_CFLAGS   =     -c -fpic
+$PROD_CFLAGS   =     -O3
+$DEV_CFLAGS    =     -O1
+$DEBUG_CFLAGS  =     -check bounds
+$OMP_FFLAGS    =     -qopenmp -qopenmp-threadprivate=compat
+
+# Preprocessor
+$FPP_FLAGS     =     LINUX LITTLE_ENDIAN LITTLE REPRO48
+$CPP_FLAGS     =     LINUX LITTLE_ENDIAN LITTLE
+
+# Linker
+$LINK          =     ifort
+$BASE_LD       =     -fPIC -i4 -r8 -auto -qopenmp -qopenmp-threadprivate compat
+$OMP_LD        =
+$LD_EXE_TO_SHARED = -shared
+
+# Other
+$AR            =     ar
diff --git a/build/with_fcm/arch/arch-mesonh-LXgfortran.env b/build/with_fcm/arch/arch-mesonh-LXgfortran.env
new file mode 100644
index 000000000..27928ee46
--- /dev/null
+++ b/build/with_fcm/arch/arch-mesonh-LXgfortran.env
@@ -0,0 +1,3 @@
+# This file has been produced automatically from a mesonh profile on ubuntu22
+
+export PATH=~rodierq/UBUNTU22/GCC940:$PATH 
diff --git a/build/with_fcm/arch/arch-mesonh-LXgfortran.fcm b/build/with_fcm/arch/arch-mesonh-LXgfortran.fcm
new file mode 100644
index 000000000..775444834
--- /dev/null
+++ b/build/with_fcm/arch/arch-mesonh-LXgfortran.fcm
@@ -0,0 +1,28 @@
+# This file has been produced automatically from a mesonh profile on ubuntu22
+# Then, the file has been modified (-ffree-line-length-none -fopenmp -fconvert=swap -fPIC -shared)
+
+# Compilation
+$FCOMPILER     =     mpif90
+$BASE_FFLAGS   =     -c -g -fno-second-underscore -fPIC  -ffpe-trap=overflow,zero,invalid  -fbacktrace -fdefault-real-8 -fdefault-double-8 -O0 -fbounds-check -finit-real=nan -ffree-line-length-none -fopenmp -fconvert=swap
+$PROD_FFLAGS   =     
+$DEV_FFLAGS    =     
+$DEBUG_FFLAGS  =     
+$CCOMPILER     =     gcc
+$BASE_CFLAGS   =     -c -g -O0 -fPIC
+$PROD_CFLAGS   =     
+$DEV_CFLAGS    =     
+$DEBUG_CFLAGS  =     
+$OMP_FFLAGS    =
+
+# Preprocessor
+$FPP_FLAGS     =     MNH SFX_MNH MNH_IOLFI MNH_IOCDF4 REPRO55 MNH_INT=4 MNH_REAL=8 LEN_HREC=16 REPRO55 MNH_PARALLEL in_surfex  MNH_USE_MPI_STATUSES_IGNORE DEV_NULL  SWAPIO LINUX LFI_INT=8 LITTLE_endian
+$CPP_FLAGS     =     MNH SFX_MNH MNH_IOLFI MNH_IOCDF4 REPRO55 MNH_INT=4 MNH_REAL=8 LEN_HREC=16 REPRO55 MNH_PARALLEL in_surfex  MNH_USE_MPI_STATUSES_IGNORE DEV_NULL  SWAPIO LINUX LFI_INT=8 LITTLE_endian
+
+# Linker
+$LINK          =     gfortran 
+$BASE_LD       =     -fopenmp
+$OMP_LD        =
+$LD_EXE_TO_SHARED = -shared
+
+# Other
+$AR            =     ar
diff --git a/build/with_fcm/make_fcm.sh b/build/with_fcm/make_fcm.sh
index 4402641c5..85bdb7fb4 100755
--- a/build/with_fcm/make_fcm.sh
+++ b/build/with_fcm/make_fcm.sh
@@ -10,6 +10,7 @@ function parse_args() {
   ARCH_PATH=$PWD/arch
   ARCH=
   GMKFILE=
+  MESONHPROFILE=
   # pass unrecognized arguments to fcm
   FCM_ARGS=""
   
@@ -23,6 +24,7 @@ $0 [options]
 --arch-path ARCH_PATH directory for architecture specific files (see below) [./arch]
 --arch ARCH  	        build using arch files $ARCH_PATH/arch-ARCH.* [gnu]
 --gmkfile FILE        build using a gmkpack configuration file (--arch must be used to give a name to the build dir)
+--mesonhprofile FILE  build using Méso-NH profile and rules (--arch must be used to give a name to the build dir)
 
 Unrecognized options are passed to the fcm build command. Useful options include :
 --new                   clean build tree before building
@@ -40,15 +42,21 @@ EOF
         ARCH_PATH=$1 ; shift ;; 
       "--gmkfile")
         GMKFILE=$1 ; shift ;;
+      "--mesonhprofile")
+        MESONHPROFILE=$1 ; shift ;;
       *)
         FCM_ARGS="$FCM_ARGS $OPTION" ;;
     esac
   done
-  [ "$GMKFILE" == "" -a "$ARCH" == "" ] && ARCH=gnu
+  [ "$GMKFILE" == "" -a "$MESONHPROFILE" == "" -a "$ARCH" == "" ] && ARCH=gnu
   if [ "$GMKFILE" != "" -a "$ARCH" == "" ]; then
     echo "--arch option is mandatory if --gmkfile option is used"
     exit 2
   fi
+  if [ "$MESONHPROFILE" != "" -a "$ARCH" == "" ]; then
+    echo "--arch option is mandatory if --mesonhprofile option is used"
+    exit 3
+  fi
 }
 
 function check_install_fcm() {
@@ -110,6 +118,73 @@ cat <<EOF > $ARCHFILE
 EOF
 }
 
+function mesonhprofile2archenv() {
+  MESONHPROFILE=$1
+  ARCHFILE=$2
+  ENVFILE=$3
+
+  echo "
+   You are trying to produce a configuration file for fcm from a Meso-NH configuration.
+   The resulting file is certainly incomplete and must be modified as follows:
+      Optimisation level:
+        The opt level is set in the mesonh profile file; as a consequence, the BASE_FFLAGS contains
+        the base *and* the opt flags.
+        To compile with other opt level, the profile file must be modified before executing this function.
+      Long lines:
+        Meso-NH rules does not allow the compilation of long lines. Depending on compilers, it might be needed to
+        manually add an option to allow long lines.
+        For gfortran: add '-ffree-line-length-none' to BASE_FFLAGS
+      OpenMP:
+        Meso-NH does not use OpenMP but testprogs do; as a consequence, openmp flags are not included in the
+        Meso-NH rules, they must be manually added.
+        For gfortran: add '-fopenmp' to BASE_FFLAGS and to BASE_LD
+      Position Independent Code:
+        Meso-NH does not need to build position independent code, flags must be set manually.
+        For gfortran ('-fPIC' already in BASE_FFLAGS): add '-fPIC' to BASE_CFLAGS
+      Shared lib:
+        Flags needed to build shared lib are not defined in Meso-NH rules, only hard coded in Makefile to build a
+        specific lib. The flags to set for building a shared lib, in addition to flags used to build an object, must
+        be manually set.
+        For gfortran: add '-shared' to LD_EXE_TO_SHARED
+      Swap:
+        Meso-NH rules does not swap IO byte order (litle-/big-endian). Depending on your endianess, the
+        corresponding flag may have to be set manually.
+        For gfortran: add '-fconvert=swap' to BASE_FFLAGS"
+  tac $MESONHPROFILE | grep -m1 '#' -B $(cat $MESONHPROFILE | wc -l) | tac | grep -v '#' > $ENVFILE
+  MAKEFILE='
+include Rules.$(ARCH)$(F).mk
+
+archfile :
+	echo "# Compilation"
+	echo "\$$FCOMPILER     =     $(F90)"
+	echo "\$$BASE_FFLAGS   =     -c $(F90FLAGS)"
+	echo "\$$PROD_FFLAGS   =     "
+	echo "\$$DEV_FFLAGS    =     "
+	echo "\$$DEBUG_FFLAGS  =     "
+	echo "\$$CCOMPILER     =     $(CC)"
+	echo "\$$BASE_CFLAGS   =     -c $(CFLAGS)"
+	echo "\$$PROD_CFLAGS   =     "
+	echo "\$$DEV_CFLAGS    =     "
+	echo "\$$DEBUG_CFLAGS  =     "
+	echo "\$$OMP_FFLAGS    ="
+	echo ""
+	echo "# Preprocessor"
+	echo "\$$FPP_FLAGS     =     $(CPPFLAGS)"
+	echo "\$$CPP_FLAGS     =     $(CPPFLAGS)" 
+	echo ""
+	echo "# Linker"
+	echo "\$$LINK          =     $(FC)"
+	echo "\$$BASE_LD       =     $(LDFLAGS)"
+	echo "\$$OMP_LD        ="
+	echo "\$$LD_EXE_TO_SHARED =  "
+	echo ""
+	echo "# Other" 
+	echo "\$$AR            =     $(AR)"
+
+'
+  (. $MESONHPROFILE; make -f <(echo -e "$MAKEFILE") -s -I $(dirname $MESONHPROFILE)/../src archfile) | sed 's/-D//g' > $ARCHFILE
+}
+
 function build_compilation_script() {
 cat <<EOF > compilation.sh
 #!/bin/bash
@@ -175,6 +250,9 @@ mkdir $builddir
 if [ "$GMKFILE" != "" ]; then
   touch $builddir/arch.env
   gmkfile2arch $GMKFILE $builddir/arch.fcm
+elif [ "$MESONHPROFILE" != "" ]; then
+  touch $builddir/arch.env
+  mesonhprofile2archenv $MESONHPROFILE $builddir/arch.fcm $builddir/arch.env
 else
   cp ${ARCH_PATH}/arch-${ARCH}.env $builddir/arch.env
   cp ${ARCH_PATH}/arch-${ARCH}.fcm $builddir/arch.fcm 
diff --git a/docs/Offline.md b/docs/Offline.md
index be8f806cb..5a219dead 100644
--- a/docs/Offline.md
+++ b/docs/Offline.md
@@ -7,28 +7,35 @@ Some offline test programs are provided with the package and a library suitable
 
 ## Compilation
 
-The build/with\_fcm directory contains a build system.
+The build/with\_fcm directory in the master branch contains a build system.
 This build system has two dependencies (installation is done automatically by the compilation script):
 
   - [fcm](https://metomi.github.io/fcm/doc/user_guide/)
   - [fiat](https://github.com/ecmwf-ifs/fiat)
 
 The script build/with\_fcm/make\_fcm.sh uses a configuration file and build the library and test programs.
-They can be found in the build/bin subdirectory in the architecture specific directory arch\_\<architecture name\>.
+These executables can be found in the build/bin subdirectory in the architecture specific directory arch\_\<architecture name\>.
 
 Some more details on the build system can be found in [build/with\_fcm/README.md file](../build/with_fcm/README.md).
 
-### Compilation in the repository
+### Compilation directly in the repository without execution (or manual execution)
 
 When on a master commit, the build/with\_fcm/make\_fcm.sh script can be used to compile the offline tools.
 
-### Compilation of a testprogs specific commit
+### Compilation and execution
 
-When on a master commit, the tools/check\_commit\_testprogs.sh script can be used to compile a testprogs\_\<commit\_hash\> commit already available in the PHYEX repository.
+When on a master commit, the tools/check\_commit\_testprogs.sh script can be used to compile and execute the testprogs.
+The check\_commit\_testprogs.sh script uses the PHYEX source code:
 
-### Modification of a testprogs specific commit
+  - of a specific commit on the master branch available on a remote repository
+  - or, the last commit of a testprogs\_\<commit\_hash\> branch available on a remote repository
+  - or, the content of a local repository.
+
+In the latter case, it can be interesting to clone the PHYEX repository twice.
+A first one to have the build tools on the master branch, and a second one to checkout the source code version to use.
+This solution is especially useful when working on a testprogs\_\<commit\_hash\> branch (because these branches does not
+contain the build tools).
 
-The idea is to use two clones of PHYEX: one to provide the building tools, the another one to provide the source code.
 Something like this can be used:
 
 - cd $HOME; git clone \<PHYEX url\> PHYEXtools
@@ -37,7 +44,7 @@ Something like this can be used:
 - cd PHYEX; git checkout arome\_\<commit\_hash\>; source code moddifications...
 - . PHYEXtools/tools/env.sh; check\_commit\_testprogs.sh $HOME/PHYEX REF
 
-The last step will creates a directory (in $HOME/TESTPROGS) with a copy of your source code and the build system, builds the testprogs and executes them.
+The last step will create a directory (in $HOME/TESTPROGS) with a copy of your source code and the build system, builds the testprogs and executes them.
 
 ## Test program
 
diff --git a/docs/TODO b/docs/TODO
index 1cbb2622d..4a0c7da95 100644
--- a/docs/TODO
+++ b/docs/TODO
@@ -72,9 +72,6 @@ Nettoyage ultérieur :
 Une fois toutes les routines de la turbulence ont leur dimension horizontale packée et
 utilisent LES_MEAN_SUBGRID_PHY a la place de LES_MEAN_SUBGRID_PHY : renommer les routines arome/turb et mesonh/turb en les_mean_subgrid.F90 et remplacer partout dans le code les appels (enlever le _PHY) + supprimer les anciennes routines
 
-
-Enable mnh_expand with fcm build
-
 Reprendre les différents outils en deux scripts principaux:
 - outil pour reprendre toutes les fonctionnalités sur le code: filepp, MNH_Expand_Array, correct_indent.py, verify_mnh_expand.py et renommage
 - outil prep_code débarrassé du renommage pour ne faire que la gestion des commit et lancer la manipulation sur le code
diff --git a/tools/check_commit_testprogs.sh b/tools/check_commit_testprogs.sh
index 782d0cb9f..02881780e 100755
--- a/tools/check_commit_testprogs.sh
+++ b/tools/check_commit_testprogs.sh
@@ -21,10 +21,10 @@ PHYEXTOOLSDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
 dirdata=$PHYEXTOOLSDIR/testprogs_data
 if [ $(hostname | cut -c 1-7) == 'belenos' -o $(hostname | cut -c 1-7) == 'taranis' ]; then
   HPC=1
-  archfile=MIMPIIFC1805.EPONA
+  defaultarchfile=MIMPIIFC1805.EPONA
 else
   HPC=0
-  archfile=gnu
+  defaultarchfile=gnu
 fi
 defaultRef=ref
 
@@ -32,7 +32,7 @@ function usage {
   echo "Usage: $0 [-h] [-c] [-r] [-C] [-s] [-f] [--noexpand] [-t test] commit reference"
   echo "commit          commit hash (or a directory, or among $specialName) to test"
   echo "reference       commit hash (or a directory, or among $specialName) REF to use as a reference"
-  echo "-s              suppress compilation pack"
+  echo "-s              suppress compilation directory"
   echo "-c              performs compilation"
   echo "-r              runs the tests"
   echo "-C              checks the result against the reference"
@@ -43,6 +43,8 @@ function usage {
   echo "                defaults to the env variable PHYEXREOuser (=$PHYEXREOuser)"
   echo "--repo-protocol protocol (https or ssh) to reach the PHYEX repository on github,"
   echo "                defaults to the env variable PHYEXREOprotocol (=$PHYEXREOprotocol)"
+  echo "-a arch         architecture name to use to build and run the commit (=$defaultarchfile)"
+  echo "-A arch         architecture name to use for the reference simulation (=$defaultarchfile)"
   echo ""
   echo "If nothing is asked (compilation, running, check) everything is done"
   echo
@@ -63,6 +65,8 @@ reference=""
 tests=""
 suppress=0
 useexpand=1
+archfile=$defaultarchfile
+refarchfile=$defaultarchfile
 
 while [ -n "$1" ]; do
   case "$1" in
@@ -75,6 +79,8 @@ while [ -n "$1" ]; do
     '--noexpand') useexpand=0;;
     '--repo-user') export PHYEXREPOuser=$2; shift;;
     '--repo-protocol') export PHYEXREPOprotocol=$2; shift;;
+    '-a') archfile="$2"; shift;;
+    '-A') refarchfile="$2"; shift;;
     #--) shift; break ;;
      *) if [ -z "${commit-}" ]; then
           commit=$1
@@ -129,7 +135,6 @@ if [ $check -eq 1 -a -z "${reference-}" ]; then
   exit 3
 fi
 
-#Name is choosen such as it can be produced with a main pack: PHYEX/48t1_XXXXXXXXX.01.${gmkpack_l}.${gmkpack_o}
 fromdir=''
 if echo $commit | grep '/' > /dev/null; then
   fromdir=$commit
@@ -163,7 +168,7 @@ if [ $compilation -eq 1 ]; then
   fi
 
   if [ -d $TESTDIR/$name ]; then
-    echo "Pack already exists ($TESTDIR/$name), suppress it to be able to compile it again (or use the -s option to automatically suppress it)"
+    echo "Directory already exists ($TESTDIR/$name), suppress it to be able to compile it again (or use the -s option to automatically suppress it)"
     exit 5
   fi
   mkdir $TESTDIR/$name
@@ -205,7 +210,7 @@ if [ $run -ge 1 ]; then
 
   for t in $(echo $tests | sed 's/,/ /g'); do
     if [ ! -f $TESTDIR/$name/build/with_fcm/arch_${archfile}/build/bin/main_${t}.exe ]; then
-      echo "Pack does not exist ($TESTDIR/$name) or compilation has failed, please check"
+      echo "Directory does not exist ($TESTDIR/$name) or compilation has failed, please check"
       exit 6
     fi
   done
@@ -213,16 +218,16 @@ if [ $run -ge 1 ]; then
   #Cleaning to suppress old results that may be confusing in case of a crash during the run
   for t in $(echo $tests | sed 's/,/ /g'); do
     cd $TESTDIR/$name
-    if [ -d tests/$t ]; then
-      rm -rf tests/$t
+    if [ -d tests/with_fcm/arch_${archfile}/$t ]; then
+      rm -rf tests/with_fcm/arch_${archfile}/$t
     fi
   done
 
   #Run the tests one after the other
   for t in $(echo $tests | sed 's/,/ /g'); do
     cd $TESTDIR/$name
-    mkdir -p tests/$t
-    cd tests/$t
+    mkdir -p tests/with_fcm/arch_${archfile}/$t
+    cd tests/with_fcm/arch_${archfile}/$t
     ln -s $dirdata/$t data
     $TESTDIR/$name/build/with_fcm/arch_${archfile}/build/bin/main_${t}.exe --check 2>&1 > Output_run
   done
@@ -234,8 +239,8 @@ if [ $check -eq 1 ]; then
   alltests=0
   message=""
   for t in $(echo $tests | sed 's/,/ /g'); do
-    file1=$TESTDIR/$name/tests/$t/Output_run
-    file2=$TESTDIR/$refname/tests/$t/Output_run
+    file1=$TESTDIR/$name/tests/with_fcm/arch_${archfile}/$t/Output_run
+    file2=$TESTDIR/$refname/tests/with_fcm/arch_${refarchfile}/$t/Output_run
     mess=""
     te=0
     if [ ! -f "$file1" ]; then
-- 
GitLab