0

我整天都在坚持这个。以下程序将给出“超出范围共享或本地地址”错误。注释掉这一行将解决这个问题。

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;
    }
}
4

1 回答 1

6

改变这个:

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

对此:

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

您传递给内核的动态共享分配的大小以字节为单位。您需要分配足够的字节来覆盖 88 个float数量。

于 2013-07-22T16:01:29.420 回答