Skip to content

Commit

Permalink
benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
lan496 committed Apr 1, 2024
1 parent babe280 commit fdb4dc4
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
6 changes: 6 additions & 0 deletions moyo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ criterion = { version = "0.4", features = ["html_reports"] }

[[bench]]
name = "translation_search"
path = "benches/translation_search.rs"
harness = false

[[bench]]
name = "dataset"
path = "benches/dataset.rs"
harness = false
23 changes: 23 additions & 0 deletions moyo/benches/dataset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use criterion::{criterion_group, criterion_main, Criterion};

use serde_json;
use std::fs;
use std::path::Path;

use moyo::base::{AngleTolerance, Cell};
use moyo::data::Setting;
use moyo::MoyoDataset;

pub fn benchmark(c: &mut Criterion) {
let path = Path::new("tests/assets/mp-1201492.json");
let cell: Cell = serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap();
let symprec = 1e-4;
let angle_tolerance = AngleTolerance::Default;
let setting = Setting::Standard;
c.bench_function("dataset_clathrate_Si", |b| {
b.iter(|| MoyoDataset::new(&cell, symprec, angle_tolerance, setting))
});
}

criterion_group!(benches, benchmark);
criterion_main!(benches);
84 changes: 84 additions & 0 deletions moyo/benches/translation_search.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};

use nalgebra::{matrix, vector};

use moyo::base::{Cell, Lattice, Position};
use moyo::search::{solve_correspondence, solve_correspondence_naive, PeriodicKdTree};

/// O(num_atoms^3)
fn naive(reduced_cell: &Cell) {
let num_atoms = reduced_cell.num_atoms();
let symprec = 1e-5;
for j in 0..num_atoms {
let translation = reduced_cell.positions[j] - reduced_cell.positions[0];
let new_positions: Vec<Position> = reduced_cell
.positions
.iter()
.map(|pos| pos + translation)
.collect();

solve_correspondence_naive(reduced_cell, &new_positions, symprec);
}
}

/// O(num_atoms^2 * log(num_atoms))
fn kdtree(reduced_cell: &Cell) {
let num_atoms = reduced_cell.num_atoms();
let symprec = 1e-5;
let pkdtree = PeriodicKdTree::new(reduced_cell, symprec);
for j in 0..num_atoms {
let translation = reduced_cell.positions[j] - reduced_cell.positions[0];
let new_positions: Vec<Position> = reduced_cell
.positions
.iter()
.map(|pos| pos + translation)
.collect();

solve_correspondence(&pkdtree, reduced_cell, &new_positions);
}
}

fn cell_for_benchmark(n: usize) -> Cell {
let mut positions = vec![];
let mut numbers = vec![];
for i in 0..n {
for j in 0..n {
for k in 0..n {
positions.push(vector![
i as f64 / n as f64,
j as f64 / n as f64,
k as f64 / n as f64
]);
numbers.push(0);
}
}
}

Cell::new(
Lattice::new(matrix![
n as f64, 0.0, 0.0;
0.0, n as f64, 0.0;
0.0, 0.0, n as f64;
]),
positions,
numbers,
)
}

pub fn benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("translation search");
for n in 1..=8 {
let cell = cell_for_benchmark(n);
group.throughput(Throughput::Elements(cell.num_atoms() as u64));
group.bench_with_input(BenchmarkId::new("naive", n), &cell, |b, cell| {
b.iter(|| naive(&cell));
});
group.bench_with_input(BenchmarkId::new("kdtree", n), &cell, |b, cell| {
b.iter(|| kdtree(&cell));
});
}
group.finish();
}

criterion_group!(benches, benchmark);
criterion_main!(benches);

0 comments on commit fdb4dc4

Please sign in to comment.