diff options
| author | Miguel Raz Guzmán Macedo <miguelraz@gmail.com> | 2022-03-26 14:04:37 -0600 |
|---|---|---|
| committer | The Atelier <workingjubilee@gmail.com> | 2022-12-03 16:12:00 -0800 |
| commit | c08a4d1f10473bfbdddf3d2eefc40e1194a633a7 (patch) | |
| tree | 79c815aa14cfb5cd6ad290b5ca963c1f13fc2d73 | |
| parent | df3a63906c44b23de7065d60c20bf99e2571ccc8 (diff) | |
| download | rust-c08a4d1f10473bfbdddf3d2eefc40e1194a633a7.tar.gz rust-c08a4d1f10473bfbdddf3d2eefc40e1194a633a7.zip | |
add more basic dot products and comments, README
| -rw-r--r-- | crates/core_simd/examples/README.md | 19 | ||||
| -rw-r--r-- | crates/core_simd/examples/dot_product.rs | 29 |
2 files changed, 45 insertions, 3 deletions
diff --git a/crates/core_simd/examples/README.md b/crates/core_simd/examples/README.md new file mode 100644 index 00000000000..b37dffa8eaa --- /dev/null +++ b/crates/core_simd/examples/README.md @@ -0,0 +1,19 @@ +### `stdsimd` examples + +This crate is a port of example uses of `stdsimd`, mostly taken from the `packed_simd` crate. + +The examples contain, as in the case of `dot_product.rs`, multiple ways of solving the problem, in order to show idiomatic uses of SIMD and iteration of performance designs. + +Run the tests with the command + +``` +cargo run --example dot_product +``` + +and the benchmarks via the command + +``` +cargo run --example --benchmark ??? +``` + +and measure the timings on your local system. diff --git a/crates/core_simd/examples/dot_product.rs b/crates/core_simd/examples/dot_product.rs index 812b0b23eeb..3e415fc4471 100644 --- a/crates/core_simd/examples/dot_product.rs +++ b/crates/core_simd/examples/dot_product.rs @@ -3,7 +3,27 @@ #![feature(array_chunks)] use core_simd::*; -pub fn dot_prod(a: &[f32], b: &[f32]) -> f32 { +/// This is your barebones dot product implementation: +/// Take 2 vectors, multiply them element wise and *then* +/// add up the result. In the next example we will see if there +/// is any difference to adding as we go along multiplying. +pub fn dot_prod_0(a: &[f32], b: &[f32]) -> f32 { + assert_eq!(a.len(), b.len()); + + a.iter() + .zip(b.iter()) + .map(|a, b| a * b) + .sum() +} + +pub fn dot_prod_1(a: &[f32], b: &[f32]) -> f32 { + assert_eq!(a.len(), b.len()); + a.iter() + .zip(b.iter()) + .fold(0.0, |a, b| a * b) +} + +pub fn dot_prod_simd_0(a: &[f32], b: &[f32]) -> f32 { assert_eq!(a.len(), b.len()); // TODO handle remainder when a.len() % 4 != 0 @@ -21,11 +41,14 @@ fn main() { #[cfg(test)] mod tests { #[test] - fn test() { + fn smoke_test() { use super::*; let a: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]; let b: Vec<f32> = vec![-8.0, -7.0, -6.0, -5.0, 4.0, 3.0, 2.0, 1.0]; - assert_eq!(0.0, dot_prod(&a, &b)); + assert_eq!(0.0, dot_prod_0(&a, &b)); + assert_eq!(0.0, dot_prod_1(&a, &b)); + assert_eq!(0.0, dot_prod_simd_0(&a, &b)); + assert_eq!(0.0, dot_prod_simd_1(&a, &b)); } } |
