void your_rgba_to_greyscale(const uchar4 * const h_rgbaImage, uchar4 * const d_rgbaImage, unsigned char* const d_greyImage, size_t numRows, size_t numCols)
{
size_t totalPixels = numRows * numCols;
size_t gridRows = totalPixels / 32;
size_t gridCols = totalPixels / 32;
const dim3 blockSize(32,32,1);
const dim3 gridSize(gridCols,gridRows,1);
rgba_to_greyscale<<<gridSize, blockSize>>>(d_rgbaImage, d_greyImage, numRows, numCols);
cudaDeviceSynchronize(); checkCudaErrors(cudaGetLastError());
}
另一种方法是:
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;
uchar4 rgba = rgbaImage[x * numCols + y];
float channelSum = 0.299f * rgba.x + 0.587f * rgba.y + 0.114f * rgba.z;
greyImage[x * numCols + y] = channelSum;
}
错误消息如下所示:
libdc1394 error: failed to initialize libdc1394
Cuda error at student_func.cu:76
unspecified launch failure cudaGetLastError()
we were unable to execute your code. Did you set the grid and/or block size correctly?
但随后它表示代码已经编译完成,
Your code compiled!
error output: libdc1394 error: Failed to initialize libdc1394
Cuda error at student_func.cu:76
unspecified launch failure cudaGetLastError()
第一段代码块的最后一行是第76行,据我所知,我没有对其进行任何更改。 第76行如下:
rgba_to_greyscale<<<gridSize, blockSize>>>(d_rgbaImage, d_greyImage, numRows, numCols);
我实际上找不到 cudaGetLastError()
的声明。
我主要关心如何设置网格/块的维度以及第一种方法是否正确地映射了一维像素位置数组和我的线程之间的关系。
编辑:
我想我误解了一些东西。 numRows
是垂直方向上的像素数吗?numCols
是水平方向上的像素数吗?
我的块由 8 x 8 个线程组成,其中每个线程代表一个像素?如果是这样,我假设这就是我在计算 gridRows
时必须除以 4 的原因,因为图像不是正方形?我假设我也可以创建一个比例为 2:1 的列:行的块?
编辑 2:
我刚试图将我的块更改为2:1的比例,以便我可以将numRows
和numCol
除以相同的数字,但现在它显示底部和侧面有空白区域。为什么底部和侧面都有空白区域?我没有改变我的网格或块的y维度。