Hello World Array

🔴 Red sticky = I need help. Put it up the moment you are stuck — an instructor will come to you.

🟢 Green sticky = I have done 1–4 and am moving on to the bonus work, and I am ready to help my table.

Day 2 arc — you are on step 5, scale (Part 2). profile document submit read logs scale (Part 2) debug 1 2 3 4 5

Everything on this page runs from your clone, with the environment active:

cd ~/yens-onboarding-2026
source .venv/bin/activate

Task: Submit slurm/hello_array.slurm unchanged, and confirm you got four tasks and four separate logs out of one submission.

The repo ships two scripts that do the same trivial thing — one as a single job, one as an array. Read them both:

cat slurm/hello.slurm
cat slurm/hello_array.slurm

You can spot the difference by eye, but let diff isolate it:

diff slurm/hello.slurm slurm/hello_array.slurm
📖 How to read diff output

diff tells you how to turn the first file into the second. Lines starting with < come from the first file (hello.slurm), lines starting with > from the second (hello_array.slurm), and --- separates the two sides.

The lines in between are line numbers, with a letter saying what happens there:

     
c change 2c2 — line 2 becomes line 2. 4,5c4,5 — lines 4–5 become lines 4–5.
a add 8a9 — after line 8 of the first file, add line 9 of the second. Only a > side, because there is nothing in the first file to show against it.
d delete Doesn’t appear here — nothing was removed.

One thing worth noticing: the last chunk is 12c13, not 12c12. Once a line has been added, the two files no longer agree on numbering — echo "Hello, world!" is line 12 of hello.slurm but line 13 of hello_array.slurm.

If two files are identical, diff prints nothing at all.

One directive is genuinely new — --array=0-3 — and that is what turns one job into four. The job name and the log paths are also different.

%j, %A and %a. Slurm substitutes these when it writes the log file. %j is the job ID, which is all a single job needs. An array needs two numbers: %A is the array’s job ID, the same for all four tasks, and %a is that task’s own index. So hello_%A_%a.out lands as hello_12345678_0.out, hello_12345678_1.out and so on — four separate files, rather than four tasks overwriting one.

Submit it:

mkdir -p logs
sbatch --reservation=class_cpu slurm/hello_array.slurm
squeue --me

You get one job ID back, but squeue shows four rows — 12345678_0 through 12345678_3. When they finish:

cat logs/hello_*_*.out

That one sbatch produced four logs, each printing a different task number.

One array script fans out into many tasks A single submission script with the directive array equals 0 to N minus 1 fans out into N independent tasks, numbered 0, 1, 2 and so on up to N minus 1. What each task does is determined by your code together with its array task ID. Slurm script --array=0–(N−1) task 0 determined by your code and SLURM_ARRAY_TASK_ID task 1 determined by your code and SLURM_ARRAY_TASK_ID task 2 determined by your code and SLURM_ARRAY_TASK_ID ⋮ task N−1 determined by your code and SLURM_ARRAY_TASK_ID One submission becomes N independent tasks, each with its own task ID.

The task number is what makes this general. Every task runs the identical script, and SLURM_ARRAY_TASK_ID is the only thing that differs between them — so wherever the work needs to vary, you derive it from that number: which file to read, which row of a list to process, which parameter value to try.

How a task's array index reaches your Python script Three stages. Slurm sets the variable SLURM_ARRAY_TASK_ID in every task's environment, here with the value 2. Your Slurm script reads it with bash and passes it to Python as a command-line argument. Your Python script reads that argument back out of sys.argv and converts it to an integer. Every task runs the same script, and only this number differs. Slurm sets one variable in every task's environment SLURM_ARRAY_TASK_ID=2 read by bash as $SLURM_ARRAY_TASK_ID your Slurm script passes it to Python as a command-line argument python scripts/extract_array.py "$SLURM_ARRAY_TASK_ID" arrives as the argument "2" your Python reads argument 1 back out of sys.argv task_id = int(sys.argv[1]) # 2 One task shown. Task 0 gets 0, task 1 gets 1 — same script, a different number each time.

The script could equally read the variable straight from its environment with os.environ["SLURM_ARRAY_TASK_ID"] and take no argument at all. Passing it in keeps the handover visible in the .slurm, and lets you run a single task by hand to test it. scripts/extract_array.py is the file you write on the next page — so this is what testing one task of it will look like, once you have it:

python scripts/extract_array.py 0

Page last modified: Sep 17, 2026.

Your details

Set these once and the commands on every page fill themselves in. Stored in this browser.

Built with Just the Docs.