#!/bin/bash
# SLURM job template for ML-KEM benchmarking.
# Instantiated by slurm/submit.sh — do not submit directly.
#
# Template variables (filled by envsubst in submit.sh):
#   PARAM      — 512 | 768 | 1024
#   VARIANT    — ref | avx2 | refnv | refo0
#   NSPINS     — outer loop iterations (default 1000)
#   BUILD_DIR  — path to directory containing the benchmark binaries
#   OUTPUT_DIR — directory where this job's .out file is written

#SBATCH -J bench_mlkem${PARAM}_${VARIANT}
#SBATCH -p batch
#SBATCH -n 1
#SBATCH -c 1
#SBATCH --mem=256M
#SBATCH -t 00:45:00
#SBATCH -o ${OUTPUT_DIR}/%j.out

# ── Environment ──────────────────────────────────────────────────────────────
# Pin to a single logical core for deterministic measurements.
taskset -cp 0 $$ 2>/dev/null || true

# Disable CPU frequency scaling if we have permission; ignore otherwise.
cpupower frequency-set -g performance 2>/dev/null || true

# ── Metadata (parsed by analysis/pkg/parse) ──────────────────────────────────
# These ## lines are picked up by the parser alongside the OSCAR prolog lines.
echo "## BENCH_VARIANT  : ${VARIANT}"
echo "## BENCH_PARAM    : ${PARAM}"
echo "## BENCH_NSPINS   : ${NSPINS}"
echo "## BENCH_NODE_REQ : ${BENCH_NODE}"
echo "## BENCH_BINARY   : ${BUILD_DIR}/bench_mlkem${PARAM}_${VARIANT}"
echo "## BENCH_DATE     : $(date -Iseconds)"
echo "## CPU_MODEL      : $(grep 'model name' /proc/cpuinfo | head -1 | cut -d: -f2 | xargs)"
echo "## PERF_PARANOID  : $(cat /proc/sys/kernel/perf_event_paranoid 2>/dev/null || echo unknown)"
echo "## PAPI_BUILD     : ${WITH_PAPI:-OFF}"

BINARY="${BUILD_DIR}/bench_mlkem${PARAM}_${VARIANT}"
NSPINS="${NSPINS:-1000}"

if [[ ! -x "$BINARY" ]]; then
  echo "ERROR: binary not found or not executable: $BINARY" >&2
  exit 1
fi

# ── Run ───────────────────────────────────────────────────────────────────────
"$BINARY" "$NSPINS"
