SLURM

From CSCWiki
Revision as of 20:09, 19 November 2026 by S23adhik (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Why

Slurm (Simple Linux Utility for Resource Management) is a job scheduler. Use it when too many people are trying to use a machine at the same time and it keeps crashing, or when you want to share resources (especially GPUs) more fairly.

On tiramisu the two NVIDIA RTX PRO 6000 Blackwell GPUs can only be used through Slurm. Outside a job, nvidia-smi prints Failed to initialize NVML: Insufficient Permissions. That is expected: you get access to a GPU when your job starts, and lose it when the job ends.

Rules on tiramisu

What Limit
Running jobs per user 1 (others wait in the queue, reason QOSMaxJobsPerUserLimit)
GPUs per job 1 whole GPU (--gres=gpu:1), or a slice of one GPU with MPS (--gres=mps:N, N ≤ 100 = N% of one GPU)
Both GPUs 2 GPUs (--gres=gpu:2) for at most 1 hour
No GPU requested You get --gres=mps:25 (25% of one GPU) automatically
Time limit Default 4 hours, maximum 12 hours (2-GPU jobs: default and maximum 1 hour)
CPUs 96 of the 128 CPUs are available to Slurm (the rest are kept for normal use of the machine). A whole-GPU job gets 16 CPUs by default
Memory 2 GB per CPU by default (--mem / --mem-per-cpu to change)
Priority Fair-share: the more GPU time you used recently, the lower your priority. A 2-GPU job counts double

You can't combine gpu and mps in the same job. If a request breaks a rule, it is rejected at submit time with a message starting with tiramisu: that explains why.

Every current CSC member is registered automatically (synced from LDAP every hour). If you just renewed your membership and get Invalid account, wait up to an hour.

Interactive sessions

Interactive jobs follow the same rules as batch jobs: they count as your one running job, and they end when the time limit is reached.

Shell on a GPU (recommended)

# one whole GPU for 2 hours
srun --gres=gpu:1 --time=2:00:00 --pty bash

# 25% of one GPU (the default if you leave out --gres), 1 hour
srun --gres=mps:25 --time=1:00:00 --pty bash

# both GPUs (the time is set to 1 hour automatically, which is also the maximum)
srun --gres=gpu:2 --pty bash

# more CPUs/RAM
srun --gres=gpu:1 --cpus-per-task=16 --mem=64G --time=3:00:00 --pty bash

When the prompt comes back you are inside the job: nvidia-smi shows your GPU(s). Type exit to end the job and free the GPU.

Run it inside tmux or screen. If your SSH connection drops, srun and your job are killed. To keep it running:

tmux new -s gpu
srun --gres=gpu:1 --time=4:00:00 --pty bash
# detach: Ctrl-b d      reattach later: tmux attach -t gpu

salloc (allocate first, run steps later)

salloc --gres=gpu:1 --time=2:00:00
srun --pty bash          # now you are in the job, on the GPU
# ... work, exit the inner shell, run more srun steps ...
exit                     # leave salloc: this ends the job and frees the GPU

The shell salloc gives you is not inside the job (CUDA_VISIBLE_DEVICES is not set there). Always start work with srun inside it, or just use srun --pty bash directly as shown above.

Jupyter

On tiramisu (inside tmux), with a port number of your choice so you don't collide with others:

PORT=$((20000 + RANDOM % 10000)); echo $PORT
srun --gres=gpu:1 --time=3:00:00 --pty jupyter lab --no-browser --ip=127.0.0.1 --port=$PORT

On your laptop:

ssh -N -L 8888:127.0.0.1:<PORT> <user>@tiramisu.csclub.uwaterloo.ca

Then open the http://127.0.0.1:8888/...?token=... link Jupyter printed, replacing its port with 8888. Stop Jupyter (Ctrl-C twice) when you are done so the GPU is freed. Don't leave it idle until the time limit.

Getting a shell inside a job that is already running

Because you can only run one job at a time, a new srun waits while your batch job runs. To look at a running job (e.g. run nvidia-smi or htop on its GPU), attach to it instead:

srun --jobid=<JOBID> --overlap --pty bash

Batch jobs

Job config

  • --gres=gpu:1 one whole GPU; --gres=gpu:2 both GPUs (max 1 hour); --gres=mps:N N% of one GPU
  • --time time limit (e.g. 30:00, 4:00:00); ask for what you need, since shorter jobs start sooner
  • --ntasks the number of tasks (usually 1)
  • --cpus-per-task the number of CPUs per task
  • --mem-per-cpu RAM per CPU, or --mem total RAM (does NOT depend on CPU count)
  • --job-name, --output (default slurm-<jobid>.out in the directory you submitted from)

Example: one GPU

#!/bin/bash
#SBATCH --job-name=my_test
#SBATCH --gres=gpu:1
#SBATCH --cpus-per-task=8
#SBATCH --mem=32G
#SBATCH --time=4:00:00
#SBATCH --output=%x-%j.out

nvidia-smi
python train.py

Submit with sbatch job.sh.

Example: both GPUs (max 1 hour)

#!/bin/bash
#SBATCH --job-name=two_gpu
#SBATCH --gres=gpu:2
#SBATCH --time=1:00:00             # more than 1:00:00 is rejected
#SBATCH --cpus-per-task=32

torchrun --nproc_per_node=2 train.py

2-GPU jobs are put in the gpu2 QOS automatically; you don't need --qos. They wait until both GPUs are free.

Example: a slice of a GPU (MPS)

#!/bin/bash
#SBATCH --gres=mps:50              # 50% of one GPU's compute
#SBATCH --time=2:00:00

python infer.py

MPS limits the share of compute, not GPU memory, so be reasonable with memory, since others are sharing the card.

Email

You get an email at <user>@csclub.uwaterloo.ca when a batch job ends. To turn this off, add export SLURM_NO_MAIL=1 to your ~/.bashrc (or run SLURM_NO_MAIL=1 sbatch job.sh for one job). --mail-type=BEGIN,END,FAIL / --mail-user= work as usual. Interactive (srun/salloc) jobs don't send email.

Useful commands

sinfo                                  # node/partition state
squeue                                 # everyone's jobs (ST: R=running, PD=pending)
squeue --me                            # your jobs
squeue --me --start                    # estimated start time of pending jobs
scontrol show job <JOBID>              # full details, incl. Reason= for pending jobs
scancel <JOBID>                        # cancel a job
sstat -j <JOBID>.batch                 # live CPU/memory use of a running batch job
sacct -j <JOBID> --format=JobID,Elapsed,State,ExitCode,MaxRSS
sshare -U                              # your fair-share standing
sprio -l                               # priority of pending jobs

Why is my job pending?

Reason Meaning
QOSMaxJobsPerUserLimit You already have a running job. It starts when that one ends
Resources Waiting for a GPU (or both GPUs) to be free
Priority Someone with higher priority (less recent usage) is ahead of you
QOSMaxWallDurationPerJobLimit The time limit is over what that QOS allows. Shouldn't happen, since such jobs are rejected at submit time

Installation/Config

This is the generic single-node setup. Tiramisu's actual config is described in the next section.

First install the Slurm packages and dependencies:

sudo apt update
sudo apt install slurm-wlm slurmd slurmctld munge -y

Then check that the MUNGE key exists (it should already):

sudo test -f /etc/munge/munge.key && echo "Key exists" || echo "Key is missing"

For the next part you need the hostname, CPU count and total RAM. slurmd -C prints a ready-made NodeName= line. Create /etc/slurm/slurm.conf with the following, replacing the placeholders:

ClusterName=localcluster
SlurmctldHost=<your-hostname>

# Authentication
AuthType=auth/munge
CredType=cred/munge

# Process tracking and resource limits
ProctrackType=proctrack/cgroup
TaskPlugin=task/cgroup,task/affinity
SelectType=select/cons_tres
SelectTypeParameters=CR_CPU_Memory

# Paths and logging
SlurmUser=slurm
StateSaveLocation=/var/lib/slurm/slurmctld
SlurmdSpoolDir=/var/lib/slurm/slurmd
SlurmctldPidFile=/run/slurmctld.pid
SlurmdPidFile=/run/slurmd.pid
SlurmctldLogFile=/var/log/slurmctld.log
SlurmdLogFile=/var/log/slurmd.log

# Node definition
NodeName=<your-hostname> CPUs=<CPU-cores> RealMemory=<Total-RAM-MB> State=UNKNOWN

# Partition definition
PartitionName=debug Nodes=<your-hostname> Default=YES MaxTime=INFINITE State=UP

Also create /etc/slurm/cgroup.conf (CgroupAutomount is obsolete in current Slurm and is no longer needed):

ConstrainCores=yes
ConstrainRAMSpace=yes
ConstrainDevices=yes

Create the state and spool directories and make slurm their owner:

sudo mkdir -p /var/lib/slurm/slurmd /var/lib/slurm/slurmctld
sudo chown slurm:slurm /var/lib/slurm/slurmd /var/lib/slurm/slurmctld

Then enable and start the controller and compute daemons:

sudo systemctl enable --now slurmctld slurmd

Tiramisu configuration (admins)

Slurm 24.11, one node (tiramisu), one partition gpu (DefaultTime 4h, MaxTime 12h). Files are in /etc/slurm/.

  • Resources: select/cons_tres with CR_CPU_Memory. CoreSpecCount=32 and MemSpecLimit=98304 keep 32 CPUs and 96 GB for normal use of the machine. DefMemPerCPU=2048, DefCpuPerGPU=16.
  • GPUs (gres.conf): gpu:nvidia_rtx_pro_6000_blackwell_server_edition:2 plus mps:200 (100 per GPU). The MPS control daemon runs in multi-user mode (nvidia-cuda-mps-control -d -M).
  • Accounting: slurmdbd + MariaDB, AccountingStorageEnforce=associations,limits,qos, AccountingStorageTRES=gres/gpu,gres/mps (required or the GPU limits do nothing). Every user needs an association in account gpu-users. /usr/local/sbin/slurm-sync-users (hourly, /etc/cron.d/slurm-sync-users) adds current members from LDAP (term/nonMemberTerm) and removes lapsed ones. Extra always-allowed users go in /etc/slurm/extra-users.
  • Priority: priority/multifactor, PriorityWeightFairShare=10000, PriorityWeightAge=1000, PriorityMaxAge=1-0. Billing CPU=0.01,GRES/gpu=1.0,GRES/mps=0.01.
  • QOS:
QOS Used by Limits
gpu1 Partition QOS (applies to every job) MaxJobsPerUser=1, MaxTRESPerJob=gres/gpu=1,gres/mps=100, Flags=DenyOnLimit
gpu2 2-GPU jobs (set by job_submit.lua) MaxTRESPerJob=gres/gpu=2,gres/mps=100, MaxWall=01:00:00, Flags=DenyOnLimit,OverPartQOS. No MaxJobsPerUser, so gpu1's one-job limit still applies
gpu2 must be in the users' allowed QOS list: sacctmgr modify account gpu-users set qos+=gpu2.
  • Submit filter (job_submit.lua): gives clear errors for >1 GPU, >mps:100, gpu+mps together, and per-socket requests. It defaults to mps:25 when no GPU is requested, and turns on --mail-type=END for batch jobs (SLURM_NO_MAIL=1 opts out). For 2-GPU jobs it sets qos=gpu2 and --time=60 if no time was given, rejects >60 min, and rejects --qos=gpu2 on smaller jobs. The QOS limits are the real enforcement; the filter is for friendly messages.
  • GPU ACLs: /dev/nvidia* are root:root 0660. prolog.sh (PrologFlags=Contain, so it runs at allocation) adds setfacl u:<uid>:rw on /dev/nvidiactl, /dev/nvidia-uvm*, /dev/nvidia-modeset and the job's /dev/nvidiaN. It records each grant in /run/gpu-acl/<ctl|gpuN>/<uid>.<jobid>. epilog.sh removes a user's ACL once they have no job left on that device. ConstrainDevices=yes also limits processes inside the job cgroup to the job's GPUs.
  • Mail: MailDomain=csclub.uwaterloo.ca. Exim relays via mail.csclub.uwaterloo.ca.

Setup scripts: tiramisu-slurm-limits.sh (base setup: dbd, QOS gpu1, filter, sync cron, mail) and add-gpu2-qos.sh (adds gpu2 and the 2-GPU filter; --rollback undoes it). Re-running tiramisu-slurm-limits.sh apply puts back the 1-GPU-only filter, so run add-gpu2-qos.sh again afterwards.

Basic Testing

Check that the daemons are running:

systemctl status slurmctld slurmd slurmdbd
sinfo
sacctmgr show qos format=name,flags%30,maxwall,maxtres%40,maxjobspu

If sinfo shows the node as down or drain, set it back to idle:

sudo scontrol update nodename=tiramisu state=resume

Quick GPU checks (as a normal user):

srun --gres=gpu:1 nvidia-smi -L                     # 1 GPU
srun --gres=gpu:2 nvidia-smi -L                     # 2 GPUs, QOS gpu2, 1h limit
sbatch --gres=gpu:2 --time=2:00:00 --wrap=true      # must be rejected

Check the ACLs while a job is running, and again after it ends (afterwards there should be no user: lines and /run/gpu-acl should be empty):

getfacl -p /dev/nvidia0 /dev/nvidia1 /dev/nvidiactl | grep '^user:'
ls -R /run/gpu-acl

If sinfo fails to connect, the node is unresponsive, or jobs fail to start, check the logs:

sudo tail -f /var/log/slurmctld.log /var/log/slurmd.log
journalctl -t slurm-gpu-acl -n 50            # prolog/epilog ACL messages