rstsr_core/device_cpu_serial/
device.rs1use crate::prelude_dev::*;
2use num::{complex::ComplexFloat, Num};
3use rstsr_dtype_traits::DTypeIntoFloatAPI;
4
5#[derive(Clone, Debug, Default)]
6pub struct DeviceCpuSerial {
7 default_order: FlagOrder,
8}
9
10impl DeviceBaseAPI for DeviceCpuSerial {
11 fn same_device(&self, other: &Self) -> bool {
12 self.default_order == other.default_order
13 }
14
15 fn default_order(&self) -> FlagOrder {
16 self.default_order
17 }
18
19 fn set_default_order(&mut self, order: FlagOrder) {
20 self.default_order = order;
21 }
22}
23
24impl<T> DeviceRawAPI<T> for DeviceCpuSerial {
25 type Raw = Vec<T>;
26}
27
28impl<T> DeviceStorageAPI<T> for DeviceCpuSerial {
29 fn len<R>(storage: &Storage<R, T, Self>) -> usize
30 where
31 R: DataAPI<Data = Self::Raw>,
32 {
33 storage.raw().len()
34 }
35
36 fn to_cpu_vec<R>(storage: &Storage<R, T, Self>) -> Result<Vec<T>>
37 where
38 Self::Raw: Clone,
39 R: DataAPI<Data = Self::Raw>,
40 {
41 Ok(storage.raw().clone())
42 }
43
44 fn into_cpu_vec<R>(storage: Storage<R, T, Self>) -> Result<Vec<T>>
45 where
46 Self::Raw: Clone,
47 R: DataCloneAPI<Data = Self::Raw>,
48 {
49 let (raw, _) = storage.into_raw_parts();
50 Ok(raw.into_owned().into_raw())
51 }
52
53 #[inline]
54 fn get_index<R>(storage: &Storage<R, T, Self>, index: usize) -> T
55 where
56 T: Clone,
57 R: DataAPI<Data = Self::Raw>,
58 {
59 storage.raw()[index].clone()
60 }
61
62 #[inline]
63 fn get_index_ptr<R>(storage: &Storage<R, T, Self>, index: usize) -> *const T
64 where
65 R: DataAPI<Data = Self::Raw>,
66 {
67 storage.raw().get(index).unwrap() as *const T
68 }
69
70 #[inline]
71 fn get_index_mut_ptr<R>(storage: &mut Storage<R, T, Self>, index: usize) -> *mut T
72 where
73 R: DataMutAPI<Data = Self::Raw>,
74 {
75 storage.raw_mut().get_mut(index).unwrap() as *mut T
76 }
77
78 #[inline]
79 fn set_index<R>(storage: &mut Storage<R, T, Self>, index: usize, value: T)
80 where
81 R: DataMutAPI<Data = Self::Raw>,
82 {
83 storage.raw_mut()[index] = value;
84 }
85}
86
87impl<T> DeviceAPI<T> for DeviceCpuSerial {}
88
89impl<T, D> DeviceComplexFloatAPI<T, D> for DeviceCpuSerial
90where
91 T: ComplexFloat + DTypeIntoFloatAPI<FloatType = T>,
92 T::Real: DTypeIntoFloatAPI<FloatType = T::Real>,
93 D: DimAPI,
94{
95}
96
97impl<T, D> DeviceNumAPI<T, D> for DeviceCpuSerial
98where
99 T: Clone + Num,
100 D: DimAPI,
101{
102}
103
104#[cfg(test)]
105mod tests {
106 use super::*;
107
108 #[test]
109 fn test_cpu_device_same_device() {
110 let device1 = DeviceCpuSerial::default();
111 let device2 = DeviceCpuSerial::default();
112 assert!(device1.same_device(&device2));
113 }
114
115 #[test]
116 fn test_cpu_storage_to_vec() {
117 let storage = Storage::new(DataOwned::from(vec![1, 2, 3]), DeviceCpuSerial::default());
118 let vec = storage.to_cpu_vec().unwrap();
119 assert_eq!(vec, vec![1, 2, 3]);
120 }
121
122 #[test]
123 fn test_cpu_storage_into_vec() {
124 let storage = Storage::new(DataOwned::from(vec![1, 2, 3]), DeviceCpuSerial::default());
125 let vec = storage.into_cpu_vec().unwrap();
126 assert_eq!(vec, vec![1, 2, 3]);
127 }
128}