0
我有以下CUDA程序,它将图像从RGBA转换为灰度并行。我想也有一个将按顺序运行的版本,这将允许我比较两者并获得像加速等指标。将parralel CUDA程序转换为按顺序运行
从我的理解,为了顺序运行,我需要以一种方式编辑意味着使用两个for循环(一个用于X,一个用于Y)逐像素地逐个图像。然后应该在像素上运行灰度转换,然后再转到下一个像素上。
虽然我对我应该做什么有一些想法,但我不确定我应该在哪里编辑代码以及从哪里开始。
编辑:我现在明白,它是内核本身,我需要进行编辑,以使我的程序顺序。
如下图所示,
__global__ void colorConvert(unsigned char * grayImage, unsigned char * rgbImage, unsigned int width, unsigned int height)
{
unsigned int x = threadIdx.x + blockIdx.x * blockDim.x;
//unsigned int y = threadIdx.y + blockIdx.y * blockDim.y; //this is needed if you use 2D grid and blocks
//if ((x < width) && (y < height)) {
//check if out of bounds
if ((x < width*height)) {
// get 1D coordinate for the grayscale image
unsigned int grayOffset = x;// y*width + x; //this is needed if you use 2D grid and blocks
// one can think of the RGB image having
// CHANNEL times columns than the gray scale image
unsigned int rgbOffset = grayOffset*CHANNELS;
unsigned char r = rgbImage[rgbOffset]; // red value for pixel
unsigned char g = rgbImage[rgbOffset + 1]; // green value for pixel
unsigned char b = rgbImage[rgbOffset + 2]; // blue value for pixel
// perform the rescaling and store it
// We multiply by floating point constants
grayImage[grayOffset] = 0.21f*r + 0.71f*g + 0.07f*b;
}
}
因为有很多的太期待通过我已删除了我的代码的其余部分从问题。如果我想让这个内核以连续的方式运行,使用两个for循环遍历每个像素并将代码行应用于每一个像素,我将如何去做这件事?
嗨,三江源非常多的功能!这是x
benjano
在你的内核中,你可以看到你的图像就像一维图片,所以你可以用你的CPU代码做同样的事情。在这种情况下,您不需要像素周围像素的信息,因此您可以像看到一维数组一样更容易地看到图像 – Alexandre