在numpy/openblas上设置运行时的最大线程数

Thé*_*o T 9 python numpy blas openblas

我想知道是否可以在(Python)运行时更改OpenBLAS在numpy后面使用的最大线程数?

我知道可以在通过环境变量运行解释器之前设置它OMP_NUM_THREADS,但我想在运行时更改它.

通常,使用MKL而不是OpenBLAS时,可能:

import mkl
mkl.set_num_threads(n)
Run Code Online (Sandbox Code Playgroud)

ali*_*i_m 13

您可以通过调用openblas_set_num_threads函数来执行此操作ctypes.我经常发现自己想要这样做,所以我写了一个小上下文管理器:

import contextlib
import ctypes
from ctypes.util import find_library

# Prioritize hand-compiled OpenBLAS library over version in /usr/lib/
# from Ubuntu repos
try_paths = ['/opt/OpenBLAS/lib/libopenblas.so',
             '/lib/libopenblas.so',
             '/usr/lib/libopenblas.so.0',
             find_library('openblas')]
openblas_lib = None
for libpath in try_paths:
    try:
        openblas_lib = ctypes.cdll.LoadLibrary(libpath)
        break
    except OSError:
        continue
if openblas_lib is None:
    raise EnvironmentError('Could not locate an OpenBLAS shared library', 2)


def set_num_threads(n):
    """Set the current number of threads used by the OpenBLAS server."""
    openblas_lib.openblas_set_num_threads(int(n))


# At the time of writing these symbols were very new:
# https://github.com/xianyi/OpenBLAS/commit/65a847c
try:
    openblas_lib.openblas_get_num_threads()
    def get_num_threads():
        """Get the current number of threads used by the OpenBLAS server."""
        return openblas_lib.openblas_get_num_threads()
except AttributeError:
    def get_num_threads():
        """Dummy function (symbol not present in %s), returns -1."""
        return -1
    pass

try:
    openblas_lib.openblas_get_num_procs()
    def get_num_procs():
        """Get the total number of physical processors"""
        return openblas_lib.openblas_get_num_procs()
except AttributeError:
    def get_num_procs():
        """Dummy function (symbol not present), returns -1."""
        return -1
    pass


@contextlib.contextmanager
def num_threads(n):
    """Temporarily changes the number of OpenBLAS threads.

    Example usage:

        print("Before: {}".format(get_num_threads()))
        with num_threads(n):
            print("In thread context: {}".format(get_num_threads()))
        print("After: {}".format(get_num_threads()))
    """
    old_n = get_num_threads()
    set_num_threads(n)
    try:
        yield
    finally:
        set_num_threads(old_n)
Run Code Online (Sandbox Code Playgroud)

你可以像这样使用它:

with num_threads(8):
    np.dot(x, y)
Run Code Online (Sandbox Code Playgroud)

正如评论中所提到的,openblas_get_num_threads并且openblas_get_num_procs在编写本文时是非常新的功能,因此除非您从最新版本的源代码编译OpenBLAS,否则可能无法使用.

  • 请注意,从v0.2.14开始,pthread openblas_get_num_procs不考虑亲和力,因此当可用cpus的数量受到限制时(例如在容器中),它可能导致超额订阅,使用len(os.sched_getaffinity(0))(python> = 3.3)代替 (2认同)

Tho*_*eau 9

我们最近开发threadpoolctl了一个跨平台的包来控制在 python 中调用 C 级线程池时使用的线程数。它的工作原理类似于@ali_m 的答案,但通过循环所有加载的库自动检测需要限制的库。它还带有自省 API。

这个包可以使用pip install threadpoolctl上下文管理器安装并附带上下文管理器,允许您控制包使用的线程数,例如numpy

from threadpoolctl import threadpool_limits
import numpy as np


with threadpool_limits(limits=1, user_api='blas'):
    # In this block, calls to blas implementation (like openblas or MKL)
    # will be limited to use only one thread. They can thus be used jointly
    # with thread-parallelism.
    a = np.random.randn(1000, 1000)
    a_squared = a @ a
Run Code Online (Sandbox Code Playgroud)

你也可以有不同的线程池(如differenciating更精确的控制blas,从openmp呼叫)。

注意:这个包仍在开发中,欢迎任何反馈。