使用 dask 分布式时出现 OMP_NUM_THREADS 错误

Error with OMP_NUM_THREADS when using dask distributed

我正在使用 distributed, a framework to allow parallel computation. In this, my primary use case is with NumPy. When I include NumPy code that relies on np.linalg, I get an error with OMP_NUM_THREADS, which is related to the OpenMP library

一个最小的例子:

from distributed import Executor
import numpy as np
e = Executor('144.92.142.192:8786')

def f(x, m=200, n=1000):
    A = np.random.randn(m, n)
    x = np.random.randn(n)
    #  return np.fft.fft(x)  # tested; no errors
    #  return np.random.randn(n)  # tested; no errors
    return A.dot(y).sum()  # tested; throws error below

s = [e.submit(f, x) for x in [1, 2, 3, 4]]
s = e.gather(s)

当我使用 linalg 测试时,e.gather 失败,因为每个作业都会抛出以下错误:

OMP: Error #34: System unable to allocate necessary resources for OMP thread:
OMP: System error #11: Resource temporarily unavailable
OMP: Hint: Try decreasing the value of OMP_NUM_THREADS.

我应该将 OMP_NUM_THREADS 设置为什么?

简答

export OMP_NUM_THREADS=1

or 

dask-worker --nthreads 1

说明

OMP_NUM_THREADS 环境变量控制许多库(包括支持 numpy.dotBLAS 库)在其计算中使用的线程数,例如矩阵乘法。

这里的冲突是你有两个相互调用的并行库,BLAS 和 dask.distributed。每个库都设计为使用与系统中可用的逻辑内核一样多的线程。

例如,如果您有八个内核,那么 dask.distributed 可能 运行 您的函数 f 一次在不同的线程上执行八次。 f 中的 numpy.dot 函数调用每次调用将使用八个线程,从而导致一次 运行ning 64 个线程。

这实际上很好,你会遇到性能下降,但一切都可以 运行 正确,但它会比你一次只使用八个线程慢,要么通过限制 dask.distributed 或通过限制 BLAS。

您的系统可能已将 OMP_THREAD_LIMIT 设置为某个合理的数字,例如 16,以便在事件发生时向您发出警告。

如果您使用 MKL blas,您可能 使用 TBB 线程层也会得到一些改进。我实际上还没有机会尝试,所以 YMMV。

http://conference.scipy.org/proceedings/scipy2018/anton_malakhov.html