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

你可能感兴趣的文章
Wireshark抓包分析/TCP/Http/Https及代理IP的识别
查看>>
不同包下,相同数据结构的两个类进行转换
查看>>
软件安装(linux)
查看>>
TeamPlain for VSTS - Web Access for Team System-TFS 跨平台的客户端
查看>>
面对前车之鉴的AR,现在的VR要做些什么?
查看>>
vscode 换行符\n 变成\r\n
查看>>
一个绘制虚线的非常规函数(常规方法,打印机上绘制不出虚线)
查看>>
获得本机的IP,掩码和网关
查看>>
大数据之 ZooKeeper原理及其在Hadoop和HBase中的应用
查看>>
Delphi中将XML文件数据装入DataSet
查看>>
你刚才在淘宝上买了一件东西
查看>>
发布一个 Linux 下的 C++ 多线程库
查看>>
Python序列类型
查看>>
再谈ThinkPHP
查看>>
Hibernate问题浅析
查看>>
出现访问apache资源直接下载php文件的解决办法-----yum 安装 php mysql
查看>>
七种Mysql表类型
查看>>
归并与归并排序
查看>>
linux和windows互传文件、用户配置文件和密码配置文件、用户组管理、用户管理...
查看>>
spark 应用程序性能优化经验
查看>>