Skip to content

Commit

Permalink
refactor benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
PatStiles committed Oct 20, 2023
1 parent 2e810b5 commit 5a35caf
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 4 deletions.
6 changes: 3 additions & 3 deletions math/benches/criterion_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use criterion::{criterion_group, criterion_main, Criterion};
use pprof::criterion::{Output, PProfProfiler};

mod fields;
use fields::stark252::starkfield_ops_benchmarks;
use fields::{stark252::starkfield_ops_benchmarks, u64_goldilocks::u64_goldilocks_ops_benchmarks};

criterion_group!(
name = field_benches;
config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
targets = starkfield_ops_benchmarks
targets = starkfield_ops_benchmarks, u64_goldilocks_ops_benchmarks
);
criterion_main!(field_benches);
criterion_main!(field_benches);
1 change: 1 addition & 0 deletions math/benches/fields/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod stark252;
pub mod u64_goldilocks;
2 changes: 1 addition & 1 deletion math/benches/fields/stark252.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,4 @@ pub fn starkfield_ops_benchmarks(c: &mut Criterion) {
});
});
}
}
}
201 changes: 201 additions & 0 deletions math/benches/fields/u64_goldilocks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
use std::hint::black_box;

use criterion::Criterion;
use lambdaworks_math::field::{
element::FieldElement,
fields::u64_goldilocks_field::Goldilocks64Field,
};
use rand::random;

pub type F = FieldElement<Goldilocks64Field>;

#[inline(never)]
#[no_mangle]
#[export_name = "util::rand_goldilocks_field_elements"]
pub fn rand_field_elements(num: usize) -> Vec<(F, F)> {
let mut result = Vec::with_capacity(num);
for _ in 0..result.capacity() {
result.push((F::new(random()), F::new(random())));
}
result
}

pub fn u64_goldilocks_ops_benchmarks(c: &mut Criterion) {
let input: Vec<Vec<(F, F)>> = [1, 10, 100, 1000, 10000, 100000, 1000000]
.into_iter()
.map(rand_field_elements)
.collect::<Vec<_>>();
let mut group = c.benchmark_group("u64 Goldilocks FP operations");

for i in input.clone().into_iter() {
group.bench_with_input(format!("add {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, y) in i {
black_box(black_box(x) + black_box(y));
}
});
});
}

for i in input.clone().into_iter() {
group.bench_with_input(format!("mul {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, y) in i {
black_box(black_box(x) * black_box(y));
}
});
});
}

for i in input.clone().into_iter() {
group.bench_with_input(format!("pow by 1 {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, _) in i {
black_box(black_box(x).pow(1_u64));
}
});
});
}

for i in input.clone().into_iter() {
group.bench_with_input(format!("square {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, _) in i {
black_box(black_box(x).square());
}
});
});
}

for i in input.clone().into_iter() {
group.bench_with_input(format!("square with pow {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, _) in i {
black_box(black_box(x).pow(2_u64));
}
});
});
}

for i in input.clone().into_iter() {
group.bench_with_input(format!("square with mul {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, _) in i {
black_box(black_box(x) * black_box(x));
}
});
});
}

for i in input.clone().into_iter() {
group.bench_with_input(
format!("pow {:?}", &i.len()),
&(i, 5u64),
|bench, (i, a)| {
bench.iter(|| {
for (x, _) in i {
black_box(black_box(x).pow(*a));
}
});
},
);
}

for i in input.clone().into_iter() {
group.bench_with_input(format!("sub {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, y) in i {
black_box(black_box(x) - black_box(y));
}
});
});
}

for i in input.clone().into_iter() {
group.bench_with_input(format!("inv {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, _) in i {
black_box(black_box(x).inv().unwrap());
}
});
});
}

for i in input.clone().into_iter() {
group.bench_with_input(format!("div {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, y) in i {
black_box(black_box(x) / black_box(y));
}
});
});
}

for i in input.clone().into_iter() {
group.bench_with_input(format!("eq {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, y) in i {
black_box(black_box(x) == black_box(y));
}
});
});
}

for i in input.clone().into_iter() {
group.bench_with_input(format!("sqrt {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, _) in i {
black_box(black_box(x).sqrt());
}
});
});
}

for i in input.clone().into_iter() {
group.bench_with_input(format!("sqrt squared {:?}", &i.len()), &i, |bench, i| {
let i: Vec<F> = i.iter().map(|(x, _)| x * x).collect();
bench.iter(|| {
for x in &i {
black_box(black_box(x).sqrt());
}
});
});
}

for i in input.clone().into_iter() {
group.bench_with_input(format!("bitand {:?}", &i.len()), &i, |bench, i| {
// Note: we should strive to have the number of limbs be generic... ideally this benchmark group itself should have a generic type that we call into from the main runner.
let i: Vec<(u64, u64)> =
i.iter().map(|(x, y)| (*x.value(), *y.value())).collect();
bench.iter(|| {
for (x, y) in &i {
black_box(black_box(*x) & black_box(*y));
}
});
});
}

for i in input.clone().into_iter() {
group.bench_with_input(format!("bitor {:?}", &i.len()), &i, |bench, i| {
let i: Vec<(u64, u64)> =
i.iter().map(|(x, y)| (*x.value(), *y.value())).collect();
bench.iter(|| {
for (x, y) in &i {
black_box(black_box(*x) | black_box(*y));
}
});
});
}

for i in input.clone().into_iter() {
group.bench_with_input(format!("bitxor {:?}", &i.len()), &i, |bench, i| {
let i: Vec<(u64, u64)> =
i.iter().map(|(x, y)| (*x.value(), *y.value())).collect();
bench.iter(|| {
for (x, y) in &i {
black_box(black_box(*x) ^ black_box(*y));
}
});
});
}
}

0 comments on commit 5a35caf

Please sign in to comment.