Stu*_*erg 20 python multithreading numpy
注意:请仔细阅读此问题.我知道CPython有GIL.对于大多数功能,Numpy通常不受GIL的限制.
更新:结果与此问题中描述的问题相同.如果你将numpy链接到OpenBLAS,它会在你导入numpy时立即设置整个过程的CPU亲和力.这可以通过OpenBLAS构建的标志来修复.
我的应用程序使用numpy,我从源代码构建(即没有easy_install等).通常,我的自定义构建工作正常.最近,我做了一些事(我的构建?到我的操作系统?),这阻止了numpy使用多个CPU内核.
考虑这个简单的程序,它执行以下操作:
在正常工作的numpy安装中,第二个(并行)步骤几乎与第一步一样快.但在我的特殊构建中,第二步需要两倍的时间!仅使用1个CPU.它表现得好像numpy.sqrt不释放GIL,但我知道它应该.
男人,我不知道如何打破这样的numpy构建,即使我想.它拒绝使用超过1个CPU核心!我是怎么做到的?我如何解决它?
编辑:更多细节:numpy-1.7.0,gcc,Linux(Fedora 16),但我不认为这些细节太重要了.我之前使用此配置构建而没有遇到此问题.我想我想知道是否有特定的操作系统或python设置会导致这样的行为.
import numpy, threading, time
a1 = numpy.random.random((500,500,200)).astype(numpy.float32)
a2 = numpy.random.random((500,500,200)).astype(numpy.float32)
a3 = numpy.random.random((500,500,200)).astype(numpy.float32)
def numpy_workload(name, a):
print "starting numpy_workload " + name
for _ in range(10):
numpy.sqrt(a)
print "finished numpy_workload " + name
t1 = threading.Thread(target=lambda: numpy_workload("1", a1))
t2 = threading.Thread(target=lambda: numpy_workload("2", a2))
t3 = threading.Thread(target=lambda: numpy_workload("3", a3))
start = time.time()
t1.start()
t1.join()
stop = time.time()
print "Single thread done after {} seconds\n".format( stop - start )
start = time.time()
t2.start()
t3.start()
t2.join()
t3.join()
stop = time.time()
print "Two threads done after {} seconds\n".format( stop - start )
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
885 次 |
| 最近记录: |