暂时mark一下一份英文资料,有空补充其他优化细节转自:https://xcorr.net/2013/05/19/python-refuses-to-use-multiple-cores-solution/ Python refuses to use multiple cores – solution?I was trying to get...
暂时mark一下一份英文资料,有空补充其他优化细节
转自:https://xcorr.net/2013/05/19/python-refuses-to-use-multiple-cores-solution/
Python refuses to use multiple cores – solution
?I was trying to get parallel Python 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 numpy causes Python to stick all computations in one core. This is an issue with CPU affinity, 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 affinity (Linux only AFAIK) that lets you set and get CPU affinity. Download it, run python setup.py install, and run this in Python or ipython:
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 )
|
本文标题为:[转] python numpy 导致多进程绑定同一个CPU问题解决方法
- python线程池ThreadPoolExecutor与进程池ProcessPoolExecutor 2023-09-04
- CentOS7 安装 Python3.6 2023-09-04
- 在centos6.4下安装python3.5 2023-09-04
- Python实现将DNA序列存储为tfr文件并读取流程介绍 2022-10-20
- Python Pandas如何获取和修改任意位置的值(at,iat,loc,iloc) 2023-08-04
- Python 保存数据的方法(4种方法) 2023-09-04
- python中defaultdict用法实例详解 2022-10-20
- python中列表添加元素的几种方式(+、append()、ext 2022-09-02
- windows安装python2.7.12和pycharm2018教程 2023-09-03
- Python之路-Python中的线程与进程 2023-09-04