python - 运行 python cuda 程序时出错
问题描述
我是 python 内核编程的新手,为了学习,我点击了这个链接。在尝试运行示例 Cuda python 程序时,出现如下错误。我不知道,这是怎么回事?请帮我解决这个问题,以便我可以继续学习。
Traceback (most recent call last):
File "numpycuda.py", line 17, in <module>
my_kernel[blockspergrid, threadsperblock](data)
File "/home/face/.local/lib/python2.7/site-
packages/numba/cuda/simulator/kernel.py", line 103, in
__getitem__
normalize_kernel_dimensions(*configuration[:2])
File "/home/face/.local/lib/python2.7/site-
packages/numba/cuda/errors.py", line 38, in
normalize_kernel_dimensions
griddim = check_dim(griddim, 'griddim')
File "/home/face/.local/lib/python2.7/site-
packages/numba/cuda/errors.py", line 33, in check_dim
% (name, dim)).
TypeError: griddim must be a sequence of integers, got [1.0]
Python程序
from __future__ import division
from numba import cuda
import numpy
import math
# CUDA kernel
@cuda.jit
def my_kernel(io_array):
pos = cuda.grid(1)
if pos < io_array.size:
io_array[pos] *= 2 # do the computation
# Host code
data = numpy.ones(256)
threadsperblock = 256
blockspergrid = math.ceil(data.shape[0] / threadsperblock)
my_kernel[blockspergrid, threadsperblock](data)
print(data)
我安装了 numba、CUDA 和 numpy 库,可能是什么问题?我正在使用 2.7.12 的 python 版本
解决方案
Numba 内核启动要求执行参数是整数或整数元组。您使用的math.ceil(data.shape[0] / threadsperblock)
是产生一个浮点数,该浮点数用作执行参数是非法的。
你可以这样做:
data = numpy.ones(250)
threadsperblock = 64
blockspergrid = (data.shape[0] + threadsperblock - 1) // threadsperblock
my_kernel[blockspergrid, threadsperblock](data)
应该可以正常工作
推荐阅读
- xml - 双值的分号分隔列表的 XSD 模式
- python - 在 Pandas 中使用 groupby、transpose、append 或 pivot 的哪些功能?以及如何使用它们?
- azure-devops - 如何通过 API 以不报告它是手动触发的方式触发 Azure 管道
- cron - 使用注释动态调度 Spring 批处理作业
- flyway - 如何从 Flyway 中的 dotenv 文件加载环境变量?
- python - 识别日期时间列表
- python - 无法连接类型为“的对象”
"; 只有 pd.Series、pd.DataFrame 和 pd.Panel(已弃用)obj 有效 - python - 在特定列中保留元素最接近 0 的行
- amazon-web-services - 使用 Lambda 函数通过 Python 读取某个 Cloudwatch 警报状态及其指标
- ssh - ssh 向远程发送命令时如何获取 ~/.bashrc