SBATCH Script Layout
How to structure job submission scripts for optimal results
A well-structured SBATCH script follows a consistent layout that makes it readable, maintainable, and less prone to errors. This guide shows you the recommended structure and best practices.
Standard Script Structure
Every SBATCH script should follow this basic structure:
Shebang Line
Always start with the interpreter directive
SBATCH Directives
Resource requirements and job configuration
Environment Setup
Module loading and variable definitions
Job Execution
The actual commands to run
Complete Script Template
#SBATCH --job-name=my_job
#SBATCH --output=results_%j.out
#SBATCH --error=results_%j.err
# Resource allocation
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=40
#SBATCH --mem=64GB
#SBATCH --time=02:00:00
# Queue Selection
#SBATCH -p short-40core
module purge
module load intel/oneAPI/2022.2
module load compiler/latest
module load mpi/latest
# Set environment variables
export OMP_NUM_THREADS=1
export I_MPI_PIN_DOMAIN=omp
# Display job info
echo "Job ID: $SLURM_JOBID"
echo "Running on nodes: $SLURM_JOB_NODELIST"
echo "Number of tasks: $SLURM_NTASKS"
echo "Start time: $(date)"
cd $SLURM_SUBMIT_DIR
# Compile application (if needed)
mpiicc -O3 -o my_app my_source.c
# Run the application
echo "Starting computation..."
mpirun ./my_app input.dat > computation.log
# Post-processing (if needed)
echo "Job completed at: $(date)"
SBATCH Directive Best Practices
Group Related Directives
Organize directives logically with comments:
#SBATCH --job-name=protein_folding
#SBATCH --output=folding_%j.out
#SBATCH --error=folding_%j.err
# Resource requirements
#SBATCH --nodes=4
#SBATCH --ntasks-per-node=40
#SBATCH --mem-per-cpu=2GB
#SBATCH --time=12:00:00
# Job placement
#SBATCH -p long-40core
Use Descriptive Names
Make output files easy to identify:
#SBATCH --output=simulation_%x_%j.out
#SBATCH --error=simulation_%x_%j.err
# %x = job name, %j = job ID
Include All Required Resources
Be explicit about your needs:
#SBATCH --nodes=2
#SBATCH --ntasks-per-node=28
#SBATCH --cpus-per-task=1
#SBATCH --mem=120GB # For shared partitions
#SBATCH --time=06:30:00
#SBATCH --gres=gpu:2 # For GPU jobs
Environment Setup Guidelines
Always Purge Modules First
This ensures a clean environment and prevents module conflicts.
Set Important Variables
export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK
# MPI settings
export I_MPI_PIN_DOMAIN=omp
export I_MPI_PIN_PROCESSOR_LIST=allcores
# Application-specific variables
export TMPDIR=/tmp/$USER/$SLURM_JOBID
Add Job Information
echo "========================================="
echo "Job ID: $SLURM_JOBID" echo "Job Name: $SLURM_JOB_NAME"
echo "Nodes: $SLURM_JOB_NODELIST"
echo "CPUs per task: $SLURM_CPUS_PER_TASK"
echo "Tasks per node: $SLURM_NTASKS_PER_NODE"
echo "Working directory: $(pwd)"
echo "Start time: $(date)"
echo "========================================="
Job Execution Best Practices
Change to Submit Directory
Ensures your job runs from the directory where you submitted it.
Add Error Checking
Include Timing and Logging
Common Script Types
CPU-Only Job
#SBATCH --job-name=cpu_analysis
#SBATCH --nodes=2
#SBATCH --ntasks-per-node=40
#SBATCH --time=04:00:00
#SBATCH -p short-40core
module load intel/oneAPI/2022.2
cd $SLURM_SUBMIT_DIR
mpirun ./analysis_tool
GPU Job
#SBATCH --job-name=gpu_training
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=28
#SBATCH --gres=gpu:1
#SBATCH --time=08:00:00
#SBATCH -p gpu
module load anaconda/3 cuda/11.8
source activate my_env
cd $SLURM_SUBMIT_DIR
python train_model.py
Array Job
#SBATCH --job-name=parameter_sweep
#SBATCH --array=1-100
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=40
#SBATCH --time=02:00:00
#SBATCH -p short-40core
module load python/3.9
cd $SLURM_SUBMIT_DIR
python simulation.py --param-set $SLURM_ARRAY_TASK_ID
Remember: Always test your scripts with small resource requests first. Use interactive sessions to debug before submitting large batch jobs.
Pro Tip: Keep a template script for each type of job you commonly run. This saves time and reduces errors when submitting new jobs.