我正在使用numpy,我的模型涉及到大量的矩阵乘法。为了加速计算,我使用OpenBLAS多线程库来并行化numpy.dot函数。
我的设置如下:
- 操作系统:CentOS 6.2服务器 # CPU = 12,#MEM = 96GB
- Python版本:Python2.7.6
- NumPy:NumPy 1.8.0
- OpenBLAS + IntelMKL
$ OMP_NUM_THREADS=8 python test_mul.py
代码取自https://gist.github.com/osdf/
test_mul.py
:
import numpy
import sys
import timeit
try:
import numpy.core._dotblas
print 'FAST BLAS'
except ImportError:
print 'slow blas'
print "version:", numpy.__version__
print "maxint:", sys.maxint
print
x = numpy.random.random((1000,1000))
setup = "import numpy; x = numpy.random.random((1000,1000))"
count = 5
t = timeit.Timer("numpy.dot(x, x.T)", setup=setup)
print "dot:", t.timeit(count)/count, "sec"
当我使用OMP_NUM_THREADS=1 python test_mul.py时,结果为:
dot: 0.200172233582 sec
OMP_NUM_THREADS=2
dot: 0.103047609329 sec
OMP_NUM_THREADS=4
dot: 0.0533880233765 sec
一切都很顺利。
然而,当我设置OMP_NUM_THREADS=8
时......代码开始“偶尔工作”。
有时它可以正常工作,有时它甚至不能运行并给出核心转储。
当OMP_NUM_THREADS > 10
时,代码似乎总是出问题。
我想知道这里发生了什么?是否有像每个进程可以使用的最大线程数这样的东西?如果我的机器上有12个CPU,我能否提高限制?
谢谢