【问题标题】:Bounds checking is not supported for CUDACUDA 不支持边界检查
【发布时间】:2020-04-23 17:38:36
【问题描述】:

我正在尝试使用 Numba 并访问 GPU 以加速代码,但我收到以下错误:

in jit raise NotImplementedError("bounds checking is not supported for CUDA")
NotImplementedError: bounds checking is not supported for CUDA

我看到有人提出了另一个问题,但没有完全指定也没有回答here。 当我看到矢量化代码 (y = corr*x + np.sqrt(1.-corr**2)*z) 不起作用(同样的错误)时,我实现了 2-for 循环。我还尝试使用选项boundscheck,但这并没有改变结果。 未指定 target 时不会出现该错误,因为它会自动在 CPU 上运行(我猜)。

import numpy as np
from numba import jit

N = int(1e8)
@jit(nopython=True, target='cuda', boundscheck=False)
def Brownian_motions(T, N, corr):
    x = np.random.normal(0, 1, size=(T,N))
    z = np.random.normal(0, 1, size=(T,N))
    y = np.zeros(shape=(T,N))
    for i in range(T):
        for j in range(N):
            y[i,j] = corr*x[i,j] + np.sqrt(1.-corr**2)*z[i,j]
    return(x,y)

x, y = Brownian_motions(T = 500, N = N, corr = -0.45)

你能帮帮我吗? Python 是 3.7.6,Numba 是 0.48.0。

【问题讨论】:

  • Cuda 版本为 10.2。另一方面,如果我尝试使用@vectorize(['UniTuple(float64, 2)(float64, float64, float64)'], target='cuda') 作为装饰器,我得到的错误是Unknown attribute 'normal' of type Module

标签: python python-3.x jit numba


【解决方案1】:

在我的例子中,我还替换为 @jit,它是使用 XLA 编译多个操作的装饰器。这是查看 CPU 和 GPU 性能的示例代码。

from numba import jit
import numpy as np 
# to measure exec time 
from timeit import default_timer as timer    

# normal function to run on cpu 
def func(a):                                 
    for i in range(10000000): 
        a[i]+= 1      

# function optimized to run on gpu  
@jit
#(target ="cuda")                          
def func2(a): 
    for i in range(10000000): 
        a[i]+= 1
if __name__=="__main__": 
    n = 10000000                            
    a = np.ones(n, dtype = np.float64) 
    b = np.ones(n, dtype = np.float32) 

    start = timer() 
    func(a) 
    print("without GPU:", timer()-start)     

    start = timer() 
    func2(a) 
    print("with GPU:", timer()-start) 

结果: 没有 GPU:5.353004818000045 使用 GPU:0.23115529000006063

【讨论】:

    【解决方案2】:

    将@jit(nopython=True, target='cuda', boundscheck=False) 替换为@jit

    import numpy as np
    from numba import jit
    
    N = int(1e8)
    @jit
    def Brownian_motions(T, N, corr):
        x = np.random.normal(0, 1, size=(T,N))
        z = np.random.normal(0, 1, size=(T,N))
        y = np.zeros(shape=(T,N))
        for i in range(T):
            for j in range(N):
                y[i,j] = corr*x[i,j] + np.sqrt(1.-corr**2)*z[i,j]
        return(x,y)
    
    x, y = Brownian_motions(T = 500, N = N, corr = -0.45)
    

    【讨论】:

    • 嗨@Amit,谢谢你的回答。当我用@jit 更改装饰器时的问题是我得到Segmentation fault (core dumped)。你知道为什么会这样吗?提前致谢。如果我只使用 @jit(nopython=True) 也是一样。
    猜你喜欢
    • 1970-01-01
    • 2014-05-14
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    相关资源
    最近更新 更多