首页 > 解决方案 > 运行 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 版本

标签: pythonpython-2.7cudanumba

解决方案


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)

应该可以正常工作


推荐阅读