【发布时间】:2010-06-09 18:20:50
【问题描述】:
我正在尝试编写一个简单的矩阵乘法应用程序,该应用程序使用 CUDA 将两个方阵相乘。我遇到了一个问题,我的内核只能在网格的块 (0,0) 中正确计算。
这是我的调用代码:
dim3 dimBlock(4,4,1);
dim3 dimGrid(4,4,1);
//Launch the kernel;
MatrixMulKernel<<<dimGrid,dimBlock>>>(Md,Nd,Pd,Width);
这是我的内核函数
__global__ void MatrixMulKernel(int* Md, int* Nd, int* Pd, int Width)
{
const int tx = threadIdx.x;
const int ty = threadIdx.y;
const int bx = blockIdx.x;
const int by = blockIdx.y;
const int row = (by * blockDim.y + ty);
const int col = (bx * blockDim.x + tx);
//Pvalue stores the Pd element that is computed by the thread
int Pvalue = 0;
for (int k = 0; k < Width; k++)
{
Pvalue += Md[row * Width + k] * Nd[k * Width + col];
}
__syncthreads();
//Write the matrix to device memory each thread writes one element
Pd[row * Width + col] = Pvalue;
}
我认为问题可能与记忆有关,但我有点迷茫。我应该怎么做才能使这段代码跨多个块工作?
【问题讨论】:
-
经过手工乘法后,我发现并非所有值都是正确的。我想我的索引推导可能有问题。
标签: cuda