|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | +set -o pipefail |
| 4 | + |
| 5 | +############################## |
| 6 | + |
| 7 | +mod_perftools="" |
| 8 | +# mod_perftools=perftools-lite # perftools-lite |
| 9 | +#mod_perftools=perftools # perftools |
| 10 | + |
| 11 | + use_gprof=1 |
| 12 | +#use_gprof=0 |
| 13 | + [[ $use_gprof -eq 1 ]] && mod_perftools="" |
| 14 | + |
| 15 | + #export WRF_DIR=${PWD}/WRF |
| 16 | +#export WRF_DIR=${PWD}/v4.4/WRF |
| 17 | +#export WRF_DIR=${PWD}/WRF-4.2 |
| 18 | + #[[ -n ${mod_perftools} ]] && export WRF_DIR=${PWD}/WRF_perftools |
| 19 | + #[[ $use_gprof -gt 0 ]] && export WRF_DIR=${PWD}/WRF_gprof |
| 20 | + export WRF_DIR=${PWD} |
| 21 | + |
| 22 | +############################## |
| 23 | + |
| 24 | +#change the following boolean variables to run/skip certain compiling steps |
| 25 | + |
| 26 | +doclean=false #true if WRF source code is modified since the last compilation |
| 27 | + |
| 28 | +doclean_all=false #true if previously compiled with different configure options |
| 29 | + |
| 30 | +docompile=false #run WRF's compile script if true, else run the configure command |
| 31 | + |
| 32 | +debug=false #true to compile WRF with debug flag (no optimizations, -g flag for debugger, etc.) |
| 33 | + |
| 34 | +imach="pm" #target system name. "pm" for Perlmutter. |
| 35 | + |
| 36 | +# set the top directory of the WRF source code as an environmental variable |
| 37 | +#export WRF_DIR="PATH_TO_YOUR_WRFcode_LOCATION" |
| 38 | + |
| 39 | +#Modules -------------------------------------------------------------------- |
| 40 | +#general modules |
| 41 | +module load cpu |
| 42 | +module load PrgEnv-nvidia |
| 43 | + |
| 44 | +#module for WRF file I/O |
| 45 | +#order of loading matters! |
| 46 | +module load cray-hdf5 #required to load netcdf library |
| 47 | +module load cray-netcdf |
| 48 | +module load cray-parallel-netcdf |
| 49 | +module load cudatoolkit |
| 50 | + |
| 51 | +module list #check what modules are loaded |
| 52 | + |
| 53 | +#set environmental variables used by WRF build system, |
| 54 | +#using the environmental variables set by the modules |
| 55 | + |
| 56 | +#use classic (CDF1) as default |
| 57 | +export NETCDF_classic=1 |
| 58 | +#use 64-bit offset format (CDF2) of netcdf files |
| 59 | +export WRFIO_NCD_LARGE_FILE_SUPPORT=1 |
| 60 | +#do not use netcdf4 compression (serial), need hdf5 module |
| 61 | +export USE_NETCDF4_FEATURES=0 |
| 62 | + |
| 63 | +export HDF5=$HDF5_DIR |
| 64 | +export HDF5_LIB="$HDF5_DIR/lib" |
| 65 | +export HDF5_BIN="$HDF5_DIR/bin" |
| 66 | + |
| 67 | +export NETCDF=$NETCDF_DIR |
| 68 | +export NETCDF_BIN="$NETCDF_DIR/bin" |
| 69 | +export NETCDF_LIB="$NETCDF_DIR/lib" |
| 70 | + |
| 71 | +#create PNETCDF environment variable to use the parallel netcdf library |
| 72 | +export PNETCDF=$PNETCDF_DIR |
| 73 | + |
| 74 | +export LD_LIBRARY_PATH="/usr/lib64":${LD_LIBRARY_PATH} |
| 75 | +export PATH=${NETCDF_BIN}:${HDF5_BIN}:${PATH} |
| 76 | +export LD_LIBRARY_PATH=${NETCDF_LIB}:${LD_LIBRARY_PATH} |
| 77 | + |
| 78 | +#other special flags |
| 79 | +export PNETCDF_QUILT="0" #Quilt output is not stable, better not use it |
| 80 | + |
| 81 | +#check environment variables |
| 82 | +echo "LD_LIBRARY_PATH: "$LD_LIBRARY_PATH |
| 83 | +echo "PATH: "$PATH |
| 84 | +echo "MANPATH: "$MANPATH |
| 85 | + |
| 86 | +echo "NETCDF is $NETCDF" |
| 87 | +echo "NETCDF_LIB is $NETCDF_LIB" |
| 88 | + |
| 89 | +echo "HDF5 is $HDF5" |
| 90 | +echo "HDF5_LIB is $HDF5_LIB" |
| 91 | + |
| 92 | +echo "PNETCDF: ${PNETCDF}" |
| 93 | +echo "PNETCDF_QUILT: ${PNETCDF_QUILT}" |
| 94 | + |
| 95 | +##capture the date and time for log file name |
| 96 | +idate=$(date "+%Y-%m-%d-%H_%M") |
| 97 | +# |
| 98 | +##run WRF build scripts located in the top WRF directory |
| 99 | +cd $WRF_DIR |
| 100 | + |
| 101 | +if [ "$doclean_all" = true ]; then |
| 102 | + ./clean -a |
| 103 | + #"The './clean –a' command is required if you have edited the configure.wrf |
| 104 | + #or any of the Registry files.", but this deletes configure.wrf.... |
| 105 | + |
| 106 | +fi |
| 107 | + |
| 108 | +if [ "$doclean" = true ]; then |
| 109 | + ./clean |
| 110 | +fi |
| 111 | + |
| 112 | +#echo "running configure" |
| 113 | +if [ "$docompile" = false ]; then |
| 114 | + |
| 115 | + if [ "$debug" = true ]; then |
| 116 | + echo "configure debug mode" |
| 117 | + ./configure -d |
| 118 | + else |
| 119 | + ./configure |
| 120 | + fi |
| 121 | + |
| 122 | + ##configure options selected are: |
| 123 | + # 32. (serial) 33. (smpar) 34. (dmpar) 35. (dm+sm) GNU (gfortran/gcc) |
| 124 | + # choose 35 for real (not idealized) cases |
| 125 | + |
| 126 | + configfile="${WRF_DIR}/configure.wrf" |
| 127 | + |
| 128 | + #the sed commands below will change the following lines in configure.wrf |
| 129 | + #--- original |
| 130 | + #SFC = gfortran |
| 131 | + #SCC = gcc |
| 132 | + #CCOMP = gcc |
| 133 | + #DM_FC = mpif90 |
| 134 | + #DM_CC = mpicc |
| 135 | + |
| 136 | + #--- edited (FC and CC with MPI) |
| 137 | + #SFC = gfortran |
| 138 | + #SCC = gcc |
| 139 | + #CCOMP = cc |
| 140 | + #DM_FC = ftn |
| 141 | + #DM_CC = cc |
| 142 | + |
| 143 | + if [ -f "$configfile" ]; then |
| 144 | + echo "editing configure.wrf" |
| 145 | + #need to remove -cc=$(SCC) in DM_CC |
| 146 | + sed -i 's/-cc=\$(SCC)/ /' ${configfile} |
| 147 | + sed -i 's/mpif90/ftn/' ${configfile} |
| 148 | + sed -i 's/mpicc/cc/' ${configfile} |
| 149 | + sed -i 's/pgf90/ftn/' ${configfile} |
| 150 | + sed -i 's/pgcc/cc/' ${configfile} |
| 151 | + sed -i 's/gcc/cc/' ${configfile} |
| 152 | + |
| 153 | + #also user can remove the flag -DWRF_USE_CLM |
| 154 | + #from ARCH_LOCAL if not planning to |
| 155 | + #use the CLM4 land model to speed up compilation |
| 156 | + #sed -i 's/-DWRF_USE_CLM/ /' ${configfile} |
| 157 | + |
| 158 | + if [[ $use_gprof -eq 1 ]]; then |
| 159 | +#CFLAGS = $(CFLAGS_LOCAL) -DDM_PARALLEL \ |
| 160 | +# -DLANDREAD_STUB=1 \ |
| 161 | +# -DMAX_HISTORY=$(MAX_HISTORY) -DNMM_CORE=$(WRF_NMM_CORE) |
| 162 | +#FCFLAGS = $(FCOPTIM) $(FCBASEOPTS) |
| 163 | + sed -i 's/^\(\s*FCFLAGS\s*=\s*\).*$/\1$(FCOPTIM) $(FCBASEOPTS) -pg/ |
| 164 | + /^\s*CFLAGS\s*=/ {N; N; s/\(-DMAX_HISTORY=$(MAX_HISTORY) -DNMM_CORE=$(WRF_NMM_CORE)\)\s*/\1 -pg/}' \ |
| 165 | + ${configfile} |
| 166 | + fi |
| 167 | + |
| 168 | + # Set OpenMP flags for offloading on nvidia compilers |
| 169 | + sed -i '/FCFLAGS\s*=/ s/$/ -Minform=warn -mp=gpu -target-accel=nvidia80 -Minfo=mp,accel/' ${configfile} |
| 170 | + fi |
| 171 | + |
| 172 | +fi |
| 173 | + |
| 174 | +if [ "$docompile" = true ]; then |
| 175 | + export J="-j 8" #build in parallel |
| 176 | + echo "J = $J" |
| 177 | + |
| 178 | + bldlog=log.txt |
| 179 | + echo "compile log file is ${bldlog}" |
| 180 | + |
| 181 | + #run the compile script |
| 182 | + ./compile wrf &> ${bldlog} |
| 183 | + |
| 184 | + #check if there is an error in the compile log |
| 185 | + #grep command exits the script in case of nomatch |
| 186 | + #after the 2022-12 maintenance |
| 187 | + set +e #release the exit flag before grep |
| 188 | + |
| 189 | + grep "Problems building executables" ${bldlog} |
| 190 | + RESULT=$? |
| 191 | + |
| 192 | + #set the exit flag again |
| 193 | + set -e |
| 194 | + |
| 195 | + if [ $RESULT -ne 0 ]; then |
| 196 | + echo "compile failed, check ${bldlog}" |
| 197 | + else |
| 198 | + echo "compile success" |
| 199 | + #sometimes renaming executable with descriptive information is useful |
| 200 | + #cp $WRF_DIR/main/ideal.exe $WRF_DIR/main/ideal_${idate}_${imach}.exe |
| 201 | + #cp $WRF_DIR/main/real.exe $WRF_DIR/main/real_${idate}_${imach}.exe |
| 202 | + #cp $WRF_DIR/main/wrf.exe $WRF_DIR/main/wrf_${idate}_${imach}.exe |
| 203 | + #cp $WRF_DIR/main/ndown.exe $WRF_DIR/main/ndown_${idate}_${imach}.exe |
| 204 | + [[ ${mod_perftools} == perftools ]] && cd $WRF_DIR/main && pat_build -f wrf.exe |
| 205 | + fi |
| 206 | + |
| 207 | +fi |
0 commit comments