-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_reduce_avg.rs
158 lines (136 loc) · 4.26 KB
/
map_reduce_avg.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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 = 54487;
fn inputs(len: usize) -> Vec<usize> {
let mut rng = ChaCha8Rng::seed_from_u64(SEED);
(0..len).map(|_| rng.gen_range(0..15791)).collect()
}
fn map(a: usize) -> (usize, usize) {
(a, 1)
}
fn red(a: (usize, usize), b: (usize, usize)) -> (usize, usize) {
(a.0 + b.0, a.1 + b.1)
}
fn final_red(agg: (usize, usize)) -> usize {
agg.0 / agg.1
}
fn seq(inputs: &[usize]) -> Option<usize> {
inputs.iter().copied().map(map).reduce(red).map(final_red)
}
#[allow(clippy::unnecessary_map_on_constructor)]
fn rayon_reduce(inputs: &[usize]) -> Option<usize> {
use rayon::iter::ParallelIterator;
Some(
inputs
.into_par_iter()
.copied()
.map(map)
.reduce(|| (0, 0), red),
)
.map(final_red)
}
fn rayon_reduce_with(inputs: &[usize]) -> Option<usize> {
use rayon::iter::ParallelIterator;
inputs
.into_par_iter()
.copied()
.map(map)
.reduce_with(red)
.map(final_red)
}
fn orx_parallel_reduce(inputs: &[usize], num_threads: usize, chunk_size: usize) -> Option<usize> {
inputs
.iter()
.cloned()
.par()
.num_threads(num_threads)
.chunk_size(chunk_size)
.map(map)
.reduce(red)
.map(final_red)
}
fn orx_parallel_sum(inputs: &[usize], num_threads: usize, chunk_size: usize) -> Option<usize> {
Some(
inputs
.iter()
.cloned()
.par()
.num_threads(num_threads)
.chunk_size(chunk_size)
.sum()
/ inputs.len(),
)
}
fn orx_parallel_default(inputs: &[usize]) -> Option<usize> {
Some(inputs.iter().cloned().par().sum() / inputs.len())
}
fn map_reduce_avg(c: &mut Criterion) {
let lengths = [262_144 * 16];
let params = [(1, 1), (4, 256), (8, 512), (8, 1024)];
let mut group = c.benchmark_group("map_reduce_avg");
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);
})
},
);
let t = 8;
let c = 1024;
let par_str = format!("orx-parallel-reduce-t{}-c{}", t, c);
group.bench_with_input(BenchmarkId::new(par_str, name.clone()), &name, |b, _| {
b.iter(|| {
let result = orx_parallel_reduce(black_box(&input), t, c);
assert_eq!(result, expected);
})
});
for (t, c) in params {
let params = format!("orx-parallel-sum-t{}-c{}", t, c);
group.bench_with_input(BenchmarkId::new(params, name.clone()), &name, |b, _| {
b.iter(|| {
let result = orx_parallel_sum(black_box(&input), t, c);
assert_eq!(result, expected);
})
});
}
}
group.finish();
}
criterion_group!(benches, map_reduce_avg);
criterion_main!(benches);