Array Jobs

Array jobs come in handy if you have a lot of small independent jobs, e.g. a parameter study.

Consider a program called simulation, which will analyse datasets in your /data/<your-group>/<user>/datasets folder. The datasets are in the form

.
├── dataset-1.dat
├── dataset-2.dat
├── dataset-3.dat
..
└── dataset-10.dat

Each simulation run needs two cores and will independently analyse one dataset (1-10). Instead of writing 10 job files, you can use an array job:

#!/bin/bash
#SBATCH -J ArrayJob
#SBATCH -c 2
#SBATCH -p small_cpu
#SBATCH --mail-type=ALL
#SBATCH --mail-user=<your-email-address>

# Set Array Job Options
#SBATCH --output=%x_%A_%a.out
#SBATCH --error=%x_%A_%a.err

# Define number of subjobs
#SBATCH --array=1-10

echo "Running Task: ${SLURM_ARRAY_TASK_ID} on host $(hostname)"
simulation -c ${SLURM_CPUS_PER_TASK} ~/data/<your-group>/<user>/datasets/dataset-${SLURM_ARRAY_TASK_ID}.dat

After you submitted the job, you will get a job id like: 50993848 Since we set --output=%x_%A_%a.out and --error=%x_%A_%a.err we will get an output and error file for each task of the job in the form

ArrayJob_50993848_1.out
ArrayJob_50993848_1.err
ArrayJob_50993848_2.out
ArrayJob_50993848_2.err
...
ArrayJob_50993848_10.out
ArrayJob_50993848_10.err

The %x will be replaced with the job name (see #SBATCH -J). %A will result in SLURM_ARRAY_JOB_ID which should also be the SLURM_JOB_ID and %a results in SLURM_ARRAY_TASK_ID.

Slurm will schedule 10 jobs which can also run in parallel, instead of one after one other, if there are enough free resources. Check the SLURM Documention on array jobs for more information.