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.
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.slurmunchanged, 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,%Aand%a. Slurm substitutes these when it writes the log file.%jis the job ID, which is all a single job needs. An array needs two numbers:%Ais the array’s job ID, the same for all four tasks, and%ais that task’s own index. Sohello_%A_%a.outlands ashello_12345678_0.out,hello_12345678_1.outand 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.
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.
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.pyis 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