From 7467e240010c6c73d7ab20e5dc58d9fb013a2610 Mon Sep 17 00:00:00 2001
From: Philippe WAUTELET <philippe.wautelet@aero.obs-mip.fr>
Date: Mon, 10 Jan 2022 16:48:12 +0100
Subject: [PATCH] Philippe 10/01/2021: OpenACC: workaround for problems with
 NVHPC 21.1 compiler

---
 src/MNH/advection_metsv.f90      |  8 +++++---
 src/MNH/ice4_fast_rs.f90         |  4 +++-
 src/MNH/turb_hor_dyn_corr.f90    | 11 +++++++----
 src/MNH/turb_hor_thermo_flux.f90 |  5 +++--
 src/MNH/turb_hor_uv.f90          |  5 +++--
 5 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/src/MNH/advection_metsv.f90 b/src/MNH/advection_metsv.f90
index 233674414..395d44553 100644
--- a/src/MNH/advection_metsv.f90
+++ b/src/MNH/advection_metsv.f90
@@ -1,4 +1,4 @@
-!MNH_LIC Copyright 1994-2021 CNRS, Meteo-France and Universite Paul Sabatier
+!MNH_LIC Copyright 1994-2022 CNRS, Meteo-France and Universite Paul Sabatier
 !MNH_LIC This is part of the Meso-NH software governed by the CeCILL-C licence
 !MNH_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt
 !MNH_LIC for details. version 1.
@@ -543,12 +543,14 @@ END IF
 !PW: not necessary: data already on device due to contrav_device !$acc update device(ZRUCPPM,ZRVCPPM,ZRWCPPM)
 ! acc kernels
 IF (.NOT. L1D) THEN
-  !$acc kernels 
+  !$acc kernels
   ZCFLU(:,:,:) = 0.0 ; ZCFLV(:,:,:) = 0.0 ;  ZCFLW(:,:,:) = 0.0
   ZCFLU(IIB:IIE,IJB:IJE,:) = ABS(ZRUCPPM(IIB:IIE,IJB:IJE,:) * PTSTEP)
   ZCFLV(IIB:IIE,IJB:IJE,:) = ABS(ZRVCPPM(IIB:IIE,IJB:IJE,:) * PTSTEP)
   ZCFLW(IIB:IIE,IJB:IJE,:) = ABS(ZRWCPPM(IIB:IIE,IJB:IJE,:) * PTSTEP)
+  !$acc end kernels
   IF (LIBM) THEN
+    !$acc kernels
 #ifndef MNH_BITREP
     ZCFLU(IIB:IIE,IJB:IJE,:) = ZCFLU(IIB:IIE,IJB:IJE,:)*(1.-exp(-(XIBM_LS(IIB:IIE,IJB:IJE,:,2)/&
                                                         (XRHODJ(IIB:IIE,IJB:IJE,:)/XRHODREF(IIB:IIE,IJB:IJE,:))**(1./3.))**2.))
@@ -567,8 +569,8 @@ IF (.NOT. L1D) THEN
     WHERE (XIBM_LS(IIB:IIE,IJB:IJE,:,2).GT.(-XIBM_EPSI)) ZCFLU(IIB:IIE,IJB:IJE,:)=0.
     WHERE (XIBM_LS(IIB:IIE,IJB:IJE,:,3).GT.(-XIBM_EPSI)) ZCFLV(IIB:IIE,IJB:IJE,:)=0.
     WHERE (XIBM_LS(IIB:IIE,IJB:IJE,:,4).GT.(-XIBM_EPSI)) ZCFLW(IIB:IIE,IJB:IJE,:)=0.
+    !$acc end kernels
   ENDIF
-  !$acc end kernels
 #ifndef MNH_BITREP
   IF (.NOT. L2D) THEN
      !$acc kernels 
diff --git a/src/MNH/ice4_fast_rs.f90 b/src/MNH/ice4_fast_rs.f90
index b7a571863..ea3c59731 100644
--- a/src/MNH/ice4_fast_rs.f90
+++ b/src/MNH/ice4_fast_rs.f90
@@ -1,4 +1,4 @@
-!MNH_LIC Copyright 1994-2021 CNRS, Meteo-France and Universite Paul Sabatier
+!MNH_LIC Copyright 1994-2022 CNRS, Meteo-France and Universite Paul Sabatier
 !MNH_LIC This is part of the Meso-NH software governed by the CeCILL-C licence
 !MNH_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt
 !MNH_LIC for details. version 1.
@@ -357,6 +357,8 @@ ELSE
       ZVEC1(JJ) =   XGAMINC_RIM1( IVEC2(JJ)+1 )* ZVEC2(JJ)      &
                   - XGAMINC_RIM1( IVEC2(JJ)   )*(ZVEC2(JJ) - 1.0)
     END DO
+!$acc end kernels
+!$acc kernels
     ZZW(:) = 0.
 !$acc loop independent
     DO JJ = 1, IGRIM
diff --git a/src/MNH/turb_hor_dyn_corr.f90 b/src/MNH/turb_hor_dyn_corr.f90
index ceebdd109..ad9da4172 100644
--- a/src/MNH/turb_hor_dyn_corr.f90
+++ b/src/MNH/turb_hor_dyn_corr.f90
@@ -1,4 +1,4 @@
-!MNH_LIC Copyright 1994-2021 CNRS, Meteo-France and Universite Paul Sabatier
+!MNH_LIC Copyright 1994-2022 CNRS, Meteo-France and Universite Paul Sabatier
 !MNH_LIC This is part of the Meso-NH software governed by the CeCILL-C licence
 !MNH_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt
 !MNH_LIC for details. version 1.
