博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 导入numpy 导致多进程绑定同一个CPU问题解决方法
阅读量:6475 次
发布时间:2019-06-23

本文共 1582 字,大约阅读时间需要 5 分钟。

python 如果有导入numpy模块的import语句,会导致默认将多进程程序的每个进程都绑定到同一个CPU core上,

失去了多进程在多核CPU上的性能优越性,这和CPU affinity(CPU亲和性)有关,解决办法:

导入affinity包,执行:

affinity.set_process_affinity_mask(0,2**multiprocessing.cpu_count()-1)

以下是英文文档原文,供参考:

 

Python refuses to use multiple cores – solution

I was trying to get  to work and I noticed that if I run two Python scripts simultaneously – say, in two different terminals – they use the same core. Hence, I get no speedup from multiprocessing/parallel Python. After some searching around, I found out that in some circumstances importing  causes Python to stick all computations in one core. This is an issue with , and apparently it only happens for some mixtures of Numpy and BLAS libraries – other packages may cause the CPU affinity issue as well.

There’s a package called  (Linux only AFAIK) that lets you set and get CPU affinity. Download it, run python setup.py install, and run this in Python or :

1
2
3
4
In [
1
]:
import
affinity
 
In [
2
]: affinity.get_process_affinity_mask(
0
)
Out[
2
]:
63

This is good: 63 is a bitmask corresponding to 111111 – meaning all 6 cores are available to Python. Now running this, I get:

1
2
3
4
In [
4
]:
import
numpy as np
 
In [
5
]: affinity.get_process_affinity_mask(
0
)
Out[
5
]:
1

So now only one core is available to Python. The solution is simply to set the CPU affinity appropriately after import numpy, for instance:

1
2
3
4
5
import
numpy as np
import
affinity
import
multiprocessing
 
affinity.set_process_affinity_mask(
0
,
2
*
*
multiprocessing.cpu_count()
-
1
)

 

转载于:https://www.cnblogs.com/Arborday/p/9858108.html

你可能感兴趣的文章
Linux scp命令详解
查看>>
struct和typedef struct
查看>>
cell reuse & disposebag
查看>>
【故障处理】ORA-12545: Connect failed because target host or object does not exist
查看>>
云时代,程序员将面临的分化
查看>>
js判断移动端是否安装某款app的多种方法
查看>>
学习angularjs的内置API函数
查看>>
4、输出名称 Exported names
查看>>
paste工具
查看>>
Pre-echo(预回声),瞬态信号检测与TNS
查看>>
【转载】如何发送和接收 Windows Phone 的 Raw 通知
查看>>
poj2378
查看>>
【译】SQL Server误区30日谈-Day12-TempDB的文件数和需要和CPU数目保持一致
查看>>
Java文件清单列表
查看>>
js url传值中文乱码之解决之道
查看>>
Atitit.获取某个服务 网络邻居列表 解决方案
查看>>
Trusty TEE
查看>>
[LeetCode] Reverse String 翻转字符串
查看>>
学习iOS【3】数组、词典和集合
查看>>
Hessian 原理分析--转
查看>>