Recording multiple jobs#

In Recording a single job we saw that one recording wasn’t enough to fit a model. Here we record 13 runs of the same command in a single job, train on them, and get a prediction of how much memory would be required for a future run of this command.

We’re still using the perfectScaler command from the last lesson.

To follow along, cd into 02_multiple_jobs/.

Many runs in one job, and why --step-id#

The run_perfectScaler_loop.sbatch file runs perfectScaler 13 times, each with a different --intensity, and records each run straight after it finishes. Here is the head of this repetitive file:

run_perfectScaler_loop.sbatch#
#!/bin/bash
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1
#SBATCH --mem=10G
#SBATCH --time=00:10:00
#SBATCH --job-name=perfectScaler_loop
#SBATCH --output=out_slurm_logs/slurm-%x-%j.out

# 13 runs of perfectScaler, one after another in this one job. Each srun is a step
# of the job, so each record gets its own --step-id.

srun ../bin/perfectScaler --intensity 500
slurmise record --step-id 0 "perfectScaler --intensity 500"

srun ../bin/perfectScaler --intensity 1000
slurmise record --step-id 1 "perfectScaler --intensity 1000"

srun ../bin/perfectScaler --intensity 1500
slurmise record --step-id 2 "perfectScaler --intensity 1500"

#RB NOTE All 13 srun calls share one $SLURM_JOB_ID, because they are steps of the same job. Without something to tell them apart, slurmise would file all 13 recordings under the same key and each would overwrite the last. --step-id gives each record its own number.

The toml is lesson 01’s with defaults filled in for both resources:

slurmise.toml#
[slurmise]
base_dir = "."

[slurmise.job.perfectScaler]
job_spec = "--intensity {intensity}"

[slurmise.job.perfectScaler.runtime]
default = 30

[slurmise.job.perfectScaler.memory]
default = 5000

[slurmise.job.perfectScaler.variables]
intensity = {type = "numeric"}

Submit it#

mkdir -p out_slurm_logs
sbatch --wait run_perfectScaler_loop.sbatch

--wait doesn’t return until the job finishes, which here means all 13 steps — a minute or so. There’s also the wait time of the job queueing and starting.

bash mock_perfectScaler_loop.sh

Nothing is submitted. The mock makes the same 13 recordings with slurmise raw-record, each with its own --step-id. Again, this is a mock for the sake of the tutorial. In practice you would use srun with slurmise record.

mock_perfectScaler_loop.sh#
#!/bin/bash
# Instead of `record` we use `raw-record` and tell `slurmise` that we already
# ran 13 steps of a job with slurmid 12345, and know how much time and memory
# each one took, so no job submission occurs. perfectScaler uses exactly
# --intensity MB, and each step finishes within a second, which slurmise
# records as 0 minutes.

slurmise raw-record --job-name perfectScaler --slurm-id 12345 --step-id 0 \
    --numerics '"intensity":500' --used-mbs 500 --used-minutes 0

slurmise raw-record --job-name perfectScaler --slurm-id 12345 --step-id 1 \
    --numerics '"intensity":1000' --used-mbs 1000 --used-minutes 0

slurmise raw-record --job-name perfectScaler --slurm-id 12345 --step-id 2 \

Inspect#

After your job, or mock, runs of the perfectScaler command finish, each one should be recorded in the slurmise.h5 database:

slurmise print slurmise.h5 | head -30
perfectScaler
|--- 12345.0
|    |--- intensity: () int64 500
|    |--- memory: () int64 500
|    |--- runtime: () int64 0
|--- 12345.1
|    |--- intensity: () int64 1000
|    |--- memory: () int64 1000
|    |--- runtime: () int64 0
|--- 12345.10
|    |--- intensity: () int64 5500
|    |--- memory: () int64 5500
|    |--- runtime: () int64 0
|--- 12345.11
|    |--- intensity: () int64 6000
|    |--- memory: () int64 6000
|    |--- runtime: () int64 0
|--- 12345.12
|    |--- intensity: () int64 6500
|    |--- memory: () int64 6500
|    |--- runtime: () int64 0
|--- 12345.2
|    |--- intensity: () int64 1500
|    |--- memory: () int64 1500
|    |--- runtime: () int64 0
|--- 12345.3
|    |--- intensity: () int64 2000
|    |--- memory: () int64 2000
|    |--- runtime: () int64 0
|--- 12345.4

