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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//! A minimal OpenCL, CUDA and host CPU array manipulation engine / framework written in Rust.
//! This crate provides the tools for executing custom array operations with the CPU, as well as with CUDA and OpenCL devices.<br>
//! This guide demonstrates how operations can be implemented for the compute devices: [implement_operations.md](implement_operations.md)<br>
//! or to see it at a larger scale, look here: [custos-math]
//!
//! ## [Examples]
//!
//! [examples]: https://github.com/elftausend/custos/tree/main/examples
//!
//! Using the host CPU as the compute device:
//!
//! [cpu_readme.rs]
//!
//! [cpu_readme.rs]: https://github.com/elftausend/custos/blob/main/examples/cpu_readme.rs
//!
//! ```rust
//! use custos::{CPU, ClearBuf, VecRead, Buffer};
//!
//! let device = CPU::new();
//! let mut a = Buffer::from(( &device, [1, 2, 3, 4, 5, 6]));
//!     
//! // specify device for operation
//! device.clear(&mut a);
//! assert_eq!(device.read(&a), [0; 6]);
//!
//! let device = CPU::new();
//!
//! let mut a = Buffer::from(( &device, [1, 2, 3, 4, 5, 6]));
//! a.clear();
//!
//! assert_eq!(a.read(), vec![0; 6]);
//! ```
use std::{ffi::c_void, ptr::null_mut};

//pub use libs::*;
pub use buffer::*;
pub use count::*;
pub use devices::*;
pub use graph::*;
pub use error::*;

pub use devices::cpu::CPU;
#[cfg(feature = "cuda")]
pub use devices::cuda::CudaDevice;
#[cfg(feature = "opencl")]
pub use devices::opencl::{CLDevice, InternCLDevice};

pub mod devices;

mod buffer;
mod count;
mod graph;
mod error;

pub mod number;

/// Used to determine which device type [`Device`] is of.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeviceType {
    CPU = 0,
    #[cfg(feature = "cuda")]
    CUDA = 1,
    #[cfg(feature = "opencl")]
    CL = 2,
    None = 3,
}

/// `Device` is another representation of a compute device.<br>
/// It stores the type of the device and a pointer to the device from which `Device` originates from.<br>
/// This is used instead of another "device" generic for [`Buffer`].
///
/// # Example
/// ```rust
/// use custos::{CPU, AsDev, Device, DeviceType};
///
/// let cpu = CPU::new();
/// let device: Device = cpu.dev();
/// assert_eq!(device.device_type, DeviceType::CPU);
/// assert_eq!(device.device as *const CPU, &cpu as *const CPU);
/// ```
#[derive(Debug, Clone, Copy)]
pub struct Device {
    pub device_type: DeviceType,
    pub device: *mut u8,
}

impl Default for Device {
    fn default() -> Self {
        Self {
            device_type: DeviceType::None,
            device: null_mut(),
        }
    }
}

thread_local! {
    pub static GLOBAL_CPU: CPU = CPU::new();
}

/// This trait is for allocating memory on the implemented device.
///
/// # Example
/// ```
/// use custos::{CPU, Alloc, Buffer, VecRead, BufFlag, AsDev, GraphReturn};
///
/// let device = CPU::new();
/// let ptrs: (*mut f32, *mut std::ffi::c_void, u64) = device.alloc(12);
///
/// let buf = Buffer {
///     ptr: ptrs,
///     len: 12,
///     device: AsDev::dev(&device),
///     flag: BufFlag::None,
///     node: device.graph().add_leaf(12),
///     p: std::marker::PhantomData
/// };
/// assert_eq!(vec![0.; 12], device.read(&buf));
/// ```
pub trait Alloc<T> {
    /// Allocate memory on the implemented device.
    /// # Example
    /// ```
    /// use custos::{CPU, Alloc, Buffer, VecRead, BufFlag, AsDev, GraphReturn};
    ///
    /// let device = CPU::new();
    /// let ptrs: (*mut f32, *mut std::ffi::c_void, u64) = device.alloc(12);
    ///
    /// let buf = Buffer {
    ///     ptr: ptrs,
    ///     len: 12,
    ///     device: AsDev::dev(&device),
    ///     flag: BufFlag::None,
    ///     node: device.graph().add_leaf(12),
    ///     p: std::marker::PhantomData
    /// };
    /// assert_eq!(vec![0.; 12], device.read(&buf));
    /// ```
    fn alloc(&self, len: usize) -> (*mut T, *mut c_void, u64);

    /// Allocate new memory with data
    /// # Example
    /// ```
    /// use custos::{CPU, Alloc, Buffer, VecRead, BufFlag, AsDev, GraphReturn};
    ///
    /// let device = CPU::new();
    /// let ptrs: (*mut u8, *mut std::ffi::c_void, u64) = device.with_data(&[1, 5, 4, 3, 6, 9, 0, 4]);
    ///
    /// let buf = Buffer {
    ///     ptr: ptrs,
    ///     len: 8,
    ///     device: AsDev::dev(&device),
    ///     flag: BufFlag::None,
    ///     node: device.graph().add_leaf(8),
    ///     p: std::marker::PhantomData
    /// };
    /// assert_eq!(vec![1, 5, 4, 3, 6, 9, 0, 4], device.read(&buf));
    /// ```
    fn with_data(&self, data: &[T]) -> (*mut T, *mut c_void, u64)
    where
        T: Clone;

    /// If the vector `vec` was allocated previously, this function can be used in order to reduce the amount of allocations, which may be faster than using a slice of `vec`.
    fn alloc_with_vec(&self, vec: Vec<T>) -> (*mut T, *mut c_void, u64)
    where
        T: Clone,
    {
        self.with_data(&vec)
    }

