如何降低CUDA归约操作中__syncthreads()产生的屏障等待延迟?
我的代码从全局内存读取数据执行计算,最终需要用对应线程块内所有数据的和做归一化。我采用了Mark Harris所著《Optimizing Parallel Reduction in CUDA》中的归约技术实现求和,相比循环求和性能提升了近5倍。但求和完成后,__syncthreads()会导致warp出现大量停顿,成为代码的性能瓶颈。
我的代码SM使用率约为39%,显存使用率约为8%,据此判断代码属于延迟绑定型。每个调度器理论支持8个warp,活跃warp数为7.71,但合格(eligible)warp数仅为0.24,「无合格warp占比」为77%。我判断每指令停顿等待(stall wait)和屏障停顿(stall barrier)分别为13.59和13.04周期,是合格warp数偏低的原因。我在下方代码注释中标注了停顿行的采样数据,其中占比29%的stall 1是我重点关注的优化点。
我的实际代码由于要读写的变量更多、使用的方程不同,复杂度更高,但核函数逻辑和下方给出的示例基本一致:
#include <stdio.h> #define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); } inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort = true) { if (code != cudaSuccess) { fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line); if (abort) exit(code); } } #define length_1 256 #define length_2 100 #define length_3 250 __device__ float * d; __global__ void kernel() { int tid = threadIdx.x; __shared__ float d_for_current_block[512]; d_for_current_block[tid] = d[blockIdx.x * blockDim.x + threadIdx.x]; // Move to shared memory because originally d isn't coalesced. for (int k = 0; k < length_2; k++) { __shared__ float power_sums[512]; __shared__ float powers[512]; float power = (2 * d_for_current_block[tid] - 1.0) / 1.0; // Stall 5; sampling data [all] = 1,111 powers[tid] = power; power_sums[tid] = power * power; // Sum powers of all threads in current block. __syncthreads(); if (tid < 128) // Stall 4; sampling data [all] = 1,159 power_sums[tid] += power_sums[tid + 128]; __syncthreads(); if (tid < 64) // Stall 3; sampling data [all] = 2,213 power_sums[tid] += power_sums[tid + 64]; __syncthreads(); if (tid < 32) { // Stall 2; sampling data [all] = 3,100 power_sums[tid] += power_sums[tid + 32]; power_sums[tid] += power_sums[tid + 16]; power_sums[tid] += power_sums[tid + 8]; power_sums[tid] += power_sums[tid + 4]; power_sums[tid] += power_sums[tid + 2]; power_sums[tid] += power_sums[tid + 1]; } __syncthreads(); powers[tid] = powers[tid] / power_sums[0]; // Stall 1; sampling data [all] = 17,016 } } int main(){ float * h, * d_ptr; h = (float *) malloc(length_1 * length_2 * length_3 * sizeof(float)); for (int k = 0; k < length_3; k++) for (int j = 0; j < length_1; j++) for (int i = 0; i < length_2; i++) h[i] = 100 * (float) rand() / (float)(RAND_MAX); gpuErrchk( cudaMalloc((void**) &d_ptr, length_1 * length_2 * length_3 * sizeof(float)) ); gpuErrchk( cudaMemcpy(d_ptr, h, length_1 * length_2 * length_3 * sizeof(float), cudaMemcpyHostToDevice) ); gpuErrchk( cudaMemcpyToSymbol(d, &d_ptr, sizeof(float*)) ); kernel<<<length_3, length_1>>>(); gpuErrchk( cudaFree(d_ptr) ); free( h ); gpuErrchk( cudaPeekAtLastError() ); gpuErrchk( cudaDeviceSynchronize() ); }
我的问题是:有没有办法降低屏障等待产生的延迟?我明白该延迟的产生原因,但想知道是否可以通过代码重构或其他我没有想到的方法降低延迟。我尝试过调整线程块大小,但性能没有明显变化,这也符合预期,因为无论块大小如何,warp都需要等待所有线程执行完成才能继续后续操作。
据我所知,隐藏延迟的一种方法是让warp在等待时执行其他计算,但我除了获取归一化值power_sums[0]并对所有powers做归一化外,没有其他需要执行的计算。请问是否有其他优化方案,还是说这个问题的性能上限就是我当前得到的结果?
编辑更新
我根据Robert Crovella的评论更新了代码,新的性能指标如下:SM使用率=39%;显存使用率=67%;活跃warp=7.95;每个调度器合格warp数=0.25;无合格warp占比=76.04%。
我的原始完整代码的SM使用率和显存使用率分别为49%和5%,其余指标和上述结果相近。
更新后的代码:
#include <stdio.h> #define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); } inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort = true) { if (code != cudaSuccess) { fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line); if (abort) exit(code); } } #define length_1 256 #define length_2 100 #define length_3 250 __device__ float * d; __global__ void kernel(float * d_all_sums) { int tid = threadIdx.x; float final = 0.0; for (int i = 0; i < length_2; i++) { __shared__ float power_sums[512]; __shared__ float powers[512]; int index = blockIdx.x * blockDim.x * length_2 + threadIdx.x * length_2 + i; float po = d[index]; // Stall 2; sampling data [all] = 4,114 float power = (2 * po - 1.0) / 2.0; // Stall 5; sampling data [all] = 1,111 powers[tid] = power; power_sums[tid] = power * power; // Sum powers of all threads in current block. __syncthreads(); if (tid < 128) // Stall 5; sampling data [all] = 1,172 power_sums[tid] += power_sums[tid + 128]; __syncthreads(); if (tid < 64) // Stall 4; sampling data [all] = 2,037 power_sums[tid] += power_sums[tid + 64]; __syncthreads(); if (tid < 32) { // Stall 3; sampling data [all] = 2,843 power_sums[tid] += power_sums[tid + 32]; power_sums[tid] += power_sums[tid + 16]; power_sums[tid] += power_sums[tid + 8]; power_sums[tid] += power_sums[tid + 4]; power_sums[tid] += power_sums[tid + 2]; power_sums[tid] += power_sums[tid + 1]; } __syncthreads(); final += powers[tid] / power_sums[0]; // Stall 1; sampling data [all] = 15,775 } if (tid == 0) d_all_sums[blockIdx.x] = final; } int main(){ float * h, * h_all_sums; float * d_ptr, * d_all_sums; h = (float *) malloc(length_1 * length_2 * length_3 * sizeof(float)); h_all_sums = (float *) malloc(length_3 * sizeof(float)); memset(h_all_sums, 0.0, length_3); for (int k = 0; k < length_3; k++) for (int j = 0; j < length_1; j++) for (int i = 0; i < length_2; i++) h[k * length_1 * length_2 + j * length_2 + i] = 100 * (float) rand() / (float)(RAND_MAX); gpuErrchk( cudaMalloc((void**) &d_ptr, length_1 * length_2 * length_3 * sizeof(float)) ); gpuErrchk( cudaMemcpy(d_ptr, h, length_1 * length_2 * length_3 * sizeof(float), cudaMemcpyHostToDevice) ); gpuErrchk( cudaMemcpyToSymbol(d, &d_ptr, sizeof(float*)) ); gpuErrchk( cudaMalloc( (void**) &d_all_sums, length_3 * sizeof(float) ) ); gpuErrchk( cudaMalloc((void**) &d_ptr, length_1 * length_2 * length_3 * sizeof(float)) ); gpuErrchk( cudaMemcpy(d_ptr, h, length_1 * length_2 * length_3 * sizeof(float), cudaMemcpyHostToDevice) ); kernel<<<length_3, length_1>>>(d_all_sums); gpuErrchk( cudaMemcpy(h_all_sums, d_all_sums, length_3 * sizeof(float), cudaMemcpyDeviceToHost) ); FILE *f = fopen("test_output.txt", "w"); if (f != NULL) { for (int k = 0; k < length_3; k++) fprintf(f, "k = %d; power = %f.\n", k, h_all_sums[k]); } fclose( f ); gpuErrchk( cudaFree(d_ptr) ); gpuErrchk( cudaFree(d_all_sums) ); free( h ); free( h_all_sums ); gpuErrchk( cudaPeekAtLastError() ); gpuErrchk( cudaDeviceSynchronize() ); }
可从以下几个方向降低屏障延迟,提升性能:
- 优化高开销的除法操作
占比最高的Stall1主要来自除法运算,GPU的除法吞吐量仅为乘法的1/10~1/15,你可以先计算float inv_sum = 1.0f / power_sums[0],后续所有线程用乘法powers[tid] * inv_sum替代除法,这一项就能带来显著的性能提升。 - 减少不必要的屏障同步
你当前的归约逻辑中,tid<32的操作仅在同一个warp内执行,warp内线程天然同步,无需额外同步。如果改用__shfl_down_syncwarp洗牌指令实现归约,还可以进一步减少__syncthreads()的调用次数:256线程的块仅需2次全局同步即可完成归约,相比当前的4次同步直接降低一半屏障开销。 - 提升Grid规模隐藏延迟
你当前的Grid仅250个块,远低于主流GPU的块驻留上限,没有足够的活跃warp用来隐藏屏障等待的延迟。可以通过调整任务划分方式,增大Grid规模,让每个SM同时驻留更多块,当某个块等待屏障时,SM可以调度其他块的warp执行,直接降低无合格warp的占比。 - 修复冗余代码
更新后的主函数存在重复的cudaMalloc和cudaMemcpy操作,第一次分配的显存没有释放会造成内存泄漏,同时额外的显存拷贝也会浪费执行时间,删除冗余代码即可。
内容的提问来源于stack exchange,提问作者tripatheea

