Skip to content

Commit 27d4df5

Browse files
committed
Support to let users create Array from raw device pointers
- Reorganize the book and modified links in it to point to the documentation hosted by arrayfire. This will remove redirection from book to arrayfire docs from docs.rs. The book will hosted separately - CUDA ArrayFire Interop - af_cuda_interop crate - examples - OpenCL ArrayFire Interop - af_opencl_interop crate - examples Also modified tutorial book to be on same directory level as crates.
1 parent 6cd3893 commit 27d4df5

29 files changed

+1492
-387
lines changed

Cargo.toml

+10-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ exclude = [
1515
"arrayfire/*",
1616
]
1717

18+
[workspace]
19+
members = [
20+
"cuda-interop",
21+
"opencl-interop",
22+
]
23+
24+
[lib]
25+
name = "arrayfire"
26+
path = "src/lib.rs"
27+
1828
[package.metadata.docs.rs]
1929
rustdoc-args = [ "--html-in-header", "./scripts/mathjax.script", ]
2030

@@ -53,10 +63,6 @@ serde_derive = "1.0"
5363
serde = "1.0"
5464
rustc_version = "0.2"
5565

56-
[lib]
57-
name = "arrayfire"
58-
path = "src/lib.rs"
59-
6066
[[example]]
6167
name = "helloworld"
6268
path = "examples/helloworld.rs"

README.md

+59-64
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,61 @@
1+
[![ci][19]][16] [![docs][18]][3] [![book][22]][21] [![slack][17]][4] [![google-groups][20]][5]
2+
13
# Arrayfire Rust Bindings
24

