Job arrays
GNU parallel and the pool run every task inside one job, on one node. When there are more tasks than one node can get through in the time of one job, the scheduler can run them for you, each task as a job of its own, on whichever nodes are free.
One submission for many jobs
You may be tempted to submit them in a loop, on the login node:
for seed in $(seq 1 64); do
sbatch job.sh "$seed"
doneThat works, and gives you 64 jobs, each with its own job ID, to keep track of and cancel one by one. A job array is the same in one submission: one batch script, submitted once, which Slurm runs as many array tasks, each with its own index.
#!/bin/bash
#SBATCH --job-name=pi-array
#SBATCH --array=1-64%16
#SBATCH --output=pi-array-%A_%a.out
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1
#SBATCH --time=00:05:00
set -eo pipefail
source "$(conda info --base)/etc/profile.d/conda.sh"
conda activate "${CONDA_ENV:-pi}"
source ../pi/num-threads.sh 1
# This array task is one task of the problem, with its index as the seed.
mkdir -p results
python ../pi/pi.py count "$SLURM_ARRAY_TASK_ID" >"results/$SLURM_ARRAY_TASK_ID.txt"--array=1-64%16 makes 64 array tasks, with indices 1 to 64, and runs at most 16 of them at a time. The %16 is the throttle: without it, Slurm runs as many as there are free resources for, which is fine for 64, but with thousands, a throttle stops your array from taking over the cluster, or your share of it, and leaves room for your other jobs.1
Each array task runs the whole script, with the resources the script asks for, here 1 CPU, and with its own index in SLURM_ARRAY_TASK_ID. That index is all that tells the tasks apart, so it picks the task’s work: here it is the seed, and in your case it might pick the line of a task list, with sed -n "${SLURM_ARRAY_TASK_ID}p" tasks.txt.
Each task writes its result to results/<index>.txt. Its output goes to pi-array-%A_%a.out, where %A is the job ID of the array, and %a the index of the task, so each task gets its own file. Without %a in the name, the tasks would write to the same file, and overwrite each other’s output.
In squeue --me, the running tasks show up one per line, as <job ID>_<index>, and those still waiting all together on one line. scancel <job ID> cancels the whole array, and scancel <job ID>_<index> one task.
The reduce, after every task
The results still need adding up, and that has to wait until all 64 tasks are done. A job can wait for another with --dependency, so the reduce is a job of its own:
#!/bin/bash
#SBATCH --job-name=pi-reduce
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --time=00:02:00
set -eo pipefail
source "$(conda info --base)/etc/profile.d/conda.sh"
conda activate "${CONDA_ENV:-pi}"
python ../pi/pi.py reduce results/*.txtsubmitted together with the array by a script you run on the login node:
#!/bin/bash
# Run this on the login node: it submits the array, and the job that adds up
# its results, which starts only once every task of the array has succeeded.
set -eo pipefail
array="$(sbatch --parsable job.sh)"
array="${array%%;*}"
sbatch --dependency=afterok:"$array" reduce.shsbatch --parsable prints only the job ID, so the script can pass it on, and --dependency=afterok:<job ID> keeps the reduce job in the queue until that job has finished successfully. Given the job ID of an array, it waits for every one of its tasks.2 Its output ends with the same line as every other strategy:
pi = 3.141543 from 4294967296 samples
If a task fails, the dependency can never be satisfied, and the reduce job stays in the queue, waiting for something that won’t happen, until you cancel it (unless your cluster is set to cancel such jobs by itself). To find which tasks failed, sacct -j <job ID> -X --format=JobID,State,ExitCode lists every task with its state; once you’ve fixed the problem, rerun only those, e.g. sbatch --array=5,17 job.sh, since an option on the command line overrides the one in the script, then submit the reduce again.
A pipeline with more steps than one reduce is the subject of the page on workflows, later in this part.
The limit: overwhelming the scheduler
Each array task is a job, with everything that comes with one: the scheduler has to find room for it, start it, track it while it runs, and account for it after it ends. An array is cheaper for the scheduler than as many separate jobs, but 10,000 array tasks are still 10,000 jobs to schedule, and the scheduler is shared by everyone on the cluster. When it is overwhelmed, it is slow for all of them, squeue and sbatch included.
Each task also pays for itself. It waits in the queue, then starts from nothing: a new shell, activating the environment, starting Python and importing NumPy, all reading from the shared filesystem, the cost of activation multiplied by the number of tasks. For a task that takes an hour, none of that matters. For one that takes 10 seconds, it can be more than the task itself.
Clusters protect themselves with limits. MaxArraySize in Slurm’s configuration caps the indices of an array (by default, 1001, so indices up to 1000), which scontrol show config | grep -i MaxArraySize shows.
Many clusters also limit how many jobs one user can have in the queue, which a large array can run into, and which shows up in squeue as a pending reason such as QOSMaxSubmitJobPerUserLimit. And on a cluster that charges a project’s time as credits, Slurm may reserve credits for every queued task by its requested resources and --time, so a large array with a long --time can block new submissions until some of its tasks complete.
So an array is for tasks that are each worth a job: few enough, and long enough. When you have many short tasks, group them, so that each array task runs many of them.
An array of whole nodes, each running GNU parallel
Nothing prevents you from mixing them. An array where each task has a whole node to itself, and runs its share of the tasks with GNU parallel inside it, reduces the amount of spam to the scheduler to one job per node, while still going beyond one node:
#!/bin/bash
#SBATCH --job-name=pi-array-parallel
#SBATCH --array=1-2
#SBATCH --output=pi-array-parallel-%A_%a.out
#SBATCH --nodes=1
#SBATCH --exclusive
#SBATCH --time=00:05:00
set -eo pipefail
source "$(conda info --base)/etc/profile.d/conda.sh"
conda activate "${CONDA_ENV:-pi}"
source ../pi/num-threads.sh 1
# This array task's share of the 64 seeds: 1 to 32 for the first, 33 to 64 for the second.
share=$((64 / SLURM_ARRAY_TASK_COUNT))
first=$(((SLURM_ARRAY_TASK_ID - 1) * share + 1))
last=$((first + share - 1))
# Run them on the whole node, one per CPU.
mkdir -p results
seq "$first" "$last" | parallel --jobs "$SLURM_CPUS_ON_NODE" \
'python ../pi/pi.py count {} > results/{}.txt'--exclusive gives each array task a whole node, whatever its size, and SLURM_CPUS_ON_NODE is then the number of CPUs of that node, so GNU parallel runs one task per CPU.3 SLURM_ARRAY_TASK_COUNT is the number of array tasks, here 2, so the 64 seeds split into two shares of 32, the first array task taking 1 to 32 and the second 33 to 64. GNU parallel reads them from its input, made by seq.
The results go to the same results/<seed>.txt as before, so the reduce is the same job, submitted the same way:
#!/bin/bash
# Run this on the login node: the array, then the same reduce job as for the plain array.
set -eo pipefail
array="$(sbatch --parsable job.sh)"
array="${array%%;*}"
sbatch --dependency=afterok:"$array" ../pi-array/reduce.shThe sizes here are small, to keep the example quick: 2 nodes for 64 tasks of a second each is a waste of both. In real use, each array task’s share would be hundreds or thousands of tasks, sized so that one node gets through them in a few hours, and the array would be as many nodes as you want to use at a time.
On nodes of 144 cores, such as those with two 72-core NVIDIA Grace CPUs, an array of 10 whole nodes runs 1,440 tasks at a time, as 10 jobs, where a plain array would need 1,440 array tasks running at once to do the same.
The next page goes beyond one node from one Python script instead, with a pool of workers on MPI ranks.
Footnotes
Indices need not start at 1, or be consecutive:
--array=0-90:10is 0, 10, 20, …, 90, and--array=5,17is just those two, which is useful for rerunning tasks that failed, below.↩︎afterokis the strictest kind of dependency.afteranywaits for the tasks to finish whether or not they succeeded, andaftercorr, between two arrays, lets each task of the second start once the task with the same index in the first has succeeded. The Slurm documentation on job arrays has the details.↩︎If the nodes of your cluster present two logical CPUs per core, and Slurm counts those, use
--jobshalf that, or GNU parallel’s--use-cores-instead-of-threads, which makes its default of one task per CPU count physical cores instead. See the tip on physical cores.↩︎