@@ -621,7 +621,8 @@ ZFLX(:,:,IKB-1) =                                                            &
       PVSLOPEM(:,:) * PCOSSLOPE(:,:)    * PSINSLOPE(:,:) * ZDIRSINZW(:,:)    &
     - PUSLOPEM(:,:) * PCOSSLOPE(:,:)**2 * ZDIRSINZW(:,:) * PDIRCOSZW(:,:)    )
 #else
-!$acc loop independent collapse(2)   
+!PW: BUG: commented 'acc loop independent collapse(2)' to workaround compiler bug (NVHPC 21.1)
+!acc loop independent collapse(2)
 DO CONCURRENT ( JI=1:JIU,JJ=1:JJU )
 ZFLX(JI,JJ,IKB-1) =                                                             &
         PTAU11M(JI,JJ) * BR_P2(PCOSSLOPE(JI,JJ)) * BR_P2(PDIRCOSZW(JI,JJ))          &
@@ -844,7 +845,8 @@ ZFLX(:,:,IKB-1) =                                                            &
       PUSLOPEM(:,:) * PSINSLOPE(:,:)**2 * ZDIRSINZW(:,:) * PDIRCOSZW(:,:)    &
     + PVSLOPEM(:,:) * PCOSSLOPE(:,:)    * PSINSLOPE(:,:) * ZDIRSINZW(:,:)    )
 #else
-!$acc loop independent collapse(2)   
+!PW: BUG: commented 'acc loop independent collapse(2)' to workaround compiler bug (NVHPC 21.1)
+!acc loop independent collapse(2)
 DO CONCURRENT ( JI=1:JIU,JJ=1:JJU )
 ZFLX(JI,JJ,IKB-1) =                                                             &
         PTAU11M(JI,JJ) * BR_P2(PSINSLOPE(JI,JJ)) * BR_P2(PDIRCOSZW(JI,JJ))          &
@@ -1062,7 +1064,8 @@ ZFLX(:,:,IKB-1) = &
   +     PTAU33M(:,:) * PDIRCOSZW(:,:)**2                                &
   +2. * PCDUEFF(:,:)* PUSLOPEM(:,:)  * ZDIRSINZW(:,:) * PDIRCOSZW(:,:)
 #else
-!$acc loop independent collapse(2)
+!PW: BUG: commented 'acc loop independent collapse(2)' to workaround compiler bug (NVHPC 21.1)
+!acc loop independent collapse(2)
 DO CONCURRENT ( JI=1:JIU,JJ=1:JJU )        
 ZFLX(JI,JJ,IKB-1) = &
         PTAU11M(JI,JJ) * BR_P2(ZDIRSINZW(JI,JJ))                                &
diff --git a/src/MNH/turb_hor_thermo_flux.f90 b/src/MNH/turb_hor_thermo_flux.f90
index 96e82cb70..5b629bd4e 100644
--- a/src/MNH/turb_hor_thermo_flux.f90
+++ b/src/MNH/turb_hor_thermo_flux.f90
@@ -1,4 +1,4 @@
-!MNH_LIC Copyright 1994-2021 CNRS, Meteo-France and Universite Paul Sabatier
+!MNH_LIC Copyright 1994-2022 CNRS, Meteo-France and Universite Paul Sabatier
 !MNH_LIC This is part of the Meso-NH software governed by the CeCILL-C licence
 !MNH_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt
 !MNH_LIC for details. version 1.
@@ -382,7 +382,8 @@ ZFLX(:,:,IKB-1:IKB-1) = 2. * MXM(  SPREAD( PSFTHM(:,:)* PDIRCOSXW(:,:), 3,1) )
                        - ZFLX(:,:,IKB:IKB)
 #else
 !$acc kernels
-!$acc loop independent collapse(2)
+!PW: BUG: commented 'acc loop independent collapse(2)' to workaround compiler bug (NVHPC 21.1)
+!acc loop independent collapse(2)
 DO CONCURRENT ( JI=1:JIU,JJ=1:JJU )
    ZTMP1_DEVICE(JI,JJ,1) = PSFTHM(JI,JJ)* PDIRCOSXW(JI,JJ)
 END DO
diff --git a/src/MNH/turb_hor_uv.f90 b/src/MNH/turb_hor_uv.f90
index 50b42d36f..f206bcee4 100644
--- a/src/MNH/turb_hor_uv.f90
+++ b/src/MNH/turb_hor_uv.f90
@@ -1,4 +1,4 @@
-!MNH_LIC Copyright 1994-2021 CNRS, Meteo-France and Universite Paul Sabatier
+!MNH_LIC Copyright 1994-2022 CNRS, Meteo-France and Universite Paul Sabatier
 !MNH_LIC This is part of the Meso-NH software governed by the CeCILL-C licence
 !MNH_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt
 !MNH_LIC for details. version 1.
@@ -458,7 +458,8 @@ ZFLX(:,:,IKB-1) =                                                           &
           PDIRCOSZW(:,:) * ZDIRSINZW(:,:)                                     &
     +PVSLOPEM(:,:) * (PCOSSLOPE(:,:)**2 - PSINSLOPE(:,:)**2) * ZDIRSINZW(:,:) )
 #else
-!$acc loop independent collapse(2)
+!PW: BUG: commented 'acc loop independent collapse(2)' to workaround compiler bug (NVHPC 21.1)
+!acc loop independent collapse(2)
 DO CONCURRENT ( JI=1:JIU,JJ=1:JJU )
 ZFLX(JI,JJ,IKB-1) =                                                           &
    PTAU11M(JI,JJ) * PCOSSLOPE(JI,JJ) * PSINSLOPE(JI,JJ) * BR_P2(PDIRCOSZW(JI,JJ))     &
-- 
GitLab