
<!DOCTYPE html><html><head><meta charset="utf-8"><title>Untitled Document.md</title><style></style></head><body id="preview">
<p>In this post, we would like to introduce our latest &amp; newest language bindings for ArrayFire library, ArrayFire-Rust - Rust bindings for ArrayFire. The wrapper itself has been a work in progress for the past few months and we think now it is ready for it’s first release.</p>
<h4><a id="How_to_use_arrayfire_from_cratesio_2"></a>How to use arrayfire from <a href="http://crates.io">crates.io</a></h4>
<p>To use the rust bindings for ArrayFire from <strong><a href="http://crates.io">crates.io</a></strong>, the following requirements are to be met first.</p>
<ol>
<li>Download and install ArrayFire binaries based on your operating system.</li>
<li>Set the evironment variable <code>AF_PATH</code> to point to ArrayFire installation root folder.</li>
<li>Make sure you add the path to library files to your path environment variables.
<ul>
<li>On Linux &amp; OSX: do <code>export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$AF_PATH/lib</code></li>
<li>On Windows: Add <code>%AF_PATH%\lib</code> to your PATH environment variable.</li>
</ul>
</li>
<li>Do <code>cargo new --bin helloworld &amp;&amp; helloworld</code></li>
<li>Edit <code>Cargo.toml</code> to add the line <code>arrayfire = &quot;3.2.0&quot;</code> to the <code>[dependencies]</code> section.</li>
<li>Add the following sample rust code to your source file <code>helloworld/src/main.rs</code> file<pre><code class="language-rust">
    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 = match randu(dims, Aftype::F32) {
            Ok(value) => value,
            Err(error) => panic!("{}", error),
        };
        print(&a);
    }
</code></pre>
</li>
<li>Do <code>cargo run</code> and you should see output similar to the one below. Depending on the type of acceleration (GPU - {NVIDIA | AMD}/CPU) devices you have on your machine, the device information will vary as the default device priority is to use any CUDA compatible device available, if not use OpenCL compatbile device and fallback to CPU if no GPUs are available.<pre><code class="language-bash">ArrayFire v3.<span class="hljs-number">2.0</span> (CUDA, <span class="hljs-number">64</span>-bit Mac OSX, build d8d4b38)
Platform: CUDA Toolkit <span class="hljs-number">7</span>, Driver: CUDA Driver Version: <span class="hljs-number">7000</span>
[<span class="hljs-number">0</span>] GeForce GT <span class="hljs-number">750</span>M, <span class="hljs-number">2048</span> MB, CUDA Compute <span class="hljs-number">3.0</span>
Create a <span class="hljs-number">5</span>-by-<span class="hljs-number">3</span> matrix of random floats on the GPU
[<span class="hljs-number">5</span> <span class="hljs-number">3</span> <span class="hljs-number">1</span> <span class="hljs-number">1</span>]
    <span class="hljs-number">0.7402</span>     <span class="hljs-number">0.4464</span>     <span class="hljs-number">0.7762</span>
    <span class="hljs-number">0.9210</span>     <span class="hljs-number">0.6673</span>     <span class="hljs-number">0.2948</span>
    <span class="hljs-number">0.0390</span>     <span class="hljs-number">0.1099</span>     <span class="hljs-number">0.7140</span>
    <span class="hljs-number">0.9690</span>     <span class="hljs-number">0.4702</span>     <span class="hljs-number">0.3585</span>
    <span class="hljs-number">0.9251</span>     <span class="hljs-number">0.5132</span>     <span class="hljs-number">0.6814</span>
</code></pre>
</li>
</ol>
<h4><a id="Documentation_44"></a>Documentation</h4>
<p>You can find the documentation for all the functions and macros available in the crate over <a href="http://arrayfire.github.io/arrayfire-rust/arrayfire/index.html">here</a>. You can also look at the ArrayFire native API documentation over <a href="http://sandbox.arrayfire.com/docs/group__func__categories.htm">here</a> which are categorized into different domains.</p>
<h4><a id="Example_Project_47"></a>Example Project</h4>
<p><a href="https://github.com/jramapuram/hal">hal</a> project is one of the initial adaptors of our rust wrapper for ArrayFire to enable use of GPUs from Rust via ArrayFire.</p>
<h4><a id="Credits_50"></a>Credits</h4>
<p>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.</p>

</body></html>