#cuda #bindings #kernel #automatic #interface #gpu #interact

build bindgen_cuda

Bindgen like interface to build cuda kernels to interact with within Rust

6 releases

0.1.5 Apr 1, 2024
0.1.4 Feb 6, 2024
0.1.3 Jan 30, 2024

#130 in Concurrency

Download history 1472/week @ 2024-08-19 1565/week @ 2024-08-26 1163/week @ 2024-09-02 1293/week @ 2024-09-09 1689/week @ 2024-09-16 1837/week @ 2024-09-23 1720/week @ 2024-09-30 1396/week @ 2024-10-07 1417/week @ 2024-10-14 1300/week @ 2024-10-21 1140/week @ 2024-10-28 1594/week @ 2024-11-04 1237/week @ 2024-11-11 1342/week @ 2024-11-18 1445/week @ 2024-11-25 1132/week @ 2024-12-02

5,272 downloads per month
Used in 48 crates (7 directly)

MIT license

24KB
435 lines

Bindgen Cuda

Latest version Documentation License

Similar crate than bindgen in philosophy. It will help create automatic bindgen to cuda kernels source files and make them easier to use directly from Rust.

PTX inclusion

Let's say you have a file

src/cuda.cu

__global__ void cuda_hello(){
    printf("Hello World from GPU!\n");
}

You can add bindgen_cuda as a build dependency:

cargo add --build bindgen_cuda

And then create this build.rs

fn main() {
    let builder = bindgen_cuda::Builder::default();
    let bindings = builder.build_ptx().unwrap();
    bindings.write("src/lib.rs");
}

This will create a src file containing the following code:

pub const CUDA: &str = include_str!(concat!(env!("OUT_DIR"), "/cuda.ptx"));

You can then use the PTX directly in your rust code with a library like cudarc.

Raw cuda calls

Alternatively you can build a static library that you can link against in build.rs in order to call cuda directly with the c code.

src/cuda.cu

__global__ void cuda_hello(){
    printf("Hello World from GPU!\n");
}

int run() {
    cuda_hello<<<1,1>>>(); 
    return 0;
}

Then write the build.rs:

fn main() {
    let builder = bindgen_cuda::Builder::default();
    builder.build_lib("libcuda.a");
    println!("cargo:rustc-link-lib=cuda");
}

Which you can then interface through FFI in src/lib.rs:

extern "C" {
    fn cuda_hello();
}
fn main(){
    unsafe{ cuda_hello();}
}

Dependencies

~1.5MB
~28K SLoC