Useful Slurm features
This page describes Slurm features that can save you time. For the options themselves, see sbatch options. For more detail, see the Slurm documentation, or man sbatch. COSMA runs Slurm 25.11.
Job arrays
A job array runs many similar tasks from one script, which is much better than submitting them one at a time. Each task receives its number in SLURM_ARRAY_TASK_ID:
#!/bin/bash -l
#SBATCH -J myarray
#SBATCH -o myarray.%A_%a.out
#SBATCH -p cosma8-serial
#SBATCH -A PROJECT
#SBATCH --cpus-per-task=1
#SBATCH --mem=1G
#SBATCH -t 01:00:00
#SBATCH --array=1-100%10
./myprogram input_${SLURM_ARRAY_TASK_ID}.dat
--array=1-100%10 runs tasks 1 to 100, with at most 10 running at once. You can also use lists, such as --array=1,5,9, or steps, such as --array=0-100:10. Task numbers can go up to 1000.
If your inputs do not have numbers in their names, list them in a file, and pick a line in the script:
FILE=$(sed -n "${SLURM_ARRAY_TASK_ID}p" inputs.txt)
./myprogram "$FILE"
To cancel a single task, use scancel JOBID_TASK, for example scancel 12263079_3.
Running jobs in order
A dependency makes one job wait for another. This is useful for a simulation followed by analysis, or for a run that is split into stages:
FIRST=$(sbatch --parsable simulate.sh)
sbatch --dependency=afterok:$FIRST analyse.sh
Dependency |
The job starts |
|---|---|
|
After the job finishes successfully |
|
After the job fails |
|
After the job ends, however it ended |
|
After the job starts |
|
After all of your jobs with the same name have ended |
If the first job fails, an afterok job can never run, and stays in the queue. Add --kill-on-invalid-dep=yes to have it cancelled instead.
Saving work before the time limit
Slurm stops your job when it reaches its time limit. To write a checkpoint first, ask Slurm to send a signal to your script:
#!/bin/bash -l
#SBATCH -p cosma8-serial
#SBATCH -A PROJECT
#SBATCH --signal=B:USR1@300
#SBATCH -t 12:00:00
trap 'echo "Saving"; touch stop_now; wait; exit 0' USR1
./myprogram &
wait
--signal=B:USR1@300 sends the USR1 signal around 5 minutes before the time limit. It can arrive up to 60 seconds earlier than requested.
The B: sends the signal to the batch script itself, which needs to run your program in the background with & followed by wait, otherwise it cannot handle the signal until your program ends. What the script does next depends on your program. This example creates a file, which your program would need to check for, then waits for your program to finish. The exit 0 stops Slurm from recording the job as failed.
Changing a job after submitting it
Task |
Command |
|---|---|
Hold a pending job |
|
Release it |
|
Shorten its time limit |
|
Change its name |
|
Cancel all of your jobs |
|
Cancel your jobs by name |
|
Monitoring jobs
Task |
Command |
|---|---|
See your jobs |
|
See estimated start times |
|
See everything about a job |
|
See memory use of a running job |
|
See finished jobs since a date |
|
See a job’s priority |
|
See the queues |
|
See a node’s details |
|
sstat only works while a job is running. See also cjobload for how a running job is using its nodes.
Variables in job scripts
Slurm sets these variables for your job:
Variable |
Meaning |
|---|---|
|
The job number |
|
The job name |
|
The queue |
|
The project |
|
The nodes allocated to the job |
|
The number of nodes |
|
The number of tasks |
|
CPUs for each task, if you set it |
|
CPUs allocated on this node |
|
The directory that you submitted the job from |
|
The task number in an array |
|
The job number of an array |
Interactive jobs
To get a shell on a compute node:
srun -p cosma8-serial -A PROJECT -c 4 --mem=8G -t 01:00:00 --pty /bin/bash
Please exit as soon as you have finished. An idle interactive job wastes resources, and your project is charged for it. On queues that give whole nodes, your project is charged for the whole node.
What is not available on COSMA
seffis not installed. Usesacctinstead: see when a job goes wrong.scrontab, for scheduled jobs, is disabled.Jobs are not restarted after a node failure unless you use
--requeue.