coaster/frameworks/native/
mod.rs

1//! Provides informations about the software system, such as OpenCL, CUDA, that contains the set of
2//! components to support [hardwares][hardware] with kernel execution.
3//! [hardware]: ../hardware/index.html
4//!
5//!
6
7pub use self::binary::Binary;
8pub use self::device::Cpu;
9pub use self::error::Error;
10pub use self::function::Function;
11use self::hardware::Hardware;
12#[cfg(not(feature = "unstable_alloc"))]
13pub use self::stable_alloc::allocate_boxed_slice;
14#[cfg(feature = "unstable_alloc")]
15pub use self::unstable_alloc::allocate_boxed_slice;
16use crate::backend::{Backend, IBackend};
17use crate::framework::IFramework;
18use crate::hardware::{HardwareType, IHardware};
19
20pub mod binary;
21pub mod device;
22mod error;
23pub mod flatbox;
24pub mod function;
25pub mod hardware;
26#[cfg(not(feature = "unstable_alloc"))]
27mod stable_alloc;
28#[cfg(feature = "unstable_alloc")]
29mod unstable_alloc;
30
31/// Initialise the Native Backend for running Tensor Operations
32pub fn get_native_backend() -> Backend<Native> {
33    Backend::<Native>::default().unwrap()
34}
35
36#[derive(Debug, Clone)]
37/// Provides the Native framework.
38///
39/// Native means host CPU only. The setup one relies on by default.
40pub struct Native {
41    hardwares: Vec<Hardware>,
42    binary: Binary,
43}
44
45/// Provides the Native framework trait for explicit Backend behaviour.
46///
47/// You usually would not need to care about this trait.
48pub trait INative {}
49
50impl INative for Native {}
51
52impl IFramework for Native {
53    type H = Hardware;
54    type D = Cpu;
55    type B = Binary;
56
57    fn ID() -> &'static str {
58        "NATIVE"
59    }
60
61    fn new() -> Native {
62        let hardwares = Native::load_hardwares().expect("Native hardwares are always ok. qed");
63        Self {
64            hardwares,
65            binary: Binary::new(),
66        }
67    }
68
69    fn load_hardwares() -> Result<Vec<Hardware>, crate::framework::Error> {
70        let cpu = Hardware::new(1)
71            .set_name(Some(String::from("Host CPU")))
72            .set_hardware_type(Some(HardwareType::CPU))
73            .set_compute_units(Some(1))
74            .build();
75        Ok(vec![cpu])
76    }
77
78    fn hardwares(&self) -> &[Hardware] {
79        &self.hardwares
80    }
81
82    fn binary(&self) -> &Binary {
83        &self.binary
84    }
85
86    fn new_device(&self, devices: &[Hardware]) -> Result<Self::D, crate::framework::Error> {
87        Ok(Cpu::new(devices.to_vec()))
88    }
89}
90
91impl IBackend for Backend<Native> {
92    type F = Native;
93
94    fn device(&self) -> &Cpu {
95        &self.device()
96    }
97}