首页 > 解决方案 > 在 Numba 中,如何调用在 GPU 上运行的递归函数?

问题描述

根据文档,似乎支持调用递归函数。但我用一个简单的例子试了一下,它失败了:

@cuda.jit
def call_recursive(out):
    out[0] = recursive(0)


@cuda.jit(types.int64(types.int64), device=True)
def recursive(val):
    if val == 10:
        return val
    else:
        return recursive(val + 1)

# copy an array into the device where we will store the output
data = np.zeros(1)
d_data = cuda.to_device(data)

# invoking kernel with one block and thread, just for testing
call_recursive[1,1](d_data)

# get the data back
h_data = cuda.to_host(d_data)

print(h_data[0])

在这种情况下,我所做的只是调用一个调用递归函数的函数。它调用自己 10 次,然后返回一个数字,该数字存储在给定的数组中并返回给主机。

我期待主机接收填充的数组并打印10。相反,我看到了这个错误:

numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
NameError: name 'recursive' is not defined

我使用的是最新的 Python 3.7 和 Numba 0.50.1。

任何帮助是极大的赞赏!

标签: pythonpython-3.xrecursiongpgpunumba

解决方案


啊,刚刚发现它显然不支持。我正在阅读的文档是一项增强提案 - 不是实际文档。

必须使用迭代和堆栈或数组来模拟递归以保持状态。

好吧,希望这对某人有所帮助。


推荐阅读