Code Monkey home page Code Monkey logo

bindgen_cuda's Introduction

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();}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.