python - 在 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。
任何帮助是极大的赞赏!
解决方案
啊,刚刚发现它显然不支持。我正在阅读的文档是一项增强提案 - 不是实际文档。
必须使用迭代和堆栈或数组来模拟递归以保持状态。
好吧,希望这对某人有所帮助。
推荐阅读
- ios - 我想在单个单元格中显示按钮数组,而不是在 tableview 中创建一堆单元格
- haskell - 在 Lambda 函数中没有显示实例?
- django - Django CBV - Formsets:'NoneType'对象没有属性'id'
- plsql - 在PLSQL中调用过程一分钟后是否可以设置超时
- ios - 应用程序进入后台时如何发送本地通知
- mongodb - 错误:未加载 mongodb.jdbc.MongoDriver。你确定你已经包含了正确的 jdbc 驱动程序
- java - 如何有效地返回给定位数的最大可能整数
- javascript - 在 symfony4 树枝模板中分离 JavaScripts 文件
- image - 如何在 vuetify 中设置轮播图像的大小
- string - 多行中的 LLVM IR 字符串文字