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 normal --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

export OMP_NUM_THREADS=${SLURM_JOB_CPUS_PER_NODE}
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.


Last update: September 28, 2023