accel/lib.rs
1//! GPGPU framework for Rust based on [CUDA Driver API]
2//!
3//! [CUDA Driver API]: https://docs.nvidia.com/cuda/cuda-driver-api/
4//!
5//! Setup
6//! -----
7//! Currently (0.3.0), accel works only on Linux system. Windows support will come in future release (0.3.x or 0.4~).
8//!
9//! 1. Install [CUDA](https://developer.nvidia.com/cuda-downloads) on your system
10//! 2. Setup Rust environement using rustup (Requires 1.42 or later)
11//! 3. Add `nvptx64-nvidia-cuda` target and install `ptx-linker`, or run
12//!
13//! ```shell
14//! curl -sSL https://gitlab.com/termoshtt/accel/raw/master/setup_nvptx_toolchain.sh | bash
15//! ```
16//!
17//! Examples
18//! --------
19//! accel works with stable Rust
20//!
21//! ```toml
22//! [dependencies]
23//! accel = "=0.3.0-alpha.2"
24//! ```
25//!
26//! Do **NOT** add `accel-core` to `[dependencies]`.
27//! It will be linked automatically into the device code.
28//!
29//! ### Vector Add
30//!
31//! ```
32//! use accel::*;
33//!
34//! #[kernel]
35//! unsafe fn add(a: *const f32, b: *const f32, c: *mut f32, n: usize) {
36//! let i = accel_core::index();
37//! if (i as usize) < n {
38//! *c.offset(i) = *a.offset(i) + *b.offset(i);
39//! }
40//! }
41//!
42//! fn main() -> error::Result<()> {
43//! let device = Device::nth(0)?;
44//! let ctx = device.create_context();
45//!
46//! // Allocate memories on GPU
47//! let n = 32;
48//! let mut a = DeviceMemory::<f32>::zeros(ctx.clone(), n);
49//! let mut b = DeviceMemory::<f32>::zeros(ctx.clone(), n);
50//! let mut c = DeviceMemory::<f32>::zeros(ctx.clone(), n);
51//!
52//! // Accessible from CPU as usual Rust slice (though this will be slow)
53//! for i in 0..n {
54//! a[i] = i as f32;
55//! b[i] = 2.0 * i as f32;
56//! }
57//! println!("a = {:?}", a.as_slice());
58//! println!("b = {:?}", b.as_slice());
59//!
60//! // Launch kernel synchronously
61//! add(ctx,
62//! 1 /* grid */,
63//! n /* block */,
64//! &(&a.as_ptr(), &b.as_ptr(), &c.as_mut_ptr(), &n)
65//! ).expect("Kernel call failed");
66//!
67//! println!("c = {:?}", c.as_slice());
68//! Ok(())
69//! }
70//! ```
71//!
72//! ### Assertion on GPU
73//!
74//! ```
75//! use accel::*;
76//!
77//! #[kernel]
78//! fn assert() {
79//! accel_core::assert_eq!(1 + 2, 4); // will fail
80//! }
81//!
82//! fn main() -> error::Result<()> {
83//! let device = Device::nth(0)?;
84//! let ctx = device.create_context();
85//! let result = assert(ctx, 1 /* grid */, 4 /* block */, &());
86//! assert!(result.is_err()); // assertion failed
87//! Ok(())
88//! }
89//! ```
90//!
91//! ### Print from GPU
92//!
93//! ```
94//! use accel::*;
95//!
96//! #[kernel]
97//! pub fn print() {
98//! let i = accel_core::index();
99//! accel_core::println!("Hello from {}", i);
100//! }
101//!
102//! fn main() -> error::Result<()> {
103//! let device = Device::nth(0)?;
104//! let ctx = device.create_context();
105//! print(ctx, 1, 4, &())?;
106//! Ok(())
107//! }
108//! ```
109
110extern crate cuda_driver_sys as cuda;
111
112pub use accel_derive::kernel;
113
114pub mod device;
115pub mod error;
116pub mod linker;
117pub mod memory;
118pub mod module;
119pub mod profiler;
120pub mod stream;
121
122pub use device::*;
123pub use linker::*;
124pub use memory::*;
125pub use module::*;
126pub use profiler::*;
127pub use stream::*;