#!/bin/sh
#
# submission script for PMSCO calculations on the Ra cluster
#
# CAUTION: the job will execute the pmsco code which is present in the directory tree
#          of this script _at the time of job execution_, not submission!
#          before changing the code, make sure that all pending jobs have started execution,
#          otherwise you will experience version conflicts.
#          it's better to use the qpmsco.ra-git.sh script which clones the code.

if [ $# -lt 1 ]; then
  echo "Usage: $0 [NOSUB] DESTDIR JOBNAME NODES TASKS_PER_NODE WALLTIME:HOURS PROJECT MODE [ARGS [ARGS [...]]]"
  echo ""
  echo "       NOSUB (optional): do not submit the script to the queue. default: submit."
  echo "       DESTDIR: destination directory. must exist. a sub-dir \$JOBNAME is created."
  echo "       JOBNAME (text): name of job. use only alphanumeric characters, no spaces."
  echo "       NODES (integer): number of computing nodes. (1 node = 24 or 32 processors)."
  echo "          do not specify more than 2."
  echo "       TASKS_PER_NODE (integer): 1...24, or 32."
  echo "          24 or 32 for full-node allocation."
  echo "          1...23 for shared node allocation."
  echo "       WALLTIME:HOURS (integer): requested wall time."
  echo "          1...24 for day partition"
  echo "          24...192 for week partition"
  echo "          1...192 for shared partition"
  echo "       PROJECT: python module (file path) that declares the project and starts the calculation."
  echo "       MODE: PMSCO calculation mode (single|swarm|gradient|grid)."
  echo "       ARGS (optional): any number of further PMSCO or project arguments (except mode and time)."
  echo ""
  echo "the job script is written to \$DESTDIR/\$JOBNAME which is also the destination of calculation output."
  exit 1
fi

# location of the pmsco package is derived from the path of this script
SCRIPTDIR="$(dirname $(readlink -f $0))"
SOURCEDIR="$SCRIPTDIR/.."
PMSCO_SOURCE_DIR="$SOURCEDIR"

# read arguments
if [ "$1" == "NOSUB" ]; then
  NOSUB="true"
  shift
else
  NOSUB="false"
fi

DEST_DIR="$1"
shift

PMSCO_JOBNAME=$1
shift

PMSCO_NODES=$1
PMSCO_TASKS_PER_NODE=$2
PMSCO_TASKS=$(expr $PMSCO_NODES \* $PMSCO_TASKS_PER_NODE)
shift 2

PMSCO_WALLTIME_HR=$1
PMSCO_WALLTIME_MIN=$(expr $PMSCO_WALLTIME_HR \* 60)
shift

# select partition
if [ $PMSCO_WALLTIME_HR -ge 25 ]; then
    PMSCO_PARTITION="week"
else
    PMSCO_PARTITION="day"
fi
if [ $PMSCO_TASKS_PER_NODE -lt 24 ]; then
    PMSCO_PARTITION="shared"
fi

PMSCO_PROJECT_FILE="$(readlink -f $1)"
shift

PMSCO_MODE="$1"
shift

PMSCO_PROJECT_ARGS="$*"

# use defaults, override explicitly in PMSCO_PROJECT_ARGS if necessary
PMSCO_SCAN_FILES=""
PMSCO_LOGLEVEL=""
PMSCO_CODE=""

# set up working directory
cd "$DEST_DIR"
if [ ! -d "$PMSCO_JOBNAME" ]; then
    mkdir "$PMSCO_JOBNAME"
fi
cd "$PMSCO_JOBNAME"
WORKDIR="$(pwd)"
PMSCO_WORK_DIR="$WORKDIR"

# provide revision information, requires git repository
cd "$SOURCEDIR"
PMSCO_REV=$(git log --pretty=format:"%h, %ai" -1)
if [ $? -ne 0 ]; then
   PMSCO_REV="revision unknown, "$(date +"%F %T %z")
fi
cd "$WORKDIR"
echo "$PMSCO_REV" > revision.txt

# generate job script from template
sed -e "s:_PMSCO_WORK_DIR:$PMSCO_WORK_DIR:g" \
    -e "s:_PMSCO_JOBNAME:$PMSCO_JOBNAME:g" \
    -e "s:_PMSCO_NODES:$PMSCO_NODES:g" \
    -e "s:_PMSCO_WALLTIME_HR:$PMSCO_WALLTIME_HR:g" \
    -e "s:_PMSCO_PROJECT_FILE:$PMSCO_PROJECT_FILE:g" \
    -e "s:_PMSCO_PROJECT_ARGS:$PMSCO_PROJECT_ARGS:g" \
    -e "s:_PMSCO_CODE:$PMSCO_CODE:g" \
    -e "s:_PMSCO_MODE:$PMSCO_MODE:g" \
    -e "s:_PMSCO_SOURCE_DIR:$PMSCO_SOURCE_DIR:g" \
    -e "s:_PMSCO_SCAN_FILES:$PMSCO_SCAN_FILES:g" \
    -e "s:_PMSCO_LOGLEVEL:$PMSCO_LOGLEVEL:g" \
    "$SCRIPTDIR/pmsco.ra.template" > $PMSCO_JOBNAME.job

chmod u+x "$PMSCO_JOBNAME.job"

# request nodes and tasks
#
# The option --ntasks-per-node is meant to be used with the --nodes option.
# (For the --ntasks option, the default is one task per node, use the --cpus-per-task option to change this default.)
#
# sbatch options
# --cores-per-socket=16
#   32 cores per node
# --partition=[shared|day|week]
# --time=8-00:00:00
#   override default time limit (2 days in long queue)
#   time formats: "minutes", "minutes:seconds", "hours:minutes:seconds", "days-hours", "days-hours:minutes", "days-hours:minutes:seconds"
# --mail-type=ALL
# --test-only
#   check script but do not submit
#
SLURM_ARGS="--nodes=$PMSCO_NODES --ntasks-per-node=$PMSCO_TASKS_PER_NODE"

if [ $PMSCO_TASKS_PER_NODE -gt 24 ]; then
    SLURM_ARGS="--cores-per-socket=16 $SLURM_ARGS"
fi

SLURM_ARGS="--partition=$PMSCO_PARTITION $SLURM_ARGS"

SLURM_ARGS="--time=$PMSCO_WALLTIME_HR:00:00 $SLURM_ARGS"

CMD="sbatch $SLURM_ARGS $PMSCO_JOBNAME.job"
echo $CMD
if [ "$NOSUB" != "true" ]; then
  $CMD
fi

exit 0
