Skip to content

Commit

Permalink
feat(benches): add a criterion bench for triangulation functions (#207)
Browse files Browse the repository at this point in the history
* add files for a new bench#

* add new example vtk file

* add benchmarks

* replace quad example file to have more cells

* Revert "replace quad example file to have more cells"

This reverts commit d65c70f.

* better preallocs
  • Loading branch information
imrn99 authored Oct 29, 2024
1 parent e529756 commit 4c16750
Show file tree
Hide file tree
Showing 4 changed files with 584 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Cargo.lock
*.vtk
!/assets/*.vtk
!/examples/shape.vtk
!/examples/quads.vtk

# ignore coverage-related files
*.profraw
Expand All @@ -36,4 +37,4 @@ perf.data.old
# ignore bench result directories
/fixed
/grid
/thread
/thread
5 changes: 5 additions & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,8 @@ harness = false
name = "grisubal_grid_size"
path = "benches/grisubal/grid_size.rs"
harness = false

[[bench]]
name = "triangulate_quads"
path = "benches/triangulate/quads.rs"
harness = false
93 changes: 93 additions & 0 deletions benches/benches/triangulate/quads.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// ------ IMPORTS

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use honeycomb::prelude::{
triangulation::{earclip_cell, fan_cell, TriangulateError},
CMap2, CMapBuilder, DartIdentifier, Orbit2, OrbitPolicy,
};
use honeycomb_benches::FloatType;

// ------ CONTENT

const PATH: &str = "../examples/quads.vtk";

fn fan_bench() -> Result<(), TriangulateError> {
let mut map: CMap2<FloatType> = CMapBuilder::default().vtk_file(PATH).build().unwrap();

// prealloc darts
let faces = map.fetch_faces().identifiers.clone();
let n_darts_per_face: Vec<_> = faces
.iter()
.map(|id| (Orbit2::new(&map, OrbitPolicy::Face, *id as DartIdentifier).count() - 3) * 2)
.collect();
let n_tot: usize = n_darts_per_face.iter().sum();
let tmp = map.add_free_darts(n_tot) as usize;
// the prefix sum gives an offset that corresponds to the starting index of each slice, minus
// the location of the allocated dart block (given by `tmp`)
// end of the slice is deduced using these values and the number of darts the current seg needs
let prefix_sum = n_darts_per_face.iter().scan(0, |state, &n_d| {
*state += n_d;
Some(*state - n_d) // we want an offset, not the actual sum
});
#[allow(clippy::cast_possible_truncation)]
let dart_slices: Vec<Vec<DartIdentifier>> = n_darts_per_face
.iter()
.zip(prefix_sum)
.map(|(n_d, start)| {
((tmp + start) as DartIdentifier..(tmp + start + n_d) as DartIdentifier)
.collect::<Vec<_>>()
})
.collect();

for (face_id, new_darts) in faces.iter().zip(dart_slices.iter()) {
fan_cell(&mut map, *face_id, new_darts)?
}

Ok(())
}

fn earclip_bench() -> Result<(), TriangulateError> {
let mut map: CMap2<FloatType> = CMapBuilder::default().vtk_file(PATH).build().unwrap();

// prealloc darts
let faces = map.fetch_faces().identifiers.clone();
let n_darts_per_face: Vec<_> = faces
.iter()
.map(|id| (Orbit2::new(&map, OrbitPolicy::Face, *id as DartIdentifier).count() - 3) * 2)
.collect();
let n_tot: usize = n_darts_per_face.iter().sum();
let tmp = map.add_free_darts(n_tot) as usize;
// the prefix sum gives an offset that corresponds to the starting index of each slice, minus
// the location of the allocated dart block (given by `tmp`)
// end of the slice is deduced using these values and the number of darts the current seg needs
let prefix_sum = n_darts_per_face.iter().scan(0, |state, &n_d| {
*state += n_d;
Some(*state - n_d) // we want an offset, not the actual sum
});
#[allow(clippy::cast_possible_truncation)]
let dart_slices: Vec<Vec<DartIdentifier>> = n_darts_per_face
.iter()
.zip(prefix_sum)
.map(|(n_d, start)| {
((tmp + start) as DartIdentifier..(tmp + start + n_d) as DartIdentifier)
.collect::<Vec<_>>()
})
.collect();

for (face_id, new_darts) in faces.iter().zip(dart_slices.iter()) {
earclip_cell(&mut map, *face_id, new_darts)?
}

Ok(())
}

pub fn criterion_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("triangulation");

group.bench_function("fan", |b| b.iter(|| black_box(fan_bench())));
group.bench_function("earclip", |b| b.iter(|| black_box(earclip_bench())));
group.finish();
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
Loading

0 comments on commit 4c16750

Please sign in to comment.