0

我在 cuda 中为 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 :
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 =
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

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

4

1 回答 1

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);

我刚刚通过修改您的代码进行了检查,它可以按您的预期工作。但我强烈建议您将正确的错误检查作为良好的编程习惯。

于 2013-10-22T06:25:01.593 回答