add more basic dot products and comments, README

This commit is contained in:
Miguel Raz Guzmán Macedo 2022-03-26 14:04:37 -06:00 committed by The Atelier
parent df3a63906c
commit c08a4d1f10
2 changed files with 45 additions and 3 deletions

View file

@ -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.

View file

@ -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));
}
}