0

我正在编写 CUDA 程序来处理图像。我遇到了分段错误问题,我完全不知道为什么。我可能只是想念一些小事,但经过数小时的尝试纠正自己,我无法让它运行。
根据我硬件上的 deviceQuery(GeForce310M 总 shmem:16384,每个块的最大线程数:512 和最大块暗淡 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));

提前感谢您的帮助。

4

1 回答 1

1

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

于 2013-07-31T14:18:11.020 回答