【发布时间】:2018-06-13 08:16:46
【问题描述】:
我想了解为什么以下 PyCUDA 代码不起作用。
我的错误是:
TypeError: invalid type on parameter #3 (0-based)
并且错误发生在我调用函数的块行上。在代码中,它位于block = (MATRIX_SIZE,MATRIX_SIZE,1) 行,结束前2 行。
有谁知道这里的错误是什么?我尝试了很多东西,但我无法弄清楚。
CUDA 代码在 C++ 中工作,我现在只是想在 PyCUDA 中翻译它,但它失败了。
import numpy as np
from pycuda import driver, compiler, gpuarray, tools
# -- initialize the device
import pycuda.autoinit
kernel_code_template = """
__global__ void MatMult(float* C, float* A, float*B, int dimAx, int dimBx, int dimCx, int dimCy)
{
int row = blockDim.y*blockIdx.y+threadIdx.y;
int col = blockDim.x*blockIdx.x+threadIdx.x;
double Result = 0;
if (row<=dimCy-1 && col<=dimCx-1)
{
for (int k = 0; k < dimAx; k++)
{
Result += A[k + dimAx*row] * B[col + dimBx*k];
}
C[col + row*dimCx] = Result;
}
}
"""
MATRIX_SIZE=3
# I create my variables :
a_cpu=np.asarray([[0,1,2],[10,11,12],[20,21,22]])
b_cpu=np.asarray([[0,0,0],[1,2,3],[4,8,12]])
a_gpu = gpuarray.to_gpu(a_cpu)
b_gpu = gpuarray.to_gpu(b_cpu)
size_Ax=a_cpu.shape[1]
size_Bx=b_cpu.shape[1]
size_Ay=a_cpu.shape[0]
size_Cx=size_Bx # Cx=Bx because of matrix product
size_Cy=size_Ay # Cy=Ay
# create empty gpu array for the result (C = A * B)
c_gpu = gpuarray.empty((size_Cy, size_Cx), np.float32)
# get the kernel code from the template
kernel_code=kernel_code_template
# compile the kernel code
mod = compiler.SourceModule(kernel_code)
# get the kernel function from the compiled module
matrixmul = mod.get_function("MatMult")
# call the kernel on the card
matrixmul(
# outputs
c_gpu,
# inputs
a_gpu, b_gpu,
size_Ax,size_Bx,size_Cx,size_Cy,
# (only one) block of MATRIX_SIZE x MATRIX_SIZE threads
block = (MATRIX_SIZE,MATRIX_SIZE,1),
)
【问题讨论】:
-
错误出现在哪一行?
-
@TomášPospíšek 它位于块行,结束前 2 行:“block = (MATRIX_SIZE,MATRIX_SIZE,1)"