Cuda的分段错误

时间:2013-07-31 13:13:02

标签: c++ c cuda gpu

我正在编写CUDA程序来处理图像。我有分段故障问题,我不知道为什么。我可能只是想念一些小事,但经过几个小时的努力来纠正自己,我无法让它运转起来 根据我的硬件上的deviceQuery(GeForce310M总shmem:16384,每块最大线程:512和最大块dim 521),我正在设置正确的网格,块和碎片内存值(至少我是这么认为)。 这是我在分段错误之前得到的输出:

Cols and Rows:
256
384
total:98304 // rows*cols
Block Size:512
Grid Size:192
shared mem:2048

以下是内核代码

__global__ void reduce_min(float *minVar,float* d_logLuminance)
{
    extern __shared__ float s_data[];   //shared memeory
    unsigned int tId = threadIdx.x;
    unsigned int global_id = blockIdx.x*blockDim.x + tId;

    //Copy to shared mem
    s_data[tId] = d_logLuminance[global_id];
    __syncthreads();

    for(unsigned int i = 1;i<blockDim.x;i*=2)
    {
        if(tId%(2*i) == 0)
        {
            s_data[tId] = min(s_data[tId],s_data[tId+i]);   
        }
        __syncthreads();
    }
    if(tId == 0)
        d_logLuminance[blockIdx.x] = s_data[0];
}

我分配内存和调用内核的部分

size_t size = 512;
    size_t sizeOfgrid = (numRows*numCols)/size;
    const dim3 blockSize(size,1,1); 
    const dim3 gridSize(sizeOfgrid,1,1);
    unsigned int sharedMem =(sizeof(float)*size); //Shared memory per block

    printf("%s\n%d\n%d\n%s%d\n","Cols and Rows:",numCols,numRows,"total:",numRows*numCols);
    printf("%s%d\n%s%d\n%s%d\n","Block Size:",size,"Grid Size:",sizeOfgrid,"shared mem:",sharedMem);

    float* d_lum;
    float* outData;
    checkCudaErrors(cudaMalloc(&d_lum,sizeof(float)*numRows*numCols));
        checkCudaErrors(cudaMemcpy(d_lum,d_logLuminance,sizeof(float)*numRows*numCols,cudaMemcpyDeviceToDevice));   
    checkCudaErrors(cudaMalloc(&outData,sizeof(float)*numRows*numCols));

    reduce_min<<<gridSize,blockSize,sharedMem>>>(outData,d_lum);
    cudaDeviceSynchronize();
    checkCudaErrors(cudaGetLastError());        
    checkCudaErrors(cudaMemcpy(&min_logLum,outData,sizeof(float)*numRows*numCols,cudaMemcpyDeviceToHost));

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

checkCudaErrors(cudaMemcpy(&min_logLum,outData,sizeof(float)*numRows*numCols,cudaMemcpyDeviceToHost));这个函数正在生成一个分段错误,因为我提到了(我在编写代码时错过了它)min_logLum是 float ,而不是 float * 大小 numRows * numCols 。所以最后我试图复制更多分配的内存。