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;
- A517
- B263
- 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.