【问题标题】:Replacing the njit decorator with the cuda.jit decorator用 cuda.jit 装饰器替换 njit 装饰器
【发布时间】:2019-05-03 20:55:19
【问题描述】:

我有一个 Nvidia GPU,下载了 CUDA,并且正在尝试使用它。

假设我有这个代码:

#@cuda.jit (Attempted fix #1)
#@cuda.jit(device = True) (Attempted fix #2)
#@cuda.jit(int32(int32,int32)) (Attempted fix #3)

@njit
def product(rho, theta):
    x = rho * (theta)
    return(x)
a = product(1,2)
print(a)

如何使它与 cuda.jit 装饰器而不是 njit 一起工作?

我尝试过的事情:

当我将装饰器从 @njit 切换到 @cuda.jit 时,我得到:TypingError: No conversion from int64 to none for '$0.5', defined at None。

当我切换装饰器@cuda.jit(device = True) 时,我得到:TypeError: 'DeviceFunctionTemplate' object is not callable。

当我为输入和输出指定类型并使用装饰器 @cuda.jit(int32(int32,int32)) 时,我得到:TypeError: CUDA kernel must have void return type。

【问题讨论】:

    标签: cuda numba numba-pro


    【解决方案1】:

    numba cuda 内核don't return anything。您必须通过参数/参数将结果返回给函数。这样做的起点通常是某种 numpy 数组。这是一个例子:

    $ cat t44.py
    from numba import cuda
    import numpy as np
    
    @cuda.jit
    def product(rho, theta, x):
            x[0] = rho * (theta)
    
    x = np.ones(1,dtype=np.float32)
    product(1,2,x)
    print(x)
    $ python t44.py
    [ 2.]
    $
    

    可能还有很多其他的事情可以说;您可能希望利用上面链接的文档,或者例如this tutorial。通常,在 GPU 计算变得有趣之前,您需要处理比两个标量相乘更大的问题。

    此外,numba 提供了其他方法来访问 GPU 计算,这些方法不依赖于 @cuda.jit 装饰器的使用。这些方法(例如 @vectorize)已记录在案。

    我还将在调用product 时省略任何内核启动配置语法。这在 numba cuda 中是合法的,但它会导致启动包含 1 个线程的 1 个块的内核。这适用于这个特定的示例,但这基本上是一种使用 CUDA GPU 的荒谬方式。

    【讨论】:

      猜你喜欢
      • 2021-11-20
      • 2019-10-21
      • 1970-01-01
      • 2015-02-01
      • 2011-09-03
      • 2015-11-18
      • 2013-01-16
      • 2020-01-11
      • 1970-01-01
      相关资源
      最近更新 更多