Understand Slurm¶
This guide introduces the core Slurm entities — jobs, steps and tasks — and
puts them into practice: request an interactive allocation, run tasks on it
with srun, then reproduce the same example as a batch job with sbatch.
Before you begin¶
-
Obtain a Mila account, enable cluster access and MFA, install
uvandmilatools, configure SSH access and connect to the cluster for the first time.
What this guide covers¶
- Discovering Slurm jobs, steps and tasks
- Launching multiple tasks through an interactive job
- Launching multiple tasks from a script
Slurm concepts¶
Jobs, steps and tasks¶
The recurrent entities in Slurm are jobs, steps and tasks. As a simple mental model:
- a job can have multiple steps
- a step can run multiple tasks.
graph TD
J[Job] --> S1[Step]
J --> S2[Step]
S1 --> T1[Task]
S1 --> T2[Task]
S2 --> T3[Task]
S2 --> T4[Task]
See the technical reference for deeper information.
Login nodes and compute nodes¶
These concepts are explained in detail in What is a computer cluster?. In short, two types of nodes matter here:
| Type of node | Use |
|---|---|
| Login node | Used to connect to the cluster and manage jobs |
| Compute node | Where jobs run; provides the resources requested by a job |
Do not run jobs on login nodes
Login nodes are entry points to the cluster. Slurm commands (sbatch,
sinfo, squeue, etc.) can be called from there, but computing scripts
must be submitted through Slurm to obtain the requested resources, rather
than run directly on the login nodes.
Commands¶
This guide focuses on three Slurm commands:
| Command | Entity created | Description | Where to call it |
|---|---|---|---|
sbatch |
Batch Slurm job | Submit a batch script to Slurm | From a login node |
salloc |
Interactive job | Obtain a Slurm job allocation (a set of nodes), execute a command, and then release the allocation when the command is finished | From a login node |
srun |
Step | Run tasks | From a job |
Submitting tasks takes two steps:
- Request a resource allocation by submitting a job (
sbatchorsalloc). - Launch commands as tasks within this resource allocation (
srun).
Discover Slurm through an interactive job¶
An interactive job opens a shell directly on the allocated compute nodes.
Connect to the cluster with ssh mila, then request the allocation with
salloc.
Request an interactive allocation¶
Connect to the cluster:
Then request the allocation. The Slurm scheduler provides it once the resources are available:
salloc: --------------------------------------------------------------------------------------------------
salloc: # Using default long-cpu partition (CPU-only)
salloc: --------------------------------------------------------------------------------------------------
salloc: Pending job allocation 8888888
salloc: job 8888888 queued and waiting for resources
salloc: Granted job allocation 8888888
salloc: Nodes cn-f[001-002] are ready for job
Once the allocation is granted, Slurm reports the Job ID (8888888 in this example) and the nodes the allocation runs on (cn-f001 and cn-f002). The resource allocation is now ready.
What the allocation flags mean
--nodesmeans 2 nodes are requested for the tasks to run on--ntasks-per-nodemeans each node runs 2 tasks, sosruninvokes 4 tasks in total--memspecifies the real memory required per node.--mem-per-gpuor--mem-per-cpucan be used instead--timeasks for a 30-minute allocation. Setting it is good practice: an interactive job can last up to a week, and forgetting to leave one is a common mistake.
See the salloc documentation for more information.
Inspect where tasks run¶
Run the following in the salloc session opened above.
Running hostname reports where the process calling the command runs:
Run steps and tasks with srun:
Each task returned its own result for the hostname command.
In this example:
- two tasks ran on the node
cn-f001 - two tasks ran on the node
cn-f002
--ntasks: requesting a total instead of a shape
The same four tasks can be requested with --ntasks=4 --nodes=2, which
fixes only the total and lets Slurm spread the tasks unevenly — for
example, three tasks on one node and one on the other:
cn-f002.server.mila.quebec
cn-f002.server.mila.quebec
cn-f002.server.mila.quebec
cn-f001.server.mila.quebec
--ntasks-per-node is the safe default for deep learning and GPU jobs:
- each process selects its GPU based on its local rank, and a node that receives more tasks than GPUs breaks that binding
--memis allocated per node, so an extra task on a node shrinks the memory available to each task running there- the placement is identical on every run, which keeps failures reproducible
More details on srun and task placement
-
Note on the command:
srun hostnamefollows the formatsrun <command>.sruncan also take parameters, in the formatsrun <parameters> <command>. See srun documentation for more details.
-
Notes on the result:
- The
hostnamecommand ran four times because the allocation requests four tasks in total (2 nodes × 2 tasks per node). - The tasks on a node do not finish in a fixed order, so the node names may appear interleaved in the output.
- The
Launch a non-interactive job¶
This section reproduces the same example as before (same parameters and same
command, srun hostname) and submits the job through the sbatch command.
Connect to the cluster¶
Write the script¶
The content of job.sh is the same in both cases. It begins with the same
parameters used to request the interactive allocation:
The batch script itself runs once, on the first allocated node; the srun
call launches the four tasks, exactly as in the interactive example.
Submit the job¶
In the VSCode integrated terminal (or a login-node terminal), submit the job:
Once submitted, the job waits to be scheduled. Its status is shown by the
squeue command:
The allocation is requested by sbatch based on the script parameters. Once it
is ready, the script runs automatically (the job is running), and the allocation
is freed at the end of the job.
Retrieve the results¶
Once the job is finished, its output is available in the file
slurm-<JOB_ID>.out (here, slurm-9321166.out). The file name can be changed
with the --output
parameter.
The output in this example is:
Key concepts¶
- Job
- A resource allocation on the cluster, together with the steps that run
inside it. Created by
sbatchorsalloc. - Step
- A stage within a job, created by a call to
srun. A job can contain multiple steps, and each step can launch multiple tasks. - Task
- One instance of a command run by a step on part of the job's allocation. A step can run multiple tasks.
mila-cpu- SSH remote added by
mila initthat auto-allocates a CPU compute node for light editing from the VSCode Remote-SSH dropdown.
Next step¶
With a job submitted through salloc or sbatch, the next step covers how to
follow it through the queue, inspect and cancel it, and resolve common failures.
-
Track jobs through the queue, inspect and cancel them, read their output, and resolve common failures.