简体   繁体   English

计算引擎上的 Python3 多处理池不平衡 CPU 使用率

[英]Python3 multiprocessing pool unbalanced cpu usage on compute engine

I'm trying to run a Python3 program that uses multiple cores to do computations, on a Google cloud compute engine.我正在尝试在 Google 云计算引擎上运行一个使用多个内核进行计算的 Python3 程序。
The code looks like this:代码如下所示:

import multiprocessing
from multiprocessing import Pool
# functions and variables defined
MAX_PROCESS_COUNT = (multiprocessing.cpu_count() - 1) or 1
if __name__=="__main__":
    with Pool(processes=MAX_PROCESS_COUNT) as pool:
        result = list(tqdm.tqdm(pool.imap(single_task, range(len(my_list))), total=len(my_list)))

The compute engine has 20 cores, so I decide to use only 19 of them.计算引擎有 20 个内核,所以我决定只使用其中的 19 个。 my_list has about 200 values, and each single_task takes about 10 mins to complete on my laptop. my_list有大约 200 个值,每个single_task在我的笔记本电脑上大约需要 10 分钟才能完成。

When I actually run the program, it took about 1.6 hours to complete only 35 tasks.当我实际运行程序时,只完成了 35 个任务大约需要 1.6 小时。
So I check the htop , and find that all CPU cores are in use, and the memory usage looks unusually low (expected 14G):于是我查看了htop ,发现所有 CPU 内核都在使用中,而 memory 的使用率看起来异常低(预计 14G):
htop CPU 使用率

More importantly, the CPU usages for each individual task is highly unbalanced:更重要的是,每个单独任务的 CPU 使用率非常不平衡:
在此处输入图像描述

I believe that the unbalanced CPU usage is the problem here.我相信不平衡的 CPU 使用率是这里的问题。
Is there any way to constrain that usage?有没有办法限制这种用法? Should I config in the VM environment or change the python code?我应该在 VM 环境中配置还是更改 python 代码?

I've tested the same code on my laptop, and it runs as expected: out of 8 cores only 1 core is not fully utilized.我在笔记本电脑上测试了相同的代码,它按预期运行:在 8 个内核中,只有 1 个内核没有被充分利用。

By the way, my code uses packages like NumPy, Pandas, and sklearn, and I've already set up libblas for NumPy顺便说一句,我的代码使用 NumPy、Pandas 和 sklearn 之类的包,并且我已经为 NumPy 设置了 libblas

I found the solution here , which is what Klaus D. mentioned.我在这里找到了解决方案,这就是 Klaus D. 提到的。 The NumPy computations are not bound to any process, and need to to configured before running Python program. NumPy 计算不绑定任何进程,需要在运行 Python 程序之前进行配置。
So, in my situation, I added these lines to the top of my Python file:所以,在我的情况下,我将这些行添加到我的 Python 文件的顶部:

import os
os.environ['MKL_NUM_THREADS'] = '1'
os.environ['NUMEXPR_NUM_THREADS'] = '1'
os.environ['OMP_NUM_THREADS'] = '1'

So that each NumPy related computation is restricted to its own assigned process.这样每个 NumPy 相关的计算都被限制在自己分配的进程中。

Additionally, you can check your NumPy configuration by:此外,您可以通过以下方式检查您的 NumPy 配置:

import numpy as np
np.show_config()

And see which environment variable should be set to limit the number of threads.并查看应该设置哪个环境变量来限制线程数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM