GPU Programming
Which assumption must be respected when writing GPU kernels?
- AThreads always execute one at a time in increasing index order
- BBlock 1 never starts before block 0 finishes
- CThe execution order of threads and blocks is unspecified, so results must not depend on execution order
Show answer and explanation
Answer: C. The execution order of threads and blocks is unspecified, so results must not depend on execution order
A GPU assigns threads and blocks to free resources as they become available, so execution order varies with the GPU model and load. If one thread relies on a value written by another, the code may happen to work but fail elsewhere. When order matters, use __syncthreads() within a block, separate kernels between blocks, and atomics or reductions for accumulation.
- A. Many threads run simultaneously with no order guarantee.
- B. Blocks may run concurrently in any order.
- C. Correct. Use synchronization when order matters.