GPU Programming

int i = blockIdx.x * blockDim.x + threadIdx.x;

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?

  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 blockIdx.x instead of multiplying: 2 + 256 + 5 = 263.
  • C. That just concatenates digits.