GithubHelp home page GithubHelp logo

boustrophedon / rust-cuda-shim-example Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 4 KB

An example of a rust binary that calls CUDA kernels via a C shim

License: BSD 2-Clause "Simplified" License

Rust 27.79% Makefile 8.43% Cuda 39.14% C 24.64%

rust-cuda-shim-example's Introduction

This is a complete example of a cargo package that allows you to link C functions that call out to CUDA functions into your Rust program. The c/cuda program itself just implements min via a linear search in C and via a pairing-based "log" search in CUDA. It's only O(logn) if you can run enough (i.e. a constant fraction) of the comparisons in parallel though, and my implementation just breaks if you have too much data rather than splitting it up into batches and merging, and the linear search is faster in most cases (less than like 10 million or something) simply because of driver overhead.

Things to note:

  • build.rs allows us to do the linking. Some of it could also be done in the Cargo.toml but I thought it would be easier to put it all in the build.rs.
  • To build the C/CUDA code we just run a simple makefile.
    • The only thing special about that makefile is the -Xcompiler flag before -fPIC, which just tells nvcc to pass -fPIC to the compiler.
    • The CUDA-kernel-running C headers are wrapped in an extern C ifdef because nvcc is a C++ compiler, so it would mangle the names otherwise.
  • The rust code is very simple.
    • The external C functions are declared in the extern at the top of the main.rs.

Problems:

  • There isn't a corresponding clean.rs so I can't just run make clean
    • I considered a few things like copying the entire directory over, or running in-folder and then copying to OUT_DIR, but I really didn't want to write any object files or move any files inside the source directories, so the makefile is a bit ugly with $(OBJDIR) everywhere and what I consider to be over-use of the $< $@ $^ magic make variables.
  • It's annoying that I have to check if the previous output directory is still around so that the build.rs doesn't panic when I try to create one for the liblogmin.a file.

Sources used:

http://doc.crates.io/build-script.html
https://doc.rust-lang.org/book/ffi.html

and here's a sampling of some stack overflow questions that may help to figure out how to link everything together properly, because linkers are evil nefarious beasts:

http://stackoverflow.com/questions/13553015/cuda-c-linker-error-undefined-reference
http://stackoverflow.com/questions/4307053/cuda-cudpp-so-building
http://stackoverflow.com/questions/6045809/problem-with-g-and-undefined-reference-to-gxx-personality-v0
http://stackoverflow.com/questions/28060294/linking-to-a-c-library-that-has-extern-c-functions

rust-cuda-shim-example's People

Contributors

boustrophedon avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

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.