GPU Programming
In CUDA, you want to wait mid-kernel for every other block to finish before moving to the next phase. What is the usual approach?
- ACall __syncthreads()
- BSplit the kernel at that point and launch the second kernel after the first finishes
- CSpin in a loop waiting for flags written by other blocks
Show answer and explanation
Answer: B. Split the kernel at that point and launch the second kernel after the first finishes
Blocks are independent and may run in any order, so not all of them are necessarily resident on the GPU at once. If the block being waited for has not started, a flag-waiting loop never ends. The usual approach relies on the fact that kernels launched in the same stream start only after the previous one finishes, splitting the kernel to obtain a global synchronization.
- A. __syncthreads() synchronizes only within a block.
- B. Correct. The kernel boundary acts as a global synchronization.
- C. There is no guarantee all blocks are resident, so it can hang.