-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nested_reduction.rs
124 lines (106 loc) · 3.17 KB
/
nested_reduction.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
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use orx_parallel::*;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
const LEN_1: usize = 512;
const LEN_2: usize = 512;
const LEN_3: usize = 16;
fn seq() -> usize {
(0..LEN_1)
.map(|x| {
(0..LEN_2)
.map(|y| (0..LEN_3).map(|z| black_box(x * y * z)).sum::<usize>())
.sum::<usize>()
})
.sum::<usize>()
}
fn rayon_1() -> usize {
(0..LEN_1)
.into_par_iter()
.map(|x| {
(0..LEN_2)
.map(|y| (0..LEN_3).map(|z| black_box(x * y * z)).sum::<usize>())
.sum::<usize>()
})
.sum::<usize>()
}
fn rayon_12() -> usize {
(0..LEN_1)
.into_par_iter()
.map(|x| {
(0..LEN_2)
.into_par_iter()
.map(|y| (0..LEN_3).map(|z| black_box(x * y * z)).sum::<usize>())
.sum::<usize>()
})
.sum::<usize>()
}
fn orx_parallel_1(num_threads: usize, chunk_size: usize) -> usize {
(0..LEN_1)
.par()
.num_threads(num_threads)
.chunk_size(chunk_size)
.map(|x| {
(0..LEN_2)
.map(|y| (0..LEN_3).map(|z| black_box(x * y * z)).sum::<usize>())
.sum::<usize>()
})
.sum()
}
fn orx_parallel_defaults_1() -> usize {
(0..LEN_1)
.par()
.map(|x| {
(0..LEN_2)
.map(|y| (0..LEN_3).map(|z| black_box(x * y * z)).sum::<usize>())
.sum::<usize>()
})
.sum()
}
fn orx_parallel_defaults_12() -> usize {
(0..LEN_1)
.par()
.map(|x| {
(0..LEN_2)
.par()
.map(|y| (0..LEN_3).map(|z| black_box(x * y * z)).sum::<usize>())
.sum()
})
.sum()
}
fn orx_parallel_12(num_threads: usize, chunk_size: usize) -> usize {
(0..LEN_1)
.par()
.num_threads(num_threads)
.chunk_size(chunk_size)
.map(|x| {
(0..LEN_2)
.par()
.num_threads(num_threads)
.chunk_size(chunk_size)
.map(|y| (0..LEN_3).map(|z| black_box(x * y * z)).sum())
.sum()
})
.sum()
}
fn nested_reduction(c: &mut Criterion) {
let params = [(1, 1), (8, 32), (8, 64)];
let mut group = c.benchmark_group("nested_reduction");
group.bench_function("seq", |b| b.iter(seq));
group.bench_function("rayon_1", |b| b.iter(rayon_1));
group.bench_function("rayon_12", |b| b.iter(rayon_12));
group.bench_function("orx-parallel-defaults-1", |b| {
b.iter(orx_parallel_defaults_1)
});
group.bench_function("orx-parallel-defaults-12", |b| {
b.iter(orx_parallel_defaults_12)
});
for (t, c) in params {
let name = format!("orx-parallel-1-t{}-c{}", t, c);
group.bench_function(name, |b| b.iter(|| orx_parallel_1(t, c)));
let name = format!("orx-parallel-12-t{}-c{}", t, c);
group.bench_function(name, |b| b.iter(|| orx_parallel_12(t, c)));
}
group.finish();
}
criterion_group!(benches, nested_reduction);
criterion_main!(benches);