#!/bin/bash
# Companion code for Chapter 15: Parallel Computing.
# Slurm submission example for ch15_mpi_heat_strips (C++ MPI version).
# This is a one-node, four-process introductory job. No GPU is needed.
#
# Before submitting, compile the MPI program on your cluster with its
# recommended MPI compiler / module environment, for example:
#   mpic++ -std=c++11 -O3 ch15_mpi_heat_strips.cpp -o ch15_mpi_heat_strips
# Then run: sbatch ch15_mpi_heat_slurm.sh
#
# The job starts in the directory from which sbatch was submitted.
# Change resource requests to suit your cluster's cores, queues and policies.
# Some clusters require additional #SBATCH --account=... or --partition=...
# lines; ask your local HPC documentation rather than copying arbitrary names.
#
#SBATCH --job-name=ch15_mpi_heat
#SBATCH --nodes=1
#SBATCH --ntasks=4
#SBATCH --cpus-per-task=1
#SBATCH --time=00:10:00
#SBATCH --output=ch15_mpi_heat_%j.out

set -eu
cd "${SLURM_SUBMIT_DIR:?Submit this file with sbatch}"

# These are four MPI *processes*, not four OpenMP threads.
# Use the SAME MPI family for compiling and launching the program.
# If your cluster requires modules, load the appropriate compiler/MPI here.
# Example (site-specific, not active): module load gcc openmpi

if [ ! -x ./ch15_mpi_heat_strips ]; then
    echo 'Missing executable: ./ch15_mpi_heat_strips' >&2
    echo 'Compile the C++ MPI program first, in the submission directory.' >&2
    exit 1
fi

echo "Slurm job ${SLURM_JOB_ID:-unknown}; requested MPI processes: ${SLURM_NTASKS:-4}"
# On an Open MPI + Slurm cluster, mpirun reads the job allocation.
# Some sites prefer srun; use it only with the site's supported MPI setup.
mpirun ./ch15_mpi_heat_strips
