如何限制Windows上NumPy的OpenBlas使用的线程数?

时间:2019-06-02 23:11:00

标签: python windows numpy openblas

我正在分析在Python中执行脚本的时间,我想关闭MKL / OpenBLAS优化。在Windows上如何正确执行?

我通过pip安装了带有Python 3.6和numpy 1.16.3的Windows 10 x64。

我尝试在Windows(OPENBLAS_NUM_THREADSMKL_NUM_THREADSNUMEXPR_NUM_THREADSVECLIB_MAXIMUM_THREADS)和Python脚本中设置系统环境变量:

import os

os.environ["OMP_NUM_THREADS"] = "1"
os.environ["OPENBLAS_NUM_THREADS"] = "1"
os.environ["MKL_NUM_THREADS"] = "1"
os.environ["NUMEXPR_NUM_THREADS"] = "1"
os.environ["VECLIB_MAXIMUM_THREADS"] = "1"

但执行这段代码:

import numpy as np
N = 10**4
a = np.random.random((N, N))
b = a@a

仍然使用多于一个的线程。

我的np.__config__.show()如下:

blas_mkl_info:
  NOT AVAILABLE
blis_info:
  NOT AVAILABLE
openblas_info:
    library_dirs = ['C:\\projects\\numpy-wheels\\numpy\\build\\openblas']
    libraries = ['openblas']
    language = f77
    define_macros = [('HAVE_CBLAS', None)]
blas_opt_info:
    library_dirs = ['C:\\projects\\numpy-wheels\\numpy\\build\\openblas']
    libraries = ['openblas']
    language = f77
    define_macros = [('HAVE_CBLAS', None)]
lapack_mkl_info:
  NOT AVAILABLE
openblas_lapack_info:
    library_dirs = ['C:\\projects\\numpy-wheels\\numpy\\build\\openblas']
    libraries = ['openblas']
    language = f77
    define_macros = [('HAVE_CBLAS', None)]
lapack_opt_info:
    library_dirs = ['C:\\projects\\numpy-wheels\\numpy\\build\\openblas']
    libraries = ['openblas']
    language = f77
    define_macros = [('HAVE_CBLAS', None)]

在我的PC上,没有像C:\\projects\\numpy-wheels\\numpy\\build\\openblas这样的路径,它必须是编译库的计算机的路径。

在一个线程上实现执行的技巧是从CMD运行脚本,如下所示:

start /affinity 0x4 python script.py

但这不是管理数学库优化的正确方法(而且我不确定此命令是否确实限制了OpenBLAS。)

0 个答案:

没有答案