运行python cuda程序时出错

时间:2018-07-19 13:10:50

标签: python python-2.7 cuda numba

我是python内核编程的新手,为了学习,我遵循了link。 尝试运行示例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版本

1 个答案:

答案 0 :(得分:2)

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)

应该正常工作