#!/usr/bin/env python3
import multiprocessing
def worker():
print("Worker on CPU #%s" % multiprocessing.current_process
().name)
result=0
for j in range(20):
result += j**2
print ("Result on CPU {} is {}".format(multiprocessing.curr
ent_process().name,result))
return
if __name__ == '__main__':
pool = multiprocessing.Pool()
jobs = []
print ("This host exposed {} CPUs".format(multiprocessing.c
pu_count()))
for i in range(multiprocessing.cpu_count()):
p = multiprocessing.Process(target=worker, name=i).star
t()
Untested, but prior experience with cgroups indicates that if things are working correctly, even if your code tries to run as many processes as you have cores, those processes will be confined to the cores you reserve.
Try a more compute-intensive worker function that will take some seconds or minutes to complete, and watch the reserved node with 'top' or a similar program. If for example, the job reserved only 1 core and tried to run 20 processes, you'd see 20 processes in 'top', each at 5% CPU time.
To make the code a bit more polite, you can import the os module and create a new variable from the SLURM_CPUS_ON_NODE environment variable to guide Python into starting the correct number of processes:
cpus_reserved = int(os.environ['SLURM_CPUS_ON_NODE'])
From:
slurm-users <slurm-use...@lists.schedmd.com> on behalf of Rodrigo Santibáñez <rsantiban...@gmail.com>
Date: Friday, May 14, 2021 at 5:17 PM
To: Slurm User Community List <slurm...@lists.schedmd.com>
Subject: Re: [slurm-users] Exposing only requested CPUs to a job on a given node.
External Email Warning
This email originated from outside the university. Please use caution when opening attachments, clicking links, or responding to requests.
-- Ryan Cox Director Office of Research Computing Brigham Young University
###
#
# Slurm cgroup support configuration file
#
# See man slurm.conf and man cgroup.conf for further
# information on cgroup configuration parameters
#--
CgroupAutomount=yes
CgroupMountpoint=/sys/fs/cgroup
#ConstrainCores=no
ConstrainCores=yes
ConstrainRAMSpace=yes
ConstrainDevices=no
ConstrainKmemSpace=no #Avoid a known kernel issue
ConstrainSwapSpace=yes
TaskAffinity=no #Use task/affinity plugin instead
srun --tasks=1 --cpus-per-task=1 --partition=long show-affinity.py
pid 1122411's current affinity mask: 401
=====================================
CPUs in system: 20
PID: 1122411
Allocated CPUs/Cores: 2
Affinity List: {0, 10}
=====================================
srun --tasks=1 --cpus-per-task=4 --partition=long show-affinity.py
pid 1122446's current affinity mask: c03
=====================================
CPUs in system: 20
PID: 1122446
Allocated CPUs/Cores: 4
Affinity List: {0, 1, 10, 11}
=====================================
srun --tasks=1 --cpus-per-task=6 --partition=long show-affinity.py
pid 1122476's current affinity mask: 1c07
=====================================
CPUs in system: 20
PID: 1122476
Allocated CPUs/Cores: 6
Affinity List: {0, 1, 2, 10, 11, 12}
=====================================