Running many analyses: array jobs and GNU Parallel
Last updated on 2026-11-23 | Edit this page
Overview
Questions
- How do I run the same analysis many times with different inputs?
- Why does the cluster limit how many jobs I can submit, and what can I do about it?
- When is an array job a bad idea, and what is the alternative?
Objectives
- Write and submit an array job that uses the task number as the input.
- Follow, cancel and rerun individual tasks of an array.
- Explain what a QOS limit is, why it exists, and what to do if you need more.
- Explain why very short array tasks are a problem for the scheduler.
- Run many small tasks inside one Slurm job with GNU Parallel, with the right number of threads.
- Choose between the two approaches for a given workload.
Our researcher is now happy with the speed of one run. But a single
seed is not a study: she wants 100 seeds. Running
sbatch sweep-test.sh 1, then 2, then 3 by hand is tedious
and error-prone, and a for loop of sbatch
commands creates 100 unrelated jobs that you then have to track one by
one. Slurm has a better tool: the array job.
Array jobs
An array job is one submission that creates many
tasks. All tasks run the same script, and each one is told its
own number by an environment variable, SLURM_ARRAY_TASK_ID.
We use that number as the seed.
Here is our job script from the previous episodes. The changes are
the -o and -e file names, the
--array line, and the way the seed is chosen (explained
below the script).
BASH
#!/bin/bash --login
#SBATCH -J sweep-array
#SBATCH -o logs/%x.o.%A_%a
#SBATCH -e logs/%x.e.%A_%a
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=4
#SBATCH --mem=4G
#SBATCH -A <project-code>
#SBATCH -t 00:05:00
#SBATCH -p webmo_teaching
#SBATCH --array=1-4
set -euo pipefail
module purge
module load SciPy-bundle/2025.07-gfbf-2025b
export OMP_NUM_THREADS=${SLURM_CPUS_PER_TASK}
# the task number is the seed
SEED=${SLURM_ARRAY_TASK_ID}
# one folder for all the results, one work directory per task on scratch
# and a new directory to keep our log files
RESULTS_DIR=${SLURM_SUBMIT_DIR}/results
WORKDIR=/scratch/SCWFXXXXX/${USER}/sweep-array/${SLURM_ARRAY_JOB_ID}_${SLURM_ARRAY_TASK_ID}
LOGDIR=${SLURM_SUBMIT_DIR}/logs
mkdir -p "${RESULTS_DIR}" "${WORKDIR}" "${LOGDIR}"
cp "${SLURM_SUBMIT_DIR}/sweep_pdist.py" "${WORKDIR}"
cd "${WORKDIR}"
echo "Array ${SLURM_ARRAY_JOB_ID}, task ${SLURM_ARRAY_TASK_ID} (job ${SLURM_JOB_ID}), seed ${SEED}, node $(hostname)"
python sweep_pdist.py "${SEED}"
# copy back with a unique name, then clean up scratch
cp "result_${SEED}.csv" "${RESULTS_DIR}/result_${SEED}_${SLURM_JOB_ID}.csv"
cd "${SLURM_SUBMIT_DIR}"
rm -rf -- "${WORKDIR:?}"
Everything else is exactly what we wrote in the Protecting your data episode. The new ingredients are:
| Item | Meaning |
|---|---|
#SBATCH --array=1-4 |
create tasks numbered 1, 2, 3 and 4 |
SLURM_ARRAY_TASK_ID |
the number of this task |
SLURM_ARRAY_JOB_ID |
the number of the whole array, the same for every task |
SLURM_JOB_ID |
the job number of this task, different for every task |
%A and %a in file names |
the array job number and the task number |
Submit four tasks
OUTPUT
Submitted batch job 853400
OUTPUT
JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON)
860266_2 webmo_tea sweep-ar c.myuser R 0:05 1 cca0030
860266_3 webmo_tea sweep-ar c.myuser R 0:05 1 cca0030
860266_4 webmo_tea sweep-ar c.myuser R 0:05 1 cca0030
860266_1 webmo_tea sweep-ar c.myuser R 0:06 1 cca0030
The tasks are listed as
<array number>_<task number>. A task that is
still waiting is shown as PD, and a block of waiting tasks
can be folded into one line such as 853400_[3-4]. When they
have finished:
OUTPUT
JobID State Elapsed ExitCode
------------ ---------- ---------- --------
860266_1 COMPLETED 00:00:22 0:0
860266_2 COMPLETED 00:00:17 0:0
860266_3 COMPLETED 00:00:22 0:0
860266_4 COMPLETED 00:00:22 0:0
OUTPUT
results:
result_1_860267.csv result_2_860268.csv result_3_860269.csv result_4_860266.csv
logs:
sweep-array.e.860266_1 sweep-array.e.860266_3 sweep-array.o.860266_1 sweep-array.o.860266_3
sweep-array.e.860266_2 sweep-array.e.860266_4 sweep-array.o.860266_2 sweep-array.o.860266_4
The job numbers in the result names are examples: every task has its own, so no result can overwrite another. To put them all in one file for plotting:
Challenge: predict the names
You change the script to #SBATCH --array=10-12 and
submit it. The array gets the job number 853500.
- Which seeds will be run?
- What will the three log files for standard output be called?
- What does
squeueshow while the first of the three is running?
- 10, 11 and 12.
-
logs/sweep-array.o.853500_10,logs/sweep-array.o.853500_11andlogs/sweep-array.o.853500_12. - A line with
853500_10in theJOBIDcolumn in stateR, and the other two asPD(or folded into853500_[11-12]).
Managing an array
| Task | Command |
|---|---|
| Change the size without editing the script | sbatch --array=1-100 sweep-array.sh |
| Run only some tasks | sbatch --array=3,7,12 sweep-array.sh |
| Run at most 10 at the same time | sbatch --array=1-100%10 sweep-array.sh |
| Cancel the whole array | scancel 853400 |
| Cancel one task | scancel 853400_3 |
| Find failed tasks |
sacct -X -j 853400 --format=JobID,State,ExitCode and
look for anything but COMPLETED
|
| Rerun the failed ones | sbatch --array=3,7 sweep-array.sh |
Because each task has its own log files and its own result, a failure in one task does not affect the others, and rerunning only the failures is straightforward.
Scaling up: 100 seeds
The four tasks worked. Now the real thing:
Depending on how the limits are set, one of two things happens. Either the submission is refused at once:
OUTPUT
sbatch: error: QOSMaxSubmitJobPerUserLimit
sbatch: error: Batch job submission failed: Job violates accounting/QOS policy (job submit limit, user's size and/or time limits)
or it is accepted, but only some tasks run and the others stay pending with a reason that names the limit:
OUTPUT
JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON)
860340_[5-10] webmo_tea sweep-ar c.c10458 PD 0:00 1 (QOSMaxJobsPerUserLimit)
860340_1 webmo_tea sweep-ar c.c10458 R 0:10 1 cca0030
860340_2 webmo_tea sweep-ar c.c10458 R 0:10 1 cca0030
860340_3 webmo_tea sweep-ar c.c10458 R 0:10 1 cca0030
860340_4 webmo_tea sweep-ar c.c10458 R 0:10 1 cca0030
We have hit a QOS limit.
What is a QOS limit?
A QOS (quality of service) is a named set of rules attached to your jobs: the priority they get, and limits on what a user may have at once. The limits typically cover:
- the number of jobs you may have submitted (waiting and running) at any one time;
- the number you may have running at any one time;
- the total CPUs or memory you may use at once, and the longest time a job may ask for.
Each task of an array counts as a job, so an array of 100 tasks counts as 100.
You can look at the limits yourself:
BASH
sacctmgr show qos format=Name%20,GraceTime,PreemptMode,Flags%25,UsageThres,UsageFactor,MaxTRES%15,MaxWall,MaxTRESPU%15,MaxJobsPU,MaxSubmitPU
(The column names differ between Slurm versions.
sacctmgr show qos --help lists them.)
You can find more details about Falcon partitions and their limits in our documentation website.
Why do the limits exist?
They are there to protect the cluster, and all its users, from mistakes, including honest ones:
- A typo such as
--array=1-100000instead of1-100. - A loop or a script that submits jobs and, because of a bug, keeps submitting.
- A job that submits more jobs, and each of those does the same.
- One user’s thousands of waiting jobs pushing everybody else’s down the queue.
The scheduler has to keep track of every job: where it could run,
when, and in which order. It has a finite capacity, and when it is
overloaded, everything slows down for everybody, including commands such
as squeue and sbatch. The limits keep the
total number of jobs in a range the scheduler can handle.
What if I really need more?
The limits are not fixed for ever. They can be changed for a particular user once we know what you intend to do. Contact us and tell us:
- what the analysis is and roughly how many jobs it needs;
- how long each job runs, and how many CPUs and how much memory it needs;
- how many you want to have running at once;
- whether it is a one-off or a regular need, and whether the work could be organised into fewer, longer jobs (see below).
Do not work around the limit
A loop that submits a few jobs, waits, and submits a few more defeats the purpose of the limit and causes exactly the load the limit is meant to prevent. If you hit a limit, there is a better way: ask us, or reorganise the work, as described next.
Challenge: working under a limit
Suppose a user may have at most 50 jobs submitted at once, and needs to run 500 analyses of about 20 seconds each.
- Why does
--array=1-500%10not help? - Suggest two ways to get the work done without asking for a higher limit.
- What would you tell the support team if you did ask for a higher limit?
-
%10limits how many tasks run at once, but all 500 tasks are still submitted, so the limit on submitted jobs is still exceeded. - Make each task do more work: for example 50 array tasks of 10 seeds each (see the callout on batching below), or one job that runs all 500 with GNU Parallel (next section).
- What the analysis is, 500 runs of about 20 seconds each, the resources per run, and why the work could not be organised into fewer, longer jobs. Most likely the support team will suggest the batching or GNU Parallel approach anyway, for the reasons in the next section.
Short array tasks are a problem
Our example has a more important problem than the limit: each task runs for well under a minute. Look at one of the finished tasks and compare two numbers: the time Slurm says the job took, and the time the script measured for itself (the last column of its result file):
The difference is overhead: loading the module, starting Python, copying files to and from scratch. It is a few seconds per task here. Out of sight there is more: every task is a separate job that the scheduler must place, set up on a node, track and record in the accounting database, and clean up afterwards.
For a task that lasts an hour, all this is lost in the noise. For a task of 20 seconds it is a large share of the work, and it gets worse in bulk:
- Many short tasks put a load on the scheduler. Thousands of tasks that each live for seconds mean thousands of start-ups and clean-ups per hour. This can slow the scheduler for everybody, including people whose jobs are perfectly reasonable.
- They are wasteful. Each task pays the start-up cost again, and the CPUs sit idle while the node is set up and torn down.
- They can take longer overall. Each task waits in the queue separately, so 100 tiny jobs can take much longer to finish than one job that runs them all.
Our recommendation
Use array jobs for analyses that each take more than about an hour. For shorter tasks, group them into fewer, longer jobs (next sections). The 20-second tasks we use in this course are an illustration of the mechanics, and a bad fit for the real thing.
Challenge: measure the overhead
- For one of your four tasks, calculate the difference between
Elapsed(fromsacct) and the time the script reported. - What share of the task’s time is it?
- What would the share be for a task that really computes for one hour, if the overhead stays the same?
Your numbers will differ. As an example: if Elapsed is
21 s and the script reported 16 s, the overhead is 5 s, or about 24% of
the time. For a one-hour task with 5 s of overhead, the share is 5/3600,
or about 0.1%. The scheduler’s own work is not visible in these numbers,
but it grows with the number of jobs, not with how long
they run.
A middle way: several seeds per task
If you want to keep an array but your tasks are short, make each one do several seeds in a loop. With 10 seeds per task, 100 seeds need only 10 tasks:
BASH
#SBATCH --array=1-10
SEEDS_PER_TASK=10
FIRST=$(( (SLURM_ARRAY_TASK_ID - 1) * SEEDS_PER_TASK + 1 ))
LAST=$(( SLURM_ARRAY_TASK_ID * SEEDS_PER_TASK ))
for SEED in $(seq "${FIRST}" "${LAST}"); do
python sweep.py "${SEED}"
cp "result_${SEED}.csv" "${RESULTS_DIR}/result_${SEED}_${SLURM_JOB_ID}.csv"
done
The job count drops by a factor of 10, and the start-up cost is paid once per task.
Alternative: GNU Parallel
GNU Parallel is a command-line tool that runs many commands at the same time and keeps a fixed number going until the list is finished. Inside one Slurm job, it fills all the CPUs you were given with independent tasks. To Slurm this is a single job, so it creates no extra load on the scheduler, and no QOS limit on the number of jobs is involved.
Here is a script that runs 100 seeds, 16 at a time, on a single 16-CPU allocation:
BASH
#!/bin/bash --login
#SBATCH -J sweep-parallel
#SBATCH -o %x.o.%j
#SBATCH -e %x.e.%j
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=16
#SBATCH --mem=40G
#SBATCH -A <project-code>
#SBATCH -t 00:15:00
#SBATCH -p webmo_teaching
set -euo pipefail
module purge
module load SciPy-bundle/2025.07-gfbf-2025b
module load parallel # check the exact name with: module spider parallel
# number of seeds (override with: sbatch --export=ALL,NSEEDS=200 sweep-parallel.sh)
NSEEDS=${NSEEDS:-100}
# ONE thread per task, and as many tasks at once as there are CPUs
THREADS_PER_TASK=1
export OMP_NUM_THREADS=${THREADS_PER_TASK}
JOBS=$(( SLURM_CPUS_PER_TASK / THREADS_PER_TASK ))
RESULTS_DIR=${SLURM_SUBMIT_DIR}/results
WORKDIR=/scratch/SCWFXXXXX/${USER}/sweep-parallel/${SLURM_JOB_ID}
mkdir -p "${RESULTS_DIR}" "${WORKDIR}"
cp "${SLURM_SUBMIT_DIR}/sweep_pdist.py" "${WORKDIR}"
cd "${WORKDIR}"
echo "Job ${SLURM_JOB_ID}: ${NSEEDS} seeds, ${JOBS} at a time, ${THREADS_PER_TASK} thread(s) each, node $(hostname)"
# run the seeds. If some task fails, keep going, remember the status and report it at the end
PARALLEL_RC=0
seq 1 "${NSEEDS}" | parallel -j "${JOBS}" --joblog sweep.joblog python sweep_pdist.py {} || PARALLEL_RC=$?
# copy back whatever was produced, with the job number in the name
cp sweep.joblog "${RESULTS_DIR}/sweep_${SLURM_JOB_ID}.joblog"
for f in result_*.csv; do
[ -e "${f}" ] || continue
cp "${f}" "${RESULTS_DIR}/${f%.csv}_${SLURM_JOB_ID}.csv"
done
# clean up scratch only if everything worked
if [ "${PARALLEL_RC}" -eq 0 ]; then
cd "${SLURM_SUBMIT_DIR}"
rm -rf -- "${WORKDIR:?}"
else
echo "WARNING: ${PARALLEL_RC} task(s) failed, keeping ${WORKDIR}"
fi
echo "Finished in ${SECONDS} s"
exit "${PARALLEL_RC}"
The key line is:
-
seq 1 100prints the numbers 1 to 100, one per line. Parallel reads them as its input. -
{}is replaced by each input in turn, so it runspython sweep.py 1,python sweep.py 2, and so on. -
-j "${JOBS}"is how many to run at the same time. Always set it explicitly, because otherwise parallel may count every CPU of the node, not just the ones in your allocation. -
--joblogwrites one line per task, with its start time, run time and exit status. -
|| PARALLEL_RC=$?is the pattern from theset -esection: without it, one failed task would end the script before the results are copied back.
GNU Parallel’s exit status is the number of tasks that failed, so Slurm still sees a failed job if anything went wrong. To list the failures afterwards:
(Column 4 is the run time and column 7 is the exit status.) To rerun
only the failed tasks, use the same job log with
parallel --retry-failed.
Count the threads and the memory
Two numbers have to be right in a script like this, and they are the ones that go wrong.
Threads. Each task is a full copy of our script, and
numpy would start as many linear algebra threads as it can see. With 16
tasks at once and no limit, that is 16 × 16 = 256 threads fighting for
16 CPUs, which is slower than doing the tasks one at a time (see the
login node and 128-CPU stories from earlier episodes). Setting
OMP_NUM_THREADS=1 and running one task per CPU avoids it.
If a task is better with 2 threads, use THREADS_PER_TASK=2
and parallel runs 8 at a time.
Memory. --mem is for the whole
job, and 16 tasks run side by side. One task peaked at about
1.8 GiB in our profile, so 16 of them need about 29 GiB. We asked for 40
GB, which leaves a margin. Asking for the memory of one
task would get the job killed with OUT_OF_MEMORY.
Challenge: what does the efficiency say?
Run the Parallel script, and when it has finished:
- What is the CPU efficiency, and how does it compare with the 8-CPU single run from the efficiency episode?
- Compare
CPU Utilizedwith 100 times the CPU time of one single-CPU run. What does it tell you? - Why is the efficiency not exactly 100%?
- It should be much higher, because every CPU always has an independent task to run, with no serial bottleneck.
- They should be similar. Here, unlike the 128-CPU job, the busy CPUs are doing useful work, so the total CPU time matches the work and does not balloon.
- Some CPUs are idle at the beginning (copying files, loading the module), at the end when the last tasks finish while the others have none left to run, and during any serial start-up in each task.
Challenge: forget the thread setting
Run the Parallel script a second time with the line
export OMP_NUM_THREADS=${THREADS_PER_TASK} commented
out.
- Predict first: faster, slower or the same?
- Compare the run times (
sacct) andseff. What do you see?
Expect it to be slower, possibly much slower, because of
oversubscription: far more threads than CPUs. Whether seff
looks worse depends, as we saw earlier, on whether the extra threads are
idle or spinning, so look at the run time first. The exact effect
depends on how Slurm limits the CPUs and how numpy counts them, which is
a good reason to measure it.
Limits of GNU Parallel
- It uses one node: at most as many tasks at once as the node has CPUs. For more, combine it with an array (an array of a few large Parallel jobs), which is a good use of both.
- The allocation is held until the last task finishes. If task lengths vary a lot, CPUs sit idle at the end.
- A failed task does not affect the others, but you have to look in the job log to find it.
Which one should I use?
| Array job | GNU Parallel | |
|---|---|---|
| Jobs the scheduler sees | one per task | one |
| Best for | long tasks (over about an hour) | many short tasks |
| Counts against job limits | every task | one job |
| A task fails | rerun just that task by number | use the job log and --retry-failed
|
| Waiting in the queue | tasks can start as soon as space is free | the whole allocation has to be free at once |
| Spread over several nodes | automatically | no, one node |
Challenge: choose an approach
Which approach would you use for each, and why?
- 2,000 analyses of 30 seconds each, 1 CPU each.
- 40 simulations of 6 hours each, 8 CPUs each.
- 200 analyses of 10 minutes each, 1 CPU each.
- GNU Parallel (or a few array tasks that each run a batch of seeds, optionally with Parallel inside). 2,000 separate 30-second jobs would be a heavy load on the scheduler for almost no computation.
- An array job. The tasks are long, and each gets its own allocation.
- In between. Either GNU Parallel on a single 16 to 32 CPU job, or an array in which each task runs several analyses. Ten minutes is above the 20 seconds of our example, but still short enough for the start-up and the number of jobs to matter.
Quick reference
| Command | What it does |
|---|---|
sbatch --array=1-100 job.sh |
submit a 100-task array |
sbatch --array=1-100%10 job.sh |
at most 10 tasks running at once |
scancel <array id> or
scancel <array id>_<task>
|
cancel all, or one task |
sacct -X -j <array id> --format=JobID,State,ExitCode |
state of every task |
squeue -u $USER -t PD -o "%.12i %.10T %r" |
why jobs are pending |
sacctmgr show qos format=Name,MaxJobsPU,MaxSubmitPU,MaxTRESPU |
the QOS limits |
seq 1 N | parallel -j K --joblog log.txt cmd {} |
run cmd for 1 to N, K at a time |
Key Points
- An array job creates many tasks from one script.
SLURM_ARRAY_TASK_IDtells each task which one it is, and%A_%anames its log files. - Each task is a separate job: it can fail and be rerun on its own
(
--array=3,7), and it counts towards the job limits. - A QOS limits what a user can have submitted, running or in use at once. The limits protect the cluster from mistakes and overload, and can be changed for a user once we know the intended use.
- Many short tasks load the scheduler and waste time on start-up. Use array jobs for analyses of more than about an hour, and group shorter work into fewer, longer jobs.
- GNU Parallel runs many tasks inside one Slurm job. Set the number of
tasks at once (
-j), the threads per task and the total memory deliberately. - Choose by task length, number of tasks, differing resource needs and how you want failures handled.