#!/bin/bash
# ---------------------------------------------------------------------------------------------
# PATHSAMPLE with the SLURM keyword: PATHSAMPLE starts each OPTIM job itself, as
#   srun -n1 -N1 --exclusive -c<cores> OPTIM   in its own folder <id>-tempcopyfiles
# so several OPTIM (+ORCA) jobs run side by side without overwriting each other's ORCA files.
#
# Slurm numbers for PATHSAMPLE (NOT the same meaning as for a single OPTIM run):
#   --ntasks        = number of OPTIM jobs running at once
#   --cpus-per-task = cores for each OPTIM job = NPROCS in ORCA.template   (checked below)
#   total cores     = ntasks x cpus-per-task; the jobs may sit on different nodes
# PATHSAMPLE takes both numbers from nodes.info (written below); CPUS in pathdata is then ignored.
# Partition: CPU. Members of the wales group can use --partition=CPU,DEBUG (DEBUG: higher priority).
# ---------------------------------------------------------------------------------------------
#SBATCH --job-name=ps_orca_check
#SBATCH --partition=CPU,WALES,DEBUG
#SBATCH --ntasks=2
#SBATCH --cpus-per-task=3
# memory per core: above %maxcore 2500 (MB per rank) in ORCA.template
#SBATCH --mem-per-cpu=3800M
#SBATCH --time=10:00:00

module load orca/6.1.1                    # ORCA 6.1.1 ...
module swap openmpi/5.0.5 openmpi/4.1.7   # ... run with Open MPI 4.1.7: the module's default 5.0.5 crashes
                                          # now and then inside ORCA (segfault in its shared-memory transport)

# Settings for the mpirun that ORCA starts itself (Open MPI 4.1 reads OMPI_MCA_... names; it ignores
# the PRTE_MCA_... names that Open MPI 5 uses).
# 1. No core binding: otherwise every ORCA run on a node pins its ranks to the same cores (0, 2, 4, ...)
#    and several jobs sharing a node run 3-5x slower.
export OMPI_MCA_hwloc_base_binding_policy=none
# 2. Let mpirun start NPROCS ranks even if it counts fewer slots in the Slurm allocation;
#    without it ORCA's mpirun can refuse to start ("not enough slots").
export OMPI_MCA_rmaps_base_oversubscribe=1

chmod +x orca_retry.sh

# Stop at once if the numbers disagree:
#  - ORCA ranks per OPTIM job (%PAL NPROCS) must equal --cpus-per-task;
#  - CYCLES x (OPTIM jobs at once) is the most OPTIM jobs PATHSAMPLE will run, so for CHECKMIN/CHECKTS
#    it must be at least the number of stationary points to check, or the last ones are skipped silently.
np=$(awk 'toupper($1)=="%PAL"{for(i=1;i<=NF;i++) if(toupper($i)=="NPROCS") print $(i+1)}' ORCA.template)
[ "$np" = "$SLURM_CPUS_PER_TASK" ] || { echo "ERROR: %PAL NPROCS $np in ORCA.template but --cpus-per-task=$SLURM_CPUS_PER_TASK"; exit 1; }
check_cycles() {
  awk -v n="$SLURM_NTASKS" -v f="$1" '
    toupper($1)=="CHECKMIN"||toupper($1)=="CHECKTS" { npts=$3-$2+1 }
    toupper($1)=="CYCLES" { c=$2 }
    END { if (npts && c*n < npts) { printf "ERROR: %s checks %d points but CYCLES %d x %d jobs at once = %d\n", f, npts, c, n, c*n; exit 1 } }' "$1"
}

# nodes.info, read by the SLURM keyword: line 1 = "<OPTIM jobs at once> <cores each>",
# then one hostname per task, then user name and working directory.
{ echo "$SLURM_NTASKS $SLURM_CPUS_PER_TASK"; srun hostname; echo $USER; pwd; } > nodes.info

PS=/sharedscratch/wales/PATHSAMPLE.gfortran/PATHSAMPLE
check_cycles pathdata.checkmin || exit 1
check_cycles pathdata.checkts  || exit 1

# Two PATHSAMPLE runs in turn; PATHSAMPLE always reads the files called pathdata and odata.checksp.
# (CHECKTS checks one TS here, so one of the two OPTIM slots stays idle in the second run.)
cp odata.checksp.min odata.checksp; cp pathdata.checkmin pathdata
echo running checkmin
$PS > output.checkmin 2>&1
cp odata.checksp.ts odata.checksp;  cp pathdata.checkts pathdata
echo running checkts
$PS > output.checkts 2>&1

# orca_retry.sh (the program on the ORCA line of odata) reruns any ORCA call that failed and logs it here:
if [ -f orca_failures/failures.log ]; then echo "NOTE: some ORCA calls failed and were retried: see orca_failures/failures.log"; fi
