How to automatically determine number of cores provided by cluster?

Final update about this.
Actually the right way to do this is to use ENV[“SLURM_JOB_CPUS_PER_NODE”].

ENV[“SLURM_JOB_CPUS_PER_NODE”] gives you info about nodes in a format like this “32(4x),40” for 5 nodes with 4 nodes with 32 cores and 1 node with 40 cores.

This info can be parsed into sum of cores across all nodes using julia code like this where np is the total number of CPUs across all nodes:

cd(@__DIR__)
using Pkg
Pkg.activate(".")

using Distributed, ClusterManagers

subs = Dict("x"=>"*", "(" => "", ")" => "");
np = sum(eval(Meta.parse(replace(ENV["SLURM_JOB_CPUS_PER_NODE"], r"x|\(|\)" => s -> subs[s]))))

addprocs(SlurmManager(np); exeflags="--project")

Now you can just submit jobs using the following slurm script and it’ll automatically assign number of workers to be equal to number of cores assigned to you even if each node has different number of cores:

#!/bin/bash
# Job name:
#SBATCH --job-name=your_job_name
#
# Account:
#SBATCH --account=your_account_name
#
# Partition:
#SBATCH --partition=your_partition_name
#
# Request one node:
#SBATCH --nodes=24
#
# Processors per task:
#SBATCH --cpus-per-task=1
#
# Wall clock limit:
#SBATCH --time=24:00:00

# Load software
module purge
module load julia/1.6.0

# Run Julia script
julia your_julia_code.jl

2 Likes