Skip to content

Add benchmarks #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ jobs:

- name: Rust Test
run: cargo test --all-features

- name: Rust Bench
run: cargo bench --all-features
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ repository = "https://github.com/Rust-Scientific-Computing/feotensor"
[dependencies]
itertools = "0.13.0"
num = "0.4.3"

[dev-dependencies]
criterion = "0.5"

[[bench]]
name = "tensor_bench"
harness = false
80 changes: 80 additions & 0 deletions benches/tensor_bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use criterion::{criterion_group, criterion_main, Criterion};
use feotensor::{Matrix, Shape, Tensor, Vector};

// Contraction Methods

fn bench_sum(c: &mut Criterion) {
let shape = Shape::new(vec![500, 500]).unwrap();
let tensor = Tensor::<f64>::ones(&shape);
c.bench_function("tensor_sum", |b| b.iter(|| tensor.sum(vec![])));
}

fn bench_mean(c: &mut Criterion) {
let shape = Shape::new(vec![500, 500]).unwrap();
let tensor = Tensor::<f64>::ones(&shape);
c.bench_function("tensor_mean", |b| b.iter(|| tensor.mean(vec![])));
}

fn bench_var(c: &mut Criterion) {
let shape = Shape::new(vec![500, 500]).unwrap();
let tensor = Tensor::<f64>::ones(&shape);
c.bench_function("tensor_var", |b| b.iter(|| tensor.var(vec![])));
}

fn bench_max(c: &mut Criterion) {
let shape = Shape::new(vec![500, 500]).unwrap();
let tensor = Tensor::<f64>::ones(&shape);
c.bench_function("tensor_max", |b| b.iter(|| tensor.max(vec![])));
}

fn bench_min(c: &mut Criterion) {
let shape = Shape::new(vec![500, 500]).unwrap();
let tensor = Tensor::<f64>::ones(&shape);
c.bench_function("tensor_min", |b| b.iter(|| tensor.min(vec![])));
}

// Tensor Product

fn bench_tensor_product(c: &mut Criterion) {
let shape_a = Shape::new(vec![100, 100]).unwrap();
let shape_b = Shape::new(vec![100, 100]).unwrap();
let tensor_a = Tensor::<f64>::ones(&shape_a);
let tensor_b = Tensor::<f64>::ones(&shape_b);
c.bench_function("tensor_product", |b| b.iter(|| tensor_a.prod(&tensor_b)));
}

// Matrix Multiplication

fn bench_matmul(c: &mut Criterion) {
let shape_a = Shape::new(vec![100, 100]).unwrap();
let shape_b = Shape::new(vec![100, 100]).unwrap();
let matrix_a = Matrix::<f64>::ones(&shape_a).unwrap();
let matrix_b = Matrix::<f64>::ones(&shape_b).unwrap();
c.bench_function("matrix_multiplication", |b| {
b.iter(|| matrix_a.matmul(&matrix_b))
});
}

// Vector Multiplication

fn bench_vecmul(c: &mut Criterion) {
let shape = Shape::new(vec![100]).unwrap();
let vector_a = Vector::<f64>::ones(&shape).unwrap();
let vector_b = Vector::<f64>::ones(&shape).unwrap();
c.bench_function("vector_multiplication", |b| {
b.iter(|| vector_a.vecmul(&vector_b))
});
}

criterion_group!(
benches,
bench_sum,
bench_mean,
bench_var,
bench_max,
bench_min,
bench_matmul,
bench_vecmul,
bench_tensor_product,
);
criterion_main!(benches);
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ pub mod shape;
pub mod storage;
pub mod tensor;
pub mod vector;

// Re-export important structs
pub use axes::Axes;
pub use coordinate::Coordinate;
pub use error::ShapeError;
pub use matrix::Matrix;
pub use shape::Shape;
pub use tensor::Tensor;
pub use vector::Vector;