繁体   English   中英

在 Numba 中,如何调用在 GPU 上运行的递归 function?

[英]In Numba, how can I invoke a recursive function running on the GPU?

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

@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])

在这种情况下,我所做的只是调用一个调用递归 function 的 function。 它调用自己 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。

任何帮助是极大的赞赏!

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM