r/gpgpu • • 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.

16 Upvotes

5 comments sorted by

View all comments

5

u/Damienr74 Jul 15 '19
array[tail + __popc(writemask & __lanemask_lt())] = someItem; tail+= __prefix_sum(total)

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!

4

u/dragontamer5788 Jul 15 '19 edited Jul 15 '19

Would writemask be a ballot operation?

Yes.

writemask = __ballot(currentThreadWantsToWriteToArray);

Its difficult to know what the condition "currentThreadWantsToWriteToArray" is. Its going to depend on the context. In my case, a bitmask-malloc approach indicates that a bit is available to malloc-a-chunk of memory. In other contexts, it means that a Ray (from the raytracer) hit an object, and therefore needs to bounce (aka: recurse). Rays that miss objects do not need recursion.

So its a generic concept. Its repeated in a lot of professional SIMD / GPU code, but I don't know if this concept really has a name.

If this is the case I don't understand why you're computing a prefix sum / what total is.

Yeah, I was being quick and dirty with my post.

if(__popc(writemask&__lanemask_lt()) == 1) // If and-only if we are the first active SIMD-lane
    tail+= __prefix_sum(__popc(writemask)) // Update the tail only once

Is that more clear?

Note that "tail" is a shared variable, and needs to be protected with block-level memory fences. You need a memory fence to ensure that "tail+= (blah)" happens-after the array[tail + __popc(writemask & __lanemask_lt())] = (blah).

My code uses two memory fences as well. Which... probably is the more complicated part. (Tail += Prefix_sum... needs to happen after the previous iteration, while needing to happen before the next iteration. That's two memory fences).

If multiple warps are writing into the array, wouldn't the indexing incorporate that as well?

I prefer to "float" my warps and keep them independent. If you want multiple warps to operate in sync, you'll also need a __syncthreads() somewhere in there.

But warp-level synchronization is quicker than block-level synchronization. (probably?? I don't have proof, but at least... it makes sense to me instinctively) So I personally run it on a per-warp basis. But do whatever works for you.