GPU Programming

Both (1) and (2) above make half the threads run A and the other half run B. Which is correct about warp efficiency (32 threads per warp)?

if (threadIdx.x % 2 == 0) A(); else B();   // (1)
if ((threadIdx.x / 32) % 2 == 0) A(); else B();   // (2)
  1. A(1) is more efficient because its branches agree
  2. B(1) diverges inside every warp, while (2) is uniform per warp, so (2) is more efficient
  3. CBoth are exactly equally efficient
Show answer and explanation

Answer: B. (1) diverges inside every warp, while (2) is uniform per warp, so (2) is more efficient

In (1), even and odd threads split, so every warp contains both A and B threads and every warp executes A and B in turn. In (2), the split is by the thread index divided by 32, so all 32 threads of a warp take the same side, and each warp executes only A or only B. The same work avoids divergence when the condition is uniform per warp.

  • A. In (1), adjacent threads have different conditions.
  • B. Correct. The trick is to make the condition uniform per warp.
  • C. They differ greatly in whether divergence occurs.
Practice this topic Back to topic

More GPU Programming questions

  1. Which describes memory coalescing on a GPU?
  2. With i as each thread's index, reading the array every 32 elements as above is slower than reading a[i]. What is the main reason?
  3. When processing particle data with positions x, y and z on a GPU, which layout is better for memory coalescing?
  4. What is a characteristic of CUDA shared memory?
  5. What does CUDA's __syncthreads() do?