GPU Programming
To process a 1000-element array with one element per thread and 256 threads per block, how many blocks should be launched?
- A3
- B4
- C1000
Show answer and explanation
Answer: B. 4
1000 ÷ 256 ≈ 3.9, so rounding down to 3 blocks would cover only 256 × 3 = 768 elements and miss the rest. Rounding up to 4 blocks (1024 threads) covers everything, with the 24 extra threads doing nothing. The usual formula is "(n + 256 - 1) / 256" to round up. The kernel must check the index so the extra threads do not read or write beyond the array.
- A. Three blocks cover only 768 elements.
- B. Correct. 1000 ÷ 256 rounded up is 4.
- C. That confuses blocks with threads.