Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do GPUs handle random access?

I read some tutorials on how to implement a raytracer in opengl 4.3 compute shaders, and it made me think about something that had been bugging me for a while. How exactly do GPUs handle the massive amount of random access reads necessary for implementing something like that? Does every stream processor get its own copy of the data? It seems that the system would become very congested with memory accesses, but that's just my own, probably incorrect intuition.

like image 582
user45681 Avatar asked Dec 31 '16 19:12

user45681


1 Answers

The Stream Multiprocessors (SM) have caches, but they are relatively small and won't help with truly random access.

Instead, GPUs are trying to mask the memory access latency: that is each SM is assigned more threads to execute than it has cores. On every free clock it schedules some of the threads that aren't blocked on memory access. When the data needed for a thread isn't in the SM cache, then the thread stalls till that data arrives, letting other threads to be executed instead.

Note that this masking only works if the amount of computation exceeds the time spent on waiting for the data (e.g. per-pixel lighting calculations). If it's not the case (e.g. just summing lots of 32-bit floats), then you are likely to bottleneck at the memory bus bandwidth, and most of the time your threads will be stalled waiting for their bits to arrive.

A related thing that can help with SM utilization is data-locality. When multiple threads access nearby memory locations then one cache-line fetch will bring the data needed by multiple threads. For example, when texturing a perspectively warpped triangle, even though each fragment's texture coordinates can be 'random', nearby fragments are still likely to read nearby texels from the texture. Consequently there's a lot of common data shared between the threads.

Ray-tracing, on the other hand, is horrible at data-locality. Secondary rays tend to diverge a lot, and hit different surfaces at practically random locations. This makes it very hard to utilize the SM architecture for either ray-scene intersection or shading purposes.

like image 164
Yakov Galka Avatar answered Oct 11 '22 07:10

Yakov Galka