#!/bin/bash
#SBATCH --job-name=pyt-multi-node
#SBATCH --account=<project>
#SBATCH --time=10:00
#SBATCH --nodes=<num-of-nodes>
#SBATCH --ntasks-per-node=1
#SBATCH --gpus-per-task=<num-of-gpus>
#SBATCH --cpus-per-task=<num-of-gpus + 1>
#SBATCH --mail-type=ALL
#SBATCH --mail-user=<email-address>
#SBATCH --output=pyt_multi_node_%A.out
#SBATCH --partition=<gpu-partition>
export OMP_NUM_THREADS=1
# export NCCL_DEBUG=INFO
export LOGLEVEL=INFO
export OMP_NUM_THREADS=1
# Uncomment for NCCL related logging.
# export NCCL_DEBUG=INFO
echo "SLURM_JOB_ID:" $SLURM_JOB_ID
echo "SLURM_JOB_NODELIST:" $SLURM_JOB_NODELIST
echo "SLURM_GPUS:" $SLURM_GPUS
echo "SLURM_GPUS_ON_NODE:" $SLURM_GPUS_ON_NODE
echo "SLURM_JOB_GPUS:" $SLURM_JOB_GPUS
echo "CUDA_VISIBLE_DEVICES:" $CUDA_VISIBLE_DEVICES
echo "- - - - - - - - - - - -"
# Environment Variable used within the code.
# Sets a random port to potentially allow different jobs to
# run on the same GPU device.
export MASTER_PORT=$(expr 10000 + $(echo -n $SLURM_JOBID | tail -c 4))
echo "MASTER_PORT="$MASTER_PORT
srun --overlap smi_monitor.sh $SLURM_JOB_ID &
# Monitor GPU usage across each node.
# --overlap
# Specifying --overlap allows steps to share all resources (CPUs, memory, and GRES) with all other steps.
# A step using this option will overlap all other steps, even those that did not specify --overlap.
# By default steps do not share resources with other parallel steps. This option applies to step allocations.
module purge
module load miniconda3/<version>
conda activate <path-to-conda-environmnet>
# Choose the first node, from the nodelist to act as head.
nodes=$( scontrol show hostnames $SLURM_JOB_NODELIST )
nodes_array=($nodes)
echo $nodes_array
head_node=${nodes_array[0]}
head_node_ip=$(srun --nodes=1 --ntasks=1 -w "$head_node" hostname --ip-address)
echo "Node IP: "$head_node_ip
srun torchrun \
--nnodes "$SLURM_JOB_NUM_NODES" \
--nproc_per_node "$SLURM_GPUS_ON_NODE" \
--rdzv_id $RANDOM \
--rdzv_backend c10d \
--rdzv_endpoint $head_node_ip:$MASTER_PORT \
multi_node.py 50 10
echo "Done."
The code uses the torchrun functionality that works with Slurm to setup and run across multiple nodes.
So you don’t have to hard code values, SLURM related environment variables are used within the torchrun command:
the --nnodes option is set to "$SLURM_JOB_NUM_NODES".
the --nproc_per_node option is set to "$SLURM_GPUS_ON_NODE".
Notice each node only runs one task, but this task has multiple gpus.
Randomly assign the MASTER_PORT environment variable within the submission script to allow multiple jobs potentially running across the same compute node.
Recording the various torch and cuda versions and capabilities confirms that you have the intended environment and helps with triaging issues.
Within the submission script we use srun to start background tasks, one for each node, that use the nvidia-smi to monitor the utilization of the gpu and its memory - this uses only a few of the available options.
The srun command uses the --overlap option to allow these sub tasks to use the same resources allocated for the job, and thus be able to detect the allocated GPUs. Without this option, then the srun blocks the remaining elements within the submission script.
This uses the -l 1 option to loop the command every second.
It also demonstrates an alternative to running a long interactive desktop via ondemand where users typically just watch using this command.
These background tasks will create an individual file for each node used.
Within the submission script, use the export NCCL_DEBUG=INFO environment variable to log NCCL related functionality - useful if you’re seeing related issues.
Explicitly setting OMP_NUM_THREADS=1 removes related warnings.
For details and understanding of what the actual python code is actually doing, read the the link above.