Download src/demo.rs from Snapkitty/rust-opencl-gpu: direct link, hf CLI and curl.
- Browser
- Download file 2.11 kB
-
https://huggingface.co/Snapkitty/rust-opencl-gpu/resolve/main/src/demo.rs
- Command line
-
hf download hf://Snapkitty/rust-opencl-gpu/src/demo.rs
-
curl -L -o demo.rs https://huggingface.co/Snapkitty/rust-opencl-gpu/resolve/main/src/demo.rs
2.11 kB
| // Functor-based GPU architecture demonstration | |
| // This shows the composable kernel design without requiring actual GPU hardware | |
| trait Kernel: Sized { | |
| fn name() -> &'static str; | |
| fn local_size() -> usize; | |
| fn compute(&self, gid: usize, lid: usize) -> f32; | |
| } | |
| struct LocalIdKernel; | |
| impl Kernel for LocalIdKernel { | |
| fn name() -> &'static str { "local_id" } | |
| fn local_size() -> usize { 4 } | |
| fn compute(&self, _gid: usize, lid: usize) -> f32 { | |
| lid as f32 | |
| } | |
| } | |
| struct GlobalIdKernel; | |
| impl Kernel for GlobalIdKernel { | |
| fn name() -> &'static str { "global_id" } | |
| fn local_size() -> usize { 4 } | |
| fn compute(&self, gid: usize, _lid: usize) -> f32 { | |
| gid as f32 | |
| } | |
| } | |
| fn execute<K: Kernel>(kernel: K, work_size: usize) -> Vec<f32> { | |
| let local_size = K::local_size(); | |
| (0..work_size) | |
| .map(|gid| { | |
| let local_id = gid % local_size; | |
| kernel.compute(gid, local_id) | |
| }) | |
| .collect() | |
| } | |
| fn format_output(data: &[f32]) -> String { | |
| data.iter() | |
| .enumerate() | |
| .fold(String::new(), |acc, (i, &val)| { | |
| if i > 0 && i % 16 == 0 { | |
| format!("{}\n{:>3} ", acc, val as i32) | |
| } else { | |
| format!("{}{:>3} ", acc, val as i32) | |
| } | |
| }) | |
| } | |
| fn main() { | |
| println!("=== Functor-Based GPU Kernel Architecture Demo ===\n"); | |
| println!("Kernel: {} (local_size={})", LocalIdKernel::name(), LocalIdKernel::local_size()); | |
| let result1 = execute(LocalIdKernel, 128); | |
| println!("Output: {}\n", format_output(&result1)); | |
| println!("Kernel: {} (local_size={})", GlobalIdKernel::name(), GlobalIdKernel::local_size()); | |
| let result2 = execute(GlobalIdKernel, 128); | |
| println!("Output: {}\n", format_output(&result2)); | |
| println!("=== Architecture Proof ==="); | |
| println!("β Kernel trait abstracts GPU operations"); | |
| println!("β execute<K>() is a higher-order function parametrized on Kernel"); | |
| println!("β Immutable data flow: kernel.compute() β Vec<f32>"); | |
| println!("β Type-safe kernel composition via trait bounds"); | |
| } | |