python-3.xcudanumbanumba-pro

calculate Matrix Vector multiplication with python in cuda


I'm trying to use numbapro to write a simple matrix vector multiplication below:

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)

But when I start running I get an error in function **cu_matrix_vector ** argument 2: : wrong type

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


Solution

  • The problem lies here:

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

    in Python 3, (m+511)/512 = 196.310546875. Passing a floating point value as a launch parameter is illegal, which is the source of the type conflict error you are seeing. You want to do:

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

    which will produce an integer value and should allow the code to run correctly.