Skip to content

Commit

Permalink
Feature: add default machine
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
technic960183 committed Dec 12, 2024
1 parent 430fe01 commit 1f66a18
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ GAMER_ExtractProfile
.vscode
src/Makefile
src/Makefile.log
src/.local_settings
43 changes: 39 additions & 4 deletions src/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ":="
Expand Down Expand Up @@ -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")

Expand All @@ -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"]
Expand Down
63 changes: 63 additions & 0 deletions tool/set_settings.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/bash

cd "$(dirname "$0")"

show_help() {
echo "Usage: $0 [--local | --global] --machine=<name>"
echo ""
echo "Options:"
echo " --local Use local settings"
echo " --global Use global settings"
echo " --machine=<name> 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=<name> 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'."

0 comments on commit 1f66a18

Please sign in to comment.