cuda奇怪的内存访问错误共享内存

时间:2013-07-22 15:31:46

标签: cuda

我整天坚持这一点。 以下程序将给出“超出范围的共享或本地地址”错误。 评论这一行将解决这个问题。

hist[tidx] = 0;

但是,我认为分配大小为88 * 4字节的共享内存不会有任何问题。

注释掉这一行也将解决问题

NVMatrix Acts(acts, true);

似乎如果我在全局内存中分配Acts矩阵,共享内存将表现异常。有什么想法吗?

int main(int argc, char ** argv)
{
    float * act = new float[2985984];
    for (int i=0; i<2985984; i++)
        act[i] = 0.0001*(i+1);

    Matrix acts(act, 23328, 128);   // use act as the data to initialize the 23328x128, matrix in cpu

    NVMatrix Acts(acts, true);      // create a Acts Matrix which uses GPU global memory, and copies the value from CPU to GPU
                                    // If comment out this line, there is no problem to execute the program

    float cost = Calculate();

}

float Calculate()
{
    dim3 blocks(4,96);
    dim3 threads(32,8);

    cudaFuncSetCacheConfig(createShare<8, 32>, cudaFuncCachePreferShared);

    int numLabels = 88;

    createShare<8, 32><<<blocks, threads, numLabels>>>(numLabels);

    return 0;
}

template <int B_Y, int B_X>
__global__ void createShare(int numLabels)
{
    extern __shared__ float hist[];

    int tidx = threadIdx.y * B_X + threadIdx.x;
    if (tidx<numLabels) {
        printf("block %d %d %d\n", blockIdx.x, blockIdx.y, tidx);
        hist[tidx] = 0;
    }
}

1 个答案:

答案 0 :(得分:6)

改变这个:

createShare<8, 32><<<blocks, threads, numLabels>>>(numLabels);

到此:

createShare<8, 32><<<blocks, threads, numLabels*sizeof(float)>>>(numLabels);

传递给内核的动态共享分配的大小是 bytes 。您需要分配足够的字节以涵盖88 float个数量。