-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reduce_black_box_sum.rs
112 lines (94 loc) · 3.18 KB
/
reduce_black_box_sum.rs
1
2
3
4
5
6
7
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
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use orx_parallel::*;
use rand::prelude::*;
use rand_chacha::ChaCha8Rng;
use rayon::iter::IntoParallelIterator;
const SEED: u64 = 846356;
fn inputs(len: usize) -> Vec<usize> {
let mut rng = ChaCha8Rng::seed_from_u64(SEED);
(0..len).map(|_| rng.gen_range(0..15791)).collect()
}
fn red(a: usize, b: usize) -> usize {
black_box(black_box(a) + black_box(b))
}
fn seq(inputs: &[usize]) -> Option<usize> {
inputs.iter().copied().reduce(red)
}
fn rayon_reduce(inputs: &[usize]) -> Option<usize> {
use rayon::iter::ParallelIterator;
Some(inputs.into_par_iter().copied().reduce(|| 0, red))
}
fn rayon_reduce_with(inputs: &[usize]) -> Option<usize> {
use rayon::iter::ParallelIterator;
inputs.into_par_iter().copied().reduce_with(red)
}
fn orx_parallel(inputs: &[usize], num_threads: usize, chunk_size: usize) -> Option<usize> {
inputs
.iter()
.cloned()
.par()
.num_threads(num_threads)
.chunk_size(chunk_size)
.reduce(red)
}
fn orx_parallel_default(inputs: &[usize]) -> Option<usize> {
inputs.iter().cloned().par().reduce(red)
}
fn reduce_black_box_sum(c: &mut Criterion) {
let lengths = [262_144 * 16];
let params = [(1, 1), (8, 1024), (16, 1024), (32, 1024)];
let mut group = c.benchmark_group("reduce_black_box_sum");
for len in lengths {
let input = inputs(len);
let name = format!("n{}", len);
let expected = seq(&input);
group.bench_with_input(BenchmarkId::new("seq", name.clone()), &name, |b, _| {
b.iter(|| {
let result = seq(black_box(&input));
assert_eq!(result, expected);
})
});
group.bench_with_input(
BenchmarkId::new("rayon_reduce", name.clone()),
&name,
|b, _| {
b.iter(|| {
let result = rayon_reduce(black_box(&input));
assert_eq!(result, expected);
})
},
);
group.bench_with_input(
BenchmarkId::new("rayon_reduce_with", name.clone()),
&name,
|b, _| {
b.iter(|| {
let result = rayon_reduce_with(black_box(&input));
assert_eq!(result, expected);
})
},
);
group.bench_with_input(
BenchmarkId::new("orx-parallel-default", name.clone()),
&name,
|b, _| {
b.iter(|| {
let result = orx_parallel_default(black_box(&input));
assert_eq!(result, expected);
})
},
);
for (t, c) in params {
let params = format!("orx-parallel-t{}-c{}", t, c);
group.bench_with_input(BenchmarkId::new(params, name.clone()), &name, |b, _| {
b.iter(|| {
let result = orx_parallel(black_box(&input), t, c);
assert_eq!(result, expected);
})
});
}
}
group.finish();
}
criterion_group!(benches, reduce_black_box_sum);
criterion_main!(benches);