我正在编写一个将 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);
我正在创建更多数量的线程然后需要,然后在内核中应用绑定检查。