Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Rust CUDA Kernels: Can They Use CUDA Libraries or Run on AMD GPUs?

· 5 min read · Home Tech

In this article
  1. Background: Rust and GPU Programming
  2. How Rust CUDA Kernels Work
  3. Using CUDA Libraries from Rust Kernels
  4. Running Rust CUDA Kernels on AMD GPUs
  5. How to Choose: CUDA vs HIP for Rust
  6. Common Problems and Misconceptions
  7. Costs and Safety Considerations
  8. Frequently Asked Questions

Yes, Rust CUDA kernels can use CUDA libraries, and they can run on AMD GPUs—but with important caveats. Specifically, Rust kernels written with the `rustcuda` or `cudarc` crates can call CUDA runtime and library functions (like cuBLAS) via FFI, and they can execute on AMD GPUs through the CUDA compatibility layer (HIP) or by using a Rust HIP wrapper. However, direct compilation of CUDA kernels to AMD GPUs without translation is not possible; you must use HIP or a similar layer.

Background: Rust and GPU Programming

Rust has several GPU programming paths. The most mature is via CUDA, using crates like `rustcuda` (a Rust wrapper for the CUDA driver API) and `cudarc` (a higher-level, safer wrapper). These crates allow you to write kernels in Rust that compile to PTX (Parallel Thread Execution) and run on NVIDIA GPUs. For AMD, Rust support is less mature but exists through HIP (Heterogeneous-Computing Interface for Portability), which is AMD's CUDA-compatible API. You can write kernels in Rust and compile them to HIP code, or use a tool like `hipcc` to compile CUDA C++ kernels to AMD GPUs, but Rust kernels themselves need a HIP backend.

How Rust CUDA Kernels Work

Rust CUDA kernels are written as Rust functions annotated with `#[kernel]` and compiled to PTX using the `rustc_codegen_cuda` project (still experimental) or by writing kernels in CUDA C++ and linking them via Rust. The `cudarc` crate simplifies this: you write a kernel in Rust, compile it to a cubin/PTX, and launch it from Rust. The kernel runs on the GPU, and you can copy data to/from the GPU using `cudarc`'s memory management.

Using CUDA Libraries from Rust Kernels

Yes, you can call CUDA libraries (like cuBLAS, cuFFT, cuSPARSE) from Rust kernels. The typical approach is to use the C FFI: the CUDA libraries have C APIs, and Rust can call them via `extern "C"` blocks. Crates like `cudarc` provide safe wrappers for many CUDA libraries, so you don't have to write unsafe FFI yourself. For example, to use cuBLAS, you can do:

use cudarc::cublas::Cublas;
let mut cublas = Cublas::new(&ctx)?;
cublas.sgemm(...)?;

This works because the CUDA libraries are host-side APIs that launch kernels on the GPU; your Rust kernel is just a device function. You cannot call a CUDA library function from inside a kernel (device code), but you can call it from the host side to orchestrate GPU operations.

Running Rust CUDA Kernels on AMD GPUs

To run Rust CUDA kernels on AMD GPUs, you have two main options:

  • HIP translation: Use the HIPIFY tool to convert CUDA code (including kernels) to HIP, then compile with `hipcc`. This works for C/C++ kernels, but for Rust kernels, you'd need to translate the PTX to HIP, which is not straightforward.
  • Rust HIP wrapper: Use a crate like `hip-rust` or `hip-sys` to write kernels directly for AMD GPUs. These crates provide Rust bindings to the HIP API, allowing you to write kernels in Rust that compile to AMD GCN or RDNA machine code.

However, note that CUDA libraries (like cuBLAS) are NVIDIA-specific and will not run on AMD GPUs. You would need to use AMD's equivalents (e.g., rocBLAS, rocFFT) via their C APIs or Rust wrappers.

How to Choose: CUDA vs HIP for Rust

Your choice depends on your target hardware:

  • If you only target NVIDIA GPUs, use CUDA with `cudarc`—it's the most stable and well-documented path.
  • If you need to target AMD GPUs (or both), use HIP with a Rust binding. The `hip-rust` crate is a good starting point, but expect more friction due to less mature tooling.
  • If you want portability, consider using a higher-level abstraction like `wgpu` (WebGPU) which supports both NVIDIA and AMD, but it's not CUDA-specific and has different performance characteristics.

Common Problems and Misconceptions

A common misconception is that CUDA kernels are binary-compatible with AMD GPUs—they are not. CUDA uses the PTX ISA, while AMD uses GCN/RDNA. Another issue is that Rust CUDA kernels may not be as optimized as CUDA C++ kernels because the Rust compiler's GPU backend is less mature. Additionally, you cannot mix CUDA and HIP in the same binary; you must choose one API.

Costs and Safety Considerations

There are no direct costs for using CUDA or HIP libraries—they are free to use. However, you need an NVIDIA GPU for CUDA and an AMD GPU for HIP. Safety-wise, when writing kernels, you must ensure memory safety: Rust's ownership system helps, but unsafe blocks are still required for raw pointers. Also, be aware that GPU programming can cause system instability if you access out-of-bounds memory; always test on a non-critical system.

Frequently Asked Questions

Can I call cuBLAS from a Rust kernel?

No, you cannot call cuBLAS from inside a kernel (device code). cuBLAS functions are host-side; they launch kernels internally. You can call them from Rust host code via FFI, but not from within a kernel.

Will my Rust CUDA kernel run on an AMD GPU with HIP?

Not directly. You need to rewrite the kernel using HIP or use a tool to translate it. The PTX code produced by Rust's CUDA backend is not compatible with AMD GPUs.

Is there a Rust crate for AMD GPU programming?

Yes, `hip-rust` and `hip-sys` are crates that provide Rust bindings to the HIP API. They are less mature than CUDA crates but functional.

Can I use Rust's safety features in GPU kernels?

Partially. Rust's ownership and borrowing rules apply to host code, but kernels often require unsafe blocks for pointer arithmetic and raw memory access. The `cudarc` crate provides safe abstractions, but you still need unsafe for low-level operations.

What is the performance difference between Rust and CUDA C++ kernels?

Performance can be comparable, but Rust's GPU backend is less optimized. In practice, for compute-heavy kernels, you may see slightly lower performance, but for many workloads the difference is negligible.