Rust wrapper for ArrayFire

Pradeep GarigipatiArrayFire Leave a Comment

In this post, we would like to introduce our latest & newest language bindings for ArrayFire library: ArrayFire-Rust – Rust bindings for ArrayFire. We have been working on the wrapper and using it in daily production within the company for the last few months and now regard it stable enough for public distribution.

How to use ArrayFire from crates.io

To use the rust bindings for ArrayFire from crates.io, the following requirements are to be met first:

  1. Download and install ArrayFire binaries based on your operating system.
  2. If you are on Linux or OSX, set the environment variable AF_PATH to point to ArrayFire installation root folder. (This is automatically set for you on Windows.)
  3. Make sure you add the path to library files to your path environment variables.
    • On Linux & OSX: do export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$AF_PATH/lib
    • On Windows: Add %AF_PATH%\lib to your PATH environment variable.
  4. Run cargo new --bin helloworld && helloworld
  5. Edit Cargo.toml to add the line arrayfire = "3.3.0" to the [dependencies] section.
  6. Add the following sample rust code to your source file helloworld/src/main.rs.
    
    extern crate arrayfire as af;
    use af::*; // Ideally, you should use only names you are about to use, for example purposes * is fine.
        
    fn main() {
        let num_rows: u64 = 5;
        let num_cols: u64 = 3;
        let dims = Dim4::new(&[num_rows, num_cols, 1, 1]);
        println!("Create a 5-by-3 matrix of random floats on the GPU");
        let a = randu::(dims);
        print(&a);
    }
    
  7. Run cargo run and you should see output similar to the one below.
    
    Create a 5-by-3 matrix of random floats on the GPU
    [5 3 1 1]
        0.7402     0.4464     0.7762
        0.9210     0.6673     0.2948
        0.0390     0.1099     0.7140
        0.9690     0.4702     0.3585
        0.9251     0.5132     0.6814
    

Documentation

You can find the documentation for all the functions and macros available in the crate over here. You can also look at the ArrayFire C/C++ API documentation on ArrayFire.com which categorizes ArrayFire functions into several specific domains.

Example Project

hal project is one of the initial adaptors of our rust wrapper for ArrayFire to enable use of GPUs from Rust via ArrayFire.

Credits

We would like to thank Jason Ramapuram for helping us in writing and reviewing the rust bindings. We would also like to thank the users from rust community who provided their invaluable feedback while working on the bindings for ArrayFire.

 

Leave a Reply

Your email address will not be published. Required fields are marked *