【发布时间】:2013-07-18 10:11:14
【问题描述】:
我正在编写一个程序来将 rgba 图像转换为灰度。我在这方面做了很多工作并正确实现了内核。然而,网格大小可能是错误的,尽管我的逻辑是正确的。
内核:
__global__
void rgba_to_greyscale(const uchar4* const rgbaImage,
unsigned char* const greyImage,
int numRows, int numCols)
{
int x = (blockIdx.x * blockDim.x) + threadIdx.x;
int y = (blockIdx.y * blockDim.y) + threadIdx.y;
if(x >= numCols || y >= numRows)
return;
uchar4 rgba = rgbaImage[x+y];
float channelSum = 0.299f*rgba.x + 0.587f*rgba.y + 0.114f*rgba.z;
greyImage[x+y] = channelSum;
}
内核启动:
const dim3 blockSize(10, 10, 1); //TODO
size_t gridSizeX, gridSizeY;
gridSizeX = numCols + (10 - (numCols % 10) ); //adding some number to make it multiple of 10
gridSizeY = numRows + (10 - (numRows % 10) ); //adding some number to make it multiple of 10
const dim3 gridSize( gridSizeX, gridSizeY, 1); //TODO
rgba_to_greyscale<<<gridSize, blockSize>>>(d_rgbaImage, d_greyImage, numRows, numCols);
我正在创建更多数量的线程,然后在内核中应用绑定检查。
【问题讨论】:
-
这是一种广泛使用的逻辑,用于创建更多线程并在内核内部执行绑定检查。这是计算网格大小的通用公式。
gridSizeX = (numCols + blockSize.x - 1)/blockSize.x; -
this 的可能重复项。我认为这是 udacity 课程的标准问题..
-
@SagarMasuti;我也读了那篇文章,但我无法弄清楚我的代码有什么问题。如果您能在我的(逻辑正确的)代码中指出错误,那将会很有帮助。
-
即使在写完这个之后:gridSizeX = (numCols + blockSize.x - 1) / blockSize.x; gridSizeY = (numRows + blockSize.y - 1) / blockSize.y; const dim3 gridSize(gridSizeX, gridSizeY, 1);它只从顶部转换一条非常薄的像素。有人可以验证内核本身是否正确吗?
标签: image-processing cuda gpu