cuda复制数组从全局内存到共享内存

时间:2013-01-11 22:15:08

标签: cuda shared-memory

hy,我正在尝试将2个数组从全局内存复制到共享内存: 全局数组类型是double,每个都有32个元素。 网格是1D,块是1D。网格维度为10000,NumberThreadPerBlock为32

__global__ void kernel_0(double px[], double py[], int N)
{
int ii,

    jj,tid;
    tid=blockIdx.x*blockDim.x + threadIdx.x;
    __shared__ double s_px[256];
    __shared__ double s_py[256];
    __shared__ double s[256];

s_px[threadIdx.x]=px[tid];
s_py[threadIdx.x]=py[tid];
s[threadIdx.x]=py[tid];
__syncthreads();
}


int main (int argc, char *argv[]){
    double *px, *py , *x, *y, PI, step, *d_x, *d_y,*d_px, *d_py,sharedMemSize;
    int N, Nx, ii;
    PI = 4*atan(1.0);
    Nx = 10000; 
    N = 32; 

    px = (double *) malloc(N*sizeof(double));
    py = (double *) malloc(N*sizeof(double));

    // lookup table: sin // from 0 to PI 
    step = 1.0 / (N-1);
    for (ii = 0; ii < N; ii++){ 
        px[ii] = ii*step*PI;
        py[ii] = sin(px[ii]);
    }   

    cudaMalloc( (void **) &d_px, N*sizeof(double) );
    cudaMalloc( (void **) &d_py, N*sizeof(double) );        

    cudaMemcpy( d_px, px, N*sizeof(double), cudaMemcpyHostToDevice );
    cudaMemcpy( d_py, py, N*sizeof(double), cudaMemcpyHostToDevice );

    dim3 dimGrid(Nx);
    dim3 dimBlock(N,1,1);
    kernel_0<<< dimGrid, dimBlock>>>(px, py, N);

}

它编译但是cuda-memmcheck给我看了很多错误:

========= Invalid __global__ read of size 8
=========     at 0x00000058 in kernel_0
=========     by thread (31,0,0) in block (6,0,0)
=========     Address 0x11e0db38 is out of bounds
=========
========= ERROR SUMMARY: 96 errors
你可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

从我所看到的,分配给设备指针(px,py)的内存是32 * sizeof(double),但是你拥有的块数是10000.设备内存是全局的,所有块共享它,只有为每个块定义共享内存。因此,对于blockId.x = 1,您应该获得无效的内存访问。而且,在内核调用中,它应该是d_px,d_py。