3-
[ArrayFire](https://github.com/arrayfire/arrayfire) is a high performance
4-
library for parallel computing with an easy-to-use API. It enables users
5-
to write scientific computing code that is portable across CUDA, OpenCL
6-
and CPU devices. This project provides Rust bindings for the ArrayFire
7-
library. Given below table shows the rust bindings compatability with
8-
ArrayFire. If you find any bugs, please report them
9-
[here](https://github.com/arrayfire/arrayfire-rust/issues).
5+
[ArrayFire][1] is a high performance library for parallel computing with an easy-to-use API. It
6+
enables users to write scientific computing code that is portable across CUDA, OpenCL and CPU
7+
devices. This project provides Rust bindings for the ArrayFire library. Given below table shows
8+
the rust bindings compatability with ArrayFire. If you find any bugs, please report them [here][2].
109

1110
| arrayfire-rust | ArrayFire |
1211
|:--------------:|:---------:|
1312
| M.m.p1 | M.m.p2 |
1413

15-
Only, Major(M) & Minor(m) version numbers need to match. *p1* and *p2*
16-
are patch/fix updates for `arrayfire-rust` & `ArrayFire` respectively,
17-
and they don't need to match.
18-
19-
## Documentation
20-
21-
You can find the most recent updated documentation
22-
[here](http://arrayfire.github.io/arrayfire-rust/arrayfire/index.html).
23-
24-
## Communication
25-
26-
* [Slack Chat](https://join.slack.com/t/arrayfire-org/shared_invite/MjI4MjIzMDMzMTczLTE1MDI5ODg4NzYtN2QwNGE3ODA5OQ)
27-
* [Google Groups](https://groups.google.com/forum/#!forum/arrayfire-users)
14+
Only, Major(M) & Minor(m) version numbers need to match. *p1* and *p2* are patch/fix updates for
15+
`arrayfire-rust` & `ArrayFire` respectively, and they don't need to match.
2816

2917
## Supported platforms
3018

3119
Linux, Windows and OSX. Rust 1.15.1 or higher is required.
3220

33-
## Use from Crates.io [![](http://meritbadge.herokuapp.com/arrayfire)](https://crates.io/crates/arrayfire) [![](https://docs.rs/arrayfire/badge.svg)](https://docs.rs/arrayfire)
21+
## Use from Crates.io [![][6]][7] [![][8]][9]
3422

35-
To use the rust bindings for ArrayFire from crates.io, the following
36-
requirements are to be met first.
23+
To use the rust bindings for ArrayFire from crates.io, the following requirements are to be met first.
3724

38-
1. [Download and install ArrayFire binaries](https://arrayfire.com/download)
39-
based on your operating system.
40-
2. Set the evironment variable `AF_PATH` to point to ArrayFire installation
41-
root folder.
25+
1. [Download and install ArrayFire binaries][10] based on your operating system.
26+
2. Set the evironment variable `AF_PATH` to point to ArrayFire installation root folder.
4227
3. Make sure to add the path to lib files to your path environment variables.
4328
- On Linux: do `export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$AF_PATH/lib64`
4429
- On OSX: do `export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$AF_PATH/lib64`
4530
- On Windows: Add `%AF_PATH%\lib` to your PATH environment variable.
46-
4. Add `arrayfire = "3.7"` to the dependencies section of your project's
47-
Cargo.toml file. Make sure to change the version to latest available.
31+
4. Add `arrayfire = "3.7"` to the dependencies section of your project's Cargo.toml file. Make sure
32+
to change the version to latest available.
4833

49-
Once step (4) is over, you should be able to use ArrayFire in your Rust
50-
project. If you find any bugs, please report them
51-
[here](https://github.com/arrayfire/arrayfire-rust/issues).
34+
Once step (4) is over, you should be able to use ArrayFire in your Rust project. If you find any
35+
bugs, please report them [here][2].
5236

5337
## Build from Source
5438

55-
Edit [build.conf](build.conf) to modify the build flags. The structure is a
56-
simple JSON blob. Currently Rust does not allow key:value pairs to be passed
57-
from the CLI. To use an existing ArrayFire installation modify the first three
58-
JSON values. You can install ArrayFire using one of the following two ways.
39+
Edit [build.conf](build.conf) to modify the build flags. The structure is a simple JSON blob.
40+
Currently Rust does not allow key:value pairs to be passed from the CLI. To use an existing
41+
ArrayFire installation modify the first three JSON values. You can install ArrayFire using
42+
one of the following two ways.
5943

60-
- [Download and install binaries](https://arrayfire.com/download)
61-
- [Build and install from source](https://github.com/arrayfire/arrayfire)
44+
- [Download and install binaries][10]
45+
- [Build and install from source][1]
6246

63-
To build arrayfire submodule available in the rust wrapper, you have to do
64-
the following.
47+
To build arrayfire submodule available in the rust wrapper, you have to do the following.
6548

6649
```bash
6750
git submodule update --init --recursive
68-
cargo build
51+
cargo build // use --all to build all crates in the workspace
6952
```
70-
This is recommended way to build Rust wrapper since the submodule points to
71-
the most compatible version of ArrayFire the Rust wrapper has been tested with.
72-
You can find the ArrayFire dependencies below.
53+
This is recommended way to build Rust wrapper since the submodule points to the most compatible
54+
version of ArrayFire the Rust wrapper has been tested with. You can find the ArrayFire dependencies below.
7355

74-
- [Linux](https://github.com/arrayfire/arrayfire/wiki/Build-Instructions-for-Linux)
75-
- [OSX](https://github.com/arrayfire/arrayfire/wiki/Build-Instructions-for-OSX)
76-
- [Windows](https://github.com/arrayfire/arrayfire/wiki/Build-Instructions-for-Windows)
56+
- [Linux][11]
57+
- [OSX][12]
58+
- [Windows][13]
7759

7860
## Example
7961

@@ -102,24 +84,37 @@ Create a 5-by-3 matrix of random floats on the GPU
10284

10385
## Acknowledgements
10486

105-
The ArrayFire library is written by developers at
106-
[ArrayFire](http://arrayfire.com) LLC with
107-
[contributions from several individuals](https://github.com/arrayfire/arrayfire_rust/graphs/contributors).
108-
109-
The developers at ArrayFire LLC have received partial financial support
110-
from several grants and institutions. Those that wish to receive public
111-
acknowledgement are listed below:
112-
113-
<!--
114-
The following section contains acknowledgements for grant funding. In most
115-
circumstances, the specific phrasing of the text is mandated by the grant
116-
provider. Thus these acknowledgements must remain intact without modification.
117-
-->
87+
The ArrayFire library is written by developers at [ArrayFire][14] LLC with [contributions][15]
88+
from several individuals. The developers at ArrayFire LLC have received partial financial support
89+
from several grants and institutions. Those that wish to receive public acknowledgement are listed
90+
below:
11891

11992
### Grants
12093

121-
This material is based upon work supported by the DARPA SBIR Program Office
122-
under Contract Numbers W31P4Q-14-C-0012 and W31P4Q-15-C-0008.
123-
Any opinions, findings and conclusions or recommendations expressed in this
124-
material are those of the author(s) and do not necessarily reflect the views of
94+
This material is based upon work supported by the DARPA SBIR Program Office under Contract Numbers
95+
W31P4Q-14-C-0012 and W31P4Q-15-C-0008. Any opinions, findings and conclusions or recommendations
96+
expressed in this material are those of the author(s) and do not necessarily reflect the views of
12597
the DARPA SBIR Program Office.
98+
99+
[1]: https://github.com/arrayfire/arrayfire
100+
[2]: https://github.com/arrayfire/arrayfire-rust/issues
101+
[3]: http://arrayfire.github.io/arrayfire-rust/arrayfire/index.html
102+
[4]: https://join.slack.com/t/arrayfire-org/shared_invite/MjI4MjIzMDMzMTczLTE1MDI5ODg4NzYtN2QwNGE3ODA5OQ
103+
[5]: https://groups.google.com/forum/#!forum/arrayfire-users
104+
[6]: http://meritbadge.herokuapp.com/arrayfire
105+
[7]: https://crates.io/crates/arrayfire
106+
[8]: https://docs.rs/arrayfire/badge.svg
107+
[9]: https://docs.rs/arrayfire
108+
[10]: https://arrayfire.com/download
109+
[11]: https://github.com/arrayfire/arrayfire/wiki/Build-Instructions-for-Linux
110+
[12]: https://github.com/arrayfire/arrayfire/wiki/Build-Instructions-for-OSX
111+
[13]: https://github.com/arrayfire/arrayfire/wiki/Build-Instructions-for-Windows
112+
[14]: https://arrayfire.com/
113+
[15]: https://github.com/arrayfire/arrayfire_rust/graphs/contributors
114+
[16]: https://github.com/arrayfire/arrayfire-rust/actions?workflow=CI
115+
[17]: https://img.shields.io/badge/arrayfire-community-e69138?logo=slack
116+
[18]: https://img.shields.io/badge/arrayfire-Docs-blue?logo=readthedocs
117+
[19]: https://github.com/arrayfire/arrayfire-rust/workflows/ci/badge.svg?event=push
118+
[20]: https://img.shields.io/badge/arrayfire-google--groups-orange
119+
[21]: http://arrayfire.org/arrayfire-rust/book/index.html
120+
[22]: https://img.shields.io/badge/arrayfire-mdbook-073763?logo=readthedocs

cuda-interop/Cargo.toml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "af-cuda-interop"
3+
version = "0.1.0"
4+
authors = ["Pradeep Garigipati <pradeep@arrayfire.com>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
libc = "0.2"
9+
arrayfire = { path = "../" }
10+
cuda-runtime-sys = "0.3.0-alpha.1"
11+
12+
[dev-dependencies]
13+
rustacuda = "0.1"
14+
rustacuda_core = "0.1"
15+
16+
[[example]]
17+
name = "custom_kernel"
18+
path = "examples/custom_kernel.rs"
19+
20+
[[example]]
21+
name = "cuda_af_app"
22+
path = "examples/cuda_af_app.rs"

cuda-interop/README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[![ci][19]][16] [![][18]][3] [![][17]][4]
2+
3+
# ArrayFire CUDA Interop
4+
5+
This crate is an addition on top of [ArrayFire][1] crate to enable users to mix RAW CUDA code in rust
6+
and [ArrayFire][1].
7+
8+
## Supported platforms
9+
10+
Supported on all platforms [arrayfire-rust][1] is supported.
11+
12+
## Usage
13+
14+
Command to build the crate
15+
```
16+
cargo build -p af_cuda_interop
17+
```
18+
19+
Use the following command to run an example
20+
```
21+
cargo run -p af_cuda_interop --example custom_kernel
22+
```
23+
24+
This crate can be used by directly using the packages on crates.io or building them on your own.
25+
The process for this can be found on [arrayfire-rust#readme][2]
26+
27+
## Acknowledgements
28+
29+
The ArrayFire library is written by developers at [ArrayFire][14] LLC with [contributions][15]
30+
from several individuals. The developers at ArrayFire LLC have received partial financial support
31+
from several grants and institutions. Those that wish to receive public acknowledgement are listed
32+
below:
33+
34+
### Grants
35+
36+
This material is based upon work supported by the DARPA SBIR Program Office under Contract Numbers
37+
W31P4Q-14-C-0012 and W31P4Q-15-C-0008. Any opinions, findings and conclusions or recommendations
38+
expressed in this material are those of the author(s) and do not necessarily reflect the views of
39+
the DARPA SBIR Program Office.
40+
41+
[1]: https://github.com/arrayfire/arrayfire-rust
42+
[2]: https://github.com/arrayfire/arrayfire-rust/blob/master/README.md
43+
[3]: http://arrayfire.github.io/arrayfire-rust/af_cuda_interop/index.html
44+
[4]: https://join.slack.com/t/arrayfire-org/shared_invite/MjI4MjIzMDMzMTczLTE1MDI5ODg4NzYtN2QwNGE3ODA5OQ
45+
[14]: https://arrayfire.com/
46+
[15]: https://github.com/arrayfire/arrayfire_rust/graphs/contributors
47+
[16]: https://github.com/arrayfire/arrayfire-rust/actions?workflow=CI
48+
[17]: https://img.shields.io/badge/af_cuda_interop-community-e69138?logo=slack
49+
[18]: https://img.shields.io/badge/af_cuda_interop-Docs-blue?logo=readthedocs
50+
[19]: https://github.com/arrayfire/arrayfire-rust/workflows/ci/badge.svg?event=push

cuda-interop/examples/cuda_af_app.rs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use arrayfire::{af_print, dim4, info, set_device, Array};
2+
use rustacuda::prelude::*;
3+
use rustacuda::*;
4+
5+
fn main() {
6+
// MAKE SURE to do all rustacuda initilization before arrayfire API's
7+
// first call. It seems like some CUDA context state is getting messed up
8+
// if we mix CUDA context init(device, context, module, stream) with ArrayFire API
9+
match rustacuda::init(CudaFlags::empty()) {
10+
Ok(()) => {}
11+
Err(e) => panic!("rustacuda init failure: {:?}", e),
12+
}
13+
let device = match Device::get_device(0) {
14+
Ok(d) => d,
15+
Err(e) => panic!("Failed to get device: {:?}", e),
16+
};
17+
let _context =
18+
match Context::create_and_push(ContextFlags::MAP_HOST | ContextFlags::SCHED_AUTO, device) {
19+
Ok(c) => c,
20+
Err(e) => panic!("Failed to create context: {:?}", e),
21+
};
22+
let stream = match Stream::new(StreamFlags::NON_BLOCKING, None) {
23+
Ok(s) => s,
24+
Err(e) => panic!("Failed to create stream: {:?}", e),
25+
};
26+
27+
let mut in_x = DeviceBuffer::from_slice(&[1.0f32; 10]).unwrap();
28+
let mut in_y = DeviceBuffer::from_slice(&[2.0f32; 10]).unwrap();
29+
30+
// wait for any prior kernels to finish before passing
31+
// the device pointers to ArrayFire
32+
match stream.synchronize() {
33+
Ok(()) => {}
34+
Err(e) => panic!("Stream sync failure: {:?}", e),
35+
};
36+
37+
set_device(0);
38+
info();
39+
40+
let x = Array::new_from_device_ptr(in_x.as_device_ptr().as_raw_mut(), dim4!(10));
41+
let y = Array::new_from_device_ptr(in_y.as_device_ptr().as_raw_mut(), dim4!(10));
42+
43+
// Lock so that ArrayFire doesn't free pointers from RustaCUDA
44+
// But we have to make sure these pointers stay in valid scope
45+
// as long as the associated ArrayFire Array objects are valid
46+
x.lock();
47+
y.lock();
48+
49+
af_print!("x", x);
50+
af_print!("y", y);
51+
52+
let o = x + y;
53+
af_print!("out", o);
54+
55+
let _o_dptr = unsafe { o.device_ptr() }; // Calls an implicit lock
56+
57+
// User has to call unlock if they want to relenquish control to ArrayFire
58+
59+
// Once the non-arrayfire operations are done, call unlock.
60+
o.unlock(); // After this, there is no guarantee that value of o_dptr is valid
61+
}

0 commit comments

Comments
 (0)