Rust GPU
The future of GPU programming
use glam::UVec3;
use spirv_std::spirv;
enum Outcome {
Fizz,
Buzz,
FizzBuzz,
}
trait Game {
fn fizzbuzz(&self) -> Option<Outcome>;
}
impl Game for u32 {
fn fizzbuzz(&self) -> Option<Outcome> {
match (self % 3 == 0, self % 5 == 0) {
(true, true) => Some(Outcome::FizzBuzz),
(true, false) => Some(Outcome::Fizz),
(false, true) => Some(Outcome::Buzz),
_ => None,
}
}
}
#[spirv(compute(threads(64)))]
pub fn main(
#[spirv(global_invocation_id)] id: UVec3,
#[spirv(storage_buffer)] output: &mut [Option<Outcome>; 64],
) {
let index = id.x as u32;
output[index] = index.fizzbuzz();
}
Rust GPU makes it possible to write and run GPU software in Rust, leveraging the language's powerful safety and concurrency features to enhance performance and reliability. With Rust GPU, you can seamlessly develop for both CPU and GPU using a unified codebase while benefiting from Rust's existing ecosystem.
Multi-Vendor
The Rust GPU compiler backend emits code compatible with Vulkan, ensuring your code runs across a wide range of devices and vendors.
If instead you wish to stick to the NVIDIA-only ecosystem, stay tuned as the Rust CUDA project is in the process of being rebooted and possibly integrated with Rust GPU.
Modern and Unified
There is no longer a need to learn a GPU-specific programming language. You can write both CPU and GPU code in Rust, leveraging your existing Rust knowledge and maintaining a consistent development experience. Furthermore, the same code can run on both the CPU and GPU, with divergent behavior gated behind cfg
attributes and macros where necessary.
Even if your current codebase isn't written in Rust, choosing Rust for the GPU parts gives you more widely applicable skills in one of the fastest-growing languages on GitHub. Rust is also one of the most admired programming languages while GPU-specific languages are considered a necessary evil.
Fearless Concurrency
Rust's ownership model and type system guarantee memory safety, minimizing bugs and undefined behavior. Rust's borrow checker also enables fearless concurrency, which is essential for maximizing performance on massively parallel GPUs.
Powerful Abstractions
Programming in GPU-specific languages can often feel like taking a step back to the 90s, where primitive tools and sharp edges abound. Because of this, code written for GPUs is simplistic with low cyclomatic complexity. Rust has an expressive type system and zero-cost abstractions that enable writing high-level, reusable code without sacrificing performance. This approach leads to more maintainable and sophisticated GPU programs, streamlining the development process and enhancing productivity.
Existing Ecosystem
The state-of-the-art for sharing GPU code is copy and pasting. With Rust GPU we are excited to bring the excellent cargo
and crates.io ecosystem to GPU programming and provide some sanity.
Rust's no_std
ecosystem offers a wide array of libraries that can be used in environments without the standard library. Traditionally this has meant embedded devices, but a lot of the same assumptions apply to GPUs! As a consequence, you can reuse existing no_std
libraries from crates.io in your GPU code without the authors explicitly adding GPU support. This is uniquely enabled by Rust GPU's implementation choices and Rust's registers. Sharing and reusing code from the greater Rust ecosystem is a superpower when writing GPU programs that will massively compound over time.