Title: 为什么这段 cuda 的并行前缀算法不会数据竞争 | BestBlogs.dev
URL Source: https://www.bestblogs.dev/article/21550ef7?amp%3Butm_medium=feed&%3Butm_campaign=resources&%3Bentry=rss_article_item
Published Time: 2026-07-05 23:32:03
Markdown Content: 80
This article explains why a seemingly conflicting CUDA parallel prefix sum kernel has no data race by analyzing the __syncthreads() synchronization mechanism and thread index conditions. V V2EX
Yesterday 80 words (about 1 min) View Source →
Sign in to highlight text and take notes as you read. Sign in now
比如在 iter=1 这一轮的时候,同时会写入和读取 sum_buf[1]
naive_ker = SourceModule("""
__global__ void naive_prefix(double vec, double out)
{
__shared__ double sum_buf[1024];
int tid = threadIdx.x;
sum_buf[tid] = vec[tid];
int iter = 1;
for (int i=0; i < 10; i++)
{
__syncthreads();
if (tid >= iter )
{
sum_buf[tid]= sum_buf[tid] + sum_buf[tid - iter];
}
iter *= 2;
}
__syncthreads();
out[tid] = sum_buf[tid];
__syncthreads();
}
""")
naive_gpu = naive_ker.get_function("naive_prefix")