    /// Creates a generic representation of the device
    fn as_dev(&self) -> Device;
}

/// Trait for implementing the clear() operation for the compute devices.
pub trait ClearBuf<T> {
    /// Sets all elements of the matrix to zero.
    /// # Example
    /// ```
    /// use custos::{CPU, ClearBuf, Buffer};
    ///
    /// let device = CPU::new();
    /// let mut a = Buffer::from((&device, [2, 4, 6, 8, 10, 12]));
    /// assert_eq!(a.read(), vec![2, 4, 6, 8, 10, 12]);
    ///
    /// device.clear(&mut a);
    /// assert_eq!(a.read(), vec![0; 6]);
    /// ```
    fn clear(&self, buf: &mut Buffer<T>);
}

/// Trait for reading buffers.
pub trait VecRead<T> {
    /// Read the data of a buffer into a vector
    /// # Example
    /// ```
    /// use custos::{CPU, Buffer, VecRead};
    ///
    /// let device = CPU::new();
    /// let a = Buffer::from((&device, [1., 2., 3., 3., 2., 1.,]));
    /// let read = device.read(&a);
    /// assert_eq!(vec![1., 2., 3., 3., 2., 1.,], read);
    /// ```
    fn read(&self, buf: &Buffer<T>) -> Vec<T>;
}

/// Trait for writing data to buffers.
pub trait WriteBuf<T> {
    /// Write data to the buffer.
    /// # Example
    /// ```
    /// use custos::{CPU, Buffer, WriteBuf};
    ///
    /// let device = CPU::new();
    /// let mut buf = Buffer::new(&device, 4);
    /// device.write(&mut buf, &[9, 3, 2, -4]);
    /// assert_eq!(buf.as_slice(), &[9, 3, 2, -4])
    ///
    /// ```
    fn write(&self, buf: &mut Buffer<T>, data: &[T]);
    /// Writes data from <Device> Buffer to other <Device> Buffer.
    // TODO: implement, change name of fn? -> set_.. ?
    fn write_buf(&self, _dst: &mut Buffer<T>, _src: &Buffer<T>) {
        unimplemented!()
    }
}

/// This trait is used to clone a buffer based on a specific device type.
pub trait CloneBuf<'a, T> {
    /// Creates a deep copy of the specified buffer.
    /// # Example
    ///
    /// ```
    /// use custos::{CPU, Buffer, CloneBuf};
    ///
    /// let device = CPU::new();
    /// let buf = Buffer::from((&device, [1., 2., 6., 2., 4.,]));
    ///
    /// let cloned = device.clone_buf(&buf);
    /// assert_eq!(buf.read(), cloned.read());
    /// ```
    fn clone_buf(&'a self, buf: &Buffer<'a, T>) -> Buffer<'a, T>;
}

/// This trait is used to retrieve a cached buffer from a specific device type.
pub trait CacheBuf<'a, T> {
    #[cfg_attr(feature = "realloc", doc = "```ignore")]
    /// Adds a buffer to the cache. Following calls will return this buffer, if the corresponding internal count matches with the id used in the cache.
    /// # Example
    /// ```
    /// use custos::{CPU, VecRead, set_count, get_count, CacheBuf};
    ///
    /// let device = CPU::new();
    /// assert_eq!(0, get_count());
    ///
    /// let mut buf = CacheBuf::<f32>::cached(&device, 10);
    /// assert_eq!(1, get_count());
    ///
    /// for value in buf.as_mut_slice() {
    ///     *value = 1.5;
    /// }
    ///    
    /// set_count(0);
    /// let buf = CacheBuf::<f32>::cached(&device, 10);
    /// assert_eq!(device.read(&buf), vec![1.5; 10]);
    /// ```
    fn cached(&'a self, len: usize) -> Buffer<'a, T>;
}

/// This trait is a non-generic variant for calling [`Alloc`]'s `Alloc::<T>::as_dev(..)`
pub trait AsDev {
    fn dev(&self) -> Device
    where
        Self: Alloc<u8> + Sized,
    {
        Alloc::as_dev(self)
    }
}

/// Return a device that implements the trait provided thus giving access to the functions implemented by the trait.
///
/// # Example
/// ```
/// use custos::{Error, CPU, get_device, VecRead, AsDev, Buffer};
///
/// fn main() -> Result<(), Error> {
///     let device = CPU::new();
///     let read = get_device!(device.dev(), VecRead<f32>);
///
///     let buf = Buffer::from(( &device, [1.51, 6.123, 7., 5.21, 8.62, 4.765]));
///     let read = read.read(&buf);
///     assert_eq!(read, vec![1.51, 6.123, 7., 5.21, 8.62, 4.765]);
///     Ok(())
/// }
/// ```
#[macro_export]
macro_rules! get_device {
    ($device:expr, $t:ident<$g:ident>) => {{
        use $crate::{ DeviceType, CPU };

        let device: &dyn $t<$g> = unsafe {
            //&*($device.device as *mut CPU)
            match $device.device_type {
                DeviceType::CPU => &*($device.device as *mut CPU),
                #[cfg(feature="cuda")]
                DeviceType::CUDA => &*($device.device as *mut $crate::CudaDevice),
                #[cfg(feature="opencl")]
                DeviceType::CL => &*($device.device as *mut $crate::CLDevice),
                // TODO: convert to error
                _ => panic!(
                    "No device found to execute this operation with. 
                    If you are using get_device! in your own crate, 
                    you need to add 'opencl' and 'cuda' as features in your Cargo.toml."
                ),
            }

        };
        device
    }}
}