Cuda的线程和块不完全正常工作

时间:2013-10-22 06:12:00

标签: cuda

我在cuda中用algorithem进行Visual Studio 2010中的图像处理。在我的编码中,我遇到了处理线程和cuda块的问题。所以我的C和CUDA示例代码如下,C代码工作正常,但CUDA代码不能正常工作。 我的C代码

void checkGpuBlockValue(unsigned int *a,unsigned int *b,int length)
{
    for(int i=0;i<length;i++){
        b[i]=a[i]+i;
    }

}

int main()
{
    const int range=1000;
    unsigned int *a=new unsigned int[range];
    unsigned int *b=new unsigned int[range];

    for(int i=0;i<range;i++)
    {
        a[i]=i;
    }

checkGpuBlockValue(a,b,range);

for(int j=0;j<range;j++)
    {
        cout<<"b["<<j<<"] = "<<b[j]<<std::endl;
    }
}

OutPut =

OutPut :
b[0] = 0
b[1] = 2
b[2] = 4
b[3] = 6
b[4] = 8
.
.
.
.
.

b[996] = 1992
b[997] = 1994
b[998] = 1996
b[999] = 1998

这很好。

我的CUDA代码(效果不佳)是;

__global__
void checkGpuBlockValue(unsigned int *a,unsigned int *b,int length)
{
    unsigned int i = (blockIdx.x * blockDim.x) + threadIdx.x;

    if(i<length){
        b[i]=a[i]+i;
    }

}

int main()
{
    const int range=1000;
    unsigned int *a=new unsigned int[range];
    unsigned int *b=new unsigned int[range];

    unsigned int *dev_a;
    unsigned int *dev_b;

    for(int i=0;i<range;i++)
    {
        a[i]=i;
    }

    cudaMalloc( (void**)&dev_a, range* sizeof(unsigned int));
    cudaMalloc( (void**)&dev_b, range* sizeof(unsigned int));

    cudaMemcpy(dev_a, a, range, cudaMemcpyHostToDevice);
    cudaMemcpy(dev_b, a, range, cudaMemcpyHostToDevice);

    static const int BLOCK_WIDTH = 8;       

    //1024 is the maximum number of threads per block for modern GPUs.

    int x = static_cast<int>(ceilf(static_cast<float>(range) / BLOCK_WIDTH));


    const dim3 grid (x,1);                              
    const dim3 block(BLOCK_WIDTH,1);    

    checkGpuBlockValue<<<grid,block>>>(dev_a,dev_b,range);
    cudaDeviceSynchronize();

    cudaMemcpy(b, dev_b, range, cudaMemcpyDeviceToHost);


    for(int j=0;j<range;j++)
    {
        cout<<"b["<<j<<"] = "<<b[j]<<std::endl;
    }

    cudaFree(dev_a);
    cudaFree(dev_b);
}

OUT PUT是:

Out Put =
b[0] = 0
b[1] = 2
b[2] = 4
b[3] = 6
.
.
.
.
.
b[242] = 484
b[243] = 486
b[244] = 488
b[245] = 490
b[246] = 492
b[247] = 494
b[248] = 496
b[249] = 498
b[250] = 3452816845
b[251] = 3452816845
b[252] = 3452816845
b[253] = 3452816845
b[254] = 3452816845
b[255] = 3452816845
b[256] = 3452816845
.
.
.
.
.
.
b[996] = 3452816845
b[997] = 3452816845
b[998] = 3452816845
b[999] = 3452816845

在我的代码中,我在int * a中将值0设置为1000,然后将* a添加到0到1000之间的值,结果存储在int * b中。 因此我的代码适用于0到249(最多250)循环,但在250之后它会给出错误的值。 那我在这做什么错了?请建议我。

1 个答案:

答案 0 :(得分:1)

只需查看代码,您的问题就在这些行中

 cudaMemcpy(dev_a, a, range, cudaMemcpyHostToDevice);
 cudaMemcpy(dev_b, a, range, cudaMemcpyHostToDevice);
 ....
 ....
 cudaMemcpy(b, dev_b, range, cudaMemcpyDeviceToHost);

应该是

 cudaMemcpy(dev_a, a, range* sizeof(unsigned int), cudaMemcpyHostToDevice);
 cudaMemcpy(dev_b, a, range* sizeof(unsigned int), cudaMemcpyHostToDevice);
 ....
 ....
 cudaMemcpy(b, dev_b, range * sizeof(unsigned int), cudaMemcpyDeviceToHost);

我刚刚通过修改你的代码来检查它是否按预期工作。但我强烈建议您将正确的错误检查作为良好的编程习惯。