在 Numba 中,如何在 GPU 上调用递归函数 运行?

In Numba, how can I invoke a recursive function running on the 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 次,然后 returns 一个数字,存储在给定的数组中并返回给主机。

我期待主机接收填充的数组并打印 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。

非常感谢任何帮助!

啊,才发现好像不支持。我正在阅读的文档是增强建议 - 不是实际文档。

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

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