From ad30f4aca602ca0558de021f688c29319f548954 Mon Sep 17 00:00:00 2001 From: Hamish Ivey-Law <426294+unzvfu@users.noreply.github.com> Date: Tue, 12 Oct 2021 09:46:38 +1100 Subject: [PATCH] WIP: Remove old benchmarks (#297) * Remove old benchmarkmarking binaries now that we use Criterion. * Benchmark CrandallField alongside Goldilocks. --- benches/hashing.rs | 8 ++- src/bin/bench_field_mul.rs | 27 -------- src/bin/bench_field_mul_interleaved.rs | 35 ----------- src/bin/bench_gmimc.rs | 44 ------------- src/bin/bench_hash.rs | 85 -------------------------- src/bin/bench_rescue.rs | 44 ------------- 6 files changed, 6 insertions(+), 237 deletions(-) delete mode 100644 src/bin/bench_field_mul.rs delete mode 100644 src/bin/bench_field_mul_interleaved.rs delete mode 100644 src/bin/bench_gmimc.rs delete mode 100644 src/bin/bench_hash.rs delete mode 100644 src/bin/bench_rescue.rs diff --git a/benches/hashing.rs b/benches/hashing.rs index 2eb2aa23..179ef859 100644 --- a/benches/hashing.rs +++ b/benches/hashing.rs @@ -2,16 +2,18 @@ #![feature(generic_const_exprs)] use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; +use plonky2::field::crandall_field::CrandallField; use plonky2::field::goldilocks_field::GoldilocksField; use plonky2::hash::gmimc::GMiMC; use plonky2::hash::poseidon::Poseidon; +use plonky2::hash::rescue::rescue; use tynm::type_name; pub(crate) fn bench_gmimc, const WIDTH: usize>(c: &mut Criterion) { c.bench_function(&format!("gmimc<{}, {}>", type_name::(), WIDTH), |b| { b.iter_batched( || F::rand_arr::(), - |mut state| F::gmimc_permute(state), + |state| F::gmimc_permute(state), BatchSize::SmallInput, ) }); @@ -24,13 +26,15 @@ where c.bench_function(&format!("poseidon<{}, {}>", type_name::(), WIDTH), |b| { b.iter_batched( || F::rand_arr::(), - |mut state| F::poseidon(state), + |state| F::poseidon(state), BatchSize::SmallInput, ) }); } fn criterion_benchmark(c: &mut Criterion) { + bench_gmimc::(c); + bench_poseidon::(c); bench_gmimc::(c); bench_poseidon::(c); } diff --git a/src/bin/bench_field_mul.rs b/src/bin/bench_field_mul.rs deleted file mode 100644 index d362d17f..00000000 --- a/src/bin/bench_field_mul.rs +++ /dev/null @@ -1,27 +0,0 @@ -//! Performs a single exponentiation. - -use std::time::Instant; - -use plonky2::field::crandall_field::CrandallField; -use plonky2::field::field_types::Field; - -type F = CrandallField; - -const EXPONENT: usize = 1000000000; - -fn main() { - let base = F::rand(); - let mut state = F::ONE; - - let start = Instant::now(); - for _ in 0..EXPONENT { - state *= base; - } - let duration = start.elapsed(); - - println!("Result: {:?}", state); - println!( - "Average field mul: {:?}ns", - duration.as_secs_f64() * 1e9 / EXPONENT as f64 - ); -} diff --git a/src/bin/bench_field_mul_interleaved.rs b/src/bin/bench_field_mul_interleaved.rs deleted file mode 100644 index ed035a35..00000000 --- a/src/bin/bench_field_mul_interleaved.rs +++ /dev/null @@ -1,35 +0,0 @@ -//! Performs several exponentiations in an interleaved loop, to enable parallelism on the core. - -use std::time::Instant; - -use plonky2::field::crandall_field::CrandallField; -use plonky2::field::field_types::Field; - -type F = CrandallField; - -/// The number of exponentiations to perform in parallel. -const WIDTH: usize = 6; - -const EXPONENT: usize = 1000000000; - -fn main() { - let mut bases = [F::ZERO; WIDTH]; - for base_i in bases.iter_mut() { - *base_i = F::rand(); - } - let mut state = [F::ONE; WIDTH]; - - let start = Instant::now(); - for _ in 0..EXPONENT { - for i in 0..WIDTH { - state[i] *= bases[i]; - } - } - let duration = start.elapsed(); - - println!("Result: {:?}", state); - println!( - "Average field mul: {:?}ns", - duration.as_secs_f64() * 1e9 / (WIDTH * EXPONENT) as f64 - ); -} diff --git a/src/bin/bench_gmimc.rs b/src/bin/bench_gmimc.rs deleted file mode 100644 index 69296942..00000000 --- a/src/bin/bench_gmimc.rs +++ /dev/null @@ -1,44 +0,0 @@ -use std::convert::TryInto; -use std::thread; -use std::time::Instant; - -use plonky2::field::crandall_field::CrandallField; -use plonky2::field::field_types::Field; -use plonky2::hash::gmimc::GMiMC; - -type F = CrandallField; - -// 113 wire polys, 3 Z polys, 4 parts of quotient poly. -const PROVER_POLYS: usize = 113 + 3 + 4; - -fn main() { - const THREADS: usize = 12; - const LDE_BITS: i32 = 3; - const W: usize = 12; - const HASHES_PER_POLY: usize = 1 << ((13 + LDE_BITS) / 6); - - let threads = (0..THREADS) - .map(|_i| { - thread::spawn(move || { - let mut x: [F; W] = F::rand_vec(W).try_into().unwrap(); - - let hashes_per_thread = HASHES_PER_POLY * PROVER_POLYS / THREADS; - let start = Instant::now(); - for _ in 0..hashes_per_thread { - x = F::gmimc_permute(x); - } - let duration = start.elapsed(); - println!("took {:?}", duration); - println!( - "avg {:?}us", - duration.as_secs_f64() * 1e6 / (hashes_per_thread as f64) - ); - println!("result {:?}", x); - }) - }) - .collect::>(); - - for t in threads { - t.join().expect("oops"); - } -} diff --git a/src/bin/bench_hash.rs b/src/bin/bench_hash.rs deleted file mode 100644 index b44da485..00000000 --- a/src/bin/bench_hash.rs +++ /dev/null @@ -1,85 +0,0 @@ -use std::time::Instant; - -use plonky2::field::crandall_field::CrandallField as F; -use plonky2::field::field_types::Field; -use plonky2::hash::gmimc::GMiMC; -use plonky2::hash::poseidon::Poseidon; -use plonky2::hash::rescue::rescue; - -#[inline] -fn gmimc_hash(x: [F; W]) -> [F; W] -where - F: GMiMC, -{ - F::gmimc_permute(x) -} - -#[inline] -fn rescue_hash(x: [F; 12]) -> [F; 12] { - rescue(x) -} - -#[inline] -fn poseidon8_hash(x: [F; 8]) -> [F; 8] { - F::poseidon(x) -} - -#[inline] -fn poseidon8_naive_hash(x: [F; 8]) -> [F; 8] { - F::poseidon_naive(x) -} - -#[inline] -fn poseidon12_hash(x: [F; 12]) -> [F; 12] { - F::poseidon(x) -} - -#[inline] -fn poseidon12_naive_hash(x: [F; 12]) -> [F; 12] { - F::poseidon_naive(x) -} - -fn bench_hash(name: &str, hash: fn([F; W]) -> [F; W], gmimc_tm: &mut f64) { - // 113 wire polys, 3 Z polys, 4 parts of quotient poly. - const PROVER_POLYS: usize = 113 + 3 + 4; - const LDE_BITS: i32 = 3; - const HASHES_PER_POLY: usize = 1 << (13 + LDE_BITS) / 6; - const N_HASHES: usize = HASHES_PER_POLY * PROVER_POLYS; - - let mut input = [F::ZERO; W]; - for i in 0..W { - input[i] = F::from_canonical_u64((i as u64) * 123456 + 789); - } - - print!("{:16}", name); - - let mut x = input; - let start = Instant::now(); - for _ in 0..N_HASHES { - x = hash(x); - } - let duration = start.elapsed(); - - let tm = duration.as_micros() as f64 / N_HASHES as f64; - - if *gmimc_tm == 0.0 { - *gmimc_tm = tm; - } - - println!(" {:5.2} {:5.2}", tm, tm / *gmimc_tm); -} - -fn main() { - println!(" -- Width 8 (time μs, slowdown wrt GMiMC)--"); - let mut tm: f64 = 0.0; - // bench_hash("GMiMC", gmimc_hash::<8>, &mut tm); // Not implemented yet. - bench_hash("Poseidon", poseidon8_hash, &mut tm); - bench_hash("Poseidon naive", poseidon8_naive_hash, &mut tm); - - println!("\n -- Width 12 (time μs, slowdown wrt GMiMC) --"); - let mut tm: f64 = 0.0; - bench_hash("GMiMC", gmimc_hash::<12>, &mut tm); - bench_hash("Poseidon", poseidon12_hash, &mut tm); - bench_hash("Poseidon naive", poseidon12_naive_hash, &mut tm); - bench_hash("Rescue", rescue_hash, &mut tm); -} diff --git a/src/bin/bench_rescue.rs b/src/bin/bench_rescue.rs deleted file mode 100644 index 6aa3bd55..00000000 --- a/src/bin/bench_rescue.rs +++ /dev/null @@ -1,44 +0,0 @@ -use std::convert::TryInto; -use std::thread; -use std::time::Instant; - -use plonky2::field::crandall_field::CrandallField; -use plonky2::field::field_types::Field; -use plonky2::hash::rescue::rescue; - -type F = CrandallField; - -// 113 wire polys, 3 Z polys, 4 parts of quotient poly. -const PROVER_POLYS: usize = 113 + 3 + 4; - -fn main() { - const THREADS: usize = 12; - const LDE_BITS: i32 = 3; - const W: usize = 12; - const HASHES_PER_POLY: usize = (1 << (13 + LDE_BITS)) / 6; - - let threads = (0..THREADS) - .map(|_i| { - thread::spawn(move || { - let mut x: [F; W] = F::rand_vec(W).try_into().unwrap(); - - let hashes_per_thread = HASHES_PER_POLY * PROVER_POLYS / THREADS; - let start = Instant::now(); - for _ in 0..hashes_per_thread { - x = rescue(x); - } - let duration = start.elapsed(); - println!("took {:?}", duration); - println!( - "avg {:?}us", - duration.as_secs_f64() * 1e6 / (hashes_per_thread as f64) - ); - println!("result {:?}", x); - }) - }) - .collect::>(); - - for t in threads { - t.join().expect("oops"); - } -}