2017-04-27 121 views
0

我試圖用numbapro寫下面一個簡單的矩陣向量乘法:計算矩陣向量乘法與CUDA蟒蛇

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

回答

1

的問題就在這裏:

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

在Python 3,(m+511)/512 = 196.310546875。將浮點值作爲啓動參數傳遞是非法的,這是您所看到的類型衝突錯誤的來源。你想這樣做:

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

這將產生一個整數值,並應允許代碼正確運行。