Skip to content

Commit c08a4d1

Browse files
miguelrazworkingjubilee
authored andcommitted
add more basic dot products and comments, README
1 parent df3a639 commit c08a4d1

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

crates/core_simd/examples/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
### `stdsimd` examples
2+
3+
This crate is a port of example uses of `stdsimd`, mostly taken from the `packed_simd` crate.
4+
5+
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.
6+
7+
Run the tests with the command
8+
9+
```
10+
cargo run --example dot_product
11+
```
12+
13+
and the benchmarks via the command
14+
15+
```
16+
cargo run --example --benchmark ???
17+
```
18+
19+
and measure the timings on your local system.

crates/core_simd/examples/dot_product.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,27 @@
33
#![feature(array_chunks)]
44
use core_simd::*;
55

6-
pub fn dot_prod(a: &[f32], b: &[f32]) -> f32 {
6+
/// This is your barebones dot product implementation:
7+
/// Take 2 vectors, multiply them element wise and *then*
8+
/// add up the result. In the next example we will see if there
9+
/// is any difference to adding as we go along multiplying.
10+
pub fn dot_prod_0(a: &[f32], b: &[f32]) -> f32 {
11+
assert_eq!(a.len(), b.len());
12+
13+
a.iter()
14+
.zip(b.iter())
15+
.map(|a, b| a * b)
16+
.sum()
17+
}
18+
19+
pub fn dot_prod_1(a: &[f32], b: &[f32]) -> f32 {
20+
assert_eq!(a.len(), b.len());
21+
a.iter()
22+
.zip(b.iter())
23+
.fold(0.0, |a, b| a * b)
24+
}
25+
26+
pub fn dot_prod_simd_0(a: &[f32], b: &[f32]) -> f32 {
727
assert_eq!(a.len(), b.len());
828

929
// TODO handle remainder when a.len() % 4 != 0
@@ -21,11 +41,14 @@ fn main() {
2141
#[cfg(test)]
2242
mod tests {
2343
#[test]
24-
fn test() {
44+
fn smoke_test() {
2545
use super::*;
2646
let a: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
2747
let b: Vec<f32> = vec![-8.0, -7.0, -6.0, -5.0, 4.0, 3.0, 2.0, 1.0];
2848

29-
assert_eq!(0.0, dot_prod(&a, &b));
49+
assert_eq!(0.0, dot_prod_0(&a, &b));
50+
assert_eq!(0.0, dot_prod_1(&a, &b));
51+
assert_eq!(0.0, dot_prod_simd_0(&a, &b));
52+
assert_eq!(0.0, dot_prod_simd_1(&a, &b));
3053
}
3154
}

0 commit comments

Comments
 (0)