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
use crate::{
Prm,
HostBuffer,
Shape, Tensor, CommonTensor,
HostTensorIter, HostTensorIterMut,
};
type InnerTensor<T> = CommonTensor<T, HostBuffer<T>>;
pub struct HostTensor<T: Prm> {
inner: InnerTensor<T>,
}
impl<T: Prm> HostTensor<T> {
pub unsafe fn new_uninit(shape: &Shape) -> Self {
Self::new_uninit_in(&(), shape)
}
pub fn new_filled(shape: &Shape, value: T) -> Self {
Self::new_filled_in(&(), shape, value)
}
pub fn new_zeroed(shape: &Shape) -> Self {
Self::new_zeroed_in(&(), shape)
}
pub(crate) fn buffer(&self) -> &HostBuffer<T> {
self.inner.buffer()
}
pub(crate) fn buffer_mut(&mut self) -> &mut HostBuffer<T> {
self.inner.buffer_mut()
}
pub fn iter<'a>(&'a self) -> HostTensorIter<'a, T> {
HostTensorIter::new(self)
}
pub fn iter_mut<'a>(&'a mut self) -> HostTensorIterMut<'a, T> {
self.buffer_mut().as_mut_slice().iter_mut()
}
}
impl<T: Prm> Tensor<T> for HostTensor<T> {
type Buffer = HostBuffer<T>;
unsafe fn new_uninit_in(_: &(), shape: &Shape) -> Self {
Self { inner: InnerTensor::<T>::new_uninit_in(&(), shape) }
}
fn new_filled_in(_: &(), shape: &Shape, value: T) -> Self {
Self { inner: InnerTensor::<T>::new_filled_in(&(), shape, value) }
}
fn new_zeroed_in(_: &(), shape: &Shape) -> Self {
Self { inner: InnerTensor::<T>::new_zeroed_in(&(), shape) }
}
fn shape(&self) -> &Shape {
self.inner.shape()
}
fn reshape(&self, shape: &Shape) -> Self {
Self { inner: self.inner.reshape(shape) }
}
fn load(&self, dst: &mut [T]) {
self.inner.load(dst);
}
fn store(&mut self, src: &[T]) {
self.inner.store(src);
}
}