[英]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
這項作業, 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更好的性能。在以小值增加塊大小和網格大小方面,我只有幾ns的差異。 例如:
blockSize = (10, 1, 1)
gridSize = (10, 1, 1)
Your code ran in: 34.835167 msecs.
我不明白為什么更高的數字不會獲得更好的性能,而是導致更差的性能。 同樣,增加塊大小似乎比網格大小更好。
您可以計算啟動的每個線程中的所有像素,即內核是完全串行的。 使用更多的塊或更大的塊只是重復計算。 在后一種情況下,為什么不將for循環移出內核並讓每個線程計算一個像素?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.