-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathfork_join.rs
More file actions
146 lines (117 loc) · 3.79 KB
/
fork_join.rs
File metadata and controls
146 lines (117 loc) · 3.79 KB
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
//! A benchmark for fork-join workloads adapted from `chili`.
use chili::Scope;
use divan::Bencher;
use forte::Worker;
use tracing::info;
use tracing_subscriber::fmt;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
// -----------------------------------------------------------------------------
// Workload
/// A node in a binary tree.
struct Node {
val: u64,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
impl Node {
// Constructs a new binary tree with the given number of layers.
pub fn tree(layers: usize) -> Self {
Self {
val: 1,
left: (layers != 1).then(|| Box::new(Self::tree(layers - 1))),
right: (layers != 1).then(|| Box::new(Self::tree(layers - 1))),
}
}
}
// Returns an iterator over the number of layers. Also returns the total number
// of nodes.
const LAYERS: &[usize] = &[
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, // 10, 24, 27,
];
fn nodes() -> impl Iterator<Item = (usize, usize)> {
LAYERS.iter().map(|&l| (l, (1 << l) - 1))
}
// -----------------------------------------------------------------------------
// Benchmark
#[divan::bench(args = nodes())]
fn baseline(bencher: Bencher, nodes: (usize, usize)) {
fn join_no_overhead<A, B, RA, RB>(a: A, b: B) -> (RA, RB)
where
A: FnOnce() -> RA + Send,
B: FnOnce() -> RB + Send,
RA: Send,
RB: Send,
{
(a(), b())
}
#[inline]
fn sum(node: &Node) -> u64 {
let (left, right) = join_no_overhead(
|| node.left.as_deref().map(sum).unwrap_or_default(),
|| node.right.as_deref().map(sum).unwrap_or_default(),
);
node.val + left + right
}
let tree = Node::tree(nodes.0);
bencher.bench_local(move || {
assert_eq!(sum(&tree), nodes.1 as u64);
});
}
static COMPUTE: forte::ThreadPool = forte::ThreadPool::new();
#[divan::bench(args = nodes())]
fn forte(bencher: Bencher, nodes: (usize, usize)) {
fn sum(node: &Node, worker: &Worker) -> u64 {
let (left, right) = worker.join(
|w| node.left.as_deref().map(|n| sum(n, w)).unwrap_or_default(),
|w| node.right.as_deref().map(|n| sum(n, w)).unwrap_or_default(),
);
node.val + left + right
}
let tree = Node::tree(nodes.0);
COMPUTE.with_worker(|worker| {
info!("Staring Benchmark");
bencher.bench_local(move || {
assert_eq!(sum(&tree, worker), nodes.1 as u64);
});
});
}
#[divan::bench(args = nodes())]
fn chili(bencher: Bencher, nodes: (usize, usize)) {
fn sum(node: &Node, scope: &mut Scope<'_>) -> u64 {
let (left, right) = scope.join(
|s| node.left.as_deref().map(|n| sum(n, s)).unwrap_or_default(),
|s| node.right.as_deref().map(|n| sum(n, s)).unwrap_or_default(),
);
node.val + left + right
}
let tree = Node::tree(nodes.0);
let mut scope = Scope::global();
bencher.bench_local(move || {
assert_eq!(sum(&tree, &mut scope), nodes.1 as u64);
});
}
#[divan::bench(args = nodes())]
fn rayon(bencher: Bencher, nodes: (usize, usize)) {
fn sum(node: &Node) -> u64 {
let (left, right) = rayon::join(
|| node.left.as_deref().map(sum).unwrap_or_default(),
|| node.right.as_deref().map(sum).unwrap_or_default(),
);
node.val + left + right
}
let tree = Node::tree(nodes.0);
bencher.bench_local(move || {
assert_eq!(sum(&tree), nodes.1 as u64);
});
}
fn main() {
let fmt_layer = fmt::layer()
.without_time()
.with_target(false)
.with_thread_names(true)
.compact();
tracing_subscriber::registry().with(fmt_layer).init();
COMPUTE.resize_to_available();
divan::main();
}