无法理解 CUDA 内核启动的行为
Can't understand the behaviour of CUDA kernel launch
#include "utils.h"
__global__
void rgba_to_greyscale(const uchar4* const rgbaImage,
unsigned char* const greyImage,
int numRows, int numCols)
{
for (size_t r = 0; r < numRows; ++r) {
for (size_t c = 0; c < numCols; ++c) {
uchar4 rgba = rgbaImage[r * numCols + c];
float channelSum = 0.299f * rgba.x + 0.587f * rgba.y + 0.114f * rgba.z;
greyImage[r * numCols + c] = channelSum;
}
}
}
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)
{
const dim3 blockSize(1, 1, 1); //TODO
const dim3 gridSize( 1, 1, 1); //TODO
rgba_to_greyscale<<<gridSize, blockSize>>>(d_rgbaImage, d_greyImage, numRows, numCols);
cudaDeviceSynchronize(); checkCudaErrors(cudaGetLastError());
}
这是用于将颜色图像转换为灰度的代码。我正在为一门课程进行这项任务,并在completing it
之后得到了这些结果。
A.
blockSize = (1, 1, 1)
gridSize = (1, 1, 1)
Your code ran in: 34.772705 msecs.
B.
blockSize = (numCols, 1, 1)
gridSize = (numRows, 1, 1)
Your code ran in: 1821.326416 msecs.
C.
blockSize = (numRows, 1, 1)
gridSize = (numCols, 1, 1)
Your code ran in: 1695.917480 msecs.
D.
blockSize = (1024, 1, 1)
gridSize = (170, 1, 1) [the image size is : r=313, c=557, blockSize*gridSize ~= r*c]
Your code ran in: 1709.109863 msecs.
我尝试了更多的组合,但是没有一个比A更好的表现。我在增加块和网格时只有几个差异,而差异很小。例如:
blockSize = (10, 1, 1)
gridSize = (10, 1, 1)
Your code ran in: 34.835167 msecs.
我不明白为什么更高的数字不会获得更好的性能,而是导致性能较差。而且,似乎增加的块质量比网格大小好。
您计算启动的每个线程中的所有像素,即内核是完全串行的。使用更多块或更大的块只是重复计算。在后一种情况下,为什么不将for循环从内核中移出,并且每个线程都计算一个像素?
相关文章:
- 如何创建一个空的全局类并在启动时实例化它
- 如何在内核C++中使用1920x1080x16M图形或类似的16M颜色?(VGA)
- OpenCL 在 NVIDIA 和 Intel GPU 上启动内核时CL_INVALID_COMMAND_QUEUE
- 在Windows上坚持Jupyter:内核无法启动
- 无法理解 CUDA 内核启动的行为
- 内核AIO(libaio)写入请求在启动时从保留的内存中写入时失败
- CUDA并发内核启动不工作
- Visual Studio 在调试内核驱动程序时无法启动调试会话
- 在 CUDA 5.5 中启动内核函数时发生的错误
- CUDA在使用函数指针时启动主机函数作为内核
- 在一台有n个内核的机器中,确定要启动的线程数的最佳方法是什么?(C++)
- 从一个内核启动到另一个内核启动的共享内存持久化
- 从静态初始化代码启动CUDA内核时遇到问题
- 同一个内核的多次启动是否可以访问共享变量?
- 每次启动都重复运行CUDA内核
- 让eclipse解析或忽略CUDA内核启动参数
- 内核启动指定流,但使用默认共享内存大小
- 如何启动python内核并使用ZMQ套接字进行连接
- CUDA内核使用模板启动宏
- 启动大内核时出现未知错误