我正在查看此sum_reduction.cu示例和tutorial,並注意到對於某些問題大小它不起作用,例如,它適用於n = 2000的問題大小,但不適用於n = 3000。顯然它總是適用於塊大小倍數的問題大小,但教程和示例代碼都沒有這樣說。問題是,這種簡化算法僅適用於某些問題大小嗎?的例子,他們選擇N = 256K,其爲偶數時,二的冪並且還多個塊大小512CUDA中減少塊大小錯誤?
對於自容納的我粘貼(的模板版本)這裏的代碼的最重要的位:
template<typename T>
__global__ void kernelSum(const T* __restrict__ input, T* __restrict__ per_block_results, const size_t n) {
extern __shared__ T sdata[];
size_t tid = blockIdx.x * blockDim.x + threadIdx.x;
// load input into __shared__ memory
T x = 0.0;
if (tid < n) {
x = input[tid];
}
sdata[threadIdx.x] = x;
__syncthreads();
// contiguous range pattern
for(int offset = blockDim.x/2; offset > 0; offset >>= 1) {
if(threadIdx.x < offset) {
// add a partial sum upstream to our own
sdata[threadIdx.x] += sdata[threadIdx.x + offset];
}
// wait until all threads in the block have
// updated their partial sums
__syncthreads();
}
// thread 0 writes the final result
if(threadIdx.x == 0) {
per_block_results[blockIdx.x] = sdata[0];
}
}
,並調用內核:
// launch one kernel to compute, per-block, a partial sum
block_sum<double> <<<num_blocks,block_size,block_size * sizeof(double)>>>(d_input, d_partial_sums_and_total, num_elements);
// launch a single block to compute the sum of the partial sums
block_sum<double> <<<1,num_blocks,num_blocks * sizeof(double)>>>(d_partial_sums_and_total, d_partial_sums_and_total + num_blocks, num_blocks);
據我瞭解,如果問題大小比塊減少這種說法T x = 0.0;
確保元素置零,因此應該工作更小,但它不?
更新:我很抱歉浮動/雙重的事情是一個錯字,而準備的問題,而不是真正的問題。
你的問題是什麼? –
您可以粘貼一個完整的,可編譯的代碼版本嗎? SO期待這一點。我不想猜測您傳遞的數據,大小,標題等等,因爲問題可能出在您的應用中正在做的其他事情上。 –
爲什麼您要調用''版本的模板化內核,但爲共享內存數組傳遞'sizeof(float)'數量? –