by wizzwizz4 · 5y ago · 0 comments
struct Vec3(f32, f32, f32);
struct Ray {
origin: Vec3,
direction: Vec3,
bounces_remaining: i32
}
and then something like (made-up API): let shared = rust_cuda::shared();
let mut raystack = shared.alloc_zeroed::<[Ray; 2000]>();
or maybe: let mut raystack: Shared<[Ray; 2000]> = [Default::default(); 2000].into(); pub fn get_shared_mem_ptr<const Bytes: usize>() -> \*mut u8 {
__nvvm_get_shared_mem_ptr(Bytes)
}
For the raw version, the codegen internally intercepts the call to the intrinsic and declares an extern global in the shared addrspace, which is what libnvvm wants you to do, basically like __shared__ int foo[5];
Dynamic shared mem is a bit more weird because if you query the ptr for the dynamic smem it yields the same ptr every time.GPUs are weird. Prime numbers to 'unalign' data so that you minimize bank conflicts is a common optimization trick.
GPUs don't have one memory load/store unit. They are incredibly parallel and have like 32 load/store units that try to operate in parallel.
If all your data is aligned, then bank#0 gets more requests than bank#31. (Thread#0 accesses memory 800. Thread#1 accesses memory 832. Threas#2 accesses 864... Woops you just hammered one bank and now 31 of your memory banks are sitting around doing nothing, while bank#0 is doing all the work sequentially)
Unalignment means more read/writes are sent to bank#31, and fewer to bank#0, better balancing the load across your parallel load/store units.
[1]: https://github.com/EmbarkStudios/rust-gpu/blob/46c9ea0c9c7b7...