cuda-oxide 手册

Lobsters Hottest 工具

摘要

cuda-oxide 是一个实验性的 Rust 到 CUDA 编译器,允许开发者编写安全、符合 Rust 惯用法的 GPU 内核,并直接编译为 PTX。

<p><a href="https://lobste.rs/s/h7nu14/cuda_oxide_book">评论</a></p>
查看原文 导出为 Word 导出为 PDF
查看缓存全文

缓存时间: 2026/05/08 09:34

# The cuda-oxide Book — cuda-oxide 来源:https://nvlabs.github.io/cuda-oxide/ cuda-oxide logo (https://nvlabs.github.io/cuda-oxide/_images/logo.png)**cuda-oxide** 是一个实验性的 Rust-to-CUDA 编译器,让你能够用安全(ish)、地道的 Rust 编写 SIMT GPU 内核。它将标准 Rust 代码直接编译为 PTX —— 不需要 DSL,不需要外部语言绑定,就是 Rust。注意 本书假设读者熟悉 Rust 编程语言,包括所有权、trait 和泛型。后面关于异步 GPU 编程的章节还假设读者掌握 `async`/`.await` 以及 tokio 等运行时。如需复习,请参阅 The Rust Programming Language (https://doc.rust-lang.org/book/)、Rust by Example (https://doc.rust-lang.org/rust-by-example/) 或 Async Book (https://rust-lang.github.io/async-book/)。 --- ## 项目状态 (https://nvlabs.github.io/cuda-oxide/#project-status) v0.1.0 版本是早期 alpha 阶段:**预计会有 bug、功能不完整和 API 破坏性变更**,我们正在努力改进。我们希望你能尝试一下,并通过分享使用体验来帮助塑造它的发展方向。 --- ## 🚀 快速开始 (https://nvlabs.github.io/cuda-oxide/#quick-start) ```rust use cuda_device::{kernel, thread, DisjointSlice}; use cuda_core::{CudaContext, DeviceBuffer, LaunchConfig}; use cuda_host::{cuda_launch, load_kernel_module}; #[kernel] fn vecadd(a: &[f32], b: &[f32], mut c: DisjointSlice) { let idx = thread::index_1d(); if let Some(c_elem) = c.get_mut(idx) { *c_elem = a[idx.get()] + b[idx.get()]; } } fn main() { let ctx = CudaContext::new(0).unwrap(); let stream = ctx.default_stream(); let module = load_kernel_module(&ctx, "vecadd").unwrap(); let a = DeviceBuffer::from_host(&stream, &[1.0f32; 1024]).unwrap(); let b = DeviceBuffer::from_host(&stream, &[2.0f32; 1024]).unwrap(); let mut c = DeviceBuffer::<f32>::zeroed(&stream, 1024).unwrap(); cuda_launch! { kernel: vecadd, stream: stream, module: module, config: LaunchConfig::for_num_elems(1024), args: [slice(a), slice(b), slice_mut(c)] }.unwrap(); let result = c.to_host_vec(&stream).unwrap(); assert_eq!(result[0], 3.0); } ``` 安装 [前置条件](https://nvlabs.github.io/cuda-oxide/getting-started/installation.html) 后,使用 `cargo oxide run vecadd` 构建并运行。注意 传给 `load_kernel_module` 的模块名是内核产物的基本名称;对于工作空间示例来说,就是示例名称。 --- ## 为什么选择 cuda-oxide? (https://nvlabs.github.io/cuda-oxide/#why-cuda-oxide) 🦀 GPU 上的 Rust 用 Rust 的类型系统和所有权模型编写 GPU 内核。安全是一等目标,但 GPU 有其微妙之处 —— 阅读 [安全模型](https://nvlabs.github.io/cuda-oxide/gpu-safety/the-safety-model.html) 了解更多。 💎 SIMT 编译器 不是 DSL。而是一个自定义的 rustc 代码生成后端,将纯 Rust 编译为 PTX。 ⚡ 异步执行 将 GPU 工作组合为惰性 `DeviceOperation` 图。在流池中调度。用 `.await` 等待结果。

相似文章

介绍 Triton:神经网络开源 GPU 编程

OpenAI Blog

# 介绍 Triton:神经网络开源 GPU 编程 来源:[https://openai.com/index/triton/](https://openai.com/index/triton/) ![介绍 Triton 开源 GPU 编程神经网络](https://images.ctfassets.net/kftzwdyauwt9/cdce1ebd-19a2-4848-a08ec8c44e18/55b924fc6628318148b7c5c4902551e7/image-18.webp?w=3840&q=90&fm=webp) 我们发布了 Triton 1.0,这是一种开源的类 Python 编程语言,使没有 CUDA 经验的研究人员能够编写高效的 GPU 代码——在大多数情况下与专家能够生成的代码性能相当。