GPU Programming

A CUDA kernel computes each thread's index i with the expression above. What is i for the thread with blockIdx.x = 2, blockDim.x = 256 and threadIdx.x = 5?

int i = blockIdx.x * blockDim.x + threadIdx.x;
  1. A517
  2. B263
  3. C2565
Show answer and explanation

Answer: A. 517

blockIdx.x is the block's number in the grid, blockDim.x the number of threads per block and threadIdx.x the thread's number within its block. Block 0 handles 0-255 and block 1 handles 256-511, so thread 5 of block 2 gets 2 × 256 + 5 = 517. This expression gives every thread a unique global index that determines which array element it handles.

  • A. Correct. 2 × 256 + 5 = 517.
  • B. That adds 256 without multiplying by blockIdx.x.
  • C. That just concatenates digits.
Practice this topic Back to topic

More GPU Programming questions

  1. To process a 1000-element array with one element per thread and 256 threads per block, how many blocks should be launched?
  2. Why does the CUDA kernel above include "if (i < n)"?
  3. On NVIDIA GPUs, what is the unit of 32 threads bundled together to execute the same instruction in lockstep called?
  4. How does the GPU SIMT (single instruction, multiple threads) model differ from CPU SIMD instructions?
  5. Within one warp, a conditional branch sends half the threads to the if path and half to the else path (warp divergence). How is this generally executed?