我是cuda的新手c..i使用共享内存写了一个基本的矩阵乘法程序..但问题是我不能将矩阵大小增加到288以上,如果我这样做,我会得到堆栈溢出错误..我有nvidia gtx 480 gpu ..任何人都可以告诉我如何增加大小和我正在做的错误
#define tile_width 16
#define width 288
void mat_mul_kernel1(int *a,int *b,int *c)
{
int row= blockIdx.y*blockDim.y + threadIdx.y;
int col= blockIdx.x*blockDim.x + threadIdx.x;
int pvalue=0;
__shared__ int sha[tile_width*tile_width];
__shared__ int shb[tile_width*tile_width];
for (int m=0;m<width/tile_width;m++)
{
sha[threadIdx.y*tile_width+threadIdx.x]=a[row*width+(m*tile_width)+threadIdx.x];
shb[threadIdx.y*tile_width+threadIdx.x]=b[(m*tile_width+threadIdx.y)*width+col];
__syncthreads();
for (int k=0;k<tile_width;k++)
pvalue+=sha[threadIdx.y*tile_width+k]*shb[k*tile_width+threadIdx.x];
__syncthreads();
}
c[row*width+col]=pvalue;
}
int main()
{
int a[width*width],b[width*width],c[width*width];
int *deva,*devb,*devc;
float etime;
for (int i=0;i<width;i++)
{
for(int j=0;j<width;j++)
{
a[i*width+j]=1;
b[i*width+j]=1;
}
}
cudaEvent_t start,stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
dim3 dimGrid((int)(width)/tile_width,(int)(width)/tile_width);
dim3 dimBlock(tile_width,tile_width);
cudaError_t error;
error=cudaMalloc((void**)&deva,width*width*sizeof(int));
if(error!= cudaSuccess)
{
printf("error at a allocation");
exit(EXIT_FAILURE);
}
error=cudaMemcpy(deva,a,width*width*sizeof(int),cudaMemcpyHostToDevice);
if(error!= cudaSuccess)
{
printf("error at a copying");
exit(EXIT_FAILURE);
}
error=cudaMalloc((void**)&devb,width*width*sizeof(int));
if(error!= cudaSuccess)
{
printf("error at b allocation");
exit(EXIT_FAILURE);
}
error=cudaMemcpy(devb,b,width*width*sizeof(int),cudaMemcpyHostToDevice);
if(error!= cudaSuccess)
{
printf("error at b copying");
exit(EXIT_FAILURE);
}
error=cudaMalloc((void**)&devc,width*width*sizeof(int));
if(error!= cudaSuccess)
{
printf("error at c allocation");
exit(EXIT_FAILURE);
}
cudaEventRecord(start,0);
mat_mul_kernel1<<<dimGrid,dimBlock,tile_width*tile_width*sizeof(int)>>>(deva,devb,devc);
cudaEventRecord(stop,0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&etime,start,stop);
error=cudaMemcpy(c,devc,width*width*sizeof(int),cudaMemcpyDeviceToHost);
if(error!= cudaSuccess)
{
printf("error at c copying");
//exit(EXIT_FAILURE);
}
cudaFree(deva);
cudaFree(devb);
cudaFree(devc);
printf("ElapsedTime %f milliseconds",etime);
}
答案 0 :(得分:4)
您看到的问题与CUDA无关。问题是你的阵列a,b,c。它们被分配在堆栈中。它们的大小为288 x 288 x siezof(int) x 3
,导致972kB(sizeof(int) = 4 byte
)。所以我认为你达到了标准的最大堆栈大小,据我所知,它大约是1MB。
尝试在堆上动态分配数组
int* a = (int*) malloc(width * width * sizeof(int));
并释放最后的记忆
free(a);