Skip to content

Instantly share code, notes, and snippets.

@dk949
Created October 13, 2022 09:25
Show Gist options
  • Save dk949/a5faa7818b3d9487979644392d1c61aa to your computer and use it in GitHub Desktop.
Save dk949/a5faa7818b3d9487979644392d1c61aa to your computer and use it in GitHub Desktop.
Genrate slurm script
#!/bin/bash
set -e
___EXCLUSIVE="#SBATCH --exclusive"
___CPUS_PER_TASK="1"
help(){
echo "
Every option can be set from the command line or vie an env variable.
Command line takes priority.
Most options have snesible defualts.
-j --job-name NAME Name of the job (env var JOB_NAME)
-c --cores CORES How many cores to use per node (env var CORES)
-t --time TIME Maximum run time of the job (env var TIME)
-n --nodes NODES How many nodes to use (env var NODES)
-a --account NAME Name of the account (env var ACCOUNT)
-p --partition VAL Partition value (env var PARTITION)
-q --qos VAL QoS value (env var QOS)
--no-exclusive Do not use exclusive exclusive flag
--cpus-per-task CPUS How many cpus to use per task
-o --output FILE Name of the output file (env var OUTPUT)
-m --modules MODULES Comma separated list of modules to load (env var MODULES)
-h --help Print this message
"
exit 0
}
die(){
>&2 echo "[ERROR]:" "$*"
exit 1
}
warn() {
>&2 echo "[WARN]:" "$*"
}
defaults() {
___DEFAULT_JOB_NAME="defaultJobName"
___DEFAULT_CORES="8"
___DEFAULT_TIME="00:00:20"
___DEFAULT_NODES="1"
___DEFAULT_ACCOUNT="m22oc-$USER"
___DEFAULT_PARTITION="standard"
___DEFAULT_QOS="standard"
___DEFAULT_OUTPUT="${JOB_NAME:-$___DEFAULT_JOB_NAME}.slurm"
}
args() {
while [ -n "$1" ]; do
case "$1" in
-h|--help)
help
;;
-j|--job-name)
JOB_NAME="$2"
shift 2
;;
-c|--cores)
CORES="$2"
shift 2
;;
-t|--time)
TIME="$2"
shift 2
;;
-n|--nodes)
NODES="$2"
shift 2
;;
-a|--account)
ACCOUNT="$2"
shift 2
;;
-p|--partition)
PARTITION="$2"
shift 2
;;
-q|--qos)
QOS="$2"
shift 2
;;
-m|--modules)
MODULES="$2"
shift 2
;;
-o|--output)
OUTPUT="$2"
shift 2
;;
--no-exclusive)
___EXCLUSIVE=
shift
;;
--cpus-per-task)
___CPUS_PER_TASK="$2"
shift 2
;;
-*)
die "Option $1 not recognized"
;;
esac
done
}
setdefaults(){
for var in JOB_NAME CORES TIME NODES ACCOUNT PARTITION QOS OUTPUT; do
D="___DEFAULT_$var"
if [ -z "${!var}" ]; then
warn "$var not specified defaulting to \"${!D}\""
declare -g "$var=${!D}"
fi
done
}
run (){
if [ -f "$OUTPUT" ]; then
die "file \"$OUTPUT\" already exists. Delete it and rerun the script if intentional"
fi
echo "#!/bin/bash
#SBATCH --job-name=$JOB_NAME
#SBATCH --time=$TIME
$___EXCLUSIVE
#SBATCH --nodes=$NODES
#SBATCH --tasks-per-node=$CORES
#SBATCH --cpus-per-task=$___CPUS_PER_TASK
#SBATCH --account=$ACCOUNT
#SBATCH --partition=$PARTITION
#SBATCH --qos=$QOS
########################################
cd \$SLURM_SUBMIT_DIR
" > "$OUTPUT"
for i in $(echo "$MODULES" | tr ',' '\n'); do
echo "module load $i" >> "$OUTPUT"
done
}
args "$@"
defaults
setdefaults
run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment