第 11 页的https://buildmedia.readthedocs.org/media/pdf/cupy/latest/cupy.pdf中显示的关于使用 cp.RawKernel的示例在网格的使用方面对我来说并不清楚,因为矩阵是方形的。
我尝试改变矩阵的形状并尝试使用网格和块。我不清楚为什么要获得正确的结果我必须设置网格 8 和块 8,例如 multiply((8, ), (8, ), (p, q, z)) # grid, block and arguments
import cupy as cp #Importing CuPy
#Defining the CUDA kernel
multiply = cp.RawKernel(r'''
extern "C" __global__
void multiply(const int* p, const int* q, int* z) {
int tid = blockDim.x * blockIdx.x + threadIdx.x;
z[tid] = p[tid] + q[tid];
}
''', 'multiply')
#First two arrays are set as 0,1,2,3....upto 300
p = cp.arange(30, dtype=cp.int).reshape(6,5)
q = cp.arange(30, dtype=cp.int).reshape(6,5)
#Setting a new array with zeros to pass to kernel for computation
z = cp.zeros((6,5), dtype=cp.int)
#Invoking the kernel with a grid of 250 blocks, each consisting of 1024 threads
multiply((6, ), (5, ), (p, q, z)) # grid, block and arguments
#Displaying the output computed on the kernel
print(z)
我期待检索正确的结果设置,如上面的代码 multiply((6, ), (5, ), (p, q, z)) # grid, block and arguments
你能帮我么?