cudarc: minimal and safe api over the cuda toolkit
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 |
CUDA Versions supported
- 11.4-11.8
- 12.0-12.9
- 13.0
CUDNN versions supported:
- 9.12.0
NCCL versions supported:
- 2.28.3
Configuring CUDA version
Select cuda version with one of:
-F cuda-version-from-build-system: At build time will get the cuda toolkit version usingnvcc-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-latestis enabled, we will use the highest bindings we have.
-F cuda-<major>0<minor>0to 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 = new?;
let stream = ctx.default_stream;
// copy a rust slice to the device
let inp = stream.clone_htod?;
// or allocate directly
let mut out = stream.?;
You can also use the nvrtc api to compile kernels at runtime:
let ptx = compile_ptx?;
// Dynamically load it into the device
let module = ctx.load_module?;
let sin_kernel = module.load_function?;
cudarc provides a very simple interface to launch kernels using a builder pattern to specify kernel arguments:
let mut builder = stream.launch_builder;
builder.arg;
builder.arg;
builder.arg;
unsafe ?;
And of course it's easy to copy things back to host after you're done:
let out_host: = stream.clone_dtoh?;
assert_eq!;
Design
Goals are:
- As safe as possible (there will still be a lot of unsafe due to ffi & async)
- As ergonomic as possible
- Allow mixing of high level
safeapis, with low levelsysapis
To that end there are three levels to each wrapper (by default the safe api is exported):
use ;
use ;
use ;
use ;
use ;
use ;
where:
sysis the raw ffi apis generated with bindgenresultis a very small wrapper around sys to returnResultfrom each functionsafeis 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.