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
use crate::{PtrType, Device};
pub unsafe trait Shape {
const LEN: usize = 0;
type ARR<T>;
fn new<T: Copy + Default>() -> Self::ARR<T>;
}
unsafe impl Shape for () {
type ARR<T> = ();
fn new<T>() -> Self::ARR<T> {
()
}
}
pub trait IsShapeIndep: Device {}
#[cfg(not(feature="no-std"))]
impl<D: crate::RawConv> IsShapeIndep for D {}
pub trait IsConstDim: Shape {}
#[derive(Clone, Copy)]
pub struct Dim1<const N: usize>;
impl<const N: usize> IsConstDim for Dim1<N> {}
unsafe impl<const N: usize> Shape for Dim1<N> {
const LEN: usize = N;
type ARR<T> = [T; N];
#[inline]
fn new<T: Copy + Default>() -> Self::ARR<T> {
[T::default(); N]
}
}
#[derive(Clone, Copy)]
pub struct Dim2<const B: usize, const A: usize>;
impl<const B: usize, const A: usize> IsConstDim for Dim2<B, A> {}
unsafe impl<const B: usize, const A: usize> Shape for Dim2<B, A> {
const LEN: usize = B * A;
type ARR<T> = [[T; A]; B];
#[inline]
fn new<T: Copy + Default>() -> Self::ARR<T> {
[[T::default(); A]; B]
}
}
pub trait MayDim2<const A: usize, const B: usize>: Shape {}
impl<const A: usize, const B: usize> MayDim2<A, B> for () {}
impl<const A: usize, const B: usize> MayDim2<A, B> for Dim2<A, B> {}
#[derive(Clone, Copy)]
pub struct Dim3<const C: usize, const B: usize, const A: usize>;
impl<const C: usize, const B: usize, const A: usize> IsConstDim for Dim3<C, B, A> {}
unsafe impl<const C: usize, const B: usize, const A: usize> Shape for Dim3<C, B, A> {
const LEN: usize = B * A * C;
type ARR<T> = [[[T; A]; B]; C];
#[inline]
fn new<T: Copy + Default>() -> Self::ARR<T> {
[[[T::default(); A]; B]; C]
}
}
pub trait ToDim<T, I: Shape, O: Shape>: crate::Device {
fn to_dim(&self, ptr: Self::Ptr<T, I>) -> Self::Ptr<T, O>;
}
#[cfg(not(feature="no-std"))]
impl<T, D: crate::RawConv, I: Shape, O: Shape> ToDim<T, I, O> for D
where
Self::Ptr<T, ()>: crate::PtrType,
{
#[inline]
fn to_dim(&self, ptr: Self::Ptr<T, I>) -> D::Ptr<T, O> {
let ptr = core::mem::ManuallyDrop::new(ptr);
let raw_ptr = D::construct(&ptr, ptr.len(), Default::default());
let (ptr, _) = D::destruct(&raw_ptr, ptr.flag());
core::mem::forget(raw_ptr);
ptr
}
}
#[cfg(feature="stack")]
impl<T, S: IsConstDim> ToDim<T, S, S> for crate::Stack {
#[inline]
fn to_dim(&self, ptr: Self::Ptr<T, S>) -> Self::Ptr<T, S> {
ptr
}
}
#[cfg(test)]
mod tests {
use core::mem::size_of;
use crate::{Dim1, Dim2, Dim3, Device, Buffer, Shape};
#[cfg(not(feature="no-std"))]
fn len_of_shape<T, D: Device, S: Shape>(_: &Buffer<T, D, S>) {
println!("S::LEN {}", S::LEN);
}
#[test]
fn test_size_of_dims() {
assert_eq!(0, size_of::<Dim1<20>>());
assert_eq!(0, size_of::<Dim2<20, 10>>());
assert_eq!(0, size_of::<Dim3<4134, 20, 10>>());
assert_eq!(0, size_of::<()>());
}
#[cfg(feature="cpu")]
#[test]
fn test_transmute_of_stackless_buf() {
use crate::{CPU, Buffer};
let device = CPU::new();
let buf = Buffer::<f32, CPU, Dim2<5, 5>>::new(&device, 10);
let other_buf = unsafe {
&*(&buf as *const Buffer<f32, CPU, Dim2<5, 5,>> as *const Buffer<f32, CPU, ()>)
};
println!("other_buf: {:?}", other_buf.read());
len_of_shape(&other_buf);
println!("other_buf: {:?}", other_buf.read());
len_of_shape(&other_buf);
}
}