Tutorials/Slurm-Interactive-Srun: Difference between revisions
m Honfi001 moved page Tutorials/slurm-interactive-srun to Tutorials/Slurm-Interactive-Srun without leaving a redirect |
mNo edit summary |
||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
= Interactive Slurm, Part 1: srun = | = Interactive Slurm, Part 1: srun = | ||
''Part 1 of the '''Interactive Slurm''' series for the HPC Advanced course. Part 2 covers interactive shells (<code>srun --pty bash</code> and <code>sinteractive</code>), and Part 3 covers <code>salloc</code>.'' | ''[[Tutorials/Slurm-Interactive-Shells|Part]] 1 of the '''Interactive Slurm''' series for the HPC Advanced course. [[Tutorials/Slurm-Interactive-Shells|Part 2]] covers interactive shells (<code>srun --pty bash</code> and <code>sinteractive</code>), and [[Tutorials/Slurm-Interactive-Salloc|Part 3]] covers <code>salloc</code>.'' | ||
In the [[Tutorials/Slurm-Interactive-Sessions|beginner tutorial]] you met <code>srun</code> as the command hiding inside <code>sinteractive</code>. This series turns that around and starts with <code>srun</code>, because once you understand what it does, everything else in interactive Slurm is a variation on it. | In the [[Tutorials/Slurm-Interactive-Sessions|beginner tutorial]] you met <code>srun</code> as the command hiding inside <code>sinteractive</code>. This series turns that around and starts with <code>srun</code>, because once you understand what it does, everything else in interactive Slurm is a variation on it. | ||
| Line 182: | Line 182: | ||
== Next == | == Next == | ||
So far every <code>srun</code> has run one command and handed the allocation straight back. In '''Part 2''' you will use <code>srun</code> to open a whole interactive shell on a compute node, and see that <code>sinteractive</code> is exactly that, with a few options filled in. | So far every <code>srun</code> has run one command and handed the allocation straight back. In '''[[Tutorials/Slurm-Interactive-Shells|Part 2]]''' you will use <code>srun</code> to open a whole interactive shell on a compute node, and see that <code>sinteractive</code> is exactly that, with a few options filled in. | ||
== See also == | == See also == | ||
Latest revision as of 17:50, 23 November 2026
Interactive Slurm, Part 1: srun
Part 1 of the Interactive Slurm series for the HPC Advanced course. Part 2 covers interactive shells (srun --pty bash and sinteractive), and Part 3 covers salloc.
In the beginner tutorial you met srun as the command hiding inside sinteractive. This series turns that around and starts with srun, because once you understand what it does, everything else in interactive Slurm is a variation on it.
By the end of this page you will be able to:
- run a command on a compute node and see its output straight away;
- give one program several cores;
- run several copies of a program, on one node or several;
- tell
-c,-nand-Napart, and know which one your program needs.
You need: an Anunna account, and to have done the beginner tutorial or to be comfortable with sinteractive.
Time: about 30 minutes.
What srun actually does
Think of the cluster as a restaurant. The login node is the front desk: you can stand there and talk, but nobody cooks for you there. To eat, you need a table, and then you order dishes at it.
In Slurm terms:
- the table is an allocation: a set of cores, memory and time on one or more compute nodes, reserved for you;
- each dish is a job step: one program, started inside that allocation.
When you type srun on the login node, it does both in one go. It books a table that fits, orders one dish, waits for it, and gives the table back as soon as you have finished. srun outside a job means "one allocation plus one step". Keep that sentence in mind. Part 3 of this series is about pulling those two halves apart.
Step 1: Hello from a compute node
The classic first command prints the name of the machine it runs on:
srun --time=00:01:00 hostname
Here is what came back when we ran it:
srun: job 44376006 queued and waiting for resources
srun: job 44376006 has been allocated resources
node234
Read it line by line:
- Slurm gave your request a job ID (
44376006) and put it in the queue. This is normal. Even a one-second job waits its turn, and how long depends on how busy the cluster is. - Resources were found: your table is ready.
hostnameran on the compute node and printed its name. You were on a login node the whole time, but the command was not.
Then you are back at your prompt, and the allocation is already gone. Your job ID and node name will be different.
Always give a time limit. --time=00:01:00 asks for one minute. A short, honest time limit helps Slurm fit your job in sooner, and it protects you if something hangs. Every example on this page has one, and yours should too.
Step 2: One program, several cores
hostname is fine for "where am I?", but it cannot tell you how many cores you have, because it prints a name, not a count. For that, use nproc, which reports how many cores the current process is allowed to use:
srun --time=00:01:00 -c 4 nproc
srun: job 44376007 queued and waiting for resources
srun: job 44376007 has been allocated resources
4
-c 4 is short for --cpus-per-task=4: one copy of the program, with four cores to use. This is what you want for a threaded program, one that can use several cores from a single process. Many bioinformatics tools work this way, with an option like --threads, -t or -@.
Asking the right question about cores
Slurm gives you four cores by limiting which cores your process may run on. The rest of the node is still visible, so some tools answer the question "how many cores does this machine have?" rather than "how many cores may I use?".
| Command | What it reports in a job |
|---|---|
nproc |
your cores (4) |
Python len(os.sched_getaffinity(0)) |
your cores (4) |
Python os.cpu_count() |
the whole node |
getconf _NPROCESSORS_ONLN |
the whole node |
If a program sizes its thread pool from the whole node, it will start far more threads than you have cores, and they will fight over the few you were given. Tell your tool how many threads to use explicitly. Inside a job, $SLURM_CPUS_PER_TASK holds the number you asked for with -c.
Step 3: Several copies of a program
Now ask for four tasks instead of four cores:
srun --time=00:01:00 -n 4 hostname
srun: job 44376011 queued and waiting for resources
srun: job 44376011 has been allocated resources
node234
node234
node234
node213
-n 4 is short for --ntasks=4: run four separate copies of the program, each with one core. So hostname ran four times and printed four lines. The lines can arrive in any order, because the copies run at the same time.
Look at the node names. Three copies ran on node234 and one on node213. We did not ask for two nodes. -n says how many copies you want and nothing about where. Slurm is free to spread them over whichever nodes have room. If your tasks must share one node, say so with -N 1.
Step 4: Several nodes
To ask for nodes explicitly, use -N (--nodes):
srun --time=00:01:00 -N 2 -n 2 hostname
srun: job 44376012 queued and waiting for resources
srun: job 44376012 has been allocated resources
node213
node234
Two nodes, one copy on each.
More nodes only help programs that are written to talk to each other across nodes, which in practice means MPI programs. A normal program, even a threaded one, lives inside a single node and cannot use the cores of another. Giving it -N 2 just means it runs twice, or that half your request sits idle. If you are not sure whether your program uses MPI, it almost certainly does not, and you want one node.
The classic mistake: -n 16 when you meant -c 16
Say you have a threaded program and want it to use 16 cores. It is very easy to type:
srun --time=01:00:00 -n 16 mytool --threads 16 input.fa # wrong
That asks for 16 copies of mytool, each with one core, possibly on different nodes. Each copy then tries to start 16 threads on its single core, and all 16 copies work on the same input and write to the same output. What you meant was:
srun --time=01:00:00 -c 16 mytool --threads 16 input.fa # right
One copy, 16 cores, 16 threads.
| Option | Long form | Means | Use it for |
|---|---|---|---|
-c N |
--cpus-per-task=N |
one copy, N cores | threaded programs (most tools) |
-n N |
--ntasks=N |
N copies, one core each, anywhere | MPI programs; running the same command many times |
-N N |
--nodes=N |
spread over N nodes | MPI programs that need more than one node |
The rule of thumb: threads mean -c, copies mean -n, and -N is for MPI.
Try it yourself
- Run
srun --time=00:01:00 hostnamea few times. Do you always land on the same node? - Run
srun --time=00:01:00 -c 2 nproc, thensrun --time=00:01:00 -n 2 nproc. Explain why the outputs differ. - Run
srun --time=00:01:00 -N 1 -n 4 hostnameand compare it with Step 3. - Run
srun --time=00:01:00 -c 4 python3 -c "import os; print(os.cpu_count(), len(os.sched_getaffinity(0)))". Which number is the truth about your job?
Cheat sheet
| You want to... | Type |
|---|---|
| run one command on a compute node | srun --time=00:05:00 command
|
| give a threaded program 8 cores | srun --time=01:00:00 -c 8 command
|
| run 4 copies of a command | srun --time=00:05:00 -n 4 command
|
| keep those copies on one node | srun --time=00:05:00 -N 1 -n 4 command
|
| run an MPI program on 2 nodes | srun --time=01:00:00 -N 2 -n 2 command
|
| check how many cores you really have | nproc
|
Next
So far every srun has run one command and handed the allocation straight back. In Part 2 you will use srun to open a whole interactive shell on a compute node, and see that sinteractive is exactly that, with a few options filled in.
See also
- Interactive sessions — the beginner tutorial this series builds on
- Interactive Jobs — reference page for
sinteractiveandsalloc - Scheduler Overview (Slurm)