1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
//! GPGPU framework for Rust based on [CUDA Driver API] //! //! [CUDA Driver API]: https://docs.nvidia.com/cuda/cuda-driver-api/ //! //! Basic Examples //! -------------- //! //! ### Vector Add //! //! ``` //! use accel::*; //! use accel_derive::kernel; //! //! #[kernel] //! unsafe fn add(a: *const f64, b: *const f64, c: *mut f64, n: usize) { //! let i = accel_core::index(); //! if (i as usize) < n { //! *c.offset(i) = *a.offset(i) + *b.offset(i); //! } //! } //! //! fn main() -> error::Result<()> { //! let device = Device::nth(0)?; //! let ctx = device.create_context(); //! //! // Allocate memories on GPU //! let n = 32; //! let mut a = DeviceMemory::<f64>::new(&ctx, n); //! let mut b = DeviceMemory::<f64>::new(&ctx, n); //! let mut c = DeviceMemory::<f64>::new(&ctx, n); //! //! // Accessible from CPU as usual Rust slice (though this will be slow) //! for i in 0..n { //! a[i] = i as f64; //! b[i] = 2.0 * i as f64; //! } //! println!("a = {:?}", a.as_slice()); //! println!("b = {:?}", b.as_slice()); //! //! // Launch kernel synchronously //! add(&ctx, //! 1 /* grid */, //! n /* block */, //! &(&a.as_ptr(), &b.as_ptr(), &c.as_mut_ptr(), &n) //! ).expect("Kernel call failed"); //! //! println!("c = {:?}", c.as_slice()); //! Ok(()) //! } //! ``` //! //! ### Assertion on GPU //! //! ``` //! use accel::*; //! use accel_derive::kernel; //! //! #[kernel] //! fn assert() { //! accel_core::assert_eq!(1 + 2, 4); // will fail //! } //! //! fn main() -> error::Result<()> { //! let device = Device::nth(0)?; //! let ctx = device.create_context(); //! let result = assert(&ctx, 1 /* grid */, 4 /* block */, &()); //! assert!(result.is_err()); // assertion failed //! Ok(()) //! } //! ``` //! //! ### Print from GPU //! //! ``` //! use accel::*; //! use accel_derive::kernel; //! //! #[kernel] //! pub fn print() { //! let i = accel_core::index(); //! accel_core::println!("Hello from {}", i); //! } //! //! fn main() -> error::Result<()> { //! let device = Device::nth(0)?; //! let ctx = device.create_context(); //! print(&ctx, 1, 4, &())?; //! Ok(()) //! } //! ``` //! //! Advanced Examples //! ----------------- //! //! ### Get compiled PTX as `String` //! //! The proc-macro `#[kernel]` creates a submodule `add::` in addition to a function `add`. //! Kernel Rust code is compiled into PTX string using rustc's `nvptx64-nvidia-cuda` toolchain. //! Generated PTX string is embedded into proc-macro output as `{kernel_name}::PTX_STR`. //! //! ``` //! use accel_derive::kernel; //! //! #[kernel] //! unsafe fn add(a: *const f64, b: *const f64, c: *mut f64, n: usize) { //! let i = accel_core::index(); //! if (i as usize) < n { //! *c.offset(i) = *a.offset(i) + *b.offset(i); //! } //! } //! //! fn main() { //! // PTX assembler code is embedded as `add::PTX_STR` //! println!("{}", add::PTX_STR); //! } //! ``` //! //! ### Asynchronous launch //! //! `#[kernel]` creates `assert::Module` type definition which implements [Launchable] trait. //! This struct will read `PTX_STR` using [Module]. //! //! [Module]: ./module/struct.Module.html //! [Launchable]: ./module/trait.Launchable.html //! //! ``` //! use accel::*; //! use accel_derive::kernel; //! //! #[kernel] //! fn assert() { //! accel_core::assert_eq!(1 + 2, 4); //! } //! //! fn main() -> error::Result<()> { //! let device = Device::nth(0)?; //! let ctx = device.create_context(); //! let stream = Stream::new(&ctx); //! //! let module = assert::Module::new(&ctx)?; //! module.stream_launch(&stream, 1, 4, &())?; // lanch will succeed //! assert!(stream.sync().is_err()); // assertion failed is detected in next sync //! Ok(()) //! } //! ``` extern crate cuda_driver_sys as cuda; pub mod array; pub mod device; pub mod error; pub mod linker; pub mod memory; pub mod module; pub mod stream; pub use array::*; pub use device::*; pub use linker::*; pub use memory::*; pub use module::*; pub use stream::*;