3 ms·
Because programmers expect arbitrary types to become __shared__ (ex: if you are writing a Raytracer, you probably want your rays to be stored into __shared__ me
by dragontamer 5y ago
Because programmers expect arbitrary types to become __shared__ (ex: if you are writing a Raytracer, you probably want your rays to be stored into __shared__ memory).
So the programmer would write:
struct Vec3{
float x;
float y;
float z;
}
struct Ray{
Vec3 origin;
Vec3 direction;
int bouncesRemaining;
};
And then maybe in one function...
__local struct Ray raystack[2000];
// As rays bounce, they get new origins / directions,
// and may spawn new rays as needed.
// If the raystack overflows, transfer the rays to global memory.
And then maybe my OpenCL kernel operates over these rays in parallel, tracking wherever they go.
- nynx 5y agoI can't guarantee that it'd be implemented like this, but I could see it working like this in Rust. struct Ray { origin: Vec3, dir: Vec3, remaining_bounces: u32, } and then using it like this: static RAY_STACK: [Shared<Ray>; 2000] = [Shared::new(Ray::default()); 2000]; or static RAY_STACK: Shared<[Ray; 2000]> = Shared::default(); I guess it could also be done like this: #[shared] static RAY_STACK: [Ray; 2000] = ...; but that's not really the rust way.
- wizzwizz4 5y agoThis seems more likely than my proposals, but it does mean you won't be able to use a particular part of the `__shared__` region for more than one thing. (Mine are broken by function calls, so yours is still better.)
- wizzwizz4 5y agoThat's easy enough in Rust. 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();
- deleted 5y ago[deleted]
- raphlinus 5y agoThis is done in rust-gpu[1] by the `spirv(workgroup)` attribute on the kernel function signature. [1]: https://github.com/EmbarkStudios/rust-gpu/blob/46c9ea0c9c7b7c6acfe83ad3dfaccfa21629d658/docs/src/attributes.md#workgroup-shared-memory https://github.com/EmbarkStudios/rust-gpu/blob/46c9ea0c9c7b7...
- rdambrosio 5y agoThat is basically what im going to do. It will break down approximately like this: 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.
- nynx 5y agoYou might want alignment there as well.
- dragontamer 5y agoBoth alignment and unalignment actually. 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.