[英]About compact operation in cuddpp
以下內核功能是cudpp(一個cuda庫,http://gpgpu.org/developer/cudpp)中的緊湊操作。
我的問題是為什么開發人員重復寫作部分8次? 以及為什么它可以提高性能?
為什么一個線程處理8個元素,為什么每個線程都不處理一個元素?
template <class T, bool isBackward>
__global__ void compactData(T *d_out,
size_t *d_numValidElements,
const unsigned int *d_indices, // Exclusive Sum-Scan Result
const unsigned int *d_isValid,
const T *d_in,
unsigned int numElements)
{
if (threadIdx.x == 0)
{
if (isBackward)
d_numValidElements[0] = d_isValid[0] + d_indices[0];
else
d_numValidElements[0] = d_isValid[numElements-1] + d_indices[numElements-1];
}
// The index of the first element (in a set of eight) that this
// thread is going to set the flag for. We left shift
// blockDim.x by 3 since (multiply by 8) since each block of
// threads processes eight times the number of threads in that
// block
unsigned int iGlobal = blockIdx.x * (blockDim.x << 3) + threadIdx.x;
// Repeat the following 8 (SCAN_ELTS_PER_THREAD) times
// 1. Check if data in input array d_in is null
// 2. If yes do nothing
// 3. If not write data to output data array d_out in
// the position specified by d_isValid
if (iGlobal < numElements && d_isValid[iGlobal] > 0) {
d_out[d_indices[iGlobal]] = d_in[iGlobal];
}
iGlobal += blockDim.x;
if (iGlobal < numElements && d_isValid[iGlobal] > 0) {
d_out[d_indices[iGlobal]] = d_in[iGlobal];
}
iGlobal += blockDim.x;
if (iGlobal < numElements && d_isValid[iGlobal] > 0) {
d_out[d_indices[iGlobal]] = d_in[iGlobal];
}
iGlobal += blockDim.x;
if (iGlobal < numElements && d_isValid[iGlobal] > 0) {
d_out[d_indices[iGlobal]] = d_in[iGlobal];
}
iGlobal += blockDim.x;
if (iGlobal < numElements && d_isValid[iGlobal] > 0) {
d_out[d_indices[iGlobal]] = d_in[iGlobal];
}
iGlobal += blockDim.x;
if (iGlobal < numElements && d_isValid[iGlobal] > 0) {
d_out[d_indices[iGlobal]] = d_in[iGlobal];
}
iGlobal += blockDim.x;
if (iGlobal < numElements && d_isValid[iGlobal] > 0) {
d_out[d_indices[iGlobal]] = d_in[iGlobal];
}
iGlobal += blockDim.x;
if (iGlobal < numElements && d_isValid[iGlobal] > 0) {
d_out[d_indices[iGlobal]] = d_in[iGlobal];
}
}
我的問題是為什么開發人員重復寫作部分8次? 以及為什么它可以提高性能?
如@torrential_coding所述,循環展開可以幫助提高性能。 特別是在這種情況下,循環非常緊密(其中幾乎沒有邏輯)。 但是,編碼人員應該使用CUDA支持自動循環展開,而不是手動執行。
為什么一個線程處理8個元素,為什么每個線程都不處理一個元素?
僅計算iGlobal的完整索引並檢查每8個操作而不是每個操作的threadIdx.x為零,可能會獲得一些小的性能提升,如果每個內核僅執行一個元素,則必須這樣做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.