我使用CUDA 3.2和VS 2008实现了以下矩阵乘法代码。我在Windows Server 2008 R2企业版上运行。我正在运行Nvidia GTX 480.以下代码可以很好地处理“宽度”(矩阵宽度)值高达2500左右的值。对于大型矩阵,CUDA矩阵乘法中断
int size = Width*Width*sizeof(float);
float* Md, *Nd, *Pd;
cudaError_t err = cudaSuccess;
//Allocate Device Memory for M, N and P
err = cudaMalloc((void**)&Md, size);
err = cudaMalloc((void**)&Nd, size);
err = cudaMalloc((void**)&Pd, size);
//Copy Matrix from Host Memory to Device Memory
err = cudaMemcpy(Md, M, size, cudaMemcpyHostToDevice);
err = cudaMemcpy(Nd, N, size, cudaMemcpyHostToDevice);
//Setup the execution configuration
dim3 dimBlock(TileWidth, TileWidth, 1);
dim3 dimGrid(ceil((float)(Width)/TileWidth), ceil((float)(Width)/TileWidth), 1);
MatrixMultiplicationMultiBlock_Kernel<<<dimGrid, dimBlock>>>(Md, Nd, Pd, Width);
err = cudaMemcpy(P, Pd, size, cudaMemcpyDeviceToHost);
//Free Device Memory
cudaFree(Md);
cudaFree(Nd);
cudaFree(Pd);
当我设置的“宽”到3000或更高,我得到一个黑色的屏幕后,出现以下错误:
我在网上看了一下,我看到有些人有这个问题,因为看门狗在挂起超过5秒后杀死内核。我试图在注册表中编辑“TdrDelay”,这会延迟黑屏和同样错误出现之前的时间。所以我认为这不是我的问题。
我调试到我的代码,发现此行是罪魁祸首:
err = cudaMemcpy(P, Pd, size, cudaMemcpyDeviceToHost);
这是我用回我的结果从设备设置我的矩阵乘法内核函数被调用后。一直到这一点似乎运行良好。我相信我正确地分配内存,并不知道为什么会发生这种情况。我想也许我没有足够的内存在我的卡上,但不应该cudaMalloc返回错误? (我确认它没有在调试时)。
任何想法/援助将不胜感激!...谢谢很多家伙!
内核代码:
//Matrix Multiplication Kernel - Multi-Block Implementation
__global__ void MatrixMultiplicationMultiBlock_Kernel (float* Md, float* Nd, float* Pd, int Width)
{
int TileWidth = blockDim.x;
//Get row and column from block and thread ids
int Row = (TileWidth*blockIdx.y) + threadIdx.y;
int Column = (TileWidth*blockIdx.x) + threadIdx.x;
//Pvalue store the Pd element that is computed by the thread
float Pvalue = 0;
for (int i = 0; i < Width; ++i)
{
float Mdelement = Md[Row * Width + i];
float Ndelement = Nd[i * Width + Column];
Pvalue += Mdelement * Ndelement;
}
//Write the matrix to device memory each thread writes one element
Pd[Row * Width + Column] = Pvalue;
}
我也有使用共享内存此等功能,同时也给出了同样的错误:
电话:
MatrixMultiplicationSharedMemory_Kernel<<<dimGrid, dimBlock, sizeof(float)*TileWidth*TileWidth*2>>>(Md, Nd, Pd, Width);
内核代码:
//Matrix Multiplication Kernel - Shared Memory Implementation
__global__ void MatrixMultiplicationSharedMemory_Kernel (float* Md, float* Nd, float* Pd, int Width)
{
int TileWidth = blockDim.x;
//Initialize shared memory
extern __shared__ float sharedArrays[];
float* Mds = (float*) &sharedArrays;
float* Nds = (float*) &Mds[TileWidth*TileWidth];
int tx = threadIdx.x;
int ty = threadIdx.y;
//Get row and column from block and thread ids
int Row = (TileWidth*blockIdx.y) + ty;
int Column = (TileWidth*blockIdx.x) + tx;
float Pvalue = 0;
//For each tile, load the element into shared memory
for(int i = 0; i < ceil((float)Width/TileWidth); ++i)
{
Mds[ty*TileWidth+tx] = Md[Row*Width + (i*TileWidth + tx)];
Nds[ty*TileWidth+tx] = Nd[(ty + (i * TileWidth))*Width + Column];
__syncthreads();
for(int j = 0; j < TileWidth; ++j)
{
Pvalue += Mds[ty*TileWidth+j] * Nds[j*TileWidth+tx];
}
__syncthreads();
}
//Write the matrix to device memory each thread writes one element
Pd[Row * Width + Column] = Pvalue;
}
请问您可以发布内核代码吗? – Tom 2010-11-03 15:59:39
编辑:添加内核代码 – ntsue 2010-11-04 03:52:19
编辑:添加了两个内核代码功能 – ntsue 2010-11-04 12:22:37