Skip to content

Commit

Permalink
Improve NetCDF detection and linking (#1923)
Browse files Browse the repository at this point in the history
TYPE: enhancement

KEYWORDS: NetCDF4, Spack-Stack, gfortran

SOURCE: David Robertson, Rutgers University

DESCRIPTION OF CHANGES:
Problem:

- NetCDF4 detection and linking is unreliable and impossible if NetCDF-C and NetCDF-Fortran reside in different directories
- gfortran is intolerant of non-initialized variables in **`mediation_integrate.F`**.

Solution:
- Refactor the detection and configuration of NetCDF dependencies and add the capability to use NetCDF-C and NetCDF-Fortran libraries that don't reside in the same directory by introducing a new variable for the NetCDF-C path.
 -  The **fname** and **n2**  `intent(out)` variables are not assigned under certain circumstances so we should initialize them to empty strings.
``` 
   ierr = 0
   fname = ""
   n2 = ""

   ! Note that computation of fname and n2 are outside of the oid IF statement
```     

LIST OF MODIFIED FILES: list of changed files (used `git diff --name-status release-v4.5.2` to get formatted list)
```
M       Makefile
M       arch/Config.pl
M       arch/postamble
M       arch/preamble
M       configure
M       share/mediation_integrate.F
```
TESTS CONDUCTED: 
1. Compiled and ran with several different configurations of NetCDF, including both shared and static versions of both split (NetCDF-C and NetCDF-Fortran in separate directories) and unified library locations. The shared library versions were from Spack-Stack; the static libraries were custom-built for my systems.
2. Ran the coupled WRF-ROMS standard test case for Hurricane Irene.
3. The Jenkins tests are all passing.

RELEASE NOTE: This PR improve the detection and linking with shared and static configurations of the NetCDF-4 libraries (split or unified) inside and outside Spack-Stack.
  • Loading branch information
rjdave authored Dec 16, 2023
1 parent d69761a commit bb406d2
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 140 deletions.
38 changes: 19 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ configcheck:
echo "------------------------------------------------------------------------------" ; \
exit 21 ; \
fi


framework_only : configcheck
$(MAKE) MODULE_DIRS="$(ALL_MODULES)" ext
Expand Down Expand Up @@ -154,7 +154,7 @@ wrfplus : configcheck
@echo "build started: $(START_OF_COMPILE)"
@echo "build completed:" `date`

all_wrfvar :
all_wrfvar :
$(MAKE) MODULE_DIRS="$(DA_WRFVAR_MODULES)" ext
$(MAKE) MODULE_DIRS="$(DA_WRFVAR_MODULES)" toolsdir
if [ $(CRTM) -ne 0 ] ; then \
Expand Down Expand Up @@ -498,7 +498,7 @@ em_scm_xy : wrf
ln -sf ../../run/LANDUSE.TBL . ; \
ln -sf ../../run/SOILPARM.TBL . ; \
ln -sf ../../run/VEGPARM.TBL . ; \
ln -sf ../../run/RRTM_DATA . )
ln -sf ../../run/RRTM_DATA . )
( cd run ; /bin/rm -f ideal.exe ; ln -s ../main/ideal.exe . )
( cd run ; if test -f namelist.input ; then \
/bin/cp -f namelist.input namelist.input.backup.`date +%Y-%m-%d_%H_%M_%S` ; fi ; \
Expand Down Expand Up @@ -529,11 +529,11 @@ convert_em : framework_only
( cd main ; $(MAKE) RLFLAGS="$(RLFLAGS)" MODULE_DIRS="$(ALL_MODULES)" convert_em ) ; \
fi

# Link wrf.exe and wrf_SST_ESMF.exe into
# test/em_esmf_exp when ESMF_COUPLING is set. wrf.exe
# can be used for stand-alone testing in this case.
# wrf_SST_ESMF.exe is a coupled application. Note that make
# target $(SOLVER)_wrf_SST_ESMF builds wrf_SST_ESMF.exe.
# Link wrf.exe and wrf_SST_ESMF.exe into
# test/em_esmf_exp when ESMF_COUPLING is set. wrf.exe
# can be used for stand-alone testing in this case.
# wrf_SST_ESMF.exe is a coupled application. Note that make
# target $(SOLVER)_wrf_SST_ESMF builds wrf_SST_ESMF.exe.
em_real : wrf
@/bin/rm -f real.exe > /dev/null 2>&1
@/bin/rm -f tc.exe > /dev/null 2>&1
Expand Down Expand Up @@ -923,31 +923,31 @@ framework :
@ echo '--------------------------------------'
( cd frame ; $(MAKE) $(J) LLIST="$(LINKLIST)" framework ; \
cd ../external/io_netcdf ; \
$(MAKE) NETCDFPATH="$(NETCDFPATH)" \
$(MAKE) NETCDFPATH="$(NETCDFPATH)" NETCDF4_DEP_LIB="$(NETCDF4_DEP_LIB)" \
FC="$(FC) $(FCBASEOPTS) $(PROMOTION) $(FCDEBUG) $(OMP)" RANLIB="$(RANLIB)" \
CPP="$(CPP)" LDFLAGS="$(LDFLAGS)" TRADFLAG="$(TRADFLAG)" ESMF_IO_LIB_EXT="$(ESMF_IO_LIB_EXT)" \
LIB_LOCAL="$(LIB_LOCAL)" \
ESMF_MOD_DEPENDENCE="$(ESMF_MOD_DEPENDENCE)" AR="INTERNAL_BUILD_ERROR_SHOULD_NOT_NEED_AR" diffwrf; \
cd ../io_netcdf ; \
$(MAKE) NETCDFPATH="$(NETCDFPATH)" \
$(MAKE) NETCDFPATH="$(NETCDFPATH)" NETCDF4_DEP_LIB="$(NETCDF4_DEP_LIB)" \
FC="$(SFC) $(FCBASEOPTS) $(PROMOTION) $(FCDEBUG) $(OMP)" RANLIB="$(RANLIB)" \
CPP="$(CPP)" LDFLAGS="$(LDFLAGS)" TRADFLAG="$(TRADFLAG)" ESMF_IO_LIB_EXT="$(ESMF_IO_LIB_EXT)" \
LIB_LOCAL="$(LIB_LOCAL)" \
ESMF_MOD_DEPENDENCE="$(ESMF_MOD_DEPENDENCE)" AR="INTERNAL_BUILD_ERROR_SHOULD_NOT_NEED_AR"; \
cd ../io_netcdfpar ; \
$(NETCDFPAR_BUILD) $(MAKE) NETCDFPARPATH="$(NETCDFPATH)" \
$(NETCDFPAR_BUILD) $(MAKE) NETCDFPARPATH="$(NETCDFPATH)" NETCDF4_DEP_LIB="$(NETCDF4_DEP_LIB)" \
FC="$(FC) $(FCBASEOPTS) $(PROMOTION) $(FCDEBUG) $(OMP)" RANLIB="$(RANLIB)" \
CPP="$(CPP)" LDFLAGS="$(LDFLAGS)" TRADFLAG="$(TRADFLAG)" ESMF_IO_LIB_EXT="$(ESMF_IO_LIB_EXT)" \
LIB_LOCAL="$(LIB_LOCAL)" \
ESMF_MOD_DEPENDENCE="$(ESMF_MOD_DEPENDENCE)" AR="INTERNAL_BUILD_ERROR_SHOULD_NOT_NEED_AR" diffwrf; \
cd ../io_netcdfpar ; \
$(NETCDFPAR_BUILD) $(MAKE) NETCDFPARPATH="$(NETCDFPATH)" \
$(NETCDFPAR_BUILD) $(MAKE) NETCDFPARPATH="$(NETCDFPATH)" NETCDF4_DEP_LIB="$(NETCDF4_DEP_LIB)" \
FC="$(SFC) $(FCBASEOPTS) $(PROMOTION) $(FCDEBUG) $(OMP)" RANLIB="$(RANLIB)" \
CPP="$(CPP)" LDFLAGS="$(LDFLAGS)" TRADFLAG="$(TRADFLAG)" ESMF_IO_LIB_EXT="$(ESMF_IO_LIB_EXT)" \
LIB_LOCAL="$(LIB_LOCAL)" \
ESMF_MOD_DEPENDENCE="$(ESMF_MOD_DEPENDENCE)" AR="INTERNAL_BUILD_ERROR_SHOULD_NOT_NEED_AR"; \
cd ../io_pio ; \
echo SKIPPING PIO BUILD $(MAKE) NETCDFPATH="$(PNETCDFPATH)" \
echo SKIPPING PIO BUILD $(MAKE) NETCDFPATH="$(PNETCDFPATH)" NETCDF4_DEP_LIB="$(NETCDF4_DEP_LIB)" \
FC="$(SFC) $(FCBASEOPTS) $(PROMOTION) $(FCDEBUG) $(OMP)" RANLIB="$(RANLIB)" \
CPP="$(CPP)" LDFLAGS="$(LDFLAGS)" TRADFLAG="$(TRADFLAG)" ESMF_IO_LIB_EXT="$(ESMF_IO_LIB_EXT)" \
LIB_LOCAL="$(LIB_LOCAL)" \
Expand All @@ -964,13 +964,13 @@ framework_plus :
@ echo '--------------------------------------'
( cd frame ; $(MAKE) $(J) LLIST="$(MODLL)" framework ; \
cd ../external/io_netcdf ; \
$(MAKE) NETCDFPATH="$(NETCDFPATH)" \
$(MAKE) NETCDFPATH="$(NETCDFPATH)" NETCDF4_DEP_LIB="$(NETCDF4_DEP_LIB)" \
FC="$(FC) $(FCBASEOPTS) $(PROMOTION) $(FCDEBUG) $(OMP)" RANLIB="$(RANLIB)" \
CPP="$(CPP)" LDFLAGS="$(LDFLAGS)" TRADFLAG="$(TRADFLAG)" ESMF_IO_LIB_EXT="$(ESMF_IO_LIB_EXT)" \
LIB_LOCAL="$(LIB_LOCAL)" \
ESMF_MOD_DEPENDENCE="$(ESMF_MOD_DEPENDENCE)" AR="INTERNAL_BUILD_ERROR_SHOULD_NOT_NEED_AR" diffwrf; \
cd ../io_netcdf ; \
$(MAKE) NETCDFPATH="$(NETCDFPATH)" \
$(MAKE) NETCDFPATH="$(NETCDFPATH)" NETCDF4_DEP_LIB="$(NETCDF4_DEP_LIB)" \
FC="$(SFC) $(FCBASEOPTS) $(PROMOTION) $(FCDEBUG) $(OMP)" RANLIB="$(RANLIB)" \
CPP="$(CPP)" LDFLAGS="$(LDFLAGS)" TRADFLAG="$(TRADFLAG)" ESMF_IO_LIB_EXT="$(ESMF_IO_LIB_EXT)" \
LIB_LOCAL="$(LIB_LOCAL)" \
Expand Down Expand Up @@ -1070,9 +1070,9 @@ fseek_test :
# rule used by configure to test if this will compile with netcdf4
nc4_test:
if [ -z "$(USENETCDFPAR)" ] || [ $(USENETCDFPAR) -eq 0 ] ; then \
( cd tools ; /bin/rm -f nc4_test.{exe,nc,o} ; $(SCC) -o nc4_test.exe nc4_test.c -I$(NETCDF)/include -L$(NETCDF)/lib -lnetcdf $(NETCDF4_DEP_LIB) ; cd .. ) ; \
( cd tools ; /bin/rm -f nc4_test.{exe,nc,o} ; $(SCC) -o nc4_test.exe nc4_test.c -I$(NETCDF_C)/include $(NETCDF4_DEP_LIB) ; cd .. ) ; \
else \
( cd tools ; /bin/rm -f nc4_test.{exe,nc,o} ; $(DM_CC) -o nc4_test.exe nc4_test.c -I$(NETCDF)/include -L$(NETCDF)/lib -lnetcdf $(NETCDF4_DEP_LIB) ; cd .. ) ; \
( cd tools ; /bin/rm -f nc4_test.{exe,nc,o} ; $(DM_CC) -o nc4_test.exe nc4_test.c -I$(NETCDF_C)/include $(NETCDF4_DEP_LIB) ; cd .. ) ; \
fi

# rule used by configure to test if Fortran 2003 IEEE signaling is available
Expand Down Expand Up @@ -1110,8 +1110,8 @@ toolsdir :

# ( cd tools ; $(MAKE) CC_TOOLS="$(CC_TOOLS) -DIO_MASK_SIZE=$(IO_MASK_SIZE)" )

# Use this target to build stand-alone tests of esmf_time_f90.
# Only touches external/esmf_time_f90/.
# Use this target to build stand-alone tests of esmf_time_f90.
# Only touches external/esmf_time_f90/.
esmf_time_f90_only :
@ echo '--------------------------------------'
( cd external/esmf_time_f90 ; $(MAKE) FC="$(FC) $(FCFLAGS)" CPP="$(CPP) -DTIME_F90_ONLY" tests )
Expand Down
12 changes: 6 additions & 6 deletions arch/Config.pl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
$sw_hdf5 = "-lhdf5_hl -lhdf5";
$sw_hdf5_hl_fortran="-lhdf5_hl_fortran";
$sw_zlib = "-lz";
$sw_dep_lib_path = "";
$sw_netcdf4_dep_lib = "";
$sw_gpfs_path = "";
$sw_gpfs_lib = "-lgpfs";
$sw_curl_path = "";
Expand All @@ -70,10 +70,10 @@
{
$sw_netcdf_path = substr( $ARGV[0], 8 ) ;
}
if ( substr( $ARGV[0], 1, 13 ) eq "dep_lib_path=" )
if ( substr( $ARGV[0], 1, 16 ) eq "netcdf4_dep_lib=" )
{
$sw_dep_lib_path = substr( $ARGV[0], 14 ) ;
$sw_dep_lib_path =~ s/\r|\n/ /g ;
$sw_netcdf4_dep_lib = substr( $ARGV[0], 17 ) ;
$sw_netcdf4_dep_lib =~ s/\r|\n/ /g ;
}
if ( substr( $ARGV[0], 1, 5 ) eq "gpfs=" )
{
Expand Down Expand Up @@ -1050,7 +1050,7 @@
$_ =~ s/CONFIGURE_CONFIG_NUM/Compiler choice: $response_opt/g ;
$_ =~ s/CONFIGURE_CONFIG_NEST/Nesting option: $response_nesting/g ;

$_ =~ s/CONFIGURE_DEP_LIB_PATH/$sw_dep_lib_path/g ;
$_ =~ s/CONFIGURE_NETCDF4_DEP_LIB/$sw_netcdf4_dep_lib/g ;

$_ =~ s/CONFIGURE_COMMS_LIB/$sw_comms_lib/g ;
if ( $sw_os ne "CYGWIN_NT" ) {
Expand Down Expand Up @@ -1180,7 +1180,7 @@
$_ .= " " . $sw_curl_lib . "\n" ;
}
}
if ( $sw_dep_lib_path ne "" )
if ( $sw_netcdf4_dep_lib ne "" )
{ if (/^HDF5.*=/)
{ $_ =~ s/\r|\n//g;
$_ .= " " . $sw_hdf5 . "\n" ;
Expand Down
15 changes: 8 additions & 7 deletions arch/postamble
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,16 @@ module_dm_rsllite :
( if [ ! -e module_dm.F ] ; then /bin/cp module_dm_warning module_dm.F ; \
cat $(WRF_SRC_ROOT_DIR)/external/RSL_LITE/module_dm.F >> module_dm.F ; fi )

wrfio_nf :
wrfio_nf :
( cd $(WRF_SRC_ROOT_DIR)/external/io_netcdf ; \
make $(J) NETCDFPATH="$(NETCDFPATH)" RANLIB="$(RANLIB)" CPP="$(CPP)" \
CC="$(SCC)" CFLAGS="$(CFLAGS)" \
CC="$(SCC)" CFLAGS="$(CFLAGS)" NETCDF4_DEP_LIB="$(NETCDF4_DEP_LIB)" \
FC="$(SFC) $(PROMOTION) $(OMP) $(FCFLAGS)" TRADFLAG="$(TRADFLAG)" AR="$(AR)" ARFLAGS="$(ARFLAGS)" )

wrfio_nfpar :
( cd $(WRF_SRC_ROOT_DIR)/external/io_netcdfpar ; \
make $(J) NETCDFPARPATH="$(NETCDFPATH)" RANLIB="$(RANLIB)" CPP="$(CPP) $(ARCHFLAGS)" \
NETCDF4_DEP_LIB="$(NETCDF4_DEP_LIB)" \
FC="$(FC) $(PROMOTION) $(OMP) $(FCFLAGS)" TRADFLAG="$(TRADFLAG)" AR="$(AR)" ARFLAGS="$(ARFLAGS)" )

wrfio_pnf :
Expand All @@ -118,7 +119,7 @@ wrfio_adios2 :
wrfio_grib_share :
( cd $(WRF_SRC_ROOT_DIR)/external/io_grib_share ; \
make $(J) CC="$(SCC)" CFLAGS="$(CFLAGS)" RM="$(RM)" RANLIB="$(RANLIB)" CPP="$(CPP)" \
FC="$(SFC) $(PROMOTION) -I. $(FCDEBUG) $(FCBASEOPTS) $(FCSUFFIX)" TRADFLAG="$(TRADFLAG)" AR="$(AR)" ARFLAGS="$(ARFLAGS)" archive)
FC="$(SFC) $(PROMOTION) -I. $(FCDEBUG) $(FCBASEOPTS) $(FCSUFFIX)" TRADFLAG="$(TRADFLAG)" AR="$(AR)" ARFLAGS="$(ARFLAGS)" archive)

wrfio_grib1 :
( cd $(WRF_SRC_ROOT_DIR)/external/io_grib1 ; \
Expand All @@ -132,13 +133,13 @@ wrfio_grib2 :
FC="$(SFC) $(PROMOTION) -I. $(FCDEBUG) $(FCBASEOPTS) $(FCSUFFIX)" TRADFLAG="-traditional" AR="$(AR)" ARFLAGS="$(ARFLAGS)" \
FIXED="$(FORMAT_FIXED)" archive)

wrfio_int :
wrfio_int :
( cd $(WRF_SRC_ROOT_DIR)/external/io_int ; \
make $(J) CC="$(CC)" CFLAGS_LOCAL="$(CFLAGS_LOCAL)" RM="$(RM)" RANLIB="$(RANLIB)" CPP="$(CPP)" \
FC="$(FC) $(PROMOTION) $(FCDEBUG) $(FCBASEOPTS) $(OMP)" FGREP="$(FGREP)" \
TRADFLAG="$(TRADFLAG)" AR="$(AR)" ARFLAGS="$(ARFLAGS)" ARCHFLAGS="$(ARCHFLAGS)" all )

esmf_time :
esmf_time :
( cd $(WRF_SRC_ROOT_DIR)/external/esmf_time_f90 ; \
make $(J) FC="$(SFC) $(PROMOTION) $(FCDEBUG) $(FCBASEOPTS)" RANLIB="$(RANLIB)" \
CPP="$(CPP) -I$(WRF_SRC_ROOT_DIR)/inc -I. $(ARCHFLAGS) $(TRADFLAG)" AR="$(AR)" ARFLAGS="$(ARFLAGS)" )
Expand Down Expand Up @@ -201,12 +202,12 @@ wrfio_esmf :
mv $*.f90.tmp $*.f90 ; \
fi
$(FC) -o $@ -c $(FCFLAGS) $(OMP) $(MODULE_DIRS) $(PROMOTION) $(FCSUFFIX) $*.f90


.F.f90:
$(RM) $@
sed -e "s/^\!.*'.*//" -e "s/^ *\!.*'.*//" $*.F > $*.G
$(SED_FTN) $*.G > $*.H
$(SED_FTN) $*.G > $*.H
$(CPP) -I$(WRF_SRC_ROOT_DIR)/inc $(CPPFLAGS) $*.H > $@
$(RM) $*.G $*.H

Expand Down
3 changes: 1 addition & 2 deletions arch/preamble
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ GPFS =
CURL =
HDF5 =
ZLIB =
DEP_LIB_PATH = CONFIGURE_DEP_LIB_PATH
NETCDF4_DEP_LIB = $(DEP_LIB_PATH) $(HDF5) $(ZLIB) $(GPFS) $(CURL)
NETCDF4_DEP_LIB = CONFIGURE_NETCDF4_DEP_LIB

# NETCDF4INCLUDEGOESHERE

Expand Down
Loading

0 comments on commit bb406d2

Please sign in to comment.