Slurm job arrays are a mechanism for submitting and managing collections of similar jobs quickly and easily. Instead of submitting hundreds or thousands of individual jobs, you can use a job array to submit a single job script that will spawn multiple job tasks.
To create a job array, you use the --array
option in your Slurm batch script:
#SBATCH --array=1-100
This will create 100 job array tasks, numbered from 1 to 100.
#SBATCH --array=1-100:10
(1, 11, 21, ..., 91)#SBATCH --array=1,5,7,9
#SBATCH --array=1-5,10,20-25
Within your job script, you can use the $SLURM_ARRAY_TASK_ID
environment variable to distinguish between tasks:
#!/bin/bash
#SBATCH --array=1-100
echo "Processing file_${SLURM_ARRAY_TASK_ID}.txt"
./my_program input_${SLURM_ARRAY_TASK_ID}.dat output_${SLURM_ARRAY_TASK_ID}.result
#SBATCH --array=1-1000%20
to limit to 20 concurrently running tasks.%A
for array job ID and %a
for task ID in output file names:
#SBATCH --output=output_%A_%a.log
#!/bin/bash
#SBATCH --job-name=data_process
#SBATCH --output=output_%A_%a.log
#SBATCH --error=error_%A_%a.log
#SBATCH --array=1-100
#SBATCH --time=01:00:00
#SBATCH --mem=4G
# List of datasets
datasets=(dataset1.csv dataset2.csv dataset3.csv ... dataset100.csv)
# Get the dataset for this task
dataset=${datasets[$SLURM_ARRAY_TASK_ID - 1]}
# Run the processing script
python process_data.py $dataset
squeue -a
scancel [array_job_id]
scancel [array_job_id]_[task_id]
Job arrays are ideal for:
By using job arrays effectively, you can significantly streamline your workflow, especially when dealing with large numbers of similar computational tasks.