#!/bin/bash
#SBATCH --job-name=jacobi_cpu
#SBATCH --partition=noeud              # adjust to the x86 partition name
#SBATCH --nodes=1
#SBATCH --ntasks=48
#SBATCH --cpus-per-task=4        
#SBATCH --time=00:15:00
#SBATCH --exclusive



set -euo pipefail

module purge
module load toolchain/oneapi2025.3

#Compile to complete 
export OMP_NUM_THREADS=${SLURM_CPUS_PER_TASK}
#mpiicx -O3 -march=      -o jacobi_hybrid jacobi_cpu_hyper.c # to complete 
# https://www.calmip.univ-toulouse.fr/espace-utilisateurs/documentation-technique-kairos/environnements-logiciels#section1274-1
mpiicx -O3 -march=graniterapids -qopenmp -o jacobi_hybrid jacobi_cpu_hyper.c


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

# output directory
RESULTS_DIR="${SLURM_SUBMIT_DIR}/results_cpu"
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 "Threads     : ${OMP_NUM_THREADS}"
echo "Start       : $(date)"
echo

START_TIME=$(date +%s)

time srun ./jacobi_hybrid 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 "Threads      : ${OMP_NUM_THREADS}"
echo "Elapsed time : ${ELAPSED} seconds"
echo "Exit code    : ${EXIT_CODE}"
echo "End time     : $(date)"
echo "Results      : ${RESULT_FILE}"

exit ${EXIT_CODE}
