Skip to content

Commit 1759f8a

Browse files
committed
Update bench/test to use random number
1 parent cd0608d commit 1759f8a

File tree

3 files changed

+64
-10
lines changed

3 files changed

+64
-10
lines changed

intel-mkl-sys/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ edition = "2018"
88
path = "../intel-mkl-src"
99

1010
[dev-dependencies]
11-
criterion = "*"
11+
criterion = "0.3"
12+
rand = "0.7"
13+
approx = "0.3"
1214

1315
[[bench]]
1416
name = "cos"

intel-mkl-sys/benches/cos.rs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,58 @@
22
extern crate criterion;
33

44
use criterion::Criterion;
5+
use rand::distributions::{Distribution, Uniform};
56

67
fn criterion_benchmark(c: &mut Criterion) {
8+
// f32
79
for &n in &[100, 1000, 10000] {
8-
c.bench_function(&format!("cos{}", n), |b| {
9-
let in_ = vec![0.0_f64; n];
10-
let mut out = vec![0.0_f64; n];
10+
let in_ = {
11+
let mut rng = rand::thread_rng();
12+
let between = Uniform::from(0.0..2.0 * std::f32::consts::PI);
13+
let mut buf = vec![0.0; n];
14+
for val in buf.iter_mut() {
15+
*val = between.sample(&mut rng);
16+
}
17+
buf
18+
};
19+
20+
let mut out = vec![0.0_f32; n];
21+
c.bench_function(&format!("cos32_n{}", n), |b| {
22+
b.iter(|| {
23+
for i in 0..n {
24+
out[i] = in_[i].cos();
25+
}
26+
})
27+
});
28+
c.bench_function(&format!("vcos32_n{}", n), |b| {
29+
b.iter(|| unsafe {
30+
intel_mkl_sys::vsCos(n as i32, in_.as_ptr(), out.as_mut_ptr());
31+
})
32+
});
33+
}
34+
35+
// f64
36+
for &n in &[100, 1000, 10000] {
37+
let in_ = {
38+
let mut rng = rand::thread_rng();
39+
let between = Uniform::from(0.0..2.0 * std::f64::consts::PI);
40+
let mut buf = vec![0.0; n];
41+
for val in buf.iter_mut() {
42+
*val = between.sample(&mut rng);
43+
}
44+
buf
45+
};
46+
47+
let mut out = vec![0.0_f64; n];
48+
c.bench_function(&format!("cos64_n{}", n), |b| {
1149
b.iter(|| {
1250
for i in 0..n {
1351
out[i] = in_[i].cos();
1452
}
1553
})
1654
});
1755

18-
c.bench_function(&format!("vcos{}", n), |b| {
19-
let in_ = vec![0.0_f64; n];
20-
let mut out = vec![0.0_f64; n];
56+
c.bench_function(&format!("vcos64_n{}", n), |b| {
2157
b.iter(|| unsafe {
2258
intel_mkl_sys::vdCos(n as i32, in_.as_ptr(), out.as_mut_ptr());
2359
})

intel-mkl-sys/src/lib.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,30 @@ include!("mkl.rs");
2525
#[cfg(test)]
2626
mod tests {
2727
use super::*;
28+
use approx::ulps_eq;
29+
use rand::distributions::{Distribution, Uniform};
2830
use std::ffi::c_void;
2931

32+
fn gen_rand_array(n: usize) -> Vec<f64> {
33+
let mut rng = rand::thread_rng();
34+
let between = Uniform::from(0.0..2.0 * std::f64::consts::PI);
35+
let mut buf = vec![0.0; n];
36+
for val in buf.iter_mut() {
37+
*val = between.sample(&mut rng);
38+
}
39+
buf
40+
}
41+
3042
#[test]
3143
fn cos() {
32-
let a = vec![0.0_f64; 1024];
33-
let mut b = vec![0.0_f64; 1024];
44+
let n = 1024;
45+
let a = gen_rand_array(n);
46+
let mut b = vec![0.0_f64; n];
3447
unsafe {
35-
vdCos(1024_i32, a.as_ptr(), b.as_mut_ptr());
48+
vdCos(n as i32, a.as_ptr(), b.as_mut_ptr());
49+
}
50+
for i in 0..n {
51+
ulps_eq!(b[i], a[i].cos(), max_ulps = 4, epsilon = std::f64::EPSILON);
3652
}
3753
}
3854

0 commit comments

Comments
 (0)