c - 无法理解 __syncthreads()

标签 c cuda parallel-processing nvidia

书中引述:

In CUDA, a __syncthreads() statement , if present must be executed by all threads in a block . When a __syncthreads() is placed in an ifstatement ,either all threads in a block execute the path that includes the __syncthreads()or none of them does . For an if-then-else statement , if each path has a __syncthreads()statement , either all threads in a block execute the __syncthreads() on the then path or all of them execute the elsepath . The two __syncthreads() are different barrier synchronization points . If a thread in a block executes the then path and another executes the else path,they would be waiting at different barrier synchronization points . They would end up waiting for each other forever . It is the responsibility of the programmers to write their code so that these requirements are satisfied .

没有给出 ifif-else-then 情况的例子,所以我无法理解这个概念。请用简单的话向我解释这两种情况。

PS:我是并行编程和 CUDA 的初学者。

提前致谢。

最佳答案

假设您有一个内核,该内核使用一个由 32 个线程组成的线程 block 启动。

kernel<<<1,32>>>()

内核代码如下:

__global__ void kernel()
{
  if (threadIdx.x < 16)
  {
    // do something
    __syncthreads();
  }
  else
  {
    // do something
    __snycthreads();
  }
}

线程 block 的前 16 个线程将运行 if 语句。其他 16 个 else 语句。如果前 16 个线程中的每一个都到达 __syncthreads ,比它们阻塞直到整个线程 block 到达语句。但是这种情况永远不会出现,因为其他16个线程卡在了else分支,会出现死锁。

你应该避免使用 __syncthreads在不同的 if 和 else 分支中,否则您必须确保整个线程 block 在同一分支中运行!

关于c - 无法理解 __syncthreads(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20613460/

相关文章:

C 结构和指针混淆

C 共享库 : static variable initialization + global variable visibility among processes

c++ - C/C++ 和 CUDA 中的双线性插值

cuda - __global__ 函数如何像 C/C++ 那样返回值或中断

testing - 如何使用 Jenkins 管道在多个节点上运行同一组作业

python - 跟踪 Joblib 中并行 for 循环的索引

c - C中带有变量的“extern”有什么用?

c - 奇怪的 C 代码在 avr-gcc 下编译,但不应该

c++ - cgo : iostream:38:28: fatal error: bits/c++config. h 编译错误:没有那个文件或目录

r - 如何在R中使用循环和并行获得相同的结果?