Struct ha_ndarray::Queue

source ·
pub struct Queue { /* private fields */ }

Implementations§

source§

impl Queue

source

pub fn new(context: Context, _size_hint: usize) -> Result<Self, Error>

Examples found in repository?
examples/benchmarks.rs (line 13)
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
fn broadcast_and_multiply(context: &Context) -> Result<(), Error> {
    for m in 0..4 {
        let dim = 10usize.pow(m);
        let shape = vec![3, dim, 5, 10];
        let size = shape.iter().product::<usize>();
        let queue = Queue::new(context.clone(), size)?;

        let left = ArrayBase::<Vec<_>>::with_context(
            context.clone(),
            vec![dim, 5, 10],
            vec![1.0f64; dim * 5 * 10],
        )?;

        let right = ArrayBase::<Vec<_>>::with_context(
            context.clone(),
            vec![3, dim, 1, 10],
            vec![1.0f64; 3 * dim * 10],
        )?;

        println!(
            "broadcast and multiply {:?} and {:?} (size {})...",
            left, right, size
        );

        let product = left.broadcast(shape.to_vec())? * right.broadcast(shape)?;

        for _ in 0..ITERATIONS {
            let start = Instant::now();
            product.read(&queue)?;
            let duration = start.elapsed();
            println!("{:?} us", duration.as_micros());
        }
    }

    Ok(())
}

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(())
}

fn transpose(context: &Context) -> Result<(), Error> {
    let shape = vec![10, 20, 30, 40, 50];
    let size = shape.iter().product();
    let permutation = vec![2, 4, 3, 0, 1];

    let queue = Queue::new(context.clone(), size)?;
    let x = ArrayBase::<Vec<_>>::with_context(context.clone(), shape, vec![1; size])?;

    println!("transpose axes {permutation:?} of {x:?}...");

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

    Ok(())
}

Trait Implementations§

source§

impl Clone for Queue

source§

fn clone(&self) -> Queue

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Queue

§

impl Send for Queue

§

impl Sync for Queue

§

impl Unpin for Queue

§

impl UnwindSafe for Queue

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V