-
Notifications
You must be signed in to change notification settings - Fork 0
/
addition.rs
99 lines (96 loc) · 3.75 KB
/
addition.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
use async_std::task::block_on;
use criterion::{
criterion_group, criterion_main, AxisScale, BatchSize, BenchmarkId, Criterion,
PlotConfiguration, Throughput,
};
use std::time::{Duration, Instant};
use wgpu_heterogeneous_compute_benchmark::{
addition, addition_iterator, addition_rayon, addition_unchecked, create_device, GPUAddition,
UploadStyle,
};
pub fn criterion_benchmark(c: &mut Criterion) {
env_logger::init();
let (device, queue) = create_device();
let mut group = c.benchmark_group("addition");
group.sample_size(10);
group.measurement_time(Duration::from_secs(5));
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
for size in &[1_000, 10_000, 100_000, 1_000_000, 10_000_000, 50_000_000] {
let data = vec![1.0_f32; *size];
group.throughput(Throughput::Elements(*size as u64));
group.bench_with_input(BenchmarkId::new("scalar safe", size), size, |b, &_size| {
b.iter_batched(
|| (data.clone(), data.clone()),
|(mut left, right)| addition(&mut left, &right),
BatchSize::LargeInput,
);
});
group.bench_with_input(
BenchmarkId::new("scalar unsafe", size),
size,
|b, &_size| {
b.iter_batched(
|| (data.clone(), data.clone()),
|(mut left, right)| unsafe { addition_unchecked(&mut left, &right) },
BatchSize::LargeInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("scalar iterator", size),
size,
|b, &_size| {
b.iter_batched(
|| (data.clone(), data.clone()),
|(mut left, right)| addition_iterator(&mut left, &right),
BatchSize::LargeInput,
);
},
);
group.bench_with_input(BenchmarkId::new("scalar rayon", size), size, |b, &_size| {
b.iter_batched(
|| (data.clone(), data.clone()),
|(mut left, right)| addition_rayon(&mut left, &right),
BatchSize::LargeInput,
);
});
group.bench_with_input(BenchmarkId::new("gpu mapping", size), size, |b, &_size| {
b.iter_custom(|iterations| {
let mut duration = Duration::default();
for _ in 0..iterations {
let mut gpu = block_on(GPUAddition::new(
&device,
&queue,
UploadStyle::Mapping,
*size,
));
block_on(gpu.set_buffers(&data, &data));
let start = Instant::now();
let _mapping = block_on(gpu.run(*size));
duration += start.elapsed();
}
duration
});
});
group.bench_with_input(BenchmarkId::new("gpu staging", size), size, |b, &_size| {
b.iter_custom(|iterations| {
let mut duration = Duration::default();
for _ in 0..iterations {
let mut gpu = block_on(GPUAddition::new(
&device,
&queue,
UploadStyle::Staging,
*size,
));
block_on(gpu.set_buffers(&data, &data));
let start = Instant::now();
let _mapping = block_on(gpu.run(*size));
duration += start.elapsed();
}
duration
});
});
}
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);