GPU Programming
In CUDA, __syncthreads() is placed inside "if (threadIdx.x < 16)" so that only some threads reach it. What can happen?
- ASynchronization works correctly among just the 16 threads
- BThe other threads never arrive, so the block may hang or results may be undefined
- C__syncthreads() is ignored automatically, so nothing happens
Show answer and explanation
Answer: B. The other threads never arrive, so the block may hang or results may be undefined
__syncthreads() waits for every thread in the block, so placing it inside a branch only some threads take means some threads never arrive. The behavior is undefined by the specification; in practice it may hang or appear to work while producing wrong results. Synchronization should be placed outside branches, where every thread is guaranteed to pass.
- A. It waits for the whole block.
- B. Correct. It must be placed where every thread in the block passes.
- C. It is not ignored.