Trait ha_ndarray::NDArray

source ·
pub trait NDArray: Send + Sync {
    type DType: CDatatype;

    // Required methods
    fn context(&self) -> &Context;
    fn shape(&self) -> &[usize];

    // Provided methods
    fn ndim(&self) -> usize { ... }
    fn size(&self) -> usize { ... }
}

Required Associated Types§

Required Methods§

source

fn context(&self) -> &Context

source

fn shape(&self) -> &[usize]

Provided Methods§

source

fn ndim(&self) -> usize

source

fn size(&self) -> usize

Examples found in repository?
examples/benchmarks.rs (line 65)
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
fn matmul(context: &Context) -> Result<(), Error> {
    for m in 1..16usize {
        let dim = 2usize.pow(m as u32);

        let l = ArrayBase::<Vec<_>>::with_context(
            context.clone(),
            vec![16 * m, dim],
            vec![1.0f32; 16 * m * dim],
        )?;

        let r = ArrayBase::<Vec<_>>::with_context(
            context.clone(),
            vec![dim, m * 32],
            vec![1.0f32; dim * m * 32],
        )?;

        let num_ops = 16 * 32 * dim;
        println!("matmul {:?} with {:?} ({} ops)", l, r, num_ops);

        let x = l.matmul(r)?;
        let queue = Queue::new(context.clone(), x.size())?;

        for _ in 0..ITERATIONS {
            let start = Instant::now();
            let _output = x.read(&queue)?;
            let duration = start.elapsed();
            let rate = num_ops as f32 / duration.as_secs_f32();
            println!("{:?} us @ {} M/s", duration.as_micros(), rate / 1_000_000.);
        }
    }

    Ok(())
}

fn reduce_sum_axis(context: &Context) -> Result<(), Error> {
    let shape = vec![10, 20, 30, 40, 50];
    let size = shape.iter().product();
    let queue = Queue::new(context.clone(), size)?;
    let x = ArrayBase::<Vec<_>>::with_context(context.clone(), shape, vec![1; size])?;

    println!("reduce axis {} of {:?} (size {})", 2, x, x.size());

    let reduced = x.sum(vec![2], false)?;

    for _ in 0..ITERATIONS {
        let start = Instant::now();
        let _output = reduced.read(&queue)?;
        let duration = start.elapsed();
        println!("{:?} ms", duration.as_millis());
    }

    Ok(())
}

fn reduce_sum_all(context: &Context) -> Result<(), Error> {
    for m in 2..8 {
        let shape = (1..m).map(|dim| dim * 10).collect::<Vec<usize>>();
        let size = shape.iter().product();
        let x = ArrayBase::<Arc<Vec<_>>>::with_context(
            context.clone(),
            shape,
            Arc::new(vec![1; size]),
        )?;

        println!("reduce {:?} (size {})...", x, x.size());

        for _ in 0..ITERATIONS {
            let start = Instant::now();
            let _x = x.clone().sum_all()?;
            let duration = start.elapsed();
            println!("{:?} us", duration.as_micros());
        }
    }

    Ok(())
}

Implementations on Foreign Types§

source§

impl<A: NDArray + ?Sized> NDArray for Box<A>

§

type DType = <A as NDArray>::DType

source§

fn context(&self) -> &Context

source§

fn shape(&self) -> &[usize]

Implementors§

source§

impl<A: NDArray> NDArray for ArraySlice<A>

§

type DType = <A as NDArray>::DType

source§

impl<A: NDArray> NDArray for ArrayView<A>

§

type DType = <A as NDArray>::DType

source§

impl<Buf: BufferInstance> NDArray for ArrayBase<Buf>

§

type DType = <Buf as BufferInstance>::DType

source§

impl<Op: Op> NDArray for ArrayOp<Op>

§

type DType = <Op as Op>::Out

source§

impl<T: CDatatype> NDArray for Array<T>

§

type DType = T