推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
rungo
V2EX  ›  Python

为什么这段 cuda 的并行前缀算法不会数据竞争

  •  
  •   rungo · 3h 16m ago · 272 views

    比如在 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")
    
    4 replies    2026-07-06 00:50:40 +08:00
    r6cb
        1
    r6cb  
       3h 1m ago
    你这都是写入到 sum_buf[tid],每个线程 tid 不同,怎么会和其他线程冲突呢
    rungo
        2
    rungo  
    OP
       2h 52m ago
    @r6cb 比如 iter=1 时, tid=2 线程会读取 sum_buf[1] ,tid=1 线程会写入 sum_buf[1]
    flyqie
        3
    flyqie  
       2h 9m ago
    好奇 AI 对这种问题的解释怎么样?

    感觉现在 AI 应该对这种还算"聪明"?
    rungo
        4
    rungo  
    OP
       1h 54m ago
    @flyqie 问了几个 ai 不能统一,这段代码我从书上抄过来的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   993 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 18:44 · PVG 02:44 · LAX 11:44 · JFK 14:44
    ♥ Do have faith in what you're doing.