r/gpgpu • u/dragontamer5788 • Jul 15 '19
Some quick GPU programming thoughts
Global memory barriers are very slow on GPUs, and can only be executed maybe once per microsecond or so (once every 1000ns). Any global data structure should have "Block local" buffers which only use CUDA-block (or OpenCL Threadgroup) level synchronization, which is far faster instead. In particular, AMD Vega64 seems to compile a global-threadfence into a L1 cache flush.
Synchronizing with the CPU (cudaDeviceSynchronize / hipDeviceSynchronize) seems to be only a little bit slower than thread-fences + spinlocks.
RAM is used up ridiculously quickly. Any GPU will have the ability to run 10,000+ hardware threads. Vega64 should be run with 16384 hardware threads at a minimum for example (and supports up to 163,840 hardware threads at max occupancy). However, 16384 threads will run out of VRAM in just 512kB per thread: you don't even get the traditional "640kB" that should be enough for everyone.
Maximize the sharing of data between threads.
Because RAM needs to be used with utmost efficiency, you will end up writing your own data-structures rather often. In most cases, you'll use a simple array.
array[tail + __popc(writemask & __lanemask_lt())] = someItem; tail+= __prefix_sum(popc(writemask)) is an important pattern. This SIMD-stack paradigm should be your "bread-and-butter" collection due to its simplicity and efficiency. AMD/ROCm users can use __ockl_activelane_u32() to get the current active lane number.
SIMD-data structures are commonly "bigger" than the classic data-structures. Each "link" in a linked list should be the same size as the warpSize (32 on NVidia, 64 on AMD cards). Each node in a SIMD-Heap should also be 32+ or 64+ wide to support efficient SIMD-load/store.
Debugging 10,000+ threads one-at-a-time doesn't really scale. Use the GPU to write GPU-tests per-thread, and then use the CPU to verify data sequentially. Especially if you are hunting threadfence or memory-barrier issues: the only way to catch a memory barrier issue is if you unleash as many threads as possible and run them as long as possible.
5
u/Damienr74 Jul 15 '19
Would you mind elaborating on this? Would writemask be a ballot operation? And you're appending
__popc(writemask)elements using SIMD lanes? If this is the case I don't understand why you're computing a prefix sum / what total is. Is it for the elements in other warps? If multiple warps are writing into the array, wouldn't the indexing incorporate that as well? Am I misunderstanding something, or was it just simplified for the sake of example?Great list, I wish I had it when starting out!