没有 numpy 函数在 numba 中起作用 cuda.jit

No numpy function works in numba cuda.jit

我试过运行这个代码

@cuda.jit
def arr():
    a = np.array([1., 2., 3., 4.], dtype=np.float32)
arr()

但是它导致了这个错误TypingError: Failed in nopython mode pipeline (step: nopython frontend) Use of unsupported NumPy function 'numpy.array' or unsupported use of the function.我不明白为什么会这样,难道 cuda.jit 不应该支持大多数 numpy 函数吗?它也出现在 np.zeros 和 np.empty 中,可能还有所有其他函数(即使 nopython 为真,它也适用于普通的 @jit)

如果你查看docs,你会发现与numba的两种基本编译模式@jit@njit不同,CUDA支持的numpy特性非常少.

即只支持:

  • accessing ndarray attributes .shape, .strides, .ndim, .size, etc..
  • scalar ufuncs that have equivalents in the math module; i.e. np.sin(x[0]), where x is a 1D array.
  • indexing and slicing works.

不支持以下所有内容(也包括数组创建):

  • array creation APIs.
  • array methods.
  • functions that returns a new array.