GPU Programming

float x = a[i * 32];

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?

  1. AAdjacent threads' addresses are 128 bytes apart, so accesses do not coalesce and each thread fetches a separate memory chunk
  2. BBecause the multiplication takes a long time
  3. CBecause reading every 32 elements returns wrong values
Show answer and explanation

Answer: A. Adjacent threads' addresses are 128 bytes apart, so accesses do not coalesce and each thread fetches a separate memory chunk

A float is 4 bytes, so with a[i*32] adjacent threads' addresses are 128 bytes apart. The 32 threads of a warp read 32 separate chunks and use only 4 bytes of each. With a[i], the 32 threads share one 128-byte chunk, so the amount of data moved differs by 8× (with 32-byte sectors) to 32× (with 128-byte lines). For 2D arrays, the thread index should map to the innermost (contiguous) subscript.

  • A. Correct. Most of each fetched chunk goes unused.
  • B. The multiplication is tiny compared with memory waits.
  • C. The values are correct, just slow.