MCPcopy Index your code
hub / github.com/chelsea0x3b/cudarc

github.com/chelsea0x3b/cudarc @v0.19.8

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.19.8 ↗ · + Follow
5,049 symbols 8,070 edges 121 files 601 documented · 12% updated 19d agov0.19.8 · 2026-06-19★ 1,17217 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

cudarc: minimal and safe api over the cuda toolkit

crates.io docs.rs

Checkout cudarc on crates.io and docs.rs.

Contributions welcome!

Safe CUDA wrappers for:

library dynamic load dynamic link static link
CUDA driver N/A
NVRTC
cuRAND
cuBLAS
cuBLASLt
NCCL
cuDNN
cuSPARSE
cuSOLVER N/A
cuFILE
CUPTI
nvtx N/A
cuFFT

CUDA Versions supported (choose with -F cuda-<version>, like cuda-13010): - 11.4-11.8 - 12.0-12.9 - 13.0-13.3

CUDNN versions supported (choose with -F cudnn-<version>, like cudnn-09021): - 8.9.7 - 9.10.2 - 9.21.1

NCCL versions supported (choose with -F nccl-<version>, like nccl-02023): - 2.22-2.30

Configuring CUDA version

Select cuda version with one of: - -F cuda-version-from-build-system: At build time will get the cuda toolkit version using nvcc - -F fallback-latest: can be used to control behavior if this fails. default is not enabled, which will cause the build script to panic. if -F fallback-latest is enabled, we will use the highest bindings we have. - -F cuda-<major>0<minor>0 to build for a specific version of cuda

Configuring linking

By default we use -F dynamic-loading, which will not require any libraries to be present at build time.

You can also enable -F dynamic-linking or -F static-linking for your use case.

Getting started

It's easy to create a new device and transfer data to the gpu:

// Get a stream for GPU 0
let ctx = cudarc::driver::CudaContext::new(0)?;
let stream = ctx.default_stream();

// copy a rust slice to the device
let inp = stream.clone_htod(&[1.0f32; 100])?;

// or allocate directly
let mut out = stream.alloc_zeros::<f32>(100)?;

You can also use the nvrtc api to compile kernels at runtime:

let ptx = cudarc::nvrtc::compile_ptx("
extern \"C\" __global__ void sin_kernel(float *out, const float *inp, const size_t numel) {
    unsigned int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < numel) {
        out[i] = sin(inp[i]);
    }
}")?;

// Dynamically load it into the device
let module = ctx.load_module(ptx)?;
let sin_kernel = module.load_function("sin_kernel")?;

cudarc provides a very simple interface to launch kernels using a builder pattern to specify kernel arguments:

let mut builder = stream.launch_builder(&sin_kernel);
builder.arg(&mut out);
builder.arg(&inp);
builder.arg(&100usize);
unsafe { builder.launch(LaunchConfig::for_num_elems(100)) }?;

And of course it's easy to copy things back to host after you're done:

let out_host: Vec<f32> = stream.clone_dtoh(&out)?;
assert_eq!(out_host, [1.0; 100].map(f32::sin));

Design

Goals are: 1. As safe as possible (there will still be a lot of unsafe due to ffi & async) 2. As ergonomic as possible 3. Allow mixing of high level safe apis, with low level sys apis

To that end there are three levels to each wrapper (by default the safe api is exported):

use cudarc::driver::{safe, result, sys};
use cudarc::nvrtc::{safe, result, sys};
use cudarc::cublas::{safe, result, sys};
use cudarc::cublaslt::{safe, result, sys};
use cudarc::curand::{safe, result, sys};
use cudarc::nccl::{safe, result, sys};

where: 1. sys is the raw ffi apis generated with bindgen 2. result is a very small wrapper around sys to return Result from each function 3. safe is a wrapper around result/sys to provide safe abstractions

Heavily recommend sticking with safe APIs

License

Dual-licensed to be compatible with the Rust project.

Licensed under the Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0 or the MIT license http://opensource.org/licenses/MIT, at your option. This file may not be copied, modified, or distributed except according to those terms.

Extension points exported contracts — how you extend this code

PushKernelArg (Interface)
Something that can be copied to device memory and turned into a parameter for [result::launch_kernel]. See the [cuda do [10 …
src/driver/safe/launch.rs
UniformFill (Interface)
Fill with uniform distributed numbers of type `T`. [4 implementers]
src/curand/result.rs
Gemm (Interface)
Matrix matrix multiplication with elements of type `T`. [4 implementers]
src/cublas/safe/gemm.rs
Matmul (Interface)
Matrix matrix multiplication with elements of type `T`. [3 implementers]
src/cublaslt/safe.rs
CudnnDataType (Interface)
Maps a rust type to a [sys::cudnnDataType_t] [2 implementers]
src/cudnn/safe/core.rs
CudaTypeName (Interface)
Exposes [CudaTypeName] which maps between rust type names and the corresponding cuda kernel type names. For example, `f
src/types.rs
NcclType (Interface)
(no doc)
src/nccl/safe.rs
ValidAsZeroBits (Interface)
Marker trait to indicate that the type is valid when all of its bits are set to 0. # Safety Not all types are valid whe [25 …
src/driver/safe/core.rs

Core symbols most depended-on inside this repo

as_mut_ptr
called by 154
src/driver/safe/core.rs
result
called by 101
src/driver/result.rs
arg
called by 87
src/driver/safe/launch.rs
default_stream
called by 86
src/driver/safe/core.rs
clone_htod
called by 85
src/driver/safe/core.rs
clone_dtoh
called by 84
src/driver/safe/core.rs
clone
called by 83
src/driver/safe/core.rs
record_err
called by 75
src/driver/safe/core.rs

Shape

Function 3,632
Class 561
Enum 447
Method 389
Interface 20

Languages

Rust100%
C++1%

Modules by API surface

src/driver/sys/mod.rs804 symbols
src/runtime/sys/mod.rs605 symbols
src/cusparse/sys/mod.rs574 symbols
src/cusolver/sys/mod.rs540 symbols
src/cublas/sys/mod.rs529 symbols
src/cudnn/sys/mod.rs379 symbols
src/driver/safe/core.rs152 symbols
src/cublaslt/sys/mod.rs94 symbols
src/driver/result.rs92 symbols
src/nccl/sys/mod.rs84 symbols
src/cufile/sys/mod.rs84 symbols
src/cutensor/sys/mod.rs71 symbols

For agents

$ claude mcp add cudarc \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page