文章预览
本文由 @Simon V(https://github.com/simveit) 授权转载和翻译并发表到本公众号。 让向量求和变得非常快 06 Apr, 2025 在这篇博客文章中,我们将简要描述如何为向量归约任务实现最先进的性能,即我们的程序应该执行以下操作:给定一个向量v,返回v中所有元素的和。我们将假设向量很大,即它包含 N = 1 < < 30 = 2^30 个元素。 基准实现 template < unsigned int threadsPerBlock> __global__ void kernel_0 ( const int *d_in, int *d_out, size_t N) { extern __shared__ int sums[threadsPerBlock]; int sum = 0 ; const int tid = threadIdx.x; const int global_tid = blockIdx.x * threadsPerBlock + tid; const int threads_in_grid = threadsPerBlock * gridDim.x; for ( int i = global_tid; i < N; i += threads_in_grid) { sum += d_in[i]; } sums[tid] = sum; __syncthreads(); for ( int activeThreads = threadsPerBlock >> 1 ; activeThreads; act
………………………………