Hard
A shared-memory reduction predicated on threadIdx.x < s
This is the standard fix for the naive interleaved-addressing reduction above, same total work, same result, but restructured indexing. blockDim.x == 256. Why is this version faster, specifically with respect to warp divergence?
kernel.cu
__shared__ float sdata[256];
sdata[threadIdx.x] = input[blockIdx.x * blockDim.x + threadIdx.x];
__syncthreads();
for (int s = blockDim.x / 2; s > 0; s >>= 1) {
if (threadIdx.x < s) {
sdata[threadIdx.x] += sdata[threadIdx.x + s];
}
__syncthreads();
}
// result in sdata[0]22 of 46 in GPU / CUDA