在cuda中用python计算矩阵向量乘法

calculate Matrix Vector multiplication with python in cuda

我正在尝试使用 numbapro 在下面编写一个简单的矩阵向量乘法:

from numbapro import cuda
from numba import *
import numpy as np
import math
from timeit import default_timer as time

m = 100000 
n = 100

@cuda.jit('void(f4[:,:], f4[:], f4[:])')
def cu_matrix_vector(A, b, c):
    row = cuda.grid(1)
    if (row < m):
        sum = 0

        for i in range(n):
            sum += A[row, i] * b[i]

        c[row] = sum

A = np.array(np.random.random((m, n)), dtype=np.float32)
B = np.array(np.random.random(m), dtype=np.float32)
C = np.empty_like(B)

s = time()
dA = cuda.to_device(A)
dB = cuda.to_device(B)
dC = cuda.to_device(C)

cu_matrix_vector[(m+511)/512, 512](dA, dB, dC)

dC.to_host()

print ( C)

但是当我开始 运行 函数时出现错误 **cu_matrix_vector ** 参数 2: : 类型错误

cu_matrix_vector[(m+511)/512, 512](dA, dB, dC) File "C:\Anaconda3\lib\site-packages\numba\cuda\compiler.py", line 359, in call sharedmem=self.sharedmem) File "C:\Anaconda3\lib\site-packages\numba\cuda\compiler.py", line 433, in _kernel_call cu_func(*kernelargs) File "C:\Anaconda3\lib\site-packages\numba\cuda\cudadrv\driver.py", line 1116, in call self.sharedmem, streamhandle, args) File "C:\Anaconda3\lib\site-packages\numba\cuda\cudadrv\driver.py", line 1160, in launch_kernel None) File "C:\Anaconda3\lib\site-packages\numba\cuda\cudadrv\driver.py", line 221, in safe_cuda_api_call retcode = libfn(*args) ctypes.ArgumentError: argument 2: : wrong type

问题出在这里:

cu_matrix_vector[(m+511)/512, 512](dA, dB, dC)

在Python3、(m+511)/512 = 196.310546875。将浮点值作为启动参数传递是非法的,这是您看到的类型冲突错误的根源。你想做的事:

cu_matrix_vector[(m+511)//512, 512](dA, dB, dC)

这将产生一个整数值并且应该允许代码 运行 正确。