用python多重处理超过了工作内存限制

时间:2018-09-20 08:53:46

标签: python memory-management python-multiprocessing slurm

我正在使用slurm来管理一些计算,但是有时作业会由于内存不足错误而被杀死,即使事实并非如此。这个奇怪的问题一直存在于使用多处理的python作业中。

这是重现此行为的最小示例

#!/usr/bin/python

from time import sleep

nmem = int(3e7) # this will amount to ~1GB of numbers
nprocs = 200    # will create this many workers later
nsleep = 5      # sleep seconds

array = list(range(nmem))  # allocate some memory

print("done allocating memory")
sleep(nsleep)
print("continuing with multiple processes (" + str(nprocs) + ")")

from multiprocessing import Pool

def f(i):
    sleep(nsleep)

# this will create a pool of workers, each of which "seem" to use 1GB
# even though the individual processes don't actually allocate any memory
p = Pool(nprocs)
p.map(f,list(range(nprocs)))

print("finished successfully")

即使这可能在本地运行良好,但粗略的内存占用似乎可以汇总这些进程中每个进程的驻留内存,从而导致nprocs x 1GB的内存使用,而不是1GB(实际的mem使用)。我认为这不是应该做的事情,也不是操作系统在做的事情,它似乎也没有交换或做任何事情。

这是输出,如果我在本地运行代码

> python test-slurm-mem.py 
done allocation memory
continuing with multiple processes (0)
finished successfully

还有htop的屏幕截图

htop showing a total mem use of 8GB with 200 processes of 1GB

如果我使用slurm运行相同的命令,这是输出

> srun --nodelist=compute3 --mem=128G python test-slurm-mem.py 
srun: job 694697 queued and waiting for resources
srun: job 694697 has been allocated resources
done allocating memory
continuing with multiple processes (200)
slurmstepd: Step 694697.0 exceeded memory limit (193419088 > 131968000), being killed
srun: Exceeded job memory limit
srun: Job step aborted: Waiting up to 32 seconds for job step to finish.
slurmstepd: *** STEP 694697.0 ON compute3 CANCELLED AT 2018-09-20T10:22:53 ***
srun: error: compute3: task 0: Killed
> $ sacct --format State,ExitCode,JobName,ReqCPUs,MaxRSS,AveCPU,Elapsed -j 694697.0
     State ExitCode    JobName  ReqCPUS     MaxRSS     AveCPU    Elapsed 
---------- -------- ---------- -------- ---------- ---------- ---------- 
CANCELLED+      0:9     python        2 193419088K   00:00:04   00:00:13 

1 个答案:

答案 0 :(得分:1)

对于其他人:正如注释中含糊指出的那样,您需要更改文件slurm.conf。在此文件中,您需要将选项JobAcctGatherType设置为jobacct_gather/cgroup(完整行:JobAcctGatherType=jobacct_gather/cgroup)。

我以前将选项设置为jobacct_gather/linux,导致问题中所述的会计值错误。