python - 在 Numba 中,以 CUDA 为目标时,如何将数组复制到常量内存中?
问题描述
我有一个示例代码来说明这个问题:
import numpy as np
from numba import cuda, types
import configs
def main():
arr = np.empty(0, dtype=np.uint8)
stream = cuda.stream()
d_arr = cuda.to_device(arr, stream=stream)
kernel[configs.BLOCK_COUNT, configs.THREAD_COUNT, stream](d_arr)
@cuda.jit(types.void(
types.Array(types.uint8, 1, 'C'),
), debug=configs.CUDA_DEBUG)
def kernel(d_arr):
arr = cuda.const.array_like(d_arr)
if __name__ == "__main__":
main()
当我使用 cuda-memcheck 运行此代码时,我得到:
numba.errors.ConstantInferenceError: Failed in nopython mode pipeline (step: nopython rewrites)
Constant inference not possible for: arg(0, name=d_arr)
这似乎表明我传入的数组不是常量,因此无法将其复制到常量内存中 - 是这样吗?如果是这样,我如何将作为输入提供给内核的数组复制到常量内存?
解决方案
您不会使用作为输入提供给内核的数组复制到常量数组。这种类型的输入数组已经在设备中,并且设备代码无法写入常量内存。
常量内存只能从主机代码写入,并且常量语法要求数组是主机数组。
这是一个例子:
$ cat t32.py
import numpy as np
from numba import cuda, types, int32, int64
a = np.ones(3,dtype=np.int32)
@cuda.jit
def generate_mutants(b):
c_a = cuda.const.array_like(a)
b[0] = c_a[0]
if __name__ == "__main__":
b = np.zeros(3,dtype=np.int32)
generate_mutants[1, 1](b)
print(b)
$ python t32.py
[1 0 0]
$
请注意,Numba CUDA 中常量内存的实现与使用 CUDA C/C++ 的可能存在一些行为差异,这个问题突出了其中一些。
推荐阅读
- linux - 如何在 Python 中打开新控制台并从输入的用户那里获取数据?
- knockout.js - 淘汰赛可观察数组排序不起作用
- angular5 - 网址参数读取两次,因此语言服务无法选择
- android - 每当设备连接到互联网时,我如何提交我的表单
- sql-server - 如何从多个查询创建表
- sequelize.js - Sequelizejs:错误:重复键值违反唯一约束“message_pkey”
- c# - DateTime 现在将 Datetime 字符串转换为指定格式
- bitbucket - Bitbucket 存储库的大小限制是否包括其中的分支和标签?
- node.js - 正则表达式在聚合查询mongodb中搜索整数?
- swift - 使用 Firebase 存储数据库填充 TableView