← [ ABORT TO HUD ]
SEQ. 1
SEQ. 2

Compiling Rust Directly to PTX via nvptx64-nvidia-cuda

Writing & Launching Custom GPU Kernels in Rust (PTX & NVVM)20 min150 BASE XP⌨ HANDS-ON LAB

Writing GPU Kernels in Pure Rust

Historically, developers had to write .cu files and compile them with NVIDIA's nvcc compiler. With the LLVM nvptx64-nvidia-cuda target, you can write GPU kernels directly in pure Rust:

// In your kernel crate (target = "nvptx64-nvidia-cuda")
#![no_std]
#![feature(abi_ptx)]

#[no_mangle]
pub unsafe extern "ptx-kernel" fn vector_add_kernel(
    a: *const f32,
    b: *const f32,
    c: *mut f32,
    n: usize,
) {
    let idx = core::arch::nvptx::_thread_idx_x() as usize
        + core::arch::nvptx::_block_idx_x() as usize * core::arch::nvptx::_block_dim_x() as usize;

    if idx < n {
        *c.add(idx) = *a.add(idx) + *b.add(idx);
    }
}

Building the PTX Kernel

Compiling with cargo build --target nvptx64-nvidia-cuda --release outputs human-readable .ptx assembly, ready to be loaded by the host Rust runtime.

⌨ HANDS-ON LABCompile Rust to PTX Assembly & Launch Kernel via cudarc
⭐ +180 XP

Compile a #![no_std] Rust kernel to PTX with nvptx64-nvidia-cuda, load the module dynamically, and launch a 3D grid.

1Compile the vector addition kernel to PTX assembly using the LLVM nvptx64 target.
2Launch PTX kernel across 1,000,000 elements and verify GPU execution results.
lab-sandbox — simulated environment
INFINITY LAB SANDBOX v2.6 — simulated shell
Type the command for the current objective. Helpers: "hint", "solution", "clear".
$
OBJECTIVE 1 / 2 — type "hint" if stuck
SYNAPSE VERIFICATION
QUERY 1 // 1
What calling convention must be specified on a Rust function intended to run as a root GPU kernel?
extern "ptx-kernel"
extern "C"
extern "system"
extern "rust-call"