python 多处理的 SLURM 给出不一致的结果

SLURM with python multiprocessing give inconsistent results

我们有一个 4*64 核的小型 HPC,里面安装了 SLURM。

节点是:

sinfo -N -l
Mon Oct  3 08:58:12 2016
NODELIST    NODES PARTITION       STATE CPUS    S:C:T MEMORY TMP_DISK WEIGHT FEATURES REASON              
dlab-node1      1     dlab*        idle   64   2:16:2 257847        0      1   (null) none                
dlab-node2      1     dlab*        idle   64   2:16:2 257847        0      1   (null) none                
dlab-node3      1     dlab*        idle   64   2:16:2 257847        0      1   (null) none                
dlab-node4      1     dlab*        idle   64   2:16:2 257847        0      1   (null) none  

为了测试 SLURM,我在 python 中编写了一个带有多处理的小脚本:

import multiprocessing
import os
def func(i):
    print(n_procs)

n_procs = int(os.environ['SLURM_JOB_CPUS_PER_NODE'].split('(')[0]) * int(os.environ['SLURM_JOB_NUM_NODES'])
p = multiprocessing.Pool(n_procs)
list(p.imap_unordered(func, [i for i in range(n_procs*2)]))

我使用以下批处理 sh 脚本 运行 它与 SLURM

#!/bin/bash
#
#SBATCH -p dlab                # partition (queue)
#SBATCH -N 2                      # number of nodes
#SBATCH -n 64                     # number of cores
#SBATCH --mem 250                 # memory pool for all cores
#SBATCH -t 0-2:00                 # time (D-HH:MM)
#SBATCH -o slurm.%N.%j.out        # STDOUT
#SBATCH -e slurm.%N.%j.err        # STDERR

python3 asd.py

如我所料,这会在 STDOUT 文件中打印 128 256 次。

然而,如果我 运行 这多次,我得到的行数非常不同(它们都包含 128 这是正确的)

第一次 运行 我有 144 行,第二次我有 256 行(这是正确的),第三次我有 184 行。

有什么问题?我应该调查 SLURM 配置中的某些内容,还是 python multiprocessing 中有问题?

来自 sbatch 手册页:

SLURM_JOB_CPUS_PER_NODE

Count of processors available to the job on this node. Note the select/linear plugin allocates entire nodes to jobs, so the value indicates the total count of CPUs on the node. The select/cons_res plugin allocates individual processors to jobs, so this number indicates the number of processors on this node allocated to the job

正如突出显示的那样,该变量只会 return 分配给脚本 运行 的节点中的 cpus 数量。如果你想有一个均匀的分配,你应该指定 --ntasks-per-node=32

此外,请记住,多处理不会在多个节点中生成进程。如果你想跨越多个节点,你有一个很好的文档 here