Hard
Counting above-threshold values in the low 16 lanes of each warp
The intent is to count, for each block, how many of the values held by the low 16 lanes of each warp are above threshold. blockDim.x is 256 and the grid exactly covers data, so every thread reaches the __syncthreads(). The kernel runs on a Volta-or-later GPU. What is the status of this code, and what is the right fix?
kernel.cu
__global__ void countAbove(const float* data, int* blockCounts, float threshold) {
__shared__ int warpHits[8]; // blockDim.x == 256
int i = blockIdx.x * blockDim.x + threadIdx.x;
int lane = threadIdx.x & 31;
int warp = threadIdx.x >> 5;
float v = data[i];
if (lane < 16) {
unsigned hits = __ballot_sync(0xffffffff, v > threshold);
if (lane == 0) warpHits[warp] = __popc(hits);
}
__syncthreads();
if (threadIdx.x == 0) {
int sum = 0;
for (int w = 0; w < 8; ++w) sum += warpHits[w];
blockCounts[blockIdx.x] = sum;
}
}36 of 46 in GPU / CUDA