使用dask分发时出现OMP_NUM_THREADS错误

Sco*_*ott 12 python numpy cluster-computing dask

我使用分布式,一个允许并行计算的框架.在这里,我的主要用例是NumPy.当我包含依赖的NumPy代码时np.linalg,我收到一个错误OMP_NUM_THREADS,它与OpenMP库有关.

一个最小的例子:

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)
Run Code Online (Sandbox Code Playgroud)

当我使用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.
Run Code Online (Sandbox Code Playgroud)

OMP_NUM_THREADS该怎么办?

MRo*_*lin 18

简短的回答

export OMP_NUM_THREADS=1

or 

dask-worker --nthreads 1
Run Code Online (Sandbox Code Playgroud)

说明

OMP_NUM_THREADS环境变量控制线程许多库,包括的数BLAS库供电numpy.dot,在他们的计算使用,像矩阵乘法.

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

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

这实际上很好,你会遇到性能损失,但一切都可以正常运行,但它会比你一次只使用8个线程要慢,无论是通过限制dask.distributed还是限制BLAS.

您的系统可能OMP_THREAD_LIMIT设置了一些合理的数字,如16,以便在发生此事件时向您发出警告.