#!/bin/bash
#SBATCH --job-name=jacobi_acc
#SBATCH --partition=gpu              # adjust to the GH200 partition name
#SBATCH --nodes=1
#SBATCH --ntasks=4
#SBATCH --gres=gpu:4                 
#SBATCH --cpus-per-task=1        
#SBATCH --time=00:15:00



set -euo pipefail

module purge
module load nvidia nvhpc-hpcx-cuda13/26.1

# -Minfo=accel is what tells you the loops actually went to the device.
# https://www.calmip.univ-toulouse.fr/espace-utilisateurs/documentation-technique-kairos/environnements-logiciels#section1274-1
#nvc -O3  -o jacobi_acc jacobi_acc.c # to complete 
nvc -O3 -tp=neoverse-v2 -acc -gpu=cc90 -Minfo=accel -o jacobi_acc jacobi_acc.c


# always run from the directory where sbatch was submitted
cd "${SLURM_SUBMIT_DIR}" || exit 1

# output directory
RESULTS_DIR="${SLURM_SUBMIT_DIR}/results_gpu"
mkdir -p "${RESULTS_DIR}"

RESULT_FILE="${RESULTS_DIR}/${SLURM_JOB_NAME}_${SLURM_JOB_ID}.txt"

# Save everything printed by the job to both terminal and result file
exec > >(tee "${RESULT_FILE}") 2>&1

echo
echo "Results file : ${RESULT_FILE}"
echo

echo "=== Running Jacobi ==="
echo "Grid        : 8192 x 8192"
echo "Iterations  : 500"
echo "Tasks       : ${SLURM_NTASKS}"
echo "Start       : $(date)"
echo

START_TIME=$(date +%s)

time srun gbind ./jacobi_acc 8192 8192 500

EXIT_CODE=$?

END_TIME=$(date +%s)
ELAPSED=$((END_TIME - START_TIME))

echo
echo "=== Job summary ==="
echo "Job ID       : ${SLURM_JOB_ID}"
echo "Job name     : ${SLURM_JOB_NAME}"
echo "Node         : ${SLURM_JOB_NODELIST}"
echo "Elapsed time : ${ELAPSED} seconds"
echo "Exit code    : ${EXIT_CODE}"
echo "End time     : $(date)"
echo "Results      : ${RESULT_FILE}"

exit ${EXIT_CODE}
