Struct ha_ndarray::Queue
source · pub struct Queue { /* private fields */ }
Expand description
A queue of array operations
Implementations§
source§impl Queue
impl Queue
sourcepub fn new(context: Context, _size_hint: usize) -> Result<Self, Error>
pub fn new(context: Context, _size_hint: usize) -> Result<Self, Error>
Construct a new host Queue
with the given Context
.
size_hint
will be ignored since the “opencl” feature flag is disabled.
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more