Skip to content

Slurm: Multi-Threaded Applications

Multi-threaded applications operate faster than single-threaded applications on CPUs with multiple cores. Moreover, multiple threads of one process share the same resources (such as memory).

For multithreaded programs based on Open Multi-Processing (OpenMP), the number of threads to be used is defined by the environment variable OMP_NUM_THREADS. By default this variable is set to OMP_NUM_THREADS=1.

To submit a batch job that requires 4 CPU cores, 5000 MByte of total physical memory and a total wall clock time of 40 minutes, either execute

$ sbatch -p cpuonly --export=ALL,OMP_NUM_THREADS=4 -J OpenMP_Test -N 1 -c 4 -t 40 --mem=5000 job_omp.sh

or add the following pragmas to the top of the job_omp.sh file:

#!/bin/bash
#SBATCH --nodes=1
#SBATCH --cpus-per-task=4
#SBATCH --time=40:00
#SBATCH --mem=5000
#SBATCH --partition cpuonly

export OMP_NUM_THREADS=${SLURM_CPU_PER_TASK}
echo "Running on ${SLURM_JOB_CPUS_PER_NODE} cores with ${OMP_NUM_THREADS} threads"
./omp.exe

Note that command line options passed to sbatch overrule options set in job scripts.

SLURM_JOB_CPUS_PER_NODE on different partitions

The behavior of Slurm regarding SLURM_JOB_CPUS_PER_NODE depends on the partition chosen. When using the partitions dev_cpuonly and dev_accelerated, it will be set to the total number of CPUs requested on the node. When using the partition cpuonly and accelerated, it will be set to the total number of CPUs on the node.


Last update: September 28, 2023