Add intrinsic for dynamic group-shared memory on GPUs by Flakebi · Pull Request #146181 · rust-lang/rust (original) (raw)
I agree that it makes a lot of sense to have the discussion now. Thanks for taking a look and helping to design something useful!
Speaking of safety requirements... how does one use this pointer?
Heh, yes, that’s something that should be mentioned in the doc comment as well. (Especially comments on how to safely use it.)
I get that it is aligned, but does it point to enough memory to store a
T?
Depends on the size specified on the CPU side when launching the gpu-kernel. It may or it may not.
If it's always the same address, doesn't everyone overwrite each other's data all the time? This API looks very odd for a non-GPU person, and it's not clear to me whether that is resolved by having more magic behavior (which should be documented or at least referenced here), or whether there's higher-level APIs built on top that deal with this (but this intrinsic provides so few guarantees, I can't see how that should be possible).
There are “higher-level APIs” like “do a fast matrix-matrix multiplication”, but not much in-between. I’d assume that people usually use this in its raw form.
On GPUs, accessing memory is orders of magnitude slower than it is on CPUs. But, GPUs
- have a lot more registers (e.g. up to 256 32-bit registers on amdgpu)
- and shared memory, which is essentially a software-defined cache.
Two general use cases are: 1) All threads in a group load a part from global memory (the RAM/VRAM) and store it in shared memory. Then all threads read from the collaboratively loaded data. 2) All threads in a group do some work and collaborate on shared memory (with atomics or so) to aggregate results. Then one of the threads stores the final result to global memory.
So, shared memory is meant to be accessed collaboratively and the developer must ensure proper synchronization. It is hard to provide a safe abstraction for this and tbh, I don’t want to try 😅 (though I can see 3rd party crates doing this – at least to some extent).
From Rust’s perspective, guarantees should be the same as with memory that’s shared between processes.
Typically, intrinsic documentations should be detailed enough that I can read and write code using the intrinsic and know exactly whether the code is correct and what it will do in all circumstances. I don't know if there's any hope of achieving that with GPU intrinsics, but if not then we need to have a bit of a wider discussion -- we have had bad experience with just importing "externally defined" semantics into Rust without considering all the interactions (in general, it is not logically coherent to have semantics externally defined).
I agree, it would be nice to have good documentation for the intrinsics in Rust!