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
use crate::{
    Prm, Interop,
    DeviceBuffer, DeviceContext,
    Shape, Tensor, CommonTensor,
};

type InnerTensor<T> = CommonTensor<T, DeviceBuffer<T>>;

/// Tensor structure.
/// It consists of a contiguous one-dimensional array and a shape.
/// Tensor tries to reuse resources as long as possible and implements copy-on-write mechanism.
pub struct DeviceTensor<T: Prm + Interop> {
    inner: InnerTensor<T>,
}

impl<T: Prm + Interop> DeviceTensor<T> {}

impl<T: Prm + Interop> Tensor<T> for DeviceTensor<T> {
    type Buffer = DeviceBuffer<T>;

    unsafe fn new_uninit_in(context: &DeviceContext, shape: &Shape) -> Self {
        Self { inner: InnerTensor::<T>::new_uninit_in(context, shape) }
    }
    fn new_filled_in(context: &DeviceContext, shape: &Shape, value: T) -> Self {
        Self { inner: InnerTensor::<T>::new_filled_in(context, shape, value) }
    }
    fn new_zeroed_in(context: &DeviceContext, shape: &Shape) -> Self {
        Self { inner: InnerTensor::<T>::new_zeroed_in(context, 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);
    }
}