【发布时间】:2020-03-25 08:30:10
【问题描述】:
在 CUDA 编程指南中关于合作组的部分中,有一个网格本地同步的示例:
grid_group grid = this_grid();
grid.sync();
不幸的是,我没有找到grid.sync() 行为的精确定义。将__syncthreads 的以下定义扩展到网格级别是否正确?
void __syncthreads();等待直到线程块中的所有线程都有 达到这一点,所有全局和共享内存访问由 __syncthreads() 之前的这些线程对所有线程可见 块。
所以,我的问题是正确的:
this_grid().sync();等到 grid 中的所有线程都有 达到这一点,所有全局和共享内存访问由 this_grid().sync() 之前的这些线程对所有线程可见 网格。
我怀疑这是否正确,因为在 CUDA 编程指南中,grid.sync(); 下面的几行有以下语句:
为了保证线程块在 GPU 上的共同驻留,需要仔细考虑启动的块数。
这是否意味着如果我使用这么多线程以至于没有线程块的共同驻留,我最终可能会陷入线程可能死锁的情况?
当我尝试使用coalesced_threads().sync() 时,也会出现同样的问题。以下是正确的吗?
coalesced_threads().sync();等到 warp 中的所有 活动 线程都有 达到这一点,所有全局和共享内存访问由 coalesced_threads().sync() 之前的这些线程对所有线程可见 活动线程列表。
以下示例是否从 while 循环中退出?
auto ct = coalesced_threads();
assert(ct.size() == 2);
b = 0; // shared between all threads
if (ct.thread_rank() == 0)
while (b == 0) {
// what if only rank 0 thread is always taken due to thread divergence?
ct.sync(); // does it guarantee that rank 0 will wait for rank 1?
}
if (ct.thread_rank() == 1)
while (b == 0) {
// what if a thread with rank 1 never executed?
b = 1;
ct.sync(); // does it guarantee that rank 0 will wait for rank 1?
}
为了清楚上面的例子,没有ct.sync()是不安全的,可以死锁(无限循环):
auto ct = coalesced_threads();
assert(ct.size() == 2);
b = 0; // shared between all threads
if (ct.thread_rank() == 0)
while (b == 0) {
// what if only rank 0 thread is always taken due to thread divergence?
}
if (ct.thread_rank() == 1)
while (b == 0) {
// what if a thread with rank 1 never executed?
b = 1;
}
【问题讨论】:
标签: cuda