SLURM `srun` vs `sbatch` and their parameters
Asked Answered
P

2

176

I am trying to understand what the difference is between SLURM's srun and sbatch commands. I will be happy with a general explanation, rather than specific answers to the following questions, but here are some specific points of confusion that can be a starting point and give an idea of what I'm looking for.

According to the documentation, srun is for submitting jobs, and sbatch is for submitting jobs for later execution, but the practical difference is unclear to me, and their behavior seems to be the same. For example, I have a cluster with 2 nodes, each with 2 CPUs. If I execute srun testjob.sh & 5x in a row, it will nicely queue up the fifth job until a CPU becomes available, as will executing sbatch testjob.sh.

To make the question more concrete, I think a good place to start might be: What are some things that I can do with one that I cannot do with the other, and why?

Many of the arguments to both commands are the same. The ones that seem the most relevant are --ntasks, --nodes, --cpus-per-task, --ntasks-per-node. How are these related to each other, and how do they differ for srun vs sbatch?

One particular difference is that srun will cause an error if testjob.sh does not have executable permission i.e. chmod +x testjob.sh whereas sbatch will happily run it. What is happening "under the hood" that causes this to be the case?

The documentation also mentions that srun is commonly used inside of sbatch scripts. This leads to the question: How do they interact with each other, and what is the "canonical" usecase for each them? Specifically, would I ever use srun by itself?

Polygamy answered 3/5, 2017 at 18:49 Comment(0)
C
207

The documentation says

srun is used to submit a job for execution in real time

while

sbatch is used to submit a job script for later execution.

They both accept practically the same set of parameters. The main difference is that srun is interactive and blocking (you get the result in your terminal and you cannot write other commands until it is finished), while sbatch is batch processing and non-blocking (results are written to a file and you can submit other commands right away).

If you use srun in the background with the & sign, then you remove the 'blocking' feature of srun, which becomes interactive but non-blocking. It is still interactive though, meaning that the output will clutter your terminal, and the srun processes are linked to your terminal. If you disconnect, you will loose control over them, or they might be killed (depending on whether they use stdout or not basically). And they will be killed if the machine to which you connect to submit jobs is rebooted.

If you use sbatch, you submit your job and it is handled by Slurm ; you can disconnect, kill your terminal, etc. with no consequence. Your job is no longer linked to a running process.

What are some things that I can do with one that I cannot do with the other, and why?

A feature that is available to sbatch and not to srun is job arrays. As srun can be used within an sbatch script, there is nothing that you cannot do with sbatch.

How are these related to each other, and how do they differ for srun vs sbatch?

All the parameters --ntasks, --nodes, --cpus-per-task, --ntasks-per-node have the same meaning in both commands. That is true for nearly all parameters, with the notable exception of --exclusive.

What is happening "under the hood" that causes this to be the case?

srun immediately executes the script on the remote host, while sbatch copies the script in an internal storage and then uploads it on the compute node when the job starts. You can check this by modifying your submission script after it has been submitted; changes will not be taken into account (see this).

How do they interact with each other, and what is the "canonical" use-case for each of them?

You typically use sbatch to submit a job and srun in the submission script to create job steps as Slurm calls them. srun is used to launch the processes. If your program is a parallel MPI program, srun takes care of creating all the MPI processes. If not, srun will run your program as many times as specified by the --ntasks option. There are many use cases depending on whether your program is paralleled or not, has a long-running time or not, is composed of a single executable or not, etc. Unless otherwise specified, srun inherits by default the pertinent options of the sbatch or salloc which it runs under (from here).

Specifically, would I ever use srun by itself?

Other than for small tests, no. A common use is srun --pty bash to get a shell on a compute job.

Carthage answered 5/5, 2017 at 7:56 Comment(9)
Thank you for the answer, this is better than anything I could have hoped for. One follow-up, as this was one of my original points of confusion: why bother to call srun inside the submission script? Perhaps I'm confused about the meaning of a "job step." For example, if I have a script called runjob.sh that contains #!/bin/bash srun myjob.sh, is there a practical difference between calling (a) sbatch runjob.sh vs (b) sbatch myjob.sh vs (c) srun myjob.sh vs (d) srun runjob.sh? (Clearly the last one is silly, but I'm curious).Polygamy
maybe you could browse the slides of a training session I delivered recently for ideas on how srun is used inside a submission script: cism.ucl.ac.be/Services/Formations/slurm/2016/slurm.pdfCarthage
It looks like all of the examples in the slides (as well as the tutorial on the CECI page) use srun inside of the sbatch submission script. However, I've found that commands without srun in the submission script will run the same way. Is there actually a difference between the four invocations I mentioned above?Polygamy
All your examples will run the same way only if (1) the allocation is for one CPU and (2) the program is purely sequential. To see differences, request more than one task. Another difference is that if you do not use srun in sbatch, the sstat command will not return any useful informationCarthage
How did you make slide 11, @damienfrancois? I mean, my #SBATCH --argument are not properly highlighted, and look like comments (in Vim). Do you have a custom theme?Stiffnecked
@Stiffnecked I use a custom Slurm syntax highlighting plugin: github.com/SchedMD/slurm/tree/master/contribs/…Carthage
@Stiffnecked this version might more uptodate: github.com/damienfrancois/slurm-helper/blob/master/slurm.vimCarthage
@Carthage thanks, your presentation looks great! It's available through the Web Archive today: web.archive.org/web/20220505010613/https://www.cism.ucl.ac.be/…Gemina
@YaroslavNikitenko thanks for your nice comment. You can fin an updated version here: indico.cism.ucl.ac.be/event/121/contributions/60/attachments/…Carthage
P
9

This doesn't actually fully answer the question, but here is some more information I found that may be helpful for someone in the future:


From a related thread I found with a similar question:

In a nutshell, sbatch and salloc allocate resources to the job, while srun launches parallel tasks across those resources. When invoked within a job allocation, srun will launch parallel tasks across some or all of the allocated resources. In that case, srun inherits by default the pertinent options of the sbatch or salloc which it runs under. You can then (usually) provide srun different options which will override what it receives by default. Each invocation of srun within a job is known as a job step.

srun can also be invoked outside of a job allocation. In that case, srun requests resources, and when those resources are granted, launches tasks across those resources as a single job and job step.

There's a relatively new web page which goes into more detail regarding the -B and --exclusive options.

doc/html/cpu_management.shtml


Additional information from the SLURM FAQ page.

The srun command has two different modes of operation. First, if not run within an existing job (i.e. not within a Slurm job allocation created by salloc or sbatch), then it will create a job allocation and spawn an application. If run within an existing allocation, the srun command only spawns the application. For this question, we will only address the first mode of operation and compare creating a job allocation using the sbatch and srun commands.

The srun command is designed for interactive use, with someone monitoring the output. The output of the application is seen as output of the srun command, typically at the user's terminal. The sbatch command is designed to submit a script for later execution and its output is written to a file. Command options used in the job allocation are almost identical. The most noticable difference in options is that the sbatch command supports the concept of job arrays, while srun does not. Another significant difference is in fault tolerance. Failures involving sbatch jobs typically result in the job being requeued and executed again, while failures involving srun typically result in an error message being generated with the expectation that the user will respond in an appropriate fashion.


Another relevant conversation here

Polygamy answered 5/5, 2017 at 16:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.