From 1f66a18064b301821b818318f86d54bf12d0b470 Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Thu, 5 Dec 2024 22:14:44 +0800 Subject: [PATCH 01/29] Feature: add default machine Add a feature that read and write `.local_settings` (repository level) and `global_settings` (user level) files to save the default machine. Add tool/set_settings.sh script to setup the default machine. In src/configure.py: - Change the default of `--machine` to None. - Add function `get_machine()` to read the default machine setting. - Raise FileNotFoundError if the .config file is not found. Add `src/.local_settings` to .gitignore. --- .gitignore | 1 + src/configure.py | 43 +++++++++++++++++++++++++++--- tool/set_settings.sh | 63 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 tool/set_settings.sh diff --git a/.gitignore b/.gitignore index c54ae8b623..07d505edba 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ GAMER_ExtractProfile .vscode src/Makefile src/Makefile.log +src/.local_settings diff --git a/src/configure.py b/src/configure.py index 9749b37e81..6c2628a3ad 100755 --- a/src/configure.py +++ b/src/configure.py @@ -29,6 +29,8 @@ GAMER_CONFIG_DIR = os.path.join("..", "configs") GAMER_MAKE_BASE = "Makefile_base" GAMER_MAKE_OUT = "Makefile" +GAMER_LOCAL_SETTING = ".local_settings" +GAMER_GLOBAL_SETTING = "~/.config/gamer/global_settings" GAMER_DESCRIPTION = "Prepare a customized Makefile for GAMER.\nDefault values are marked by '*'.\nUse -lh to show a detailed help message.\n" GAMER_EPILOG = "2023 Computational Astrophysics Lab, NTU. All rights reserved.\n" @@ -371,8 +373,8 @@ def load_arguments(): # machine config setup parser.add_argument( "--machine", type=str, metavar="MACHINE", - default="eureka_intel", - help="Select the MACHINE.config file under ../configs directory.\nChoice: [eureka_intel, YOUR_MACHINE_NAME] => " + default=None, + help="Select the MACHINE.config file under ../configs directory.\nChoice: [eureka_intel, spock_intel, YOUR_MACHINE_NAME, ...] => " ) # A. options of diffierent physical models @@ -729,8 +731,11 @@ def load_config( config ): paths, compilers = {}, {"CXX":"", "CXX_MPI":""} flags = {"CXXFLAG":"", "OPENMPFLAG":"", "LIBFLAG":"", "NVCCFLAG_COM":"", "NVCCFLAG_FLU":"", "NVCCFLAG_POT":""} gpus = {"GPU_COMPUTE_CAPABILITY":""} - with open( config, 'r') as f: - lines = f.readlines() + if os.path.isfile( config ): + with open( config, 'r') as f: + lines = f.readlines() + else: + raise FileNotFoundError("The config file <%s> does not exist."%(config)) for line in lines: temp = list( filter( None, re.split(" |:=|\n", line) ) ) # separate by " " and ":=" @@ -760,6 +765,9 @@ def load_config( config ): return paths, compilers, flags, gpus def set_conditional_defaults( args ): + if args['machine'] is None: + args['machine'] = get_machine() + if args["unsplit_gravity"] is None: args["unsplit_gravity"] = (args["model"] == "HYDRO") @@ -784,6 +792,33 @@ def set_conditional_defaults( args ): args["barotropic"] = (args["eos"] == "ISOTHERMAL") return args +def get_machine(): + ''' + When the `--machine` flag is not given, this function will be called + and return the default machine in the following order: + 1. Read the local setting located at `GAMER_LOCAL_SETTING`. + 2. Read the global setting located at `GAMER_GLOBAL_SETTING`. + 3. Fall back to `eureka_intel` for backward compatibility. + ''' + # Check if GAMER_LOCAL_SETTING exists + local_setting = GAMER_LOCAL_SETTING + if os.path.isfile(local_setting): + with open(local_setting, 'r') as f: + for line in f: + tokens = line.strip().split() + if len(tokens) >= 2 and tokens[0] == 'machine_name': + return tokens[1] + # Check if GAMER_GLOBAL_SETTING exists + global_setting = os.path.expanduser(GAMER_GLOBAL_SETTING) + if os.path.isfile(global_setting): + with open(global_setting, 'r') as f: + for line in f: + tokens = line.strip().split() + if len(tokens) >= 2 and tokens[0] == 'machine_name': + return tokens[1] + # Fall back to `eureka_intel` for backward compatibility + return 'eureka_intel' + def set_gpu( gpus, flags, args ): gpu_opts = {} compute_capability = gpus["GPU_COMPUTE_CAPABILITY"] diff --git a/tool/set_settings.sh b/tool/set_settings.sh new file mode 100644 index 0000000000..33e815972d --- /dev/null +++ b/tool/set_settings.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +cd "$(dirname "$0")" + +show_help() { + echo "Usage: $0 [--local | --global] --machine=" + echo "" + echo "Options:" + echo " --local Use local settings" + echo " --global Use global settings" + echo " --machine= Specify the machine name" + echo " -h, --help Show this help message" +} + +# Parse arguments +LOCAL=false +GLOBAL=false +MACHINE="" + +while [[ "$#" -gt 0 ]]; do + case $1 in + --local) LOCAL=true ;; + --global) GLOBAL=true ;; + --machine=*) MACHINE="${1#*=}" ;; + -h|--help) show_help; exit 0 ;; + --machine) echo "Error: Use --machine= to specify the machine."; exit 1 ;; + *) echo "Unknown parameter passed: $1"; echo "Use -h or --help for usage information."; exit 1 ;; + esac + shift +done + +if [ -z "$MACHINE" ]; then + echo "Error: --machine option is required." + exit 1 +fi + +if [ "$LOCAL" = true ] && [ "$GLOBAL" = true ]; then + echo "Error: Cannot specify both --local and --global." + exit 1 +elif [ "$LOCAL" = true ]; then + SETTING_FILE="../src/.local_settings" + SETTING_TYPE="local" +elif [ "$GLOBAL" = true ]; then + SETTING_FILE="$HOME/.config/gamer/global_settings" + SETTING_TYPE="global" +else + echo "Error: Specify either --local or --global." + exit 1 +fi + +mkdir -p "$(dirname "$SETTING_FILE")" + +# Write the machine name to the setting file +echo "# GAMER setting file" > "$SETTING_FILE" +echo "machine_name $MACHINE" >> "$SETTING_FILE" + +# Check if the write was successful +if [ $? -ne 0 ]; then + echo "Error: Failed to write to $SETTING_FILE." + exit 1 +fi + +echo "Successfully wrote $SETTING_TYPE settings to $SETTING_FILE for machine='$MACHINE'." From b0573cee7917fe0c3f0d676f4f89aba1434360a8 Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Thu, 5 Dec 2024 22:31:13 +0800 Subject: [PATCH 02/29] Remove --machine=eureka_intel They will fall back to `eureka_intel` when not specified. If not removed, `--machine=eureka_intel` will override the user setting. --- example/test_problem/ELBDM/DiskHeating/generate_make.sh | 2 +- example/test_problem/ELBDM/ExtPot/generate_make.sh | 2 +- .../test_problem/ELBDM/GaussianWavePacket/generate_make.sh | 2 +- example/test_problem/ELBDM/HaloMerger/generate_make.sh | 2 +- example/test_problem/ELBDM/IsolatedHalo/generate_make.sh | 2 +- .../ELBDM/JeansInstabilityComoving/generate_make.sh | 2 +- example/test_problem/ELBDM/LSS/generate_make.sh | 2 +- example/test_problem/ELBDM/LSS_Hybrid/generate_make.sh | 2 +- example/test_problem/ELBDM/Perturbation/generate_make.sh | 2 +- .../ELBDM/Perturbation/generate_make_BaseSpectral.sh | 2 +- example/test_problem/ELBDM/PlaneWave/generate_make.sh | 2 +- .../test_problem/ELBDM/RestrictionMismatch/generate_make.sh | 2 +- example/test_problem/ELBDM/SelfSimilarHalo/generate_make.sh | 2 +- example/test_problem/ELBDM/Soliton/generate_make.sh | 2 +- example/test_problem/ELBDM/VortexPairLinear/generate_make.sh | 2 +- .../ELBDM/VortexPairLinear_Hybrid/generate_make.sh | 2 +- .../test_problem/ELBDM/VortexPairRotating/generate_make.sh | 2 +- .../ELBDM/VortexPairRotating_Hybrid/generate_make.sh | 2 +- .../test_problem/Hydro/AGORA_IsolatedGalaxy/generate_make.sh | 2 +- example/test_problem/Hydro/AcousticWave/generate_make.sh | 2 +- example/test_problem/Hydro/BlastWave/generate_make.sh | 2 +- example/test_problem/Hydro/Bondi/generate_make.sh | 2 +- example/test_problem/Hydro/CDM_LSS/generate_make.sh | 2 +- example/test_problem/Hydro/CMZ/generate_make.sh | 2 +- example/test_problem/Hydro/CR_Diffusion/generate_make.sh | 2 +- example/test_problem/Hydro/CR_ShockTube/generate_make.sh | 4 ++-- example/test_problem/Hydro/CR_SoundWave/generate_make.sh | 4 ++-- example/test_problem/Hydro/Caustic/generate_make.sh | 2 +- example/test_problem/Hydro/ClusterMerger/generate_make.sh | 2 +- .../test_problem/Hydro/EnergyPowerSpectrum/generate_make.sh | 2 +- example/test_problem/Hydro/Gravity/generate_make.sh | 2 +- example/test_problem/Hydro/JeansInstability/generate_make.sh | 2 +- example/test_problem/Hydro/Jet/generate_make.sh | 2 +- example/test_problem/Hydro/JetICMWall/generate_make.sh | 2 +- .../Hydro/KelvinHelmholtzInstability/generate_make.sh | 2 +- example/test_problem/Hydro/MHD_ABC/generate_make.sh | 2 +- example/test_problem/Hydro/MHD_LinearWave/generate_make.sh | 2 +- .../test_problem/Hydro/MHD_OrszagTangVortex/generate_make.sh | 2 +- .../test_problem/Hydro/ParticleEquilibriumIC/generate_make.sh | 2 +- example/test_problem/Hydro/ParticleTest/generate_make.sh | 2 +- example/test_problem/Hydro/Plummer/generate_make.sh | 2 +- example/test_problem/Hydro/Riemann/generate_make.sh | 2 +- .../test_problem/Hydro/Riemann/generate_make_Riemann_SRHD.sh | 2 +- example/test_problem/Hydro/SphericalCollapse/generate_make.sh | 2 +- example/test_problem/Hydro/Zeldovich/generate_make.sh | 2 +- 45 files changed, 47 insertions(+), 47 deletions(-) diff --git a/example/test_problem/ELBDM/DiskHeating/generate_make.sh b/example/test_problem/ELBDM/DiskHeating/generate_make.sh index 47257f54a3..f7b0b4c929 100644 --- a/example/test_problem/ELBDM/DiskHeating/generate_make.sh +++ b/example/test_problem/ELBDM/DiskHeating/generate_make.sh @@ -2,6 +2,6 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ +${PYTHON} configure.py --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ --model=ELBDM --gravity=true --particle=true --store_par_acc=true \ --gsl=true --max_patch=20000000 diff --git a/example/test_problem/ELBDM/ExtPot/generate_make.sh b/example/test_problem/ELBDM/ExtPot/generate_make.sh index 242546e4e1..569ad88707 100644 --- a/example/test_problem/ELBDM/ExtPot/generate_make.sh +++ b/example/test_problem/ELBDM/ExtPot/generate_make.sh @@ -2,5 +2,5 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ +${PYTHON} configure.py --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ --model=ELBDM --gravity=true diff --git a/example/test_problem/ELBDM/GaussianWavePacket/generate_make.sh b/example/test_problem/ELBDM/GaussianWavePacket/generate_make.sh index 8f9c9241e0..f4b2108cee 100644 --- a/example/test_problem/ELBDM/GaussianWavePacket/generate_make.sh +++ b/example/test_problem/ELBDM/GaussianWavePacket/generate_make.sh @@ -2,4 +2,4 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --model=ELBDM +${PYTHON} configure.py --model=ELBDM diff --git a/example/test_problem/ELBDM/HaloMerger/generate_make.sh b/example/test_problem/ELBDM/HaloMerger/generate_make.sh index d4d5c3ceb3..64cbf81f9d 100644 --- a/example/test_problem/ELBDM/HaloMerger/generate_make.sh +++ b/example/test_problem/ELBDM/HaloMerger/generate_make.sh @@ -2,5 +2,5 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ +${PYTHON} configure.py --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ --model=ELBDM --gravity=true --particle=true --gsl=true diff --git a/example/test_problem/ELBDM/IsolatedHalo/generate_make.sh b/example/test_problem/ELBDM/IsolatedHalo/generate_make.sh index 242546e4e1..569ad88707 100644 --- a/example/test_problem/ELBDM/IsolatedHalo/generate_make.sh +++ b/example/test_problem/ELBDM/IsolatedHalo/generate_make.sh @@ -2,5 +2,5 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ +${PYTHON} configure.py --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ --model=ELBDM --gravity=true diff --git a/example/test_problem/ELBDM/JeansInstabilityComoving/generate_make.sh b/example/test_problem/ELBDM/JeansInstabilityComoving/generate_make.sh index b827f3034a..d52f92fd27 100644 --- a/example/test_problem/ELBDM/JeansInstabilityComoving/generate_make.sh +++ b/example/test_problem/ELBDM/JeansInstabilityComoving/generate_make.sh @@ -2,5 +2,5 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --hdf5=true --gpu=true --fftw=FFTW3 --double=true \ +${PYTHON} configure.py --hdf5=true --gpu=true --fftw=FFTW3 --double=true \ --model=ELBDM --gravity=true --comoving=true diff --git a/example/test_problem/ELBDM/LSS/generate_make.sh b/example/test_problem/ELBDM/LSS/generate_make.sh index 60d4798491..a0b31ec202 100644 --- a/example/test_problem/ELBDM/LSS/generate_make.sh +++ b/example/test_problem/ELBDM/LSS/generate_make.sh @@ -2,5 +2,5 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ +${PYTHON} configure.py --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ --model=ELBDM --gravity=true --comoving=true diff --git a/example/test_problem/ELBDM/LSS_Hybrid/generate_make.sh b/example/test_problem/ELBDM/LSS_Hybrid/generate_make.sh index 6189e74ade..8966f93503 100644 --- a/example/test_problem/ELBDM/LSS_Hybrid/generate_make.sh +++ b/example/test_problem/ELBDM/LSS_Hybrid/generate_make.sh @@ -2,6 +2,6 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ +${PYTHON} configure.py --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ --model=ELBDM --elbdm_scheme=ELBDM_HYBRID --wave_scheme=WAVE_GRAMFE --gramfe_scheme=GRAMFE_MATMUL \ --gravity=true --comoving=true --gsl=true --spectral_interpolation=true diff --git a/example/test_problem/ELBDM/Perturbation/generate_make.sh b/example/test_problem/ELBDM/Perturbation/generate_make.sh index d009303a91..9b930a4d76 100644 --- a/example/test_problem/ELBDM/Perturbation/generate_make.sh +++ b/example/test_problem/ELBDM/Perturbation/generate_make.sh @@ -2,6 +2,6 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ +${PYTHON} configure.py --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ --model=ELBDM --elbdm_scheme=ELBDM_HYBRID --wave_scheme=WAVE_GRAMFE --gramfe_scheme=GRAMFE_MATMUL \ --gravity=true --comoving=false --gsl=true --spectral_interpolation=true diff --git a/example/test_problem/ELBDM/Perturbation/generate_make_BaseSpectral.sh b/example/test_problem/ELBDM/Perturbation/generate_make_BaseSpectral.sh index 5ac98961d2..18f8a3ea65 100644 --- a/example/test_problem/ELBDM/Perturbation/generate_make_BaseSpectral.sh +++ b/example/test_problem/ELBDM/Perturbation/generate_make_BaseSpectral.sh @@ -2,6 +2,6 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --mpi=true --hdf5=true --fftw=FFTW3 \ +${PYTHON} configure.py --mpi=true --hdf5=true --fftw=FFTW3 \ --model=ELBDM --elbdm_scheme=ELBDM_WAVE \ --gravity=true --comoving=false \ No newline at end of file diff --git a/example/test_problem/ELBDM/PlaneWave/generate_make.sh b/example/test_problem/ELBDM/PlaneWave/generate_make.sh index 47d4313566..34a71bcd3a 100644 --- a/example/test_problem/ELBDM/PlaneWave/generate_make.sh +++ b/example/test_problem/ELBDM/PlaneWave/generate_make.sh @@ -2,4 +2,4 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --model=ELBDM --double=True --passive=1 +${PYTHON} configure.py --model=ELBDM --double=True --passive=1 diff --git a/example/test_problem/ELBDM/RestrictionMismatch/generate_make.sh b/example/test_problem/ELBDM/RestrictionMismatch/generate_make.sh index 20a6a18ce7..9c869b88c0 100644 --- a/example/test_problem/ELBDM/RestrictionMismatch/generate_make.sh +++ b/example/test_problem/ELBDM/RestrictionMismatch/generate_make.sh @@ -2,4 +2,4 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --model=ELBDM --double=True --hdf5=true --fftw=FFTW3 +${PYTHON} configure.py --model=ELBDM --double=True --hdf5=true --fftw=FFTW3 diff --git a/example/test_problem/ELBDM/SelfSimilarHalo/generate_make.sh b/example/test_problem/ELBDM/SelfSimilarHalo/generate_make.sh index 60d4798491..a0b31ec202 100644 --- a/example/test_problem/ELBDM/SelfSimilarHalo/generate_make.sh +++ b/example/test_problem/ELBDM/SelfSimilarHalo/generate_make.sh @@ -2,5 +2,5 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ +${PYTHON} configure.py --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ --model=ELBDM --gravity=true --comoving=true diff --git a/example/test_problem/ELBDM/Soliton/generate_make.sh b/example/test_problem/ELBDM/Soliton/generate_make.sh index 242546e4e1..569ad88707 100644 --- a/example/test_problem/ELBDM/Soliton/generate_make.sh +++ b/example/test_problem/ELBDM/Soliton/generate_make.sh @@ -2,5 +2,5 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ +${PYTHON} configure.py --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ --model=ELBDM --gravity=true diff --git a/example/test_problem/ELBDM/VortexPairLinear/generate_make.sh b/example/test_problem/ELBDM/VortexPairLinear/generate_make.sh index 1f6c9aa12a..2c6b0c0b0a 100644 --- a/example/test_problem/ELBDM/VortexPairLinear/generate_make.sh +++ b/example/test_problem/ELBDM/VortexPairLinear/generate_make.sh @@ -2,4 +2,4 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --model=ELBDM --hdf5=true +${PYTHON} configure.py --model=ELBDM --hdf5=true diff --git a/example/test_problem/ELBDM/VortexPairLinear_Hybrid/generate_make.sh b/example/test_problem/ELBDM/VortexPairLinear_Hybrid/generate_make.sh index f825e60d11..c96b46493b 100644 --- a/example/test_problem/ELBDM/VortexPairLinear_Hybrid/generate_make.sh +++ b/example/test_problem/ELBDM/VortexPairLinear_Hybrid/generate_make.sh @@ -2,4 +2,4 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --model=ELBDM --elbdm_scheme=ELBDM_HYBRID --hdf5=true --mpi=true +${PYTHON} configure.py --model=ELBDM --elbdm_scheme=ELBDM_HYBRID --hdf5=true --mpi=true diff --git a/example/test_problem/ELBDM/VortexPairRotating/generate_make.sh b/example/test_problem/ELBDM/VortexPairRotating/generate_make.sh index 1f6c9aa12a..2c6b0c0b0a 100644 --- a/example/test_problem/ELBDM/VortexPairRotating/generate_make.sh +++ b/example/test_problem/ELBDM/VortexPairRotating/generate_make.sh @@ -2,4 +2,4 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --model=ELBDM --hdf5=true +${PYTHON} configure.py --model=ELBDM --hdf5=true diff --git a/example/test_problem/ELBDM/VortexPairRotating_Hybrid/generate_make.sh b/example/test_problem/ELBDM/VortexPairRotating_Hybrid/generate_make.sh index f825e60d11..c96b46493b 100644 --- a/example/test_problem/ELBDM/VortexPairRotating_Hybrid/generate_make.sh +++ b/example/test_problem/ELBDM/VortexPairRotating_Hybrid/generate_make.sh @@ -2,4 +2,4 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --model=ELBDM --elbdm_scheme=ELBDM_HYBRID --hdf5=true --mpi=true +${PYTHON} configure.py --model=ELBDM --elbdm_scheme=ELBDM_HYBRID --hdf5=true --mpi=true diff --git a/example/test_problem/Hydro/AGORA_IsolatedGalaxy/generate_make.sh b/example/test_problem/Hydro/AGORA_IsolatedGalaxy/generate_make.sh index a1d69463c8..8851f7a668 100644 --- a/example/test_problem/Hydro/AGORA_IsolatedGalaxy/generate_make.sh +++ b/example/test_problem/Hydro/AGORA_IsolatedGalaxy/generate_make.sh @@ -2,6 +2,6 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true --model=HYDRO \ +${PYTHON} configure.py --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true --model=HYDRO \ --particle=true --gravity=true --flu_scheme=MHM --flux=HLLC \ --passive=1 --par_attribute=1 --dual=DE_ENPY --star_formation=true --grackle=true "$@" diff --git a/example/test_problem/Hydro/AcousticWave/generate_make.sh b/example/test_problem/Hydro/AcousticWave/generate_make.sh index 1e1a433d7b..b8fa682b4c 100644 --- a/example/test_problem/Hydro/AcousticWave/generate_make.sh +++ b/example/test_problem/Hydro/AcousticWave/generate_make.sh @@ -2,4 +2,4 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --model=HYDRO --double=True "$@" +${PYTHON} configure.py --model=HYDRO --double=True "$@" diff --git a/example/test_problem/Hydro/BlastWave/generate_make.sh b/example/test_problem/Hydro/BlastWave/generate_make.sh index a6b02341cb..e6bab92044 100644 --- a/example/test_problem/Hydro/BlastWave/generate_make.sh +++ b/example/test_problem/Hydro/BlastWave/generate_make.sh @@ -2,4 +2,4 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --model=HYDRO --hdf5=true "$@" +${PYTHON} configure.py --model=HYDRO --hdf5=true "$@" diff --git a/example/test_problem/Hydro/Bondi/generate_make.sh b/example/test_problem/Hydro/Bondi/generate_make.sh index 4e8ddae896..70de7de3a3 100644 --- a/example/test_problem/Hydro/Bondi/generate_make.sh +++ b/example/test_problem/Hydro/Bondi/generate_make.sh @@ -2,5 +2,5 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --hdf5=true --gpu=true --fftw=FFTW3 \ +${PYTHON} configure.py --hdf5=true --gpu=true --fftw=FFTW3 \ --model=HYDRO --gravity=true --dual=DE_ENPY "$@" diff --git a/example/test_problem/Hydro/CDM_LSS/generate_make.sh b/example/test_problem/Hydro/CDM_LSS/generate_make.sh index b65dd79740..3625c38069 100644 --- a/example/test_problem/Hydro/CDM_LSS/generate_make.sh +++ b/example/test_problem/Hydro/CDM_LSS/generate_make.sh @@ -2,5 +2,5 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ +${PYTHON} configure.py --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ --model=HYDRO --particle=true --gravity=true --comoving=true "$@" diff --git a/example/test_problem/Hydro/CMZ/generate_make.sh b/example/test_problem/Hydro/CMZ/generate_make.sh index eeff6b4733..101724b523 100644 --- a/example/test_problem/Hydro/CMZ/generate_make.sh +++ b/example/test_problem/Hydro/CMZ/generate_make.sh @@ -2,6 +2,6 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --hdf5=true --gpu=true --fftw=FFTW3 \ +${PYTHON} configure.py --hdf5=true --gpu=true --fftw=FFTW3 \ --model=HYDRO --gravity=true --eos=ISOTHERMAL --barotropic=true \ --flu_scheme=MHM --flux=HLLC --passive=1 --par_attribute=1 "$@" diff --git a/example/test_problem/Hydro/CR_Diffusion/generate_make.sh b/example/test_problem/Hydro/CR_Diffusion/generate_make.sh index 431a9e912e..da6ed33ade 100644 --- a/example/test_problem/Hydro/CR_Diffusion/generate_make.sh +++ b/example/test_problem/Hydro/CR_Diffusion/generate_make.sh @@ -2,5 +2,5 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --model=HYDRO --flu_scheme=MHM_RP --flux=HLLD --mhd=true \ +${PYTHON} configure.py --model=HYDRO --flu_scheme=MHM_RP --flux=HLLD --mhd=true \ --cosmic_ray=true --eos=COSMIC_RAY --cr_diffusion=true --hdf5=true "$@" diff --git a/example/test_problem/Hydro/CR_ShockTube/generate_make.sh b/example/test_problem/Hydro/CR_ShockTube/generate_make.sh index 2b0048bba3..487fba62d0 100644 --- a/example/test_problem/Hydro/CR_ShockTube/generate_make.sh +++ b/example/test_problem/Hydro/CR_ShockTube/generate_make.sh @@ -2,7 +2,7 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --model=HYDRO --flu_scheme=MHM_RP --mhd=false --flux=HLLE \ +${PYTHON} configure.py --model=HYDRO --flu_scheme=MHM_RP --mhd=false --flux=HLLE \ --cosmic_ray=true --eos=COSMIC_RAY "$@" -#${PYTHON} configure.py --machine=eureka_intel --model=HYDRO --flu_scheme=MHM_RP --mhd=true --flux=HLLD \ +#${PYTHON} configure.py --model=HYDRO --flu_scheme=MHM_RP --mhd=true --flux=HLLD \ # --cosmic_ray=true --eos=COSMIC_RAY "$@" diff --git a/example/test_problem/Hydro/CR_SoundWave/generate_make.sh b/example/test_problem/Hydro/CR_SoundWave/generate_make.sh index 15352871fb..36a09353b4 100644 --- a/example/test_problem/Hydro/CR_SoundWave/generate_make.sh +++ b/example/test_problem/Hydro/CR_SoundWave/generate_make.sh @@ -2,7 +2,7 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --model=HYDRO --flu_scheme=MHM_RP --mhd=false --flux=HLLE \ +${PYTHON} configure.py --model=HYDRO --flu_scheme=MHM_RP --mhd=false --flux=HLLE \ --double=true --cosmic_ray=true --eos=COSMIC_RAY "$@" -#${PYTHON} configure.py --machine=eureka_intel --model=HYDRO --flu_scheme=MHM_RP --mhd=true --flux=HLLD \ +#${PYTHON} configure.py --model=HYDRO --flu_scheme=MHM_RP --mhd=true --flux=HLLD \ # --double=true --cosmic_ray=true --eos=COSMIC_RAY "$@" diff --git a/example/test_problem/Hydro/Caustic/generate_make.sh b/example/test_problem/Hydro/Caustic/generate_make.sh index 1e1a433d7b..b8fa682b4c 100644 --- a/example/test_problem/Hydro/Caustic/generate_make.sh +++ b/example/test_problem/Hydro/Caustic/generate_make.sh @@ -2,4 +2,4 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --model=HYDRO --double=True "$@" +${PYTHON} configure.py --model=HYDRO --double=True "$@" diff --git a/example/test_problem/Hydro/ClusterMerger/generate_make.sh b/example/test_problem/Hydro/ClusterMerger/generate_make.sh index bfbef2e3e4..b49cd03961 100644 --- a/example/test_problem/Hydro/ClusterMerger/generate_make.sh +++ b/example/test_problem/Hydro/ClusterMerger/generate_make.sh @@ -2,5 +2,5 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ +${PYTHON} configure.py --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ --model=HYDRO --particle=true --gravity=true --passive=2 "$@" diff --git a/example/test_problem/Hydro/EnergyPowerSpectrum/generate_make.sh b/example/test_problem/Hydro/EnergyPowerSpectrum/generate_make.sh index eddc712680..395fef457a 100644 --- a/example/test_problem/Hydro/EnergyPowerSpectrum/generate_make.sh +++ b/example/test_problem/Hydro/EnergyPowerSpectrum/generate_make.sh @@ -2,4 +2,4 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --fftw=FFTW3 --model=HYDRO --eos=GAMMA --mpi=true "$@" +${PYTHON} configure.py --fftw=FFTW3 --model=HYDRO --eos=GAMMA --mpi=true "$@" diff --git a/example/test_problem/Hydro/Gravity/generate_make.sh b/example/test_problem/Hydro/Gravity/generate_make.sh index f79e63d5ba..7d876d0706 100644 --- a/example/test_problem/Hydro/Gravity/generate_make.sh +++ b/example/test_problem/Hydro/Gravity/generate_make.sh @@ -2,4 +2,4 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --gpu=true --hdf5=true --fftw=FFTW3 --model=HYDRO --gravity=true "$@" +${PYTHON} configure.py --gpu=true --hdf5=true --fftw=FFTW3 --model=HYDRO --gravity=true "$@" diff --git a/example/test_problem/Hydro/JeansInstability/generate_make.sh b/example/test_problem/Hydro/JeansInstability/generate_make.sh index 4dcd3dbbd4..fa4ab9ee94 100644 --- a/example/test_problem/Hydro/JeansInstability/generate_make.sh +++ b/example/test_problem/Hydro/JeansInstability/generate_make.sh @@ -2,5 +2,5 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --gpu=true --fftw=FFTW3 --double=true \ +${PYTHON} configure.py --gpu=true --fftw=FFTW3 --double=true \ --model=HYDRO --gravity=true --eos=GAMMA "$@" diff --git a/example/test_problem/Hydro/Jet/generate_make.sh b/example/test_problem/Hydro/Jet/generate_make.sh index 952e5053dd..3052779d2a 100644 --- a/example/test_problem/Hydro/Jet/generate_make.sh +++ b/example/test_problem/Hydro/Jet/generate_make.sh @@ -2,5 +2,5 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --hdf5=true --gpu=true --mpi=true --fftw=FFTW3 \ +${PYTHON} configure.py --hdf5=true --gpu=true --mpi=true --fftw=FFTW3 \ --model=HYDRO --gravity=true "$@" diff --git a/example/test_problem/Hydro/JetICMWall/generate_make.sh b/example/test_problem/Hydro/JetICMWall/generate_make.sh index 035f4b5356..89094b3ed3 100644 --- a/example/test_problem/Hydro/JetICMWall/generate_make.sh +++ b/example/test_problem/Hydro/JetICMWall/generate_make.sh @@ -2,6 +2,6 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --mpi=true --hdf5=true --gpu=true --model=HYDRO \ +${PYTHON} configure.py --mpi=true --hdf5=true --gpu=true --model=HYDRO \ --srhd=true --eos=TAUBMATHEWS --flux=HLLC --flu_scheme=MHM \ --passive=4 "$@" diff --git a/example/test_problem/Hydro/KelvinHelmholtzInstability/generate_make.sh b/example/test_problem/Hydro/KelvinHelmholtzInstability/generate_make.sh index 12521fa79f..e4dc316bea 100644 --- a/example/test_problem/Hydro/KelvinHelmholtzInstability/generate_make.sh +++ b/example/test_problem/Hydro/KelvinHelmholtzInstability/generate_make.sh @@ -2,4 +2,4 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --hdf5=true --gpu=true --mpi=true --fftw=FFTW3 --model=HYDRO "$@" +${PYTHON} configure.py --hdf5=true --gpu=true --mpi=true --fftw=FFTW3 --model=HYDRO "$@" diff --git a/example/test_problem/Hydro/MHD_ABC/generate_make.sh b/example/test_problem/Hydro/MHD_ABC/generate_make.sh index 6b066a4162..e6179f90c0 100644 --- a/example/test_problem/Hydro/MHD_ABC/generate_make.sh +++ b/example/test_problem/Hydro/MHD_ABC/generate_make.sh @@ -2,5 +2,5 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --hdf5=true --gpu=true --mpi=true \ +${PYTHON} configure.py --hdf5=true --gpu=true --mpi=true \ --model=HYDRO --mhd=true "$@" diff --git a/example/test_problem/Hydro/MHD_LinearWave/generate_make.sh b/example/test_problem/Hydro/MHD_LinearWave/generate_make.sh index f2e70fa968..751e715fdd 100644 --- a/example/test_problem/Hydro/MHD_LinearWave/generate_make.sh +++ b/example/test_problem/Hydro/MHD_LinearWave/generate_make.sh @@ -2,4 +2,4 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --double=true --model=HYDRO --mhd=true --eos=GAMMA "$@" +${PYTHON} configure.py --double=true --model=HYDRO --mhd=true --eos=GAMMA "$@" diff --git a/example/test_problem/Hydro/MHD_OrszagTangVortex/generate_make.sh b/example/test_problem/Hydro/MHD_OrszagTangVortex/generate_make.sh index 6b066a4162..e6179f90c0 100644 --- a/example/test_problem/Hydro/MHD_OrszagTangVortex/generate_make.sh +++ b/example/test_problem/Hydro/MHD_OrszagTangVortex/generate_make.sh @@ -2,5 +2,5 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --hdf5=true --gpu=true --mpi=true \ +${PYTHON} configure.py --hdf5=true --gpu=true --mpi=true \ --model=HYDRO --mhd=true "$@" diff --git a/example/test_problem/Hydro/ParticleEquilibriumIC/generate_make.sh b/example/test_problem/Hydro/ParticleEquilibriumIC/generate_make.sh index 7ff336392f..f028629dfc 100644 --- a/example/test_problem/Hydro/ParticleEquilibriumIC/generate_make.sh +++ b/example/test_problem/Hydro/ParticleEquilibriumIC/generate_make.sh @@ -2,5 +2,5 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true --gsl=true \ +${PYTHON} configure.py --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true --gsl=true \ --model=HYDRO --particle=true --gravity=true "$@" diff --git a/example/test_problem/Hydro/ParticleTest/generate_make.sh b/example/test_problem/Hydro/ParticleTest/generate_make.sh index 40c13d3d6e..fe069d4ace 100644 --- a/example/test_problem/Hydro/ParticleTest/generate_make.sh +++ b/example/test_problem/Hydro/ParticleTest/generate_make.sh @@ -2,5 +2,5 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ +${PYTHON} configure.py --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ --model=HYDRO --particle=true --tracer=true --gravity=true "$@" diff --git a/example/test_problem/Hydro/Plummer/generate_make.sh b/example/test_problem/Hydro/Plummer/generate_make.sh index fba86cf573..e022e8b448 100644 --- a/example/test_problem/Hydro/Plummer/generate_make.sh +++ b/example/test_problem/Hydro/Plummer/generate_make.sh @@ -2,5 +2,5 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ +${PYTHON} configure.py --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ --model=HYDRO --particle=true --gravity=true "$@" diff --git a/example/test_problem/Hydro/Riemann/generate_make.sh b/example/test_problem/Hydro/Riemann/generate_make.sh index f06567704d..82120dfead 100644 --- a/example/test_problem/Hydro/Riemann/generate_make.sh +++ b/example/test_problem/Hydro/Riemann/generate_make.sh @@ -2,4 +2,4 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --model=HYDRO "$@" +${PYTHON} configure.py --model=HYDRO "$@" diff --git a/example/test_problem/Hydro/Riemann/generate_make_Riemann_SRHD.sh b/example/test_problem/Hydro/Riemann/generate_make_Riemann_SRHD.sh index 3ce51d82d4..1805834732 100644 --- a/example/test_problem/Hydro/Riemann/generate_make_Riemann_SRHD.sh +++ b/example/test_problem/Hydro/Riemann/generate_make_Riemann_SRHD.sh @@ -2,4 +2,4 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --model=HYDRO --srhd=true --double=true --eos=TAUBMATHEWS --flux=HLLC --flu_scheme=MHM --slope=PLM "$@" +${PYTHON} configure.py --model=HYDRO --srhd=true --double=true --eos=TAUBMATHEWS --flux=HLLC --flu_scheme=MHM --slope=PLM "$@" diff --git a/example/test_problem/Hydro/SphericalCollapse/generate_make.sh b/example/test_problem/Hydro/SphericalCollapse/generate_make.sh index 5849b640f0..6398a3fe92 100644 --- a/example/test_problem/Hydro/SphericalCollapse/generate_make.sh +++ b/example/test_problem/Hydro/SphericalCollapse/generate_make.sh @@ -2,5 +2,5 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ +${PYTHON} configure.py --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ --model=HYDRO --gravity=true --comoving=true --dual=DE_ENPY "$@" diff --git a/example/test_problem/Hydro/Zeldovich/generate_make.sh b/example/test_problem/Hydro/Zeldovich/generate_make.sh index 0fa500a935..ac3b9a5f8b 100644 --- a/example/test_problem/Hydro/Zeldovich/generate_make.sh +++ b/example/test_problem/Hydro/Zeldovich/generate_make.sh @@ -2,6 +2,6 @@ PYTHON=python3 -${PYTHON} configure.py --machine=eureka_intel --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ +${PYTHON} configure.py --mpi=true --hdf5=true --fftw=FFTW3 --gpu=true \ --gsl=true --double=true --model=HYDRO --gravity=true \ --comoving=true --dual=DE_ENPY --particle=true "$@" From adddaf573fdae76db4fd3d8d65172803dde6265f Mon Sep 17 00:00:00 2001 From: ChunYen-Chen Date: Fri, 13 Dec 2024 15:26:56 +0800 Subject: [PATCH 03/29] Generalize system setting and update code style --- src/configure.py | 173 ++++++++++++++++++++++++++--------------------- 1 file changed, 97 insertions(+), 76 deletions(-) diff --git a/src/configure.py b/src/configure.py index 6c2628a3ad..a185298159 100755 --- a/src/configure.py +++ b/src/configure.py @@ -26,17 +26,17 @@ CLOSE_DIST = 2 PRINT_WIDTH = 100 -GAMER_CONFIG_DIR = os.path.join("..", "configs") -GAMER_MAKE_BASE = "Makefile_base" -GAMER_MAKE_OUT = "Makefile" +GAMER_CONFIG_DIR = os.path.join("..", "configs") +GAMER_MAKE_BASE = "Makefile_base" +GAMER_MAKE_OUT = "Makefile" GAMER_LOCAL_SETTING = ".local_settings" -GAMER_GLOBAL_SETTING = "~/.config/gamer/global_settings" -GAMER_DESCRIPTION = "Prepare a customized Makefile for GAMER.\nDefault values are marked by '*'.\nUse -lh to show a detailed help message.\n" -GAMER_EPILOG = "2023 Computational Astrophysics Lab, NTU. All rights reserved.\n" +GAMER_GLOBAL_SETTING = os.path.expanduser("~/.config/gamer/global_settings") +GAMER_DESCRIPTION = "Prepare a customized Makefile for GAMER.\nDefault values are marked by '*'.\nUse -lh to show a detailed help message.\n" +GAMER_EPILOG = "2023 Computational Astrophysics Lab, NTU. All rights reserved.\n" LOGGER = logging.getLogger() -LOG_FORMAT = '%(asctime)s %(levelname)-8s: %(message)s' -logging.basicConfig( filename=GAMER_MAKE_OUT+'.log', filemode='w', level=logging.INFO, format=LOG_FORMAT ) +LOG_FORMAT = "%(asctime)s %(levelname)-8s: %(message)s" +logging.basicConfig( filename=GAMER_MAKE_OUT+".log", filemode="w", level=logging.INFO, format=LOG_FORMAT ) @@ -47,17 +47,17 @@ class CustomFormatter( logging.Formatter ): """ See: https://stackoverflow.com/questions/384076/how-can-i-color-python-logging-output """ - HEADER = '\033[95m' - OKBLUE = '\033[94m' - OKCYAN = '\033[96m' - OKGREEN = '\033[92m' - WARNING = '\033[93m' - FAIL = '\033[91m' - ENDC = '\033[0m' - BOLD = '\033[1m' - UNDERLINE = '\033[4m' - - CONSOLE_FORMAT = '%(levelname)-8s: %(message)s' + HEADER = "\033[95m" + OKBLUE = "\033[94m" + OKCYAN = "\033[96m" + OKGREEN = "\033[92m" + WARNING = "\033[93m" + FAIL = "\033[91m" + ENDC = "\033[0m" + BOLD = "\033[1m" + UNDERLINE = "\033[4m" + + CONSOLE_FORMAT = "%(levelname)-8s: %(message)s" FORMATS = { logging.DEBUG : HEADER + CONSOLE_FORMAT + ENDC, logging.INFO : ENDC + "%(message)s" + ENDC, logging.WARNING : WARNING + CONSOLE_FORMAT + ENDC, @@ -99,7 +99,7 @@ def parse_args( self, args=None, namespace=None ): msg = "\n" for arg in argv: if arg[0] != "-": - msg += 'Unrecognized positional argument: %s\n'%(arg) + msg += "Unrecognized positional argument: %s\n"%(arg) continue arg = arg.split("=")[0] # separate the assigned value. min_dist = 100000 @@ -109,10 +109,10 @@ def parse_args( self, args=None, namespace=None ): if dist >= min_dist: continue min_dist = dist pos_key = "--"+key - msg += 'Unrecognized argument: %s'%(arg) - if min_dist <= CLOSE_DIST: msg += ', do you mean: %s ?\n'%(pos_key) - msg += '\n' - if arg == '--gpu_arch': msg += "ERROR: <--gpu_arch> is deprecated. Please set in your machine *.config file (see ../configs/template.config).\n" + msg += "Unrecognized argument: %s"%(arg) + if min_dist <= CLOSE_DIST: msg += ", do you mean: %s ?\n"%(pos_key) + msg += "\n" + if arg == "--gpu_arch": msg += "ERROR: <--gpu_arch> is deprecated. Please set in your machine *.config file (see ../configs/template.config).\n" if len(argv) != 0: self.error( msg ) return args, self.gamer_names, self.depends, self.constraints @@ -123,7 +123,7 @@ def _get_option_tuples(self, option_string): # See: https://github.com/python/cpython/blob/main/Lib/argparse.py result = [] - # option strings starting with two prefix characters are only split at the '=' + # option strings starting with two prefix characters are only split at the "=" chars = self.prefix_chars if option_string[0] in chars and option_string[1] in chars: pass # we always use `allow_abbrev=False` @@ -138,7 +138,7 @@ def _get_option_tuples(self, option_string): for option_string in self._option_string_actions: if option_string == short_option_prefix: action = self._option_string_actions[option_string] - tup = action, option_string, '', short_explicit_arg + tup = action, option_string, "", short_explicit_arg result.append(tup) elif option_string.startswith(option_prefix): action = self._option_string_actions[option_string] @@ -147,7 +147,7 @@ def _get_option_tuples(self, option_string): # shouldn't ever get here else: - self.error(_('unexpected option string: %s') % option_string) + self.error(_("unexpected option string: %s") % option_string) return result # return the collected option tuples @@ -221,6 +221,14 @@ def print_help( self, *args, **kwargs ): if "print_detail" in kwargs: self.print_option() if "epilog" in self.program: print(self.program["epilog"]) +class SystemSetting( dict ): + def __init__( self, *args, **kwargs ): + super().__init__( *args, **kwargs ) + + def get_default( self, key, default_val ): + return self.__dict__[key] if key in self.__dict__ else default_val + + #################################################################################################### @@ -291,7 +299,7 @@ def get_gpu_compute_capability(): Others: https://en.wikipedia.org/wiki/CUDA#GPUs_supported """ CUDA_SUCCESS = 0 - libnames = ('libcuda.so', 'libcuda.dylib', 'cuda.dll') + libnames = ("libcuda.so", "libcuda.dylib", "cuda.dll") for libname in libnames: try: cuda = ctypes.CDLL(libname) @@ -300,7 +308,7 @@ def get_gpu_compute_capability(): else: break else: - raise OSError("could not load any of: " + ' '.join(libnames)) + raise OSError("could not load any of: " + " ".join(libnames)) nGpus, cc_major, cc_minor, device = ctypes.c_int(), ctypes.c_int(), ctypes.c_int(), ctypes.c_int() @@ -353,7 +361,7 @@ def string_align( string, indent_str, width, end_char ): if string[i] == end_char: new_line = True return new_str -def load_arguments(): +def load_arguments( sys_setting ): parser = ArgumentParser( description = GAMER_DESCRIPTION, formatter_class = argparse.RawTextHelpFormatter, epilog = GAMER_EPILOG, @@ -373,7 +381,7 @@ def load_arguments(): # machine config setup parser.add_argument( "--machine", type=str, metavar="MACHINE", - default=None, + default=sys_setting.get_default( "machine_name", "eureka_intel" ), help="Select the MACHINE.config file under ../configs directory.\nChoice: [eureka_intel, spock_intel, YOUR_MACHINE_NAME, ...] => " ) @@ -731,8 +739,9 @@ def load_config( config ): paths, compilers = {}, {"CXX":"", "CXX_MPI":""} flags = {"CXXFLAG":"", "OPENMPFLAG":"", "LIBFLAG":"", "NVCCFLAG_COM":"", "NVCCFLAG_FLU":"", "NVCCFLAG_POT":""} gpus = {"GPU_COMPUTE_CAPABILITY":""} + if os.path.isfile( config ): - with open( config, 'r') as f: + with open( config, "r" ) as f: lines = f.readlines() else: raise FileNotFoundError("The config file <%s> does not exist."%(config)) @@ -760,14 +769,50 @@ def load_config( config ): try: paths[temp[0]] = temp[1] except: - paths[temp[0]] = '' + paths[temp[0]] = "" return paths, compilers, flags, gpus -def set_conditional_defaults( args ): - if args['machine'] is None: - args['machine'] = get_machine() +def load_setting(): + """ + Return the default machine setting in the following order: + 1. Read the local setting located at `GAMER_LOCAL_SETTING`. + 2. Read the global setting located at `GAMER_GLOBAL_SETTING`. + 3. Fall back to `eureka_intel` for backward compatibility. + + Format of the setting file: + 1. Comment starts with `#`. + 2. Separate the variable name and value with space. The line starts with the variable name. + 3. Only the fisrt value of the line will be loaded. + 4. Only the last variable of duplicated variables will be loaded. + """ + sys_setting = SystemSetting() + setting_file = None + if os.path.isfile( GAMER_GLOBAL_SETTING ): setting_file = GAMER_GLOBAL_SETTING + if os.path.isfile( GAMER_LOCAL_SETTING ): setting_file = GAMER_LOCAL_SETTING + + if setting_file is None: + LOGGER.info("System setting file not detected.") + return sys_setting + else: + LOGGER.info("Using %s as setting file."%(setting_file)) + + with open( setting_file, "r" ) as f: + lines = f.readlines() + + for line in lines: + tokens = line.strip().split() + if len(tokens) == 0: continue # empty line + if tokens[0][0] == "#": continue # skip comment line + try: + sys_setting[tokens[0]] = tokens[1] + except: + sys_setting[tokens[0]] = None + + return sys_setting + +def set_conditional_defaults( args ): if args["unsplit_gravity"] is None: args["unsplit_gravity"] = (args["model"] == "HYDRO") @@ -792,33 +837,6 @@ def set_conditional_defaults( args ): args["barotropic"] = (args["eos"] == "ISOTHERMAL") return args -def get_machine(): - ''' - When the `--machine` flag is not given, this function will be called - and return the default machine in the following order: - 1. Read the local setting located at `GAMER_LOCAL_SETTING`. - 2. Read the global setting located at `GAMER_GLOBAL_SETTING`. - 3. Fall back to `eureka_intel` for backward compatibility. - ''' - # Check if GAMER_LOCAL_SETTING exists - local_setting = GAMER_LOCAL_SETTING - if os.path.isfile(local_setting): - with open(local_setting, 'r') as f: - for line in f: - tokens = line.strip().split() - if len(tokens) >= 2 and tokens[0] == 'machine_name': - return tokens[1] - # Check if GAMER_GLOBAL_SETTING exists - global_setting = os.path.expanduser(GAMER_GLOBAL_SETTING) - if os.path.isfile(global_setting): - with open(global_setting, 'r') as f: - for line in f: - tokens = line.strip().split() - if len(tokens) >= 2 and tokens[0] == 'machine_name': - return tokens[1] - # Fall back to `eureka_intel` for backward compatibility - return 'eureka_intel' - def set_gpu( gpus, flags, args ): gpu_opts = {} compute_capability = gpus["GPU_COMPUTE_CAPABILITY"] @@ -924,7 +942,7 @@ def validation( paths, depends, constraints, **kwargs ): if type(check_val) != type([]): check_val = [check_val] # transform to list if kwargs[check_opt] in check_val: continue # satisify the validation - val_str = ', '.join(str(x) for x in check_val) + val_str = ", ".join(str(x) for x in check_val) LOGGER.error("The option <--%s=%s> requires <--%s> to be set to [%s]. Current: <--%s=%s>."%(opt, str(kwargs[opt]), check_opt, val_str, check_opt, kwargs[check_opt])) success = False @@ -1031,36 +1049,39 @@ def warning( paths, **kwargs ): command = " ".join(["# This makefile is generated by the following command:", "\n#", sys.executable] + sys.argv + ["\n"]) LOGGER.info( " ".join( [sys.executable] + sys.argv ) ) - # 2. Load the input arguments - args, name_table, depends, constraints = load_arguments() + # 2. Load system settings + sys_setting = load_setting() + + # 3. Load the input arguments + args, name_table, depends, constraints = load_arguments( sys_setting ) - # 3. Prepare the makefile args - # 3.1 Load the machine setup + # 4. Prepare the makefile args + # 4.1 Load the machine setup paths, compilers, flags, gpus = load_config( os.path.join(GAMER_CONFIG_DIR, args["machine"]+".config") ) - # 3.2 Validate arguments + # 4.2 Validate arguments validation( paths, depends, constraints, **args ) warning( paths, **args ) - # 3.3 Add the SIMU_OPTION + # 4.3 Add the SIMU_OPTION LOGGER.info("========================================") LOGGER.info("GAMER has the following setting.") LOGGER.info("----------------------------------------") sims = set_sims( name_table, depends, **args ) - # 3.4 Set the compiler + # 4.4 Set the compiler compiles = set_compile( paths, compilers, flags, args ) - # 3.5 Set the GPU + # 4.5 Set the GPU gpu_setup = set_gpu( gpus, flags, args ) - # 4. Create Makefile - # 4.1 Read + # 5. Create Makefile + # 5.1 Read with open( GAMER_MAKE_BASE, "r" ) as make_base: makefile = make_base.read() - # 4.2 Replace + # 5.2 Replace LOGGER.info("----------------------------------------") for key, val in paths.items(): LOGGER.info("%-25s : %s"%(key, val)) @@ -1089,7 +1110,7 @@ def warning( paths, **kwargs ): if num == 0: raise BaseException("The string @@@%s@@@ is not replaced correctly."%key) LOGGER.warning("@@@%s@@@ is replaced to '' since the value is not given or the related option is disabled."%key) - # 4.3 Write + # 5.3 Write with open( GAMER_MAKE_OUT, "w") as make_out: make_out.write( command + makefile ) From 3f22d2484fb7934752dadf910d3f22897900ce14 Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Fri, 13 Dec 2024 23:58:50 +0800 Subject: [PATCH 04/29] Fix bug and imporve the flow of the code. Fix `get_default()` in `SystemSetting`. Replace except with non-explicit exception. Remove `_`, an alias from gettext module, which is not used. --- src/configure.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/configure.py b/src/configure.py index a185298159..a858a7dd7f 100755 --- a/src/configure.py +++ b/src/configure.py @@ -147,7 +147,7 @@ def _get_option_tuples(self, option_string): # shouldn't ever get here else: - self.error(_("unexpected option string: %s") % option_string) + self.error(("unexpected option string: %s") % option_string) return result # return the collected option tuples @@ -226,8 +226,7 @@ def __init__( self, *args, **kwargs ): super().__init__( *args, **kwargs ) def get_default( self, key, default_val ): - return self.__dict__[key] if key in self.__dict__ else default_val - + return self.get(key, default_val) @@ -361,7 +360,7 @@ def string_align( string, indent_str, width, end_char ): if string[i] == end_char: new_line = True return new_str -def load_arguments( sys_setting ): +def load_arguments( sys_setting : SystemSetting ): parser = ArgumentParser( description = GAMER_DESCRIPTION, formatter_class = argparse.RawTextHelpFormatter, epilog = GAMER_EPILOG, @@ -766,9 +765,9 @@ def load_config( config ): if gpus[temp[0]] != "": LOGGER.warning("The original value will be overwritten. <%s>: %s --> %s"%(temp[0], gpus[temp[0]], temp[1])) gpus[temp[0]] = temp[1] else: - try: + if len(temp) >= 2: paths[temp[0]] = temp[1] - except: + else: # key without value paths[temp[0]] = "" return paths, compilers, flags, gpus @@ -787,16 +786,14 @@ def load_setting(): 4. Only the last variable of duplicated variables will be loaded. """ sys_setting = SystemSetting() - setting_file = None - - if os.path.isfile( GAMER_GLOBAL_SETTING ): setting_file = GAMER_GLOBAL_SETTING - if os.path.isfile( GAMER_LOCAL_SETTING ): setting_file = GAMER_LOCAL_SETTING - if setting_file is None: + if os.path.isfile( GAMER_LOCAL_SETTING ): setting_file = GAMER_LOCAL_SETTING + elif os.path.isfile( GAMER_GLOBAL_SETTING ): setting_file = GAMER_GLOBAL_SETTING + else: LOGGER.info("System setting file not detected.") return sys_setting - else: - LOGGER.info("Using %s as setting file."%(setting_file)) + + LOGGER.info("Using %s as setting file."%(setting_file)) with open( setting_file, "r" ) as f: lines = f.readlines() @@ -805,9 +802,9 @@ def load_setting(): tokens = line.strip().split() if len(tokens) == 0: continue # empty line if tokens[0][0] == "#": continue # skip comment line - try: + if len(tokens) >= 2: sys_setting[tokens[0]] = tokens[1] - except: + else: # key without value sys_setting[tokens[0]] = None return sys_setting From 3df4413188b2e8e45fac66bf6e73496d9ad9dcbb Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Sat, 14 Dec 2024 02:41:48 +0800 Subject: [PATCH 05/29] Generalized `set_settings.sh` Give ability to add more parameters and add delete option. All done by Copilot O1-preview! --- tool/set_settings.sh | 113 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 93 insertions(+), 20 deletions(-) diff --git a/tool/set_settings.sh b/tool/set_settings.sh index 33e815972d..7c036851e8 100644 --- a/tool/set_settings.sh +++ b/tool/set_settings.sh @@ -2,38 +2,74 @@ cd "$(dirname "$0")" +# Define valid keys +VALID_KEYS=("machine" "user" "organization") + show_help() { - echo "Usage: $0 [--local | --global] --machine=" + echo "Usage: $0 [--local | --global] [--key=value ...] [--delete key ...]" echo "" echo "Options:" - echo " --local Use local settings" - echo " --global Use global settings" - echo " --machine= Specify the machine name" - echo " -h, --help Show this help message" + echo " --local Use local settings" + echo " --global Use global settings" + echo " --key=value Set a parameter" + echo " --delete, -d key Delete a parameter" + echo " -h, --help Show this help message" + echo "" + echo "Valid keys and their functionalities:" + echo " machine Specify the machine name" + echo " user Specify the user name" + echo " organization Specify the organization name" } # Parse arguments LOCAL=false GLOBAL=false -MACHINE="" +DELETE=false +SET=false +declare -A SETTINGS +DELETE_KEYS=() while [[ "$#" -gt 0 ]]; do case $1 in - --local) LOCAL=true ;; - --global) GLOBAL=true ;; - --machine=*) MACHINE="${1#*=}" ;; + --local) LOCAL=true; shift ;; + --global) GLOBAL=true; shift ;; + --delete|-d) + DELETE=true + shift # Shift past '--delete' or '-d' + if [[ "$#" -eq 0 ]]; then + echo "Error: --delete requires at least one key." + exit 1 + fi + while [[ "$#" -gt 0 && "${1:0:2}" != "--" ]]; do + if [[ ! " ${VALID_KEYS[@]} " =~ " $1 " ]]; then + echo "Error: Invalid key '$1' for deletion." + exit 1 + fi + DELETE_KEYS+=("$1") + shift + done + ;; -h|--help) show_help; exit 0 ;; - --machine) echo "Error: Use --machine= to specify the machine."; exit 1 ;; + --*) + SET=true + PARAM="${1#--}" + if [[ "$PARAM" != *=* ]]; then + echo "Error: Invalid format. Use --key=value." + exit 1 + fi + KEY="${PARAM%%=*}" + VALUE="${PARAM#*=}" + if [[ ! " ${VALID_KEYS[@]} " =~ " $KEY " ]]; then + echo "Error: Invalid key '$KEY'." + exit 1 + fi + SETTINGS["$KEY"]="$VALUE" + shift + ;; *) echo "Unknown parameter passed: $1"; echo "Use -h or --help for usage information."; exit 1 ;; esac - shift done -if [ -z "$MACHINE" ]; then - echo "Error: --machine option is required." - exit 1 -fi - if [ "$LOCAL" = true ] && [ "$GLOBAL" = true ]; then echo "Error: Cannot specify both --local and --global." exit 1 @@ -48,11 +84,48 @@ else exit 1 fi +if [ "$SET" = true ] && [ "$DELETE" = true ]; then + echo "Error: Cannot set and delete parameters at the same time." + exit 1 +elif [ "$SET" = false ] && [ "$DELETE" = false ]; then + echo "Error: No parameters to set or delete." + exit 1 +fi + mkdir -p "$(dirname "$SETTING_FILE")" -# Write the machine name to the setting file -echo "# GAMER setting file" > "$SETTING_FILE" -echo "machine_name $MACHINE" >> "$SETTING_FILE" +# Load existing settings +declare -A EXISTING_SETTINGS +if [ -f "$SETTING_FILE" ]; then + while read -r LINE; do + [[ "$LINE" =~ ^#.*$ ]] && continue + KEY="$(echo "$LINE" | awk '{print $1}')" + VALUE="$(echo "$LINE" | awk '{print $2}')" + EXISTING_SETTINGS["$KEY"]="$VALUE" + done < "$SETTING_FILE" +fi + +# Update settings +if [ "$SET" = true ]; then + for KEY in "${!SETTINGS[@]}"; do + EXISTING_SETTINGS["$KEY"]="${SETTINGS[$KEY]}" + done +fi + +# Delete specified keys +if [ "$DELETE" = true ]; then + for KEY in "${DELETE_KEYS[@]}"; do + unset EXISTING_SETTINGS["$KEY"] + done +fi + +# Write updated settings to file +{ + echo "# GAMER setting file" + for KEY in "${!EXISTING_SETTINGS[@]}"; do + echo "$KEY ${EXISTING_SETTINGS[$KEY]}" + done +} > "$SETTING_FILE" # Check if the write was successful if [ $? -ne 0 ]; then @@ -60,4 +133,4 @@ if [ $? -ne 0 ]; then exit 1 fi -echo "Successfully wrote $SETTING_TYPE settings to $SETTING_FILE for machine='$MACHINE'." +echo "Successfully updated $SETTING_TYPE settings in $SETTING_FILE." From 7fd4994fec858693ee8f9e0a26a1c5b3b6ffc475 Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Sat, 14 Dec 2024 17:08:09 +0800 Subject: [PATCH 06/29] Add new options and improve the cli Add options verbose, list, and clear-all. Some changes with a better cli flow Use PADDED_KEYS for better alignment. Accept short options to be combined. --- tool/set_settings.sh | 279 +++++++++++++++++++++++++++++++++---------- 1 file changed, 213 insertions(+), 66 deletions(-) diff --git a/tool/set_settings.sh b/tool/set_settings.sh index 7c036851e8..1b443f7f1f 100644 --- a/tool/set_settings.sh +++ b/tool/set_settings.sh @@ -1,75 +1,171 @@ #!/bin/bash -cd "$(dirname "$0")" +################### DEFINE KEYS ################### +# The keys can not contain spaces or start with `-` +declare -A KEY_DESCRIPTIONS +KEY_DESCRIPTIONS=( + ["machine"]="Specify the machine name" + ["user"]="Specify the user name" +) +################################################### + +# List of valid keys +VALID_KEYS=("${!KEY_DESCRIPTIONS[@]}") + +# Keys padded with trailing spaces for formatting output +MAX_KEY_LENGTH=0 +for KEY in "${VALID_KEYS[@]}"; do + if [ ${#KEY} -gt $MAX_KEY_LENGTH ]; then + MAX_KEY_LENGTH=${#KEY} + fi +done +declare -A PADDED_KEYS +for KEY in "${VALID_KEYS[@]}"; do + PADDED_KEYS[$KEY]=$(printf "%-${MAX_KEY_LENGTH}s" "$KEY") +done -# Define valid keys -VALID_KEYS=("machine" "user" "organization") +show_valid_keys() { + echo "Valid keys and their functionalities:" + for KEY in "${!KEY_DESCRIPTIONS[@]}"; do + echo " ${PADDED_KEYS[$KEY]} ${KEY_DESCRIPTIONS[$KEY]}" + done +} show_help() { - echo "Usage: $0 [--local | --global] [--key=value ...] [--delete key ...]" + echo "Usage:" + echo " $0 (--local | --global) [-lv] [--= ...]" + echo " $0 (--local | --global) [-lvd] [--delete key ...]" + echo " $0 -h | --help" echo "" echo "Options:" echo " --local Use local settings" echo " --global Use global settings" - echo " --key=value Set a parameter" - echo " --delete, -d key Delete a parameter" + echo " -l, --list List current settings" + echo " -v, --verbose Show verbose output" + echo " --= Set a parameter" + echo " -d, --delete key Delete a parameter" + echo " --clear-all Clear all parameters" echo " -h, --help Show this help message" echo "" - echo "Valid keys and their functionalities:" - echo " machine Specify the machine name" - echo " user Specify the user name" - echo " organization Specify the organization name" + show_valid_keys } # Parse arguments LOCAL=false GLOBAL=false +LIST=false +VERBOSE=false DELETE=false SET=false declare -A SETTINGS DELETE_KEYS=() +if [ "$#" -eq 0 ]; then + show_help + exit 0 +fi + +parse_set_parameter() { + local PARAM="$1" + local KEY="${PARAM%%=*}" + if [[ ! " ${VALID_KEYS[@]} " =~ " $KEY " ]]; then + echo "Error: Invalid key '$KEY'." + show_valid_keys + exit 1 + fi + if [[ "$PARAM" != *=* ]]; then + echo "Error: Invalid format for key '$KEY'. Use --$KEY=." + exit 1 + fi + local VALUE="${PARAM#*=}" + if [[ -z "$VALUE" ]]; then + echo "Error: Value for key '$KEY' cannot be empty. Use --$KEY=." + echo "If you want to delete '$KEY', use --delete $KEY." + exit 1 + fi + if [[ "$VALUE" =~ \ ]]; then + echo "Error: Value for key '$KEY' cannot contain spaces." + exit 1 + fi + SETTINGS["$KEY"]="$VALUE" +} + +parse_delete_keys() { + local keys_processed=0 + if [[ "$#" -eq 0 ]]; then + echo "Error: -d or --delete requires at least one key." + exit 1 + fi + while [[ "$#" -gt 0 && "${1:0:1}" != "-" ]]; do + if [[ ! " ${VALID_KEYS[@]} " =~ " $1 " ]]; then + echo "Error: Invalid key '$1' for deletion." + show_valid_keys + exit 1 + fi + DELETE_KEYS+=("$1") + shift + keys_processed=$((keys_processed + 1)) + done + return $keys_processed +} + while [[ "$#" -gt 0 ]]; do - case $1 in - --local) LOCAL=true; shift ;; - --global) GLOBAL=true; shift ;; - --delete|-d) - DELETE=true - shift # Shift past '--delete' or '-d' - if [[ "$#" -eq 0 ]]; then - echo "Error: --delete requires at least one key." - exit 1 - fi - while [[ "$#" -gt 0 && "${1:0:2}" != "--" ]]; do - if [[ ! " ${VALID_KEYS[@]} " =~ " $1 " ]]; then - echo "Error: Invalid key '$1' for deletion." - exit 1 - fi - DELETE_KEYS+=("$1") + if [[ "${1:0:1}" = "-" && "${1:0:2}" != "--" ]]; then # Short options + OPT="${1:1}" + shift + while [[ -n "$OPT" ]]; do # Possibly combined + case "${OPT:0:1}" in + l) LIST=true ;; + v) VERBOSE=true ;; + h) show_help; exit 0 ;; + d) + DELETE=true + # Put the remaining combined short options back to the argument list + # Warning: This method will only apply if all other options are not order sensitive + if [[ -n "${OPT:1}" ]]; then + set -- "$@" "-$(echo "${OPT:1}" | tr -d 'd')" + fi + parse_delete_keys "$@" + shift $? + break ;; + *) + echo "Unknown option: -${OPT:0:1}" + echo "Use -h or --help for usage information." + exit 1 ;; + esac + OPT="${OPT:1}" + done + else # Long options + case $1 in + --local) LOCAL=true; shift ;; + --global) GLOBAL=true; shift ;; + --list) LIST=true; shift ;; + --verbose) VERBOSE=true; shift ;; + --help) show_help; exit 0 ;; + --delete) + DELETE=true shift - done - ;; - -h|--help) show_help; exit 0 ;; - --*) - SET=true - PARAM="${1#--}" - if [[ "$PARAM" != *=* ]]; then - echo "Error: Invalid format. Use --key=value." - exit 1 - fi - KEY="${PARAM%%=*}" - VALUE="${PARAM#*=}" - if [[ ! " ${VALID_KEYS[@]} " =~ " $KEY " ]]; then - echo "Error: Invalid key '$KEY'." - exit 1 - fi - SETTINGS["$KEY"]="$VALUE" - shift - ;; - *) echo "Unknown parameter passed: $1"; echo "Use -h or --help for usage information."; exit 1 ;; - esac + parse_delete_keys "$@" + shift $? ;; + --clear-all) + DELETE=true + DELETE_KEYS=("${VALID_KEYS[@]}") # Set DELETE_KEYS to all valid keys + shift ;; + --*) + SET=true + parse_set_parameter "${1#--}" + shift ;; + *) + echo "Unknown parameter passed: $1" + echo "Use -h or --help for usage information." + exit 1 ;; + esac + fi done +cd "$(dirname "$0")" + +# Ensure either --local or --global is specified if [ "$LOCAL" = true ] && [ "$GLOBAL" = true ]; then echo "Error: Cannot specify both --local and --global." exit 1 @@ -84,16 +180,14 @@ else exit 1 fi +# Ensure mutually exclusive operations if [ "$SET" = true ] && [ "$DELETE" = true ]; then echo "Error: Cannot set and delete parameters at the same time." exit 1 elif [ "$SET" = false ] && [ "$DELETE" = false ]; then - echo "Error: No parameters to set or delete." - exit 1 + LIST=true fi -mkdir -p "$(dirname "$SETTING_FILE")" - # Load existing settings declare -A EXISTING_SETTINGS if [ -f "$SETTING_FILE" ]; then @@ -105,32 +199,85 @@ if [ -f "$SETTING_FILE" ]; then done < "$SETTING_FILE" fi -# Update settings -if [ "$SET" = true ]; then - for KEY in "${!SETTINGS[@]}"; do - EXISTING_SETTINGS["$KEY"]="${SETTINGS[$KEY]}" - done +# Create the directory and the settings file if it doesn't exist +DIR="$(dirname "$SETTING_FILE")" +if [ ! -d "$DIR" ]; then + mkdir -p "$DIR" + [ "$VERBOSE" = true ] && echo "Created directory '$(realpath "$DIR")'." +fi +if [ ! -f "$SETTING_FILE" ]; then + if touch "$SETTING_FILE"; then + [ "$VERBOSE" = true ] && echo "Created file $(basename "$SETTING_FILE") in '$(realpath "$DIR")'." + else + echo "Fatal: Failed to create file $(basename "$SETTING_FILE") in '$(realpath "$DIR")'." >&2 + exit 1 + fi fi -# Delete specified keys -if [ "$DELETE" = true ]; then - for KEY in "${DELETE_KEYS[@]}"; do - unset EXISTING_SETTINGS["$KEY"] - done +# The head of message to the user +if [ "$LIST" = true ]; then # Header for listing settings + echo "$SETTING_TYPE settings in $(realpath "$SETTING_FILE")" + echo "---------------------------" +elif [ "$VERBOSE" = true ]; then + echo "Loaded $SETTING_TYPE settings from '$(realpath "$SETTING_FILE")'." fi +# Main loop to update or delete settings +VERBOSE_OUTPUT="" # Store the echo output for verbose mode +for KEY in "${VALID_KEYS[@]}"; do + OLD_VALUE="${EXISTING_SETTINGS[$KEY]}" + NEW_VALUE="${SETTINGS[$KEY]}" + PADDED_KEY="${PADDED_KEYS[$KEY]}" + + if [ "$SET" = true ] && [ -n "$NEW_VALUE" ]; then # The key will be set + + EXISTING_SETTINGS["$KEY"]="$NEW_VALUE" # Update or set new value + if [ "$LIST" = true ]; then + if [ -z "$OLD_VALUE" ]; then + echo "$PADDED_KEY $NEW_VALUE (new)" + else + echo "$PADDED_KEY $OLD_VALUE -> $NEW_VALUE" + fi + fi + if [ "$VERBOSE" = true ]; then + if [ -z "$OLD_VALUE" ]; then + VERBOSE_OUTPUT+="Set '$KEY' to '$NEW_VALUE'.\n" + else + VERBOSE_OUTPUT+="Updated '$KEY' from '$OLD_VALUE' to '$NEW_VALUE'.\n" + fi + fi + + elif [ "$DELETE" = true ] && [[ " ${DELETE_KEYS[@]} " =~ " $KEY " ]]; then # The key will be deleted + + unset EXISTING_SETTINGS["$KEY"] # Delete the key + if [ "$LIST" = true ] && [ -n "$OLD_VALUE" ]; then + echo "$PADDED_KEY $OLD_VALUE -> (deleted)" + fi + if [ "$VERBOSE" = true ] && [ -n "$OLD_VALUE" ]; then + VERBOSE_OUTPUT+="Deleted '$KEY' which was set to '$OLD_VALUE'.\n" + fi + + elif [ "$LIST" = true ] && [ -n "$OLD_VALUE" ]; then + echo "$PADDED_KEY $OLD_VALUE" + fi +done +[ "$LIST" = true ] && echo "" + # Write updated settings to file { echo "# GAMER setting file" for KEY in "${!EXISTING_SETTINGS[@]}"; do - echo "$KEY ${EXISTING_SETTINGS[$KEY]}" + echo "${PADDED_KEYS[$KEY]} ${EXISTING_SETTINGS[$KEY]}" done } > "$SETTING_FILE" -# Check if the write was successful +# Check if writing to file was successful if [ $? -ne 0 ]; then - echo "Error: Failed to write to $SETTING_FILE." + echo "Fatal: Failed to write to '$SETTING_FILE'." >&2 exit 1 +elif [ "$VERBOSE" = true ]; then + echo -en "$VERBOSE_OUTPUT" +fi +if [ "$SET" = true ] || [ "$DELETE" = true ]; then + echo "Successfully updated $SETTING_TYPE settings." fi - -echo "Successfully updated $SETTING_TYPE settings in $SETTING_FILE." From 691e9eba84ad64205b1dd853766bef0159f38b9d Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Wed, 18 Dec 2024 18:01:58 +0800 Subject: [PATCH 07/29] Remove option for testing --- tool/set_settings.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/tool/set_settings.sh b/tool/set_settings.sh index 1b443f7f1f..dbe47cb4b5 100644 --- a/tool/set_settings.sh +++ b/tool/set_settings.sh @@ -5,7 +5,6 @@ declare -A KEY_DESCRIPTIONS KEY_DESCRIPTIONS=( ["machine"]="Specify the machine name" - ["user"]="Specify the user name" ) ################################################### From 209ea75c9a154ee87c6940b696d801b1e7533596 Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Fri, 27 Dec 2024 01:53:01 +0800 Subject: [PATCH 08/29] Update wiki for setting default machine --- doc/wiki/Installation.md | 8 ++--- ...nstallation:-Machine-Configuration-File.md | 30 +++++++++++++++++-- doc/wiki/Installation:-Option-List.md | 2 +- doc/wiki/Quick-Start:-1D-Shock-Tube.md | 2 +- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/doc/wiki/Installation.md b/doc/wiki/Installation.md index 64439b570d..07d0ecf55f 100644 --- a/doc/wiki/Installation.md +++ b/doc/wiki/Installation.md @@ -13,15 +13,15 @@ To get the `Makefile`, please execute the following command: ```bash - python configure.py --machine=your_configuration_file [--your_arguments] + python configure.py [--your_arguments] ``` - `your_configuration_file` is the configuration filename you got from step 1, and `[--your_arguments]` should match your simulation requirements. Please check out [[Option List | Installation:-Option-List]] for all the available options. + `[--your_arguments]` represent the options that should align with your simulation requirements. Please check out [[Option List | Installation:-Option-List]] for all the available options. - For example, the following command uses the `configs/pleiades.config` machine configuration, sets the FFTW method to `FFTW2`, and enables gravity and GPU. + For example, the following command sets the FFTW method to `FFTW2`, and enables gravity and GPU. ``` bash - python configure.py --machine=pleiades --fftw=FFTW2 --gravity=true --gpu=true + python configure.py --fftw=FFTW2 --gravity=true --gpu=true ``` > [!TIP] diff --git a/doc/wiki/Installation:-Machine-Configuration-File.md b/doc/wiki/Installation:-Machine-Configuration-File.md index 9bc9f15ce0..6cd01a5069 100644 --- a/doc/wiki/Installation:-Machine-Configuration-File.md +++ b/doc/wiki/Installation:-Machine-Configuration-File.md @@ -1,8 +1,32 @@ -> [!TIP] -> To set up your machine configuration file, make a copy of `template.config` and modify it. - The machine configuration file is under `configs`. The configuration file contains the library paths, the compiler types, the compilation flags, and the GPU compute capability. +If there is a configuration file that matches the name of your machine, you should set it as the default by + +```bash +sh tool/set_settings.sh --local --machine=your_machine +``` + +For example, setting `--machine=pleiades` with the above command will use the `configs/pleiades.config` machine configuration when compiling the code. + +If the machine configuration file is not available for your machine or the existing one is not appropriate, you will need to create a new one with the following instructions. + +> [!NOTE] +> If you want to set the default machine configuration file for all of the Gamer copies under your user account, use the `--global` option instead of `--local`. +Still, you can override the global setting for the individual Gamer copies with the `--local` option. +Furthermore, you can override the default setting with the `--machine=` option in `configure.py`. + +## Machine Configuration File + +To set up your machine configuration file, go to `configs` and make a copy of `template.config` to modify it. + +```bash +cd configs +cp template.config your_machine.config +``` + +Please refer to the following sections to set up your machine configuration file. And don't forget to set the machine configuration file as the default by the `tool/set_settings.sh` command above. + + ### 0. Rules of the configuration file * Comments must start with `#`. * The variable name and its value must be separated by space(s). diff --git a/doc/wiki/Installation:-Option-List.md b/doc/wiki/Installation:-Option-List.md index bef1b99045..fb61b6f839 100644 --- a/doc/wiki/Installation:-Option-List.md +++ b/doc/wiki/Installation:-Option-List.md @@ -24,7 +24,7 @@ disabled). See the "Restriction" of each option carefully. |:---:|:---:|---| | `-h` | - | Show a short help message. | | `-lh` | - | Show a detailed help message. | -| `--machine` | Filename string | Select the `*.config` file from the `configs` directory. | +| `--machine` | Filename string | Select the `*.config` file from the `configs` directory. It will overwrite the default settings. |                  diff --git a/doc/wiki/Quick-Start:-1D-Shock-Tube.md b/doc/wiki/Quick-Start:-1D-Shock-Tube.md index d24bee8f38..d7913433a3 100644 --- a/doc/wiki/Quick-Start:-1D-Shock-Tube.md +++ b/doc/wiki/Quick-Start:-1D-Shock-Tube.md @@ -236,7 +236,7 @@ following modifications. 1\. Re-generate `Makefile` by [[configure.py | Installation]] and recompile `gamer`. ``` bash -sh generate_make.sh --machine=YOUR_MACHINE --openmp=true --gpu=true +sh generate_make.sh --openmp=true --gpu=true make clean make -j4 ``` From 095cd84c3c24d34ff5c326a4bff06722228cb251 Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Tue, 31 Dec 2024 23:42:22 +0800 Subject: [PATCH 09/29] Apply easy suggestions from the reviewer Co-authored-by: ChunYen-Chen --- doc/wiki/Installation:-Machine-Configuration-File.md | 4 ++-- doc/wiki/Installation:-Option-List.md | 2 +- src/configure.py | 12 ++++++------ tool/set_settings.sh | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/doc/wiki/Installation:-Machine-Configuration-File.md b/doc/wiki/Installation:-Machine-Configuration-File.md index 6cd01a5069..cf7b73e799 100644 --- a/doc/wiki/Installation:-Machine-Configuration-File.md +++ b/doc/wiki/Installation:-Machine-Configuration-File.md @@ -11,8 +11,8 @@ For example, setting `--machine=pleiades` with the above command will use the `c If the machine configuration file is not available for your machine or the existing one is not appropriate, you will need to create a new one with the following instructions. > [!NOTE] -> If you want to set the default machine configuration file for all of the Gamer copies under your user account, use the `--global` option instead of `--local`. -Still, you can override the global setting for the individual Gamer copies with the `--local` option. +> If you want to set the default machine configuration file for all of the GAMER copies under your user account, use the `--global` option instead of `--local`. +Still, you can override the global setting for the individual GAMER copies with the `--local` option. Furthermore, you can override the default setting with the `--machine=` option in `configure.py`. ## Machine Configuration File diff --git a/doc/wiki/Installation:-Option-List.md b/doc/wiki/Installation:-Option-List.md index fb61b6f839..982e627540 100644 --- a/doc/wiki/Installation:-Option-List.md +++ b/doc/wiki/Installation:-Option-List.md @@ -24,7 +24,7 @@ disabled). See the "Restriction" of each option carefully. |:---:|:---:|---| | `-h` | - | Show a short help message. | | `-lh` | - | Show a detailed help message. | -| `--machine` | Filename string | Select the `*.config` file from the `configs` directory. It will overwrite the default settings. | +| `--machine` | Filename string | Select the `*.config` file from the `configs` directory. It will overwrite the default machine set in the default setting file. |                  diff --git a/src/configure.py b/src/configure.py index 1e07ce8030..f46463a407 100755 --- a/src/configure.py +++ b/src/configure.py @@ -382,7 +382,7 @@ def load_arguments( sys_setting : SystemSetting ): # machine config setup parser.add_argument( "--machine", type=str, metavar="MACHINE", default=sys_setting.get_default( "machine_name", "eureka_intel" ), - help="Select the MACHINE.config file under ../configs directory.\nChoice: [eureka_intel, spock_intel, YOUR_MACHINE_NAME, ...] => " + help="Select the *.config file under ../configs directory. Will overwrite the default machine set in the default setting file.\nChoice: [eureka_intel, spock_intel, ...] => " ) # A. options of diffierent physical models @@ -736,15 +736,15 @@ def load_arguments( sys_setting : SystemSetting ): def load_config( config ): LOGGER.info("Using %s as the config."%(config)) + if not os.path.isfile( config ): + raise FileNotFoundError("The config file <%s> does not exist."%(config)) + paths, compilers = {}, {"CXX":"", "CXX_MPI":""} flags = {"CXXFLAG":"", "OPENMPFLAG":"", "LIBFLAG":"", "NVCCFLAG_COM":"", "NVCCFLAG_FLU":"", "NVCCFLAG_POT":""} gpus = {"GPU_COMPUTE_CAPABILITY":""} - if os.path.isfile( config ): - with open( config, "r" ) as f: - lines = f.readlines() - else: - raise FileNotFoundError("The config file <%s> does not exist."%(config)) + with open( config, "r" ) as f: + lines = f.readlines() for line in lines: temp = list( filter( None, re.split(" |:=|\n", line) ) ) # separate by " " and ":=" diff --git a/tool/set_settings.sh b/tool/set_settings.sh index dbe47cb4b5..19dac6dca0 100644 --- a/tool/set_settings.sh +++ b/tool/set_settings.sh @@ -33,8 +33,8 @@ show_valid_keys() { show_help() { echo "Usage:" echo " $0 (--local | --global) [-lv] [--= ...]" - echo " $0 (--local | --global) [-lvd] [--delete key ...]" - echo " $0 -h | --help" + echo " $0 (--local | --global) [-lvd] [--delete ...]" + echo " $0 [-h | --help]" echo "" echo "Options:" echo " --local Use local settings" @@ -61,7 +61,7 @@ DELETE_KEYS=() if [ "$#" -eq 0 ]; then show_help - exit 0 + exit 1 fi parse_set_parameter() { From afdb16048c43487e1df43d30b6aab6d57bf722ea Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Wed, 8 Jan 2025 15:16:23 +0800 Subject: [PATCH 10/29] Correct the return value of a bash function - Use global variable to store the return value of a function. - Redirect error messages to stderr. --- tool/set_settings.sh | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/tool/set_settings.sh b/tool/set_settings.sh index 19dac6dca0..9aa425c0d0 100644 --- a/tool/set_settings.sh +++ b/tool/set_settings.sh @@ -60,7 +60,7 @@ declare -A SETTINGS DELETE_KEYS=() if [ "$#" -eq 0 ]; then - show_help + printf "$(show_help)\n" >&2 exit 1 fi @@ -68,44 +68,43 @@ parse_set_parameter() { local PARAM="$1" local KEY="${PARAM%%=*}" if [[ ! " ${VALID_KEYS[@]} " =~ " $KEY " ]]; then - echo "Error: Invalid key '$KEY'." - show_valid_keys + echo "Error: Invalid key '$KEY'." >&2 + printf "$(show_valid_keys)\n" >&2 exit 1 fi if [[ "$PARAM" != *=* ]]; then - echo "Error: Invalid format for key '$KEY'. Use --$KEY=." + echo "Error: Invalid format for key '$KEY'. Use --$KEY=." >&2 exit 1 fi local VALUE="${PARAM#*=}" if [[ -z "$VALUE" ]]; then - echo "Error: Value for key '$KEY' cannot be empty. Use --$KEY=." - echo "If you want to delete '$KEY', use --delete $KEY." + echo "Error: Value for key '$KEY' cannot be empty. Use --$KEY=." >&2 + echo "If you want to delete '$KEY', use --delete $KEY." >&2 exit 1 fi if [[ "$VALUE" =~ \ ]]; then - echo "Error: Value for key '$KEY' cannot contain spaces." + echo "Error: Value for key '$KEY' cannot contain spaces." >&2 exit 1 fi SETTINGS["$KEY"]="$VALUE" } parse_delete_keys() { - local keys_processed=0 + keys_processed=0 if [[ "$#" -eq 0 ]]; then - echo "Error: -d or --delete requires at least one key." + echo "Error: -d or --delete requires at least one key." >&2 exit 1 fi while [[ "$#" -gt 0 && "${1:0:1}" != "-" ]]; do if [[ ! " ${VALID_KEYS[@]} " =~ " $1 " ]]; then - echo "Error: Invalid key '$1' for deletion." - show_valid_keys + echo "Error: Invalid key '$1' for deletion." >&2 + printf "$(show_valid_keys)\n" >&2 exit 1 fi DELETE_KEYS+=("$1") shift keys_processed=$((keys_processed + 1)) done - return $keys_processed } while [[ "$#" -gt 0 ]]; do @@ -125,11 +124,11 @@ while [[ "$#" -gt 0 ]]; do set -- "$@" "-$(echo "${OPT:1}" | tr -d 'd')" fi parse_delete_keys "$@" - shift $? + shift $keys_processed break ;; *) - echo "Unknown option: -${OPT:0:1}" - echo "Use -h or --help for usage information." + echo "Error: Unknown option: -${OPT:0:1}" >&2 + echo "Use -h or --help for usage information." >&2 exit 1 ;; esac OPT="${OPT:1}" @@ -145,7 +144,7 @@ while [[ "$#" -gt 0 ]]; do DELETE=true shift parse_delete_keys "$@" - shift $? ;; + shift $keys_processed ;; --clear-all) DELETE=true DELETE_KEYS=("${VALID_KEYS[@]}") # Set DELETE_KEYS to all valid keys @@ -155,8 +154,8 @@ while [[ "$#" -gt 0 ]]; do parse_set_parameter "${1#--}" shift ;; *) - echo "Unknown parameter passed: $1" - echo "Use -h or --help for usage information." + echo "Error: Unknown parameter passed: $1" >&2 + echo "Use -h or --help for usage information." >&2 exit 1 ;; esac fi @@ -166,7 +165,7 @@ cd "$(dirname "$0")" # Ensure either --local or --global is specified if [ "$LOCAL" = true ] && [ "$GLOBAL" = true ]; then - echo "Error: Cannot specify both --local and --global." + echo "Error: Cannot specify both --local and --global." >&2 exit 1 elif [ "$LOCAL" = true ]; then SETTING_FILE="../src/.local_settings" @@ -175,13 +174,13 @@ elif [ "$GLOBAL" = true ]; then SETTING_FILE="$HOME/.config/gamer/global_settings" SETTING_TYPE="global" else - echo "Error: Specify either --local or --global." + echo "Error: Specify either --local or --global." >&2 exit 1 fi # Ensure mutually exclusive operations if [ "$SET" = true ] && [ "$DELETE" = true ]; then - echo "Error: Cannot set and delete parameters at the same time." + echo "Error: Cannot set and delete parameters at the same time." >&2 exit 1 elif [ "$SET" = false ] && [ "$DELETE" = false ]; then LIST=true From 2fe9a1d9d244a11af81e8898c861f0daa64358ce Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Wed, 8 Jan 2025 17:42:50 +0800 Subject: [PATCH 11/29] Remove verbose mode output - Improve usage messages - Make `-v` and `-l` works the same - Remove `realpath` for macOS compatibility --- tool/set_settings.sh | 37 +++++++++++-------------------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/tool/set_settings.sh b/tool/set_settings.sh index 9aa425c0d0..47179b5df9 100644 --- a/tool/set_settings.sh +++ b/tool/set_settings.sh @@ -32,18 +32,19 @@ show_valid_keys() { show_help() { echo "Usage:" - echo " $0 (--local | --global) [-lv] [--= ...]" - echo " $0 (--local | --global) [-lvd] [--delete ...]" - echo " $0 [-h | --help]" + echo " $0 (--local | --global) [-v] --= ..." + echo " $0 (--local | --global) [-v] (-d | --delete) ..." + echo " $0 (--local | --global) (-l | --list)" + echo " $0 (-h | --help)" echo "" echo "Options:" echo " --local Use local settings" echo " --global Use global settings" - echo " -l, --list List current settings" echo " -v, --verbose Show verbose output" echo " --= Set a parameter" echo " -d, --delete key Delete a parameter" echo " --clear-all Clear all parameters" + echo " -l, --list List current settings" echo " -h, --help Show this help message" echo "" show_valid_keys @@ -53,7 +54,6 @@ show_help() { LOCAL=false GLOBAL=false LIST=false -VERBOSE=false DELETE=false SET=false declare -A SETTINGS @@ -114,7 +114,7 @@ while [[ "$#" -gt 0 ]]; do while [[ -n "$OPT" ]]; do # Possibly combined case "${OPT:0:1}" in l) LIST=true ;; - v) VERBOSE=true ;; + v) LIST=true ;; h) show_help; exit 0 ;; d) DELETE=true @@ -138,7 +138,7 @@ while [[ "$#" -gt 0 ]]; do --local) LOCAL=true; shift ;; --global) GLOBAL=true; shift ;; --list) LIST=true; shift ;; - --verbose) VERBOSE=true; shift ;; + --verbose) LIST=true; shift ;; --help) show_help; exit 0 ;; --delete) DELETE=true @@ -201,27 +201,24 @@ fi DIR="$(dirname "$SETTING_FILE")" if [ ! -d "$DIR" ]; then mkdir -p "$DIR" - [ "$VERBOSE" = true ] && echo "Created directory '$(realpath "$DIR")'." + echo "Created directory $DIR." fi if [ ! -f "$SETTING_FILE" ]; then if touch "$SETTING_FILE"; then - [ "$VERBOSE" = true ] && echo "Created file $(basename "$SETTING_FILE") in '$(realpath "$DIR")'." + echo "Created file $(basename "$SETTING_FILE") in $DIR." else - echo "Fatal: Failed to create file $(basename "$SETTING_FILE") in '$(realpath "$DIR")'." >&2 + echo "Fatal: Failed to create file $(basename "$SETTING_FILE") in $DIR." >&2 exit 1 fi fi # The head of message to the user if [ "$LIST" = true ]; then # Header for listing settings - echo "$SETTING_TYPE settings in $(realpath "$SETTING_FILE")" + echo "$SETTING_TYPE settings in $SETTING_FILE" echo "---------------------------" -elif [ "$VERBOSE" = true ]; then - echo "Loaded $SETTING_TYPE settings from '$(realpath "$SETTING_FILE")'." fi # Main loop to update or delete settings -VERBOSE_OUTPUT="" # Store the echo output for verbose mode for KEY in "${VALID_KEYS[@]}"; do OLD_VALUE="${EXISTING_SETTINGS[$KEY]}" NEW_VALUE="${SETTINGS[$KEY]}" @@ -237,13 +234,6 @@ for KEY in "${VALID_KEYS[@]}"; do echo "$PADDED_KEY $OLD_VALUE -> $NEW_VALUE" fi fi - if [ "$VERBOSE" = true ]; then - if [ -z "$OLD_VALUE" ]; then - VERBOSE_OUTPUT+="Set '$KEY' to '$NEW_VALUE'.\n" - else - VERBOSE_OUTPUT+="Updated '$KEY' from '$OLD_VALUE' to '$NEW_VALUE'.\n" - fi - fi elif [ "$DELETE" = true ] && [[ " ${DELETE_KEYS[@]} " =~ " $KEY " ]]; then # The key will be deleted @@ -251,9 +241,6 @@ for KEY in "${VALID_KEYS[@]}"; do if [ "$LIST" = true ] && [ -n "$OLD_VALUE" ]; then echo "$PADDED_KEY $OLD_VALUE -> (deleted)" fi - if [ "$VERBOSE" = true ] && [ -n "$OLD_VALUE" ]; then - VERBOSE_OUTPUT+="Deleted '$KEY' which was set to '$OLD_VALUE'.\n" - fi elif [ "$LIST" = true ] && [ -n "$OLD_VALUE" ]; then echo "$PADDED_KEY $OLD_VALUE" @@ -273,8 +260,6 @@ done if [ $? -ne 0 ]; then echo "Fatal: Failed to write to '$SETTING_FILE'." >&2 exit 1 -elif [ "$VERBOSE" = true ]; then - echo -en "$VERBOSE_OUTPUT" fi if [ "$SET" = true ] || [ "$DELETE" = true ]; then echo "Successfully updated $SETTING_TYPE settings." From 3ab32b59a2bb1b4e316dbf0288d485a64d56d955 Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Wed, 8 Jan 2025 18:26:31 +0800 Subject: [PATCH 12/29] Add `print_key` and remove `PADDED_KEYS` --- tool/set_settings.sh | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/tool/set_settings.sh b/tool/set_settings.sh index 47179b5df9..524c2a6357 100644 --- a/tool/set_settings.sh +++ b/tool/set_settings.sh @@ -18,15 +18,17 @@ for KEY in "${VALID_KEYS[@]}"; do MAX_KEY_LENGTH=${#KEY} fi done -declare -A PADDED_KEYS -for KEY in "${VALID_KEYS[@]}"; do - PADDED_KEYS[$KEY]=$(printf "%-${MAX_KEY_LENGTH}s" "$KEY") -done + +print_key(){ + # $1 : the key name + # $2 : the key value or additional message + printf "%-${MAX_KEY_LENGTH}s %s\n" "$1" "$2" +} show_valid_keys() { echo "Valid keys and their functionalities:" for KEY in "${!KEY_DESCRIPTIONS[@]}"; do - echo " ${PADDED_KEYS[$KEY]} ${KEY_DESCRIPTIONS[$KEY]}" + echo " $(print_key "$KEY" "${KEY_DESCRIPTIONS[$KEY]}")" done } @@ -222,16 +224,15 @@ fi for KEY in "${VALID_KEYS[@]}"; do OLD_VALUE="${EXISTING_SETTINGS[$KEY]}" NEW_VALUE="${SETTINGS[$KEY]}" - PADDED_KEY="${PADDED_KEYS[$KEY]}" if [ "$SET" = true ] && [ -n "$NEW_VALUE" ]; then # The key will be set EXISTING_SETTINGS["$KEY"]="$NEW_VALUE" # Update or set new value if [ "$LIST" = true ]; then if [ -z "$OLD_VALUE" ]; then - echo "$PADDED_KEY $NEW_VALUE (new)" + print_key "$KEY" "$NEW_VALUE (new)" else - echo "$PADDED_KEY $OLD_VALUE -> $NEW_VALUE" + print_key "$KEY" "$OLD_VALUE -> $NEW_VALUE" fi fi @@ -239,11 +240,11 @@ for KEY in "${VALID_KEYS[@]}"; do unset EXISTING_SETTINGS["$KEY"] # Delete the key if [ "$LIST" = true ] && [ -n "$OLD_VALUE" ]; then - echo "$PADDED_KEY $OLD_VALUE -> (deleted)" + print_key "$KEY" "$OLD_VALUE -> (deleted)" fi elif [ "$LIST" = true ] && [ -n "$OLD_VALUE" ]; then - echo "$PADDED_KEY $OLD_VALUE" + print_key "$KEY" "$OLD_VALUE" fi done [ "$LIST" = true ] && echo "" @@ -252,7 +253,7 @@ done { echo "# GAMER setting file" for KEY in "${!EXISTING_SETTINGS[@]}"; do - echo "${PADDED_KEYS[$KEY]} ${EXISTING_SETTINGS[$KEY]}" + print_key "${KEY}" "${EXISTING_SETTINGS[$KEY]}" done } > "$SETTING_FILE" From bf4c1960c5be5d5821c21674382e42fdfcdbd092 Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Wed, 8 Jan 2025 19:09:58 +0800 Subject: [PATCH 13/29] Fix overwriting in the read-only action --- tool/set_settings.sh | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tool/set_settings.sh b/tool/set_settings.sh index 524c2a6357..5829a75f57 100644 --- a/tool/set_settings.sh +++ b/tool/set_settings.sh @@ -247,21 +247,21 @@ for KEY in "${VALID_KEYS[@]}"; do print_key "$KEY" "$OLD_VALUE" fi done -[ "$LIST" = true ] && echo "" +[ "$LIST" = true ] && echo "---------------------------" -# Write updated settings to file -{ - echo "# GAMER setting file" - for KEY in "${!EXISTING_SETTINGS[@]}"; do - print_key "${KEY}" "${EXISTING_SETTINGS[$KEY]}" - done -} > "$SETTING_FILE" - -# Check if writing to file was successful -if [ $? -ne 0 ]; then - echo "Fatal: Failed to write to '$SETTING_FILE'." >&2 - exit 1 -fi if [ "$SET" = true ] || [ "$DELETE" = true ]; then + # Write updated settings to file + { + echo "# GAMER setting file" + for KEY in "${!EXISTING_SETTINGS[@]}"; do + print_key "${KEY}" "${EXISTING_SETTINGS[$KEY]}" + done + } > "$SETTING_FILE" + + # Check if writing to file was successful + if [ $? -ne 0 ]; then + echo "Fatal: Failed to write to '$SETTING_FILE'." >&2 + exit 1 + fi echo "Successfully updated $SETTING_TYPE settings." fi From a160c1a38523bd63094ff2ebb08075f6d33422de Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Wed, 8 Jan 2025 20:39:14 +0800 Subject: [PATCH 14/29] Organize the code with comments chunks --- tool/set_settings.sh | 105 +++++++++++++++++++++++++------------------ 1 file changed, 61 insertions(+), 44 deletions(-) diff --git a/tool/set_settings.sh b/tool/set_settings.sh index 5829a75f57..09edb3343d 100644 --- a/tool/set_settings.sh +++ b/tool/set_settings.sh @@ -8,6 +8,8 @@ KEY_DESCRIPTIONS=( ) ################################################### +#################### UTILITIES #################### + # List of valid keys VALID_KEYS=("${!KEY_DESCRIPTIONS[@]}") @@ -52,7 +54,9 @@ show_help() { show_valid_keys } -# Parse arguments +##################### PARSER ###################### + +# Parser arguments LOCAL=false GLOBAL=false LIST=false @@ -61,11 +65,7 @@ SET=false declare -A SETTINGS DELETE_KEYS=() -if [ "$#" -eq 0 ]; then - printf "$(show_help)\n" >&2 - exit 1 -fi - +# Parser functions parse_set_parameter() { local PARAM="$1" local KEY="${PARAM%%=*}" @@ -92,7 +92,7 @@ parse_set_parameter() { } parse_delete_keys() { - keys_processed=0 + KEYS_PROCESSED=0 if [[ "$#" -eq 0 ]]; then echo "Error: -d or --delete requires at least one key." >&2 exit 1 @@ -105,10 +105,11 @@ parse_delete_keys() { fi DELETE_KEYS+=("$1") shift - keys_processed=$((keys_processed + 1)) + KEYS_PROCESSED=$((KEYS_PROCESSED + 1)) done } +# Main parser loop while [[ "$#" -gt 0 ]]; do if [[ "${1:0:1}" = "-" && "${1:0:2}" != "--" ]]; then # Short options OPT="${1:1}" @@ -126,7 +127,7 @@ while [[ "$#" -gt 0 ]]; do set -- "$@" "-$(echo "${OPT:1}" | tr -d 'd')" fi parse_delete_keys "$@" - shift $keys_processed + shift $KEYS_PROCESSED break ;; *) echo "Error: Unknown option: -${OPT:0:1}" >&2 @@ -137,8 +138,16 @@ while [[ "$#" -gt 0 ]]; do done else # Long options case $1 in - --local) LOCAL=true; shift ;; - --global) GLOBAL=true; shift ;; + --local) + LOCAL=true + SETTING_TYPE="local" + SETTING_FILE="../src/.local_settings" + shift ;; + --global) + GLOBAL=true; + SETTING_TYPE="global" + SETTING_FILE="$HOME/.config/gamer/global_settings" + shift ;; --list) LIST=true; shift ;; --verbose) LIST=true; shift ;; --help) show_help; exit 0 ;; @@ -146,7 +155,7 @@ while [[ "$#" -gt 0 ]]; do DELETE=true shift parse_delete_keys "$@" - shift $keys_processed ;; + shift $KEYS_PROCESSED ;; --clear-all) DELETE=true DELETE_KEYS=("${VALID_KEYS[@]}") # Set DELETE_KEYS to all valid keys @@ -163,20 +172,12 @@ while [[ "$#" -gt 0 ]]; do fi done -cd "$(dirname "$0")" +################ VALIDATE ARGUMENTS ############### -# Ensure either --local or --global is specified -if [ "$LOCAL" = true ] && [ "$GLOBAL" = true ]; then - echo "Error: Cannot specify both --local and --global." >&2 - exit 1 -elif [ "$LOCAL" = true ]; then - SETTING_FILE="../src/.local_settings" - SETTING_TYPE="local" -elif [ "$GLOBAL" = true ]; then - SETTING_FILE="$HOME/.config/gamer/global_settings" - SETTING_TYPE="global" -else - echo "Error: Specify either --local or --global." >&2 +# Ensure at least one operation is specified +if [ "$SET" = false ] && [ "$DELETE" = false ] && [ "$LIST" = false ]; then + echo "Error: Specify at least one operation." >&2 + printf "$(show_help)\n" >&2 exit 1 fi @@ -184,11 +185,22 @@ fi if [ "$SET" = true ] && [ "$DELETE" = true ]; then echo "Error: Cannot set and delete parameters at the same time." >&2 exit 1 -elif [ "$SET" = false ] && [ "$DELETE" = false ]; then - LIST=true fi -# Load existing settings +# Ensure either --local or --global is specified +if [ "$LOCAL" = true ] && [ "$GLOBAL" = true ]; then + echo "Error: Cannot specify both --local and --global." >&2 + exit 1 +elif [ "$LOCAL" = false ] && [ "$GLOBAL" = false ]; then + echo "Error: Specify either --local or --global." >&2 + exit 1 +fi + +################ LOAD SETTINGS FILE ############### + +cd "$(dirname "$0")" + +# Load if the settings file exists declare -A EXISTING_SETTINGS if [ -f "$SETTING_FILE" ]; then while read -r LINE; do @@ -199,22 +211,9 @@ if [ -f "$SETTING_FILE" ]; then done < "$SETTING_FILE" fi -# Create the directory and the settings file if it doesn't exist -DIR="$(dirname "$SETTING_FILE")" -if [ ! -d "$DIR" ]; then - mkdir -p "$DIR" - echo "Created directory $DIR." -fi -if [ ! -f "$SETTING_FILE" ]; then - if touch "$SETTING_FILE"; then - echo "Created file $(basename "$SETTING_FILE") in $DIR." - else - echo "Fatal: Failed to create file $(basename "$SETTING_FILE") in $DIR." >&2 - exit 1 - fi -fi +################# UPDATE SETTINGS ################# -# The head of message to the user +# The head of the list if [ "$LIST" = true ]; then # Header for listing settings echo "$SETTING_TYPE settings in $SETTING_FILE" echo "---------------------------" @@ -243,13 +242,31 @@ for KEY in "${VALID_KEYS[@]}"; do print_key "$KEY" "$OLD_VALUE -> (deleted)" fi - elif [ "$LIST" = true ] && [ -n "$OLD_VALUE" ]; then + elif [ "$LIST" = true ] && [ -n "$OLD_VALUE" ]; then # List the existing settings print_key "$KEY" "$OLD_VALUE" fi done [ "$LIST" = true ] && echo "---------------------------" +################ SAVE SETTINGS FILE ############### + if [ "$SET" = true ] || [ "$DELETE" = true ]; then + + # Create the directory and the settings file if it doesn't exist + DIR="$(dirname "$SETTING_FILE")" + if [ ! -d "$DIR" ]; then + mkdir -p "$DIR" + echo "Created directory $DIR." + fi + if [ ! -f "$SETTING_FILE" ]; then + if touch "$SETTING_FILE"; then + echo "Created file $(basename "$SETTING_FILE") in $DIR." + else + echo "Fatal: Failed to create file $(basename "$SETTING_FILE") in $DIR." >&2 + exit 1 + fi + fi + # Write updated settings to file { echo "# GAMER setting file" From c7ca8638e27b31d1176f7d61e110a13d1ada0ec3 Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Wed, 8 Jan 2025 21:58:38 +0800 Subject: [PATCH 15/29] Put checking to section `VALIDATE PARAMETERS` --- tool/set_settings.sh | 70 ++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/tool/set_settings.sh b/tool/set_settings.sh index 09edb3343d..3ff2be4ce8 100644 --- a/tool/set_settings.sh +++ b/tool/set_settings.sh @@ -13,7 +13,7 @@ KEY_DESCRIPTIONS=( # List of valid keys VALID_KEYS=("${!KEY_DESCRIPTIONS[@]}") -# Keys padded with trailing spaces for formatting output +# For padding keys with trailing spaces to format output MAX_KEY_LENGTH=0 for KEY in "${VALID_KEYS[@]}"; do if [ ${#KEY} -gt $MAX_KEY_LENGTH ]; then @@ -21,6 +21,7 @@ for KEY in "${VALID_KEYS[@]}"; do fi done +# Print keys in a formatted way print_key(){ # $1 : the key name # $2 : the key value or additional message @@ -56,7 +57,7 @@ show_help() { ##################### PARSER ###################### -# Parser arguments +# Parser parameters LOCAL=false GLOBAL=false LIST=false @@ -65,44 +66,21 @@ SET=false declare -A SETTINGS DELETE_KEYS=() -# Parser functions +# Parser utility functions parse_set_parameter() { local PARAM="$1" local KEY="${PARAM%%=*}" - if [[ ! " ${VALID_KEYS[@]} " =~ " $KEY " ]]; then - echo "Error: Invalid key '$KEY'." >&2 - printf "$(show_valid_keys)\n" >&2 - exit 1 - fi if [[ "$PARAM" != *=* ]]; then - echo "Error: Invalid format for key '$KEY'. Use --$KEY=." >&2 + echo "Error: Invalid format for the key '$KEY'. Use --$KEY=." >&2 exit 1 fi local VALUE="${PARAM#*=}" - if [[ -z "$VALUE" ]]; then - echo "Error: Value for key '$KEY' cannot be empty. Use --$KEY=." >&2 - echo "If you want to delete '$KEY', use --delete $KEY." >&2 - exit 1 - fi - if [[ "$VALUE" =~ \ ]]; then - echo "Error: Value for key '$KEY' cannot contain spaces." >&2 - exit 1 - fi SETTINGS["$KEY"]="$VALUE" } parse_delete_keys() { KEYS_PROCESSED=0 - if [[ "$#" -eq 0 ]]; then - echo "Error: -d or --delete requires at least one key." >&2 - exit 1 - fi while [[ "$#" -gt 0 && "${1:0:1}" != "-" ]]; do - if [[ ! " ${VALID_KEYS[@]} " =~ " $1 " ]]; then - echo "Error: Invalid key '$1' for deletion." >&2 - printf "$(show_valid_keys)\n" >&2 - exit 1 - fi DELETE_KEYS+=("$1") shift KEYS_PROCESSED=$((KEYS_PROCESSED + 1)) @@ -165,14 +143,14 @@ while [[ "$#" -gt 0 ]]; do parse_set_parameter "${1#--}" shift ;; *) - echo "Error: Unknown parameter passed: $1" >&2 + echo "Error: Unknown option: $1" >&2 echo "Use -h or --help for usage information." >&2 exit 1 ;; esac fi done -################ VALIDATE ARGUMENTS ############### +############### VALIDATE PARAMETERS ############### # Ensure at least one operation is specified if [ "$SET" = false ] && [ "$DELETE" = false ] && [ "$LIST" = false ]; then @@ -181,12 +159,46 @@ if [ "$SET" = false ] && [ "$DELETE" = false ] && [ "$LIST" = false ]; then exit 1 fi +# Validate the keys and values for setting +if [ "$SET" = true ]; then + for KEY in "${!SETTINGS[@]}"; do + if [[ ! " ${VALID_KEYS[@]} " =~ " $KEY " ]]; then + echo "Error: Invalid key '$KEY'." >&2 + printf "$(show_valid_keys)\n" >&2 + exit 1 + fi + if [[ -z "${SETTINGS[$KEY]}" ]]; then + echo "Error: The value for the key '$KEY' cannot be empty. Use --$KEY=." >&2 + exit 1 + fi + if [[ "${SETTINGS[$KEY]}" =~ \ ]]; then + echo "Error: The value for the key '$KEY' cannot contain spaces." >&2 + exit 1 + fi + done +fi + # Ensure mutually exclusive operations if [ "$SET" = true ] && [ "$DELETE" = true ]; then echo "Error: Cannot set and delete parameters at the same time." >&2 exit 1 fi +# Validate the keys for deletion +if [ "$DELETE" = true ]; then + if [ ${#DELETE_KEYS[@]} -eq 0 ]; then + echo "Error: No keys specified for deletion." >&2 + exit 1 + fi + for KEY in "${DELETE_KEYS[@]}"; do + if [[ ! " ${VALID_KEYS[@]} " =~ " $KEY " ]]; then + echo "Error: Invalid key '$KEY' for deletion." >&2 + printf "$(show_valid_keys)\n" >&2 + exit 1 + fi + done +fi + # Ensure either --local or --global is specified if [ "$LOCAL" = true ] && [ "$GLOBAL" = true ]; then echo "Error: Cannot specify both --local and --global." >&2 From 8302b8bf926222714917986fb77bcae7c302d955 Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Wed, 8 Jan 2025 22:06:31 +0800 Subject: [PATCH 16/29] Move `set_settings.sh` from `tool/` to `tool/config/` --- doc/wiki/Installation:-Machine-Configuration-File.md | 4 ++-- tool/{ => config}/set_settings.sh | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename tool/{ => config}/set_settings.sh (99%) diff --git a/doc/wiki/Installation:-Machine-Configuration-File.md b/doc/wiki/Installation:-Machine-Configuration-File.md index cf7b73e799..6553eff78a 100644 --- a/doc/wiki/Installation:-Machine-Configuration-File.md +++ b/doc/wiki/Installation:-Machine-Configuration-File.md @@ -3,7 +3,7 @@ The machine configuration file is under `configs`. The configuration file contai If there is a configuration file that matches the name of your machine, you should set it as the default by ```bash -sh tool/set_settings.sh --local --machine=your_machine +sh tool/config/set_settings.sh --local --machine=your_machine ``` For example, setting `--machine=pleiades` with the above command will use the `configs/pleiades.config` machine configuration when compiling the code. @@ -24,7 +24,7 @@ cd configs cp template.config your_machine.config ``` -Please refer to the following sections to set up your machine configuration file. And don't forget to set the machine configuration file as the default by the `tool/set_settings.sh` command above. +Please refer to the following sections to set up your machine configuration file. And don't forget to set the machine configuration file as the default by the `tool/config/set_settings.sh` command above. ### 0. Rules of the configuration file diff --git a/tool/set_settings.sh b/tool/config/set_settings.sh similarity index 99% rename from tool/set_settings.sh rename to tool/config/set_settings.sh index 3ff2be4ce8..6141298fc1 100644 --- a/tool/set_settings.sh +++ b/tool/config/set_settings.sh @@ -119,7 +119,7 @@ while [[ "$#" -gt 0 ]]; do --local) LOCAL=true SETTING_TYPE="local" - SETTING_FILE="../src/.local_settings" + SETTING_FILE="../../src/.local_settings" shift ;; --global) GLOBAL=true; From 1207766d2fa2b0ded5818b28258f80aa8315c15a Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Wed, 8 Jan 2025 23:41:11 +0800 Subject: [PATCH 17/29] Update wiki for a better reading flow --- ...nstallation:-Machine-Configuration-File.md | 21 ++++--------------- doc/wiki/Installation.md | 17 ++++++++++++--- doc/wiki/Quick-Start.md | 14 ++++++++++++- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/doc/wiki/Installation-related/Installation:-Machine-Configuration-File.md b/doc/wiki/Installation-related/Installation:-Machine-Configuration-File.md index 6553eff78a..7d764af4cd 100644 --- a/doc/wiki/Installation-related/Installation:-Machine-Configuration-File.md +++ b/doc/wiki/Installation-related/Installation:-Machine-Configuration-File.md @@ -1,21 +1,9 @@ The machine configuration file is under `configs`. The configuration file contains the library paths, the compiler types, the compilation flags, and the GPU compute capability. -If there is a configuration file that matches the name of your machine, you should set it as the default by - -```bash -sh tool/config/set_settings.sh --local --machine=your_machine -``` - -For example, setting `--machine=pleiades` with the above command will use the `configs/pleiades.config` machine configuration when compiling the code. - -If the machine configuration file is not available for your machine or the existing one is not appropriate, you will need to create a new one with the following instructions. - -> [!NOTE] -> If you want to set the default machine configuration file for all of the GAMER copies under your user account, use the `--global` option instead of `--local`. -Still, you can override the global setting for the individual GAMER copies with the `--local` option. -Furthermore, you can override the default setting with the `--machine=` option in `configure.py`. +> [!TIP] +> Check the `configs` directory to see if there is a configuration file already available for your machine. -## Machine Configuration File +## Set Up the Machine Configuration File To set up your machine configuration file, go to `configs` and make a copy of `template.config` to modify it. @@ -24,8 +12,7 @@ cd configs cp template.config your_machine.config ``` -Please refer to the following sections to set up your machine configuration file. And don't forget to set the machine configuration file as the default by the `tool/config/set_settings.sh` command above. - +Please refer to the following sections to set up your machine configuration file. ### 0. Rules of the configuration file * Comments must start with `#`. diff --git a/doc/wiki/Installation.md b/doc/wiki/Installation.md index 07d0ecf55f..85b504016a 100644 --- a/doc/wiki/Installation.md +++ b/doc/wiki/Installation.md @@ -2,13 +2,24 @@ Please see [[Machine Configuration File | Installation:-Machine-Configuration-File]]. -2. Go to the source directory +2. Set your machine configuration file as default + + ```bash + sh tool/config/set_settings.sh --local --machine=your_machine + ``` + + > [!NOTE] + > If you want to set the default machine configuration file for all of the GAMER copies under your user account, use the `--global` option instead of `--local`. + Still, you can override the global setting for the individual GAMER copies with the `--local` option. + Furthermore, you can override the default setting with the `--machine=` [[option | Installation:-Option-List]] in `configure.py`. + +3. Go to the source directory ```bash cd src ``` -3. Generate `Makefile` using the Python script `configure.py` +4. Generate `Makefile` using the Python script `configure.py` To get the `Makefile`, please execute the following command: @@ -28,7 +39,7 @@ > An example script `generate_make.sh` to generate Makefile can be found in each test problem folder, e.g., `example/test_problem/Hydro/AcousticWave/generate_make.sh`. -4. Compile the code +5. Compile the code ```bash make clean diff --git a/doc/wiki/Quick-Start.md b/doc/wiki/Quick-Start.md index e44b22220b..5087dcc67a 100644 --- a/doc/wiki/Quick-Start.md +++ b/doc/wiki/Quick-Start.md @@ -4,9 +4,21 @@ Follow the instructions in [[Download]] to get the latest version of GAMER. ### Setup Your Machine Configuration File -Follow the instructions in [[Machine Configuration File | Installation:-Machine-Configuration-File]] to set the configuration file of your machine. +The machine configuration file is under `configs`. The configuration file contains the library paths, the compiler types, the compilation flags, and the GPU compute capability. + +If there is a configuration file that matches the name of your machine, you should set it as the default by + +```bash +sh tool/config/set_settings.sh --local --machine=your_machine +``` + +For example, setting `--machine=pleiades` with the above command will use the `configs/pleiades.config` machine configuration when compiling the code. + +If the machine configuration file is not available for your machine or the existing one is not appropriate, you will need to create a new one. Follow the instructions in [[Machine Configuration File | Installation:-Machine-Configuration-File]] to set the configuration file of your machine. ### Quick Demos +Read the following guides to learn how to configure, compile, and run GAMER. + 1. [[1D Shock Tube: OpenMP with/without GPU acceleration | Quick-Start:-1D-Shock-Tube]] 2. [[3D Blast Wave: hybrid MPI/OpenMP/GPU + yt analysis | Quick-Start:-3D-Blast-Wave]] From 9de77ac5dd0af021cab03bb3ddb578c145895299 Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Thu, 9 Jan 2025 00:05:42 +0800 Subject: [PATCH 18/29] Add python version check to `configure.py` For supporting the use of type annotations --- src/configure.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/configure.py b/src/configure.py index f46463a407..a4fc139da0 100755 --- a/src/configure.py +++ b/src/configure.py @@ -1038,48 +1038,52 @@ def warning( paths, **kwargs ): # Main execution #################################################################################################### if __name__ == "__main__": - # 0. Set the logger + # 0. Check the version of Python + if sys.version_info[0] < 3 or sys.version_info[1] < 5: + raise BaseException("Python 3.5 or later is required.") + + # 1. Set the logger ch = logging.StreamHandler() ch.setFormatter( CustomFormatter() ) LOGGER.addHandler( ch ) - # 1. Get the execution command + # 2. Get the execution command command = " ".join(["# This makefile is generated by the following command:", "\n#", sys.executable] + sys.argv + ["\n"]) LOGGER.info( " ".join( [sys.executable] + sys.argv ) ) - # 2. Load system settings + # 3. Load system settings sys_setting = load_setting() - # 3. Load the input arguments + # 4. Load the input arguments args, name_table, depends, constraints = load_arguments( sys_setting ) - # 4. Prepare the makefile args - # 4.1 Load the machine setup + # 5. Prepare the makefile args + # 5.1 Load the machine setup paths, compilers, flags, gpus = load_config( os.path.join(GAMER_CONFIG_DIR, args["machine"]+".config") ) - # 4.2 Validate arguments + # 5.2 Validate arguments validation( paths, depends, constraints, **args ) warning( paths, **args ) - # 4.3 Add the SIMU_OPTION + # 5.3 Add the SIMU_OPTION LOGGER.info("========================================") LOGGER.info("GAMER has the following setting.") LOGGER.info("----------------------------------------") sims = set_sims( name_table, depends, **args ) - # 4.4 Set the compiler + # 5.4 Set the compiler compiles = set_compile( paths, compilers, flags, args ) - # 4.5 Set the GPU + # 5.5 Set the GPU gpu_setup = set_gpu( gpus, flags, args ) - # 5. Create Makefile - # 5.1 Read + # 6. Create Makefile + # 6.1 Read with open( GAMER_MAKE_BASE, "r" ) as make_base: makefile = make_base.read() - # 5.2 Replace + # 6.2 Replace LOGGER.info("----------------------------------------") for key, val in paths.items(): LOGGER.info("%-25s : %s"%(key, val)) @@ -1108,7 +1112,7 @@ def warning( paths, **kwargs ): if num == 0: raise BaseException("The string @@@%s@@@ is not replaced correctly."%key) LOGGER.warning("@@@%s@@@ is replaced to '' since the value is not given or the related option is disabled."%key) - # 5.3 Write + # 6.3 Write with open( GAMER_MAKE_OUT, "w") as make_out: make_out.write( command + makefile ) From af7fa6ec59e6eac5f11a85879d642766871ba807 Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Wed, 15 Jan 2025 15:51:53 +0800 Subject: [PATCH 19/29] Apply easy suggestions from code review Co-authored-by: Chun-Yen Chen <70311975+ChunYen-Chen@users.noreply.github.com> --- doc/wiki/Installation.md | 8 ++++---- doc/wiki/Quick-Start.md | 8 ++++---- src/configure.py | 36 +++++++++++++++++------------------- tool/config/set_settings.sh | 14 ++++++++------ 4 files changed, 33 insertions(+), 33 deletions(-) diff --git a/doc/wiki/Installation.md b/doc/wiki/Installation.md index 85b504016a..3e42cc0268 100644 --- a/doc/wiki/Installation.md +++ b/doc/wiki/Installation.md @@ -8,10 +8,10 @@ sh tool/config/set_settings.sh --local --machine=your_machine ``` - > [!NOTE] - > If you want to set the default machine configuration file for all of the GAMER copies under your user account, use the `--global` option instead of `--local`. - Still, you can override the global setting for the individual GAMER copies with the `--local` option. - Furthermore, you can override the default setting with the `--machine=` [[option | Installation:-Option-List]] in `configure.py`. +> [!NOTE] +> If you want to set the default machine configuration file for all of the GAMER copies under your user account, use the `--global` option instead of `--local`. +Still, you can override the global setting for the individual GAMER copies with the `--local` option. +Furthermore, you can override the default setting by passing the [[--machine | Installation:-Option-List#--machine]]=`your_machine` when executing `configure.py`. 3. Go to the source directory diff --git a/doc/wiki/Quick-Start.md b/doc/wiki/Quick-Start.md index 5087dcc67a..31a68c77bf 100644 --- a/doc/wiki/Quick-Start.md +++ b/doc/wiki/Quick-Start.md @@ -4,9 +4,11 @@ Follow the instructions in [[Download]] to get the latest version of GAMER. ### Setup Your Machine Configuration File -The machine configuration file is under `configs`. The configuration file contains the library paths, the compiler types, the compilation flags, and the GPU compute capability. +The machine configuration file is under `configs`. The configuration file contains the library paths, the compiler types, the compilation flags, and the GPU compute capability. Check the existing configuration files to see if there is one that matches your machine. -If there is a configuration file that matches the name of your machine, you should set it as the default by +If the machine configuration file is not available for your machine or the existing one is not appropriate, you will need to create a new one. Follow the instructions in [[Machine Configuration File | Installation:-Machine-Configuration-File]] to set the configuration file of your machine. + +If there is a configuration file that matches your machine, you should set it as the default by ```bash sh tool/config/set_settings.sh --local --machine=your_machine @@ -14,8 +16,6 @@ sh tool/config/set_settings.sh --local --machine=your_machine For example, setting `--machine=pleiades` with the above command will use the `configs/pleiades.config` machine configuration when compiling the code. -If the machine configuration file is not available for your machine or the existing one is not appropriate, you will need to create a new one. Follow the instructions in [[Machine Configuration File | Installation:-Machine-Configuration-File]] to set the configuration file of your machine. - ### Quick Demos Read the following guides to learn how to configure, compile, and run GAMER. diff --git a/src/configure.py b/src/configure.py index a4fc139da0..7a5be8c0a2 100755 --- a/src/configure.py +++ b/src/configure.py @@ -16,7 +16,9 @@ import re import ctypes - +# Check the version of Python +if sys.version_info[0] < 3 or sys.version_info[1] < 5: + raise BaseException("Python 3.5 or later is required.") #################################################################################################### # Global variables @@ -1038,52 +1040,48 @@ def warning( paths, **kwargs ): # Main execution #################################################################################################### if __name__ == "__main__": - # 0. Check the version of Python - if sys.version_info[0] < 3 or sys.version_info[1] < 5: - raise BaseException("Python 3.5 or later is required.") - - # 1. Set the logger + # 0. Set the logger ch = logging.StreamHandler() ch.setFormatter( CustomFormatter() ) LOGGER.addHandler( ch ) - # 2. Get the execution command + # 1. Get the execution command command = " ".join(["# This makefile is generated by the following command:", "\n#", sys.executable] + sys.argv + ["\n"]) LOGGER.info( " ".join( [sys.executable] + sys.argv ) ) - # 3. Load system settings + # 2. Load system settings sys_setting = load_setting() - # 4. Load the input arguments + # 3. Load the input arguments args, name_table, depends, constraints = load_arguments( sys_setting ) - # 5. Prepare the makefile args - # 5.1 Load the machine setup + # 4. Prepare the makefile args + # 4.1 Load the machine setup paths, compilers, flags, gpus = load_config( os.path.join(GAMER_CONFIG_DIR, args["machine"]+".config") ) - # 5.2 Validate arguments + # 4.2 Validate arguments validation( paths, depends, constraints, **args ) warning( paths, **args ) - # 5.3 Add the SIMU_OPTION + # 4.3 Add the SIMU_OPTION LOGGER.info("========================================") LOGGER.info("GAMER has the following setting.") LOGGER.info("----------------------------------------") sims = set_sims( name_table, depends, **args ) - # 5.4 Set the compiler + # 4.4 Set the compiler compiles = set_compile( paths, compilers, flags, args ) - # 5.5 Set the GPU + # 4.5 Set the GPU gpu_setup = set_gpu( gpus, flags, args ) - # 6. Create Makefile - # 6.1 Read + # 5. Create Makefile + # 5.1 Read with open( GAMER_MAKE_BASE, "r" ) as make_base: makefile = make_base.read() - # 6.2 Replace + # 5.2 Replace LOGGER.info("----------------------------------------") for key, val in paths.items(): LOGGER.info("%-25s : %s"%(key, val)) @@ -1112,7 +1110,7 @@ def warning( paths, **kwargs ): if num == 0: raise BaseException("The string @@@%s@@@ is not replaced correctly."%key) LOGGER.warning("@@@%s@@@ is replaced to '' since the value is not given or the related option is disabled."%key) - # 6.3 Write + # 5.3 Write with open( GAMER_MAKE_OUT, "w") as make_out: make_out.write( command + makefile ) diff --git a/tool/config/set_settings.sh b/tool/config/set_settings.sh index 6141298fc1..e7d2fc5065 100644 --- a/tool/config/set_settings.sh +++ b/tool/config/set_settings.sh @@ -22,16 +22,18 @@ for KEY in "${VALID_KEYS[@]}"; do done # Print keys in a formatted way -print_key(){ - # $1 : the key name +print_key() { + # $1 : the key name # $2 : the key value or additional message + # $3 : indent number (optional, default 0) + printf "%${3}s" "" printf "%-${MAX_KEY_LENGTH}s %s\n" "$1" "$2" } show_valid_keys() { echo "Valid keys and their functionalities:" for KEY in "${!KEY_DESCRIPTIONS[@]}"; do - echo " $(print_key "$KEY" "${KEY_DESCRIPTIONS[$KEY]}")" + print_key "$KEY" "${KEY_DESCRIPTIONS[$KEY]}" 2 done } @@ -109,7 +111,7 @@ while [[ "$#" -gt 0 ]]; do break ;; *) echo "Error: Unknown option: -${OPT:0:1}" >&2 - echo "Use -h or --help for usage information." >&2 + printf "$(show_help)\n" >&2 exit 1 ;; esac OPT="${OPT:1}" @@ -122,7 +124,7 @@ while [[ "$#" -gt 0 ]]; do SETTING_FILE="../../src/.local_settings" shift ;; --global) - GLOBAL=true; + GLOBAL=true; SETTING_TYPE="global" SETTING_FILE="$HOME/.config/gamer/global_settings" shift ;; @@ -144,7 +146,7 @@ while [[ "$#" -gt 0 ]]; do shift ;; *) echo "Error: Unknown option: $1" >&2 - echo "Use -h or --help for usage information." >&2 + printf "$(show_help)\n" >&2 exit 1 ;; esac fi From f876723863b2766bb454ae48773f6fe79c74dd2f Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Wed, 15 Jan 2025 16:38:33 +0800 Subject: [PATCH 20/29] Remove override of `_get_option_tuples` `_get_option_tuples` was overriden before to disable abbreviation for Python 3.4 and below. The parameter `allow_abbrev` was added in Python 3.5. Now, since in this pull request #383 we ask for Python 3.5 or above, we can remove the override and set `allow_abbrev` to `False`. --- src/configure.py | 37 ++----------------------------------- 1 file changed, 2 insertions(+), 35 deletions(-) diff --git a/src/configure.py b/src/configure.py index 7a5be8c0a2..849ff2a9f0 100755 --- a/src/configure.py +++ b/src/configure.py @@ -119,40 +119,6 @@ def parse_args( self, args=None, namespace=None ): if len(argv) != 0: self.error( msg ) return args, self.gamer_names, self.depends, self.constraints - def _get_option_tuples(self, option_string): - # This function is directly from the source code of `argparse`. - # We decided to add the function manually because versions prior to Python 3.5 do not support `allow_abbrev`. - # See: https://github.com/python/cpython/blob/main/Lib/argparse.py - result = [] - - # option strings starting with two prefix characters are only split at the "=" - chars = self.prefix_chars - if option_string[0] in chars and option_string[1] in chars: - pass # we always use `allow_abbrev=False` - - # single character options can be concatenated with their arguments - # but multiple character options always have to have their arguments separate - elif option_string[0] in chars and option_string[1] not in chars: - option_prefix = option_string - short_option_prefix = option_string[:2] - short_explicit_arg = option_string[2:] - - for option_string in self._option_string_actions: - if option_string == short_option_prefix: - action = self._option_string_actions[option_string] - tup = action, option_string, "", short_explicit_arg - result.append(tup) - elif option_string.startswith(option_prefix): - action = self._option_string_actions[option_string] - tup = action, option_string, None, None - result.append(tup) - - # shouldn't ever get here - else: - self.error(("unexpected option string: %s") % option_string) - - return result # return the collected option tuples - def print_usage( self, *args, **kwargs ): if "usage" in self.program: print("Usage: %s\n" % self.program["usage"]) @@ -367,7 +333,8 @@ def load_arguments( sys_setting : SystemSetting ): parser = ArgumentParser( description = GAMER_DESCRIPTION, formatter_class = argparse.RawTextHelpFormatter, epilog = GAMER_EPILOG, - add_help = False + add_help = False, + allow_abbrev=False ) parser.add_argument( "-h", "--help", From 4bad5b325d788c5174b454a7215404c65296d772 Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Wed, 15 Jan 2025 18:43:59 +0800 Subject: [PATCH 21/29] Completely remove `--verbose` and `-v` options --- tool/config/set_settings.sh | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tool/config/set_settings.sh b/tool/config/set_settings.sh index e7d2fc5065..f0e6cce4c7 100644 --- a/tool/config/set_settings.sh +++ b/tool/config/set_settings.sh @@ -39,15 +39,14 @@ show_valid_keys() { show_help() { echo "Usage:" - echo " $0 (--local | --global) [-v] --= ..." - echo " $0 (--local | --global) [-v] (-d | --delete) ..." + echo " $0 (--local | --global) [-l] --= ..." + echo " $0 (--local | --global) [-l] (-d | --delete) ..." echo " $0 (--local | --global) (-l | --list)" echo " $0 (-h | --help)" echo "" echo "Options:" echo " --local Use local settings" echo " --global Use global settings" - echo " -v, --verbose Show verbose output" echo " --= Set a parameter" echo " -d, --delete key Delete a parameter" echo " --clear-all Clear all parameters" @@ -97,7 +96,6 @@ while [[ "$#" -gt 0 ]]; do while [[ -n "$OPT" ]]; do # Possibly combined case "${OPT:0:1}" in l) LIST=true ;; - v) LIST=true ;; h) show_help; exit 0 ;; d) DELETE=true @@ -129,7 +127,6 @@ while [[ "$#" -gt 0 ]]; do SETTING_FILE="$HOME/.config/gamer/global_settings" shift ;; --list) LIST=true; shift ;; - --verbose) LIST=true; shift ;; --help) show_help; exit 0 ;; --delete) DELETE=true From 6ccdb0229390498e163e996b973e6df066865f2c Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Wed, 15 Jan 2025 19:40:22 +0800 Subject: [PATCH 22/29] Rename the local variables to all lowercase --- tool/config/set_settings.sh | 109 ++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 54 deletions(-) diff --git a/tool/config/set_settings.sh b/tool/config/set_settings.sh index f0e6cce4c7..69387c766b 100644 --- a/tool/config/set_settings.sh +++ b/tool/config/set_settings.sh @@ -15,9 +15,9 @@ VALID_KEYS=("${!KEY_DESCRIPTIONS[@]}") # For padding keys with trailing spaces to format output MAX_KEY_LENGTH=0 -for KEY in "${VALID_KEYS[@]}"; do - if [ ${#KEY} -gt $MAX_KEY_LENGTH ]; then - MAX_KEY_LENGTH=${#KEY} +for key in "${VALID_KEYS[@]}"; do + if [ ${#key} -gt $MAX_KEY_LENGTH ]; then + MAX_KEY_LENGTH=${#key} fi done @@ -32,8 +32,8 @@ print_key() { show_valid_keys() { echo "Valid keys and their functionalities:" - for KEY in "${!KEY_DESCRIPTIONS[@]}"; do - print_key "$KEY" "${KEY_DESCRIPTIONS[$KEY]}" 2 + for key in "${!KEY_DESCRIPTIONS[@]}"; do + print_key "$key" "${KEY_DESCRIPTIONS[$key]}" 2 done } @@ -62,21 +62,22 @@ show_help() { LOCAL=false GLOBAL=false LIST=false -DELETE=false SET=false +DELETE=false +KEYS_PROCESSED=0 declare -A SETTINGS DELETE_KEYS=() # Parser utility functions parse_set_parameter() { - local PARAM="$1" - local KEY="${PARAM%%=*}" - if [[ "$PARAM" != *=* ]]; then - echo "Error: Invalid format for the key '$KEY'. Use --$KEY=." >&2 + local param="$1" + local key="${param%%=*}" + if [[ "$param" != *=* ]]; then + echo "Error: Invalid format for the key '$key'. Use --$key=." >&2 exit 1 fi - local VALUE="${PARAM#*=}" - SETTINGS["$KEY"]="$VALUE" + local value="${param#*=}" + SETTINGS["$key"]="$value" } parse_delete_keys() { @@ -91,28 +92,28 @@ parse_delete_keys() { # Main parser loop while [[ "$#" -gt 0 ]]; do if [[ "${1:0:1}" = "-" && "${1:0:2}" != "--" ]]; then # Short options - OPT="${1:1}" + opt="${1:1}" shift - while [[ -n "$OPT" ]]; do # Possibly combined - case "${OPT:0:1}" in + while [[ -n "$opt" ]]; do # Possibly combined + case "${opt:0:1}" in l) LIST=true ;; h) show_help; exit 0 ;; d) DELETE=true # Put the remaining combined short options back to the argument list # Warning: This method will only apply if all other options are not order sensitive - if [[ -n "${OPT:1}" ]]; then - set -- "$@" "-$(echo "${OPT:1}" | tr -d 'd')" + if [[ -n "${opt:1}" ]]; then + set -- "$@" "-$(echo "${opt:1}" | tr -d 'd')" fi parse_delete_keys "$@" shift $KEYS_PROCESSED break ;; *) - echo "Error: Unknown option: -${OPT:0:1}" >&2 + echo "Error: Unknown option: -${opt:0:1}" >&2 printf "$(show_help)\n" >&2 exit 1 ;; esac - OPT="${OPT:1}" + opt="${opt:1}" done else # Long options case $1 in @@ -160,18 +161,18 @@ fi # Validate the keys and values for setting if [ "$SET" = true ]; then - for KEY in "${!SETTINGS[@]}"; do - if [[ ! " ${VALID_KEYS[@]} " =~ " $KEY " ]]; then - echo "Error: Invalid key '$KEY'." >&2 + for key in "${!SETTINGS[@]}"; do + if [[ ! " ${VALID_KEYS[@]} " =~ " $key " ]]; then + echo "Error: Invalid key '$key'." >&2 printf "$(show_valid_keys)\n" >&2 exit 1 fi - if [[ -z "${SETTINGS[$KEY]}" ]]; then - echo "Error: The value for the key '$KEY' cannot be empty. Use --$KEY=." >&2 + if [[ -z "${SETTINGS[$key]}" ]]; then + echo "Error: The value for the key '$key' cannot be empty. Use --$key=." >&2 exit 1 fi - if [[ "${SETTINGS[$KEY]}" =~ \ ]]; then - echo "Error: The value for the key '$KEY' cannot contain spaces." >&2 + if [[ "${SETTINGS[$key]}" =~ \ ]]; then + echo "Error: The value for the key '$key' cannot contain spaces." >&2 exit 1 fi done @@ -189,9 +190,9 @@ if [ "$DELETE" = true ]; then echo "Error: No keys specified for deletion." >&2 exit 1 fi - for KEY in "${DELETE_KEYS[@]}"; do - if [[ ! " ${VALID_KEYS[@]} " =~ " $KEY " ]]; then - echo "Error: Invalid key '$KEY' for deletion." >&2 + for key in "${DELETE_KEYS[@]}"; do + if [[ ! " ${VALID_KEYS[@]} " =~ " $key " ]]; then + echo "Error: Invalid key '$key' for deletion." >&2 printf "$(show_valid_keys)\n" >&2 exit 1 fi @@ -216,9 +217,9 @@ declare -A EXISTING_SETTINGS if [ -f "$SETTING_FILE" ]; then while read -r LINE; do [[ "$LINE" =~ ^#.*$ ]] && continue - KEY="$(echo "$LINE" | awk '{print $1}')" - VALUE="$(echo "$LINE" | awk '{print $2}')" - EXISTING_SETTINGS["$KEY"]="$VALUE" + key="$(echo "$LINE" | awk '{print $1}')" + value="$(echo "$LINE" | awk '{print $2}')" + EXISTING_SETTINGS["$key"]="$value" done < "$SETTING_FILE" fi @@ -231,30 +232,30 @@ if [ "$LIST" = true ]; then # Header for listing settings fi # Main loop to update or delete settings -for KEY in "${VALID_KEYS[@]}"; do - OLD_VALUE="${EXISTING_SETTINGS[$KEY]}" - NEW_VALUE="${SETTINGS[$KEY]}" +for key in "${VALID_KEYS[@]}"; do + old_value="${EXISTING_SETTINGS[$key]}" + new_value="${SETTINGS[$key]}" - if [ "$SET" = true ] && [ -n "$NEW_VALUE" ]; then # The key will be set + if [ "$SET" = true ] && [ -n "$new_value" ]; then # The key will be set - EXISTING_SETTINGS["$KEY"]="$NEW_VALUE" # Update or set new value + EXISTING_SETTINGS["$key"]="$new_value" # Update or set new value if [ "$LIST" = true ]; then - if [ -z "$OLD_VALUE" ]; then - print_key "$KEY" "$NEW_VALUE (new)" + if [ -z "$old_value" ]; then + print_key "$key" "$new_value (new)" else - print_key "$KEY" "$OLD_VALUE -> $NEW_VALUE" + print_key "$key" "$old_value -> $new_value" fi fi - elif [ "$DELETE" = true ] && [[ " ${DELETE_KEYS[@]} " =~ " $KEY " ]]; then # The key will be deleted + elif [ "$DELETE" = true ] && [[ " ${DELETE_KEYS[@]} " =~ " $key " ]]; then # The key will be deleted - unset EXISTING_SETTINGS["$KEY"] # Delete the key - if [ "$LIST" = true ] && [ -n "$OLD_VALUE" ]; then - print_key "$KEY" "$OLD_VALUE -> (deleted)" + unset EXISTING_SETTINGS["$key"] # Delete the key + if [ "$LIST" = true ] && [ -n "$old_value" ]; then + print_key "$key" "$old_value -> (deleted)" fi - elif [ "$LIST" = true ] && [ -n "$OLD_VALUE" ]; then # List the existing settings - print_key "$KEY" "$OLD_VALUE" + elif [ "$LIST" = true ] && [ -n "$old_value" ]; then # List the existing settings + print_key "$key" "$old_value" fi done [ "$LIST" = true ] && echo "---------------------------" @@ -264,16 +265,16 @@ done if [ "$SET" = true ] || [ "$DELETE" = true ]; then # Create the directory and the settings file if it doesn't exist - DIR="$(dirname "$SETTING_FILE")" - if [ ! -d "$DIR" ]; then - mkdir -p "$DIR" - echo "Created directory $DIR." + dir="$(dirname "$SETTING_FILE")" + if [ ! -d "$dir" ]; then + mkdir -p "$dir" + echo "Created directory $dir." fi if [ ! -f "$SETTING_FILE" ]; then if touch "$SETTING_FILE"; then - echo "Created file $(basename "$SETTING_FILE") in $DIR." + echo "Created file $(basename "$SETTING_FILE") in $dir." else - echo "Fatal: Failed to create file $(basename "$SETTING_FILE") in $DIR." >&2 + echo "Fatal: Failed to create file $(basename "$SETTING_FILE") in $dir." >&2 exit 1 fi fi @@ -281,8 +282,8 @@ if [ "$SET" = true ] || [ "$DELETE" = true ]; then # Write updated settings to file { echo "# GAMER setting file" - for KEY in "${!EXISTING_SETTINGS[@]}"; do - print_key "${KEY}" "${EXISTING_SETTINGS[$KEY]}" + for key in "${!EXISTING_SETTINGS[@]}"; do + print_key "${key}" "${EXISTING_SETTINGS[$key]}" done } > "$SETTING_FILE" From 3e885b7e127e97b901e047a4cba4e0abb78f9e4d Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Thu, 16 Jan 2025 20:18:51 +0800 Subject: [PATCH 23/29] Add links between wiki pages Links: - Installation:-Option-List#--machine - Installation#default_setting --- doc/wiki/Installation-related/Installation:-Option-List.md | 3 ++- doc/wiki/Installation.md | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/wiki/Installation-related/Installation:-Option-List.md b/doc/wiki/Installation-related/Installation:-Option-List.md index d088df23ba..c2a31e5694 100644 --- a/doc/wiki/Installation-related/Installation:-Option-List.md +++ b/doc/wiki/Installation-related/Installation:-Option-List.md @@ -19,12 +19,13 @@ Compile-time simulation options are classified into the following categories: disabled). See the "Restriction" of each option carefully. ## `configure.py` Only + | Option | Value | Description | |:---:|:---:|---| | `-h` | - | Show a short help message. | | `-lh` | - | Show a detailed help message. | -| `--machine` | Filename string | Select the `*.config` file from the `configs` directory. It will overwrite the default machine set in the default setting file. | +| `--machine` | Filename string | Select the `*.config` file from the `configs` directory. It will overwrite the default machine set in the [[default setting file | Installation#default_setting]]. |                  diff --git a/doc/wiki/Installation.md b/doc/wiki/Installation.md index 3e42cc0268..8b257fc85c 100644 --- a/doc/wiki/Installation.md +++ b/doc/wiki/Installation.md @@ -2,6 +2,7 @@ Please see [[Machine Configuration File | Installation:-Machine-Configuration-File]]. + 2. Set your machine configuration file as default ```bash From 9d30e5db0856ae89d58a32465994bcb8da923b4a Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu Date: Fri, 17 Jan 2025 13:48:38 +0800 Subject: [PATCH 24/29] Update wiki and style --- .../Installation-related/Installation:-Option-List.md | 7 +++---- src/configure.py | 9 ++++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/doc/wiki/Installation-related/Installation:-Option-List.md b/doc/wiki/Installation-related/Installation:-Option-List.md index c2a31e5694..038a15b51e 100644 --- a/doc/wiki/Installation-related/Installation:-Option-List.md +++ b/doc/wiki/Installation-related/Installation:-Option-List.md @@ -19,13 +19,12 @@ Compile-time simulation options are classified into the following categories: disabled). See the "Restriction" of each option carefully. ## `configure.py` Only - | Option | Value | Description | |:---:|:---:|---| -| `-h` | - | Show a short help message. | -| `-lh` | - | Show a detailed help message. | -| `--machine` | Filename string | Select the `*.config` file from the `configs` directory. It will overwrite the default machine set in the [[default setting file | Installation#default_setting]]. | +| `-h` | - | Show a short help message. | +| `-lh` | - | Show a detailed help message. | +| `--machine` | Filename string | Select the `*.config` file from the `configs` directory. It will overwrite the default machine set in the [[default setting file | Installation#default_setting]]. |                  diff --git a/src/configure.py b/src/configure.py index 248c239457..ff8a9bb172 100755 --- a/src/configure.py +++ b/src/configure.py @@ -16,10 +16,17 @@ import re import ctypes -# Check the version of Python + + +#################################################################################################### +# Validation +#################################################################################################### +# Check the Python version if sys.version_info[0] < 3 or sys.version_info[1] < 5: raise BaseException("Python 3.5 or later is required.") + + #################################################################################################### # Global variables #################################################################################################### From dfe756f1bf3f72a50bc5983ed9526f7e266d8b7c Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu <48866415+technic960183@users.noreply.github.com> Date: Mon, 20 Jan 2025 14:38:34 +0800 Subject: [PATCH 25/29] Apply suggestions from code review Docs related suggestion Co-authored-by: Hsi-Yu Schive --- .../Installation:-Machine-Configuration-File.md | 6 +++--- doc/wiki/Installation.md | 8 ++++---- doc/wiki/Quick-Start.md | 6 +++--- src/configure.py | 6 +++--- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/doc/wiki/Installation-related/Installation:-Machine-Configuration-File.md b/doc/wiki/Installation-related/Installation:-Machine-Configuration-File.md index 7d764af4cd..93e7de28c0 100644 --- a/doc/wiki/Installation-related/Installation:-Machine-Configuration-File.md +++ b/doc/wiki/Installation-related/Installation:-Machine-Configuration-File.md @@ -1,11 +1,11 @@ -The machine configuration file is under `configs`. The configuration file contains the library paths, the compiler types, the compilation flags, and the GPU compute capability. +The machine configuration file is located under `configs`. It specifies the library paths, compiler types, compilation flags, and GPU compute capability. > [!TIP] -> Check the `configs` directory to see if there is a configuration file already available for your machine. +> Check the `configs` directory to see if a configuration file is already available for your machine. ## Set Up the Machine Configuration File -To set up your machine configuration file, go to `configs` and make a copy of `template.config` to modify it. +To set up your machine configuration file, go to `configs` and make a copy of `template.config` to modify: ```bash cd configs diff --git a/doc/wiki/Installation.md b/doc/wiki/Installation.md index 8b257fc85c..9baf21a957 100644 --- a/doc/wiki/Installation.md +++ b/doc/wiki/Installation.md @@ -10,8 +10,8 @@ ``` > [!NOTE] -> If you want to set the default machine configuration file for all of the GAMER copies under your user account, use the `--global` option instead of `--local`. -Still, you can override the global setting for the individual GAMER copies with the `--local` option. +> If you want to set the default machine configuration file for all GAMER copies under your user account, use the `--global` option instead of `--local`. +You can still override the global setting for individual GAMER copies using the `--local` option. Furthermore, you can override the default setting by passing the [[--machine | Installation:-Option-List#--machine]]=`your_machine` when executing `configure.py`. 3. Go to the source directory @@ -28,9 +28,9 @@ Furthermore, you can override the default setting by passing the [[--machine | I python configure.py [--your_arguments] ``` - `[--your_arguments]` represent the options that should align with your simulation requirements. Please check out [[Option List | Installation:-Option-List]] for all the available options. + `[--your_arguments]` represent the options that should align with your simulation requirements. Refer to [[Option List | Installation:-Option-List]] for a complete list of available options. - For example, the following command sets the FFTW method to `FFTW2`, and enables gravity and GPU. + For example, the following command sets the FFTW method to `FFTW2` and enables gravity and GPU. ``` bash python configure.py --fftw=FFTW2 --gravity=true --gpu=true diff --git a/doc/wiki/Quick-Start.md b/doc/wiki/Quick-Start.md index 31a68c77bf..17a46b92f0 100644 --- a/doc/wiki/Quick-Start.md +++ b/doc/wiki/Quick-Start.md @@ -4,11 +4,11 @@ Follow the instructions in [[Download]] to get the latest version of GAMER. ### Setup Your Machine Configuration File -The machine configuration file is under `configs`. The configuration file contains the library paths, the compiler types, the compilation flags, and the GPU compute capability. Check the existing configuration files to see if there is one that matches your machine. +The machine configuration file is located under `configs`. This file contains the library paths, compiler types, compilation flags, and GPU compute capability. Check the existing configuration files to see if one matches your machine. -If the machine configuration file is not available for your machine or the existing one is not appropriate, you will need to create a new one. Follow the instructions in [[Machine Configuration File | Installation:-Machine-Configuration-File]] to set the configuration file of your machine. +If no suitable configuration file is available, you will need to create a new one. Follow the instructions in [[Machine Configuration File | Installation:-Machine-Configuration-File]] to set up a configuration file for your machine. -If there is a configuration file that matches your machine, you should set it as the default by +If a configuration file matches your machine, you can set it as the default by running ```bash sh tool/config/set_settings.sh --local --machine=your_machine diff --git a/src/configure.py b/src/configure.py index ff8a9bb172..29290bc520 100755 --- a/src/configure.py +++ b/src/configure.py @@ -358,7 +358,7 @@ def load_arguments( sys_setting : SystemSetting ): # machine config setup parser.add_argument( "--machine", type=str, metavar="MACHINE", default=sys_setting.get_default( "machine_name", "eureka_intel" ), - help="Select the *.config file under ../configs directory. Will overwrite the default machine set in the default setting file.\nChoice: [eureka_intel, spock_intel, ...] => " + help="Select the *.config file from the ../configs directory. This will overwrite the default machine specified in the default setting file.\nChoice: [eureka_intel, spock_intel, ...] => " ) # A. options of diffierent physical models @@ -771,9 +771,9 @@ def load_setting(): Format of the setting file: 1. Comment starts with `#`. - 2. Separate the variable name and value with space. The line starts with the variable name. + 2. The line begins with the variable name, followed by one or multiple spaces, and then the value. 3. Only the fisrt value of the line will be loaded. - 4. Only the last variable of duplicated variables will be loaded. + 4. If a variable is defined multiple times, only the last occurrence will be used. """ sys_setting = SystemSetting() From 4cfbca9e255e6d1000b9b6d6b6f8b31f93cf3794 Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu <48866415+technic960183@users.noreply.github.com> Date: Mon, 20 Jan 2025 15:28:11 +0800 Subject: [PATCH 26/29] Fix wiki link and indentation Co-authored-by: Hsi-Yu Schive --- doc/wiki/Installation-related/Installation:-Option-List.md | 2 +- doc/wiki/Installation.md | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/wiki/Installation-related/Installation:-Option-List.md b/doc/wiki/Installation-related/Installation:-Option-List.md index 038a15b51e..ed1c0aff93 100644 --- a/doc/wiki/Installation-related/Installation:-Option-List.md +++ b/doc/wiki/Installation-related/Installation:-Option-List.md @@ -24,7 +24,7 @@ disabled). See the "Restriction" of each option carefully. |:---:|:---:|---| | `-h` | - | Show a short help message. | | `-lh` | - | Show a detailed help message. | -| `--machine` | Filename string | Select the `*.config` file from the `configs` directory. It will overwrite the default machine set in the [[default setting file | Installation#default_setting]]. | +| `--machine` | Filename string | Select the `*.config` file from the `configs` directory. It will overwrite the default machine set in the [[default setting file \| Installation#default_setting]]. |                  diff --git a/doc/wiki/Installation.md b/doc/wiki/Installation.md index 9baf21a957..5f1e1e85f9 100644 --- a/doc/wiki/Installation.md +++ b/doc/wiki/Installation.md @@ -3,6 +3,7 @@ Please see [[Machine Configuration File | Installation:-Machine-Configuration-File]]. + 2. Set your machine configuration file as default ```bash From 7e39dcd16a687a17a9615522ed5d6218024bf0e6 Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu <48866415+technic960183@users.noreply.github.com> Date: Mon, 20 Jan 2025 20:09:59 +0800 Subject: [PATCH 27/29] Fix overwriting problem of the default setting The empty local setting will not overwrite the global setting now. --- src/configure.py | 78 ++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/src/configure.py b/src/configure.py index 29290bc520..244d99ebc2 100755 --- a/src/configure.py +++ b/src/configure.py @@ -197,11 +197,46 @@ def print_help( self, *args, **kwargs ): if "epilog" in self.program: print(self.program["epilog"]) class SystemSetting( dict ): + """ + Store the system settings from the default setting file. + + Format of the setting file: + 1. Comment starts with `#`. + 2. The line begins with the variable name, followed by one or multiple spaces, and then the value. + 3. Only the fisrt value of the line will be loaded. + 4. If a variable is defined multiple times, only the last occurrence will be used. + """ def __init__( self, *args, **kwargs ): super().__init__( *args, **kwargs ) def get_default( self, key, default_val ): return self.get(key, default_val) + + def load( self, pathname ): + """Load the system settings from the default setting file. If a setting exists, + it will be overwritten. Return `False` if the file does not exist. + + Parameters: + pathname : str - The path of the default setting file to be loaded. + + Returns: + bool - Whether the file exists. + """ + if not os.path.isfile(pathname): + return False + + with open( pathname, "r" ) as f: + lines = f.readlines() + for line in lines: + tokens = line.strip().split() + if len(tokens) == 0: continue # empty line + if tokens[0][0] == "#": continue # skip comment line + if len(tokens) >= 2: + self[tokens[0]] = tokens[1] + else: # key without value + self[tokens[0]] = None + + return True @@ -357,7 +392,7 @@ def load_arguments( sys_setting : SystemSetting ): # machine config setup parser.add_argument( "--machine", type=str, metavar="MACHINE", - default=sys_setting.get_default( "machine_name", "eureka_intel" ), + default=sys_setting.get_default( "machine", "eureka_intel" ), help="Select the *.config file from the ../configs directory. This will overwrite the default machine specified in the default setting file.\nChoice: [eureka_intel, spock_intel, ...] => " ) @@ -762,43 +797,6 @@ def load_config( config ): return paths, compilers, flags, gpus -def load_setting(): - """ - Return the default machine setting in the following order: - 1. Read the local setting located at `GAMER_LOCAL_SETTING`. - 2. Read the global setting located at `GAMER_GLOBAL_SETTING`. - 3. Fall back to `eureka_intel` for backward compatibility. - - Format of the setting file: - 1. Comment starts with `#`. - 2. The line begins with the variable name, followed by one or multiple spaces, and then the value. - 3. Only the fisrt value of the line will be loaded. - 4. If a variable is defined multiple times, only the last occurrence will be used. - """ - sys_setting = SystemSetting() - - if os.path.isfile( GAMER_LOCAL_SETTING ): setting_file = GAMER_LOCAL_SETTING - elif os.path.isfile( GAMER_GLOBAL_SETTING ): setting_file = GAMER_GLOBAL_SETTING - else: - LOGGER.info("System setting file not detected.") - return sys_setting - - LOGGER.info("Using %s as setting file."%(setting_file)) - - with open( setting_file, "r" ) as f: - lines = f.readlines() - - for line in lines: - tokens = line.strip().split() - if len(tokens) == 0: continue # empty line - if tokens[0][0] == "#": continue # skip comment line - if len(tokens) >= 2: - sys_setting[tokens[0]] = tokens[1] - else: # key without value - sys_setting[tokens[0]] = None - - return sys_setting - def set_conditional_defaults( args ): if args["unsplit_gravity"] is None: args["unsplit_gravity"] = (args["model"] == "HYDRO") @@ -1040,7 +1038,9 @@ def warning( paths, **kwargs ): LOGGER.info( " ".join( [sys.executable] + sys.argv ) ) # 2. Load system settings - sys_setting = load_setting() + sys_setting = SystemSetting() + sys_setting.load(GAMER_GLOBAL_SETTING) + sys_setting.load(GAMER_LOCAL_SETTING) # 3. Load the input arguments args, name_table, depends, constraints = load_arguments( sys_setting ) From 948ed71bcb0c53e0422c7b61132e517a5e9ce330 Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu <48866415+technic960183@users.noreply.github.com> Date: Tue, 21 Jan 2025 11:36:27 +0800 Subject: [PATCH 28/29] style applied from code review Co-authored-by: Chun-Yen Chen <70311975+ChunYen-Chen@users.noreply.github.com> --- src/configure.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/configure.py b/src/configure.py index 244d99ebc2..f889f0fc64 100755 --- a/src/configure.py +++ b/src/configure.py @@ -213,18 +213,18 @@ def get_default( self, key, default_val ): return self.get(key, default_val) def load( self, pathname ): - """Load the system settings from the default setting file. If a setting exists, + """ + Load the system settings from the default setting file. If a setting exists, it will be overwritten. Return `False` if the file does not exist. Parameters: pathname : str - The path of the default setting file to be loaded. Returns: - bool - Whether the file exists. + bool - Whether the file exists. """ if not os.path.isfile(pathname): return False - with open( pathname, "r" ) as f: lines = f.readlines() for line in lines: From 833f44a9f9ea9c7f7b124b01d04b59b2d5914be8 Mon Sep 17 00:00:00 2001 From: Yuan-Ming Hsu <48866415+technic960183@users.noreply.github.com> Date: Tue, 21 Jan 2025 11:36:27 +0800 Subject: [PATCH 29/29] style Co-authored-by: Chun-Yen Chen <70311975+ChunYen-Chen@users.noreply.github.com> --- src/configure.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/configure.py b/src/configure.py index f889f0fc64..eda31bda30 100755 --- a/src/configure.py +++ b/src/configure.py @@ -210,8 +210,8 @@ def __init__( self, *args, **kwargs ): super().__init__( *args, **kwargs ) def get_default( self, key, default_val ): - return self.get(key, default_val) - + return self.get( key, default_val ) + def load( self, pathname ): """ Load the system settings from the default setting file. If a setting exists,