【问题标题】:How to do indexing properly in numba.cuda?如何在 numba.cuda 中正确进行索引?
【发布时间】:2018-02-05 12:21:11
【问题描述】:

我正在编写一个代码,它需要使用 numba 在 python 中进行一些索引。 但是,我不能正确地做到这一点。 好像有什么被禁止的。 代码如下:

from numba import cuda
import numpy as np
@cuda.jit
def function(output, size, random_array):
    i_p, i_k1, i_k2 = cuda.grid(3)
    if i_p<size and i_k1<size and i_k2<size:
        a1=i_p**2+i_k1
        a2=i_p**2+i_k2
        a3=i_k1**2+i_k2**2
        a=[a1,a2,a3]
        for i in range(len(random_array)):
            output[i_p,i_k1,i_k2,i] = a[int(random_array[i])]
output=cuda.device_array((10,10,10,5))
random_array=cuda.to_device(np.array([np.random.random()*3 for i in range(5)]))
size=10
threadsperblock = (8, 8, 8)
blockspergridx=(size + (threadsperblock[0] - 1)) // threadsperblock[0]
blockspergrid = ((blockspergridx, blockspergridx, blockspergridx))

# Start the kernel 
function[blockspergrid, threadsperblock](output, size, random_array)
print(output.copy_to_host())

它产生一个错误:

LoweringError: Failed at nopython (nopython mode backend)
'CUDATargetContext' object has no attribute 'build_list'
File "<ipython-input-57-6058e2bfe8b9>", line 10
[1] During: lowering "$40.21 = build_list(items=[Var(a1, <ipython-input-57-6058e2bfe8b9> (7)), Var(a2, <ipython-input-57-6058e2bfe8b9> (8)), Var(a3, <ipython-input-57-6058e2bfe8b9> (9))])" at <ipython-input-57-6058e2bfe8b9> (10

谁能帮我解决这个问题?

一种选择是将 a 也作为函数的输入提供,但是当 a 真的很大时,比如一些 1000*1000*1000*7 数组,它总是让我失去记忆。

【问题讨论】:

    标签: python cuda numba


    【解决方案1】:

    这个问题与数组索引无关。在内核中,这一行:

    a=[a1,a2,a3]
    

    不支持。您不能在 @cuda.jit 函数中创建列表。内核中支持的 Python 类型的确切列表已完整记录在 here

    【讨论】:

      猜你喜欢
      • 2020-11-24
      • 2022-01-10
      • 2012-10-31
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多