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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//! custos is a minimal OpenCL, CUDA and host CPU array manipulation engine / framework.
//! It provides some matrix / buffer operations: matrix multiplication (BLAS, cuBLAS), element-wise arithmetic (vector addition, ...), set all elements to zero (or default value).
//! To use more operations: [custos-math]
//!
//! [custos-math]: https://github.com/elftausend/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 libs::*;

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

pub mod libs;

mod buffer;
mod count;

pub mod number;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeviceType {
    CPU = 0,
    #[cfg(feature="cuda")]
    CUDA = 1,
    #[cfg(feature="opencl")]
    CL = 2,
    None = 3,
}

#[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();
}

pub struct Error {
    pub error: Box<dyn std::error::Error + Send>,
}

impl<E: std::error::Error + PartialEq + 'static> PartialEq<E> for Error {
    fn eq(&self, other: &E) -> bool {
        let e = self.error.downcast_ref::<E>();
        if let Some(e) = e {
            return e == other;
        }
        false
    }
}

impl From<Error> for Box<dyn std::error::Error> {
    fn from(e: Error) -> Self {
        e.error
    }
}

impl Error {
    pub fn kind<E: std::error::Error + PartialEq + 'static>(&self) -> Option<&E> {
        self.error.downcast_ref::<E>()
    }
}

impl<T: std::error::Error + Send + 'static> From<T> for Error {
    fn from(error: T) -> Self {
        Error {
            error: Box::new(error),
        }
    }
}

impl core::fmt::Debug for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.error)?;
        Ok(())
    }
}

impl core::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.error)?;
        Ok(())
    }
}

pub type Result<T> = std::result::Result<T, Error>;

/// Manage device memory
///
/// # Example
/// ```
/// use custos::{CPU, Alloc, Buffer, VecRead, BufFlag, AsDev};
///
/// 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,
///     p: std::marker::PhantomData
/// };
/// assert_eq!(vec![0.; 12], device.read(&buf));
/// ```
pub trait Alloc<T> {
    /// Allocate memory
    /// # Example
    /// ```
    /// use custos::{CPU, Alloc, Buffer, VecRead, BufFlag, AsDev};
    ///
    /// 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,
    ///     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};
    ///
    /// 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,
    ///     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);
    fn alloc_with_vec(&self, vec: Vec<T>) -> (*mut T, *mut c_void, u64) {
        self.with_data(&vec)
    }
    fn as_dev(&self) -> Device;
}

///All 'base' traits?
pub trait BaseDevice<T>: Alloc<T> + VecRead<T> {}

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]);
    /// Write 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!()
    }
}

trait ManualMem<T> {
    fn drop_buf(&self, buf: Buffer<T>);
}

pub trait CacheBuf<'a, T> {
    /// 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>;
}

pub trait AsDev {
    fn dev(&self) -> Device 
    where
        Self: Alloc<u8> + Sized,
    {
        Alloc::as_dev(self)
    }
}

#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum DeviceError {
    ConstructError,
    CPUtoCUDA,
}

impl DeviceError {
    pub fn as_str(&self) -> &'static str {
        match self {
            DeviceError::ConstructError => {
                "Only a non-drop buffer can be converted to a CPU+OpenCL buffer"
            },
            DeviceError::CPUtoCUDA => {
                "Only a CPU Buffer can be converted to a CUDA Buffer"
            }
        }
    }
}

impl core::fmt::Debug for DeviceError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())?;
        Ok(())
    }
}

impl core::fmt::Display for DeviceError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())?;
        Ok(())
    }
}

impl std::error::Error for DeviceError {}

/// 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
    }}
}