13 records, each with the intensity parameter it was given as well as the memory and runtime it used. Note the record ids: all 13 share one job id and differ only in the step after the dot, which is --step-id doing its work.

Here are the same records as a table, ordered by intensity.

record    intensity  memory
12345.0   500        500
12345.1   1000       1000
12345.2   1500       1500
12345.3   2000       2000
12345.4   2500       2500
12345.5   3000       3000
12345.6   3500       3500
12345.7   4000       4000
12345.8   4500       4500
12345.9   5000       5000
12345.10  5500       5500
12345.11  6000       6000
12345.12  6500       6500

Again, the table leaves runtime out on purpose since this command is unrealistically short and we’re just focusing on fitting memory in this tutorial.

Train#

slurmise update-all

Now that we have enough measurments, we can train a model to understand the resource requirements of the perfectScaler command.

update-all fits a model for every command in the database — here, just perfectScaler.

How this is working under the hood is that slurmise splits the recordings into test and train datasets and by default fits a 2nd order polynomial model on the training set. It then uses the held-out testing data to estimate the model accuracy.

The fitted model lands in a sub-directory as fits.json and a couple of .pkl files. Since a polynomial model is the current default then PolynomialFit/ folder is created.

Predict#

Ask for an intensity that was never run — 2750 sits between the 2500 and 3000 in the list:

slurmise predict "perfectScaler --intensity 2750"
Predicted runtime: 30
Predicted memory: 2750
Warnings:
  Predicted runtime for job perfectScaler is zero or negative (0).
  Run: slurmise update-model --job-name perfectScaler

Memory comes back at about 2750 MB — from the model now, not from the toml’s default of 5000. Compare that with lesson 01, where the same command returned the toml’s numbers unchanged whatever you asked for.

Runtime is still the toml’s default of 30 minutes, with a warning. Every record says 0 minutes, so the runtime model predicts 0, and slurmise falls back to the default rather than suggest a job that takes no time at all.

Using bash loops#

Writing every run out by hand keeps the script easy to read, but it doesn’t scale. For your own jobs, a loop inside the .sbatch does the same thing, with the loop counter as the --step-id:

intensities=(500 1000 1500 2000 2500 3000 3500 4000 4500 5000 5500 6000 6500)

for step in "${!intensities[@]}"; do
    intensity=${intensities[$step]}

    srun ../bin/perfectScaler --intensity "$intensity"
    slurmise record --step-id "$step" \
        "perfectScaler --intensity $intensity"
done

Using the Python API#

If your workflow already lives in Python, you can loop there instead and record through slurmise’s Python API rather than the slurmise command. Slurmise() finds slurmise.toml the same way the command does, and record reads the job id from $SLURM_JOB_ID just like slurmise record.

run_perfectScaler.py does the same 13 runs this way. sbatch will submit any script with a shebang, and reads #SBATCH lines from it as long as they come before the first line of code, so the Python file is its own job script:

run_perfectScaler.py#
#!/usr/bin/env python3
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1
#SBATCH --mem=10G
#SBATCH --time=00:10:00
#SBATCH --job-name=perfectScaler_python
#SBATCH --output=out_slurm_logs/slurm-%x-%j.out

# The same 13 runs as run_perfectScaler_loop.sbatch, looped in Python and recorded
# through slurmise's Python API. Submit it directly: sbatch --wait run_perfectScaler.py

import subprocess

from slurmise.api import Slurmise

slurmise = Slurmise()

intensities = [500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500, 6000, 6500]

for step, intensity in enumerate(intensities):
    subprocess.run(["srun", "../bin/perfectScaler", "--intensity", str(intensity)], check=True)
    slurmise.record(f"perfectScaler --intensity {intensity}", step_id=str(step))
mkdir -p out_slurm_logs
sbatch --wait run_perfectScaler.py

The python3 the shebang finds has to be one with slurmise installed. sbatch passes your environment along to the job, so activating that environment before you submit is enough.

Arrays for parallel recording, and a categorical feature does the same work a different way, and needs no --step-id at all — worth comparing once you get there.

perfectScaler is the easy case: it uses exactly the memory you ask it for, so there is a clean line to fit. A job whose memory doesn’t sit still runs the same loop with a job that isn’t so obliging.