Count 2d Array Element
count
November 12,2025
#include <cuda_runtime.h>
__global__ void count_2d_equal_kernel(const int* input, int* output, int N, int M, int K) {
int idx = blockDim.x*blockIdx.x + threadIdx.x;
int idy = blockDim.y*blockIdx.y + threadIdx.y;
if ((idx<N && idy<M) && input[idx*M+idy]==K) atomicAdd(output,1);
}
// input, output are device pointers (i.e. pointers to memory on the GPU)
extern "C" void solve(const int* input, int* output, int N, int M, int K) {
dim3 threadsPerBlock(16, 16);
dim3 blocksPerGrid((N + threadsPerBlock.x - 1) / threadsPerBlock.x,
(M + threadsPerBlock.y - 1) / threadsPerBlock.y);
count_2d_equal_kernel<<<blocksPerGrid, threadsPerBlock>>>(input, output, N, M, K);
cudaDeviceSynchronize();
}