aboutsummaryrefslogtreecommitdiff
path: root/benches/benches/parallel.rs
blob: dab711ccc3c03ad2c74b49531a427f0145ca2855 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use criterion::{criterion_group, Criterion, ParameterizedBenchmark};

use plotters::coord::Shift;
use plotters::prelude::*;
use rayon::prelude::*;

const SIZES: &'static [u32] = &[100, 400, 800, 1000, 2000];

fn draw_plot(root: &DrawingArea<BitMapBackend, Shift>, pow: f64) {
    let mut chart = ChartBuilder::on(root)
        .caption(format!("y = x^{}", pow), ("Arial", 30))
        .build_cartesian_2d(-1.0..1.0, -1.0..1.0)
        .unwrap();

    chart.configure_mesh().draw().unwrap();

    chart
        .draw_series(LineSeries::new(
            (-50..=50)
                .map(|x| x as f64 / 50.0)
                .map(|x| (x, x.powf(pow))),
            &RED,
        ))
        .unwrap()
        .label(format!("y = x^{}", pow))
        .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &RED));
    chart
        .configure_series_labels()
        .background_style(&WHITE.mix(0.8))
        .border_style(&BLACK)
        .draw()
        .unwrap();
}

fn draw_func_1x1_seq(c: &mut Criterion) {
    c.bench(
        "draw_func_1x1",
        ParameterizedBenchmark::new(
            "sequential",
            |b, &&s| {
                let mut buffer = vec![0; (s * s * 3) as usize];
                b.iter(|| {
                    let root = BitMapBackend::with_buffer(&mut buffer, (s, s)).into_drawing_area();
                    root.fill(&WHITE).unwrap();
                    draw_plot(&root, 2.0);
                })
            },
            SIZES.clone(),
        ),
    );
}

fn draw_func_4x4(c: &mut Criterion) {
    c.bench(
        "draw_func_4x4",
        ParameterizedBenchmark::new(
            "sequential",
            |b, &&s| {
                let mut buffer = vec![0; (s * s * 3) as usize];
                b.iter(|| {
                    let root = BitMapBackend::with_buffer(&mut buffer, (s, s)).into_drawing_area();
                    let areas = root.split_evenly((4, 4));
                    areas.iter().for_each(|area| draw_plot(&area, 2.0));
                })
            },
            SIZES.clone(),
        )
        .with_function("blit", |b, &&s| {
            let mut buffer = vec![0; (s * s * 3) as usize];
            let mut element_buffer = vec![vec![0; (s * s / 4 * 3) as usize]; 4];
            b.iter(|| {
                let root = BitMapBackend::with_buffer(&mut buffer, (s, s)).into_drawing_area();
                let areas = root.split_evenly((4, 4));
                let elements: Vec<_> = element_buffer
                    .par_iter_mut()
                    .map(|b| {
                        let mut e = BitMapElement::with_mut((0, 0), (s / 2, s / 2), b).unwrap();
                        draw_plot(&e.as_bitmap_backend().into_drawing_area(), 2.0);
                        e
                    })
                    .collect();

                areas
                    .into_iter()
                    .zip(elements.into_iter())
                    .for_each(|(a, e)| a.draw(&e).unwrap());
            })
        })
        .with_function("inplace-blit", |b, &&s| {
            let mut buffer = vec![0; (s * s * 3) as usize];
            let mut element_buffer = vec![vec![vec![0; (s * s / 4 * 3) as usize]; 2]; 2];
            b.iter(|| {
                let mut back = BitMapBackend::with_buffer(&mut buffer, (s, s));
                back.split(&[s / 2])
                    .into_iter()
                    .zip(element_buffer.iter_mut())
                    .collect::<Vec<_>>()
                    .into_par_iter()
                    .for_each(|(back, buffer)| {
                        let root = back.into_drawing_area();
                        let areas = root.split_evenly((1, 2));

                        let elements: Vec<_> = buffer
                            .par_iter_mut()
                            .map(|b| {
                                let mut e =
                                    BitMapElement::with_mut((0, 0), (s / 2, s / 2), b).unwrap();
                                draw_plot(&e.as_bitmap_backend().into_drawing_area(), 2.0);
                                e
                            })
                            .collect();

                        areas
                            .into_iter()
                            .zip(elements.into_iter())
                            .for_each(|(a, e)| a.draw(&e).unwrap())
                    });
            })
        }),
    );
}

fn draw_func_2x1(c: &mut Criterion) {
    c.bench(
        "draw_func_2x1",
        ParameterizedBenchmark::new(
            "blit",
            |b, &&s| {
                let mut buffer = vec![0; (s * s * 3) as usize];
                let mut element_buffer = vec![vec![0; (s * s / 2 * 3) as usize]; 2];
                b.iter(|| {
                    let root = BitMapBackend::with_buffer(&mut buffer, (s, s)).into_drawing_area();
                    let areas = root.split_evenly((2, 1));
                    let elements: Vec<_> = element_buffer
                        .par_iter_mut()
                        .map(|buf| {
                            let mut element =
                                BitMapElement::with_mut((0, 0), (s, s / 2), buf).unwrap();
                            draw_plot(&element.as_bitmap_backend().into_drawing_area(), 2.0);
                            element
                        })
                        .collect();

                    areas
                        .into_iter()
                        .zip(elements.into_iter())
                        .for_each(|(a, e)| a.draw(&e).unwrap());
                })
            },
            SIZES.clone(),
        )
        .with_function("inplace", |b, &&s| {
            let mut buffer = vec![0; (s * s * 3) as usize];
            b.iter(|| {
                let mut back = BitMapBackend::with_buffer(&mut buffer, (s, s));
                back.split(&[s / 2])
                    .into_par_iter()
                    .for_each(|b| draw_plot(&b.into_drawing_area(), 2.0));
            })
        })
        .with_function("sequential", |b, &&s| {
            let mut buffer = vec![0; (s * s * 3) as usize];
            b.iter(|| {
                let root = BitMapBackend::with_buffer(&mut buffer, (s, s)).into_drawing_area();
                root.split_evenly((2, 1))
                    .iter_mut()
                    .for_each(|area| draw_plot(area, 2.0));
            })
        }),
    );
}

criterion_group! {
    name = parallel_group;
    config = Criterion::default().sample_size(10);
    targets =
        draw_func_1x1_seq,
        draw_func_4x4,
        draw_func_2x1,
}