0

我是 cuda 的新手。我正在为 cuda 中的图像处理编写代码。我的 c 和 cuda 代码如下,我尝试转换为 cuda,但效果不佳。

我的 C 代码:

void imageProcess_usingPoints(int point, unsigned short *img)
{
    // doing image process here using point variable value.
}

int main(int argc, char **argv)
{
 /* here i define and initialize some variable */

    int point=0;
    unsigned short *image_data;
     // consider that here i read image and store all pixels value in *image_data.

 for(int i=0;i<1050;i++,point+=1580)
 {


    // calling image process function like bluring image.
    imageProcess_usingPoints(point,image_data);
    /* doing some image process  using that point value on 16 bit grayscale image.*/
 } 


 return 0;
}

我试图将我的 c 代码转换为 cuda,但它是错误的。所以,我尝试过的cuda代码如下。

__global__ void processOnImage(int pointInc)
{
     int line = blockIdx.x * blockDim.x + threadIdx.x;
     int point=((line)*pointInc));
      /* here i m not getting exact vaue of point variable as same like in c code */
    /* doing image processing here using point value */

}


int main(int argc, char **argv)
{
 /* here i define and initialize some variable */

    int pointInc=1580;
    static const int BLOCK_WIDTH = 25;
    int x = static_cast<int>(ceilf(static_cast<float>(1050) / BLOCK_WIDTH));
    const dim3 grid (x,1);
    const dim3 block(BLOCK_WIDTH,1);
    processOnImage<<<grid,block>>>(pointInc);

 return 0;
}

在 cuda 代码的 processOnImage 函数中,我没有像上面的 c 代码那样获得点(int 点)变量的确切值。那么我在 cuda 代码中做错了什么。或者如何在 c 中为我的代码使用该块和线程。

4

1 回答 1

1

基本上你可以将每个块的线程设置为warpSize(或只是 32 的倍数)的倍数

http://docs.nvidia.com/cuda/cuda-c-programming-guide/#warpsize

通常 256 对于最简单的内核来说是一个不错的选择。确切的数字必须调整。CUDA安装目录中的这个工具也可以帮你选号。

$CUDA_HOME/tools/CUDA_Occupancy_Calculator.xls

在确定每个块的线程数后,您可以计算数据大小所需的块数。以下示例显示了如何执行此操作。

https://developer.nvidia.com/content/easy-introduction-cuda-c-and-c

另一方面,您也可以将固定数量的块用于任意数据大小。有时您可以通过这种方式获得更高的性能。有关更多详细信息,请参阅此内容。

https://developer.nvidia.com/content/cuda-pro-tip-write-flexible-kernels-grid-stride-loops

于 2013-10-17T10:06:46.887 回答