use std::hint::black_box; use criterion::{BenchmarkId, Criterion}; use rand::RngCore; use crate::{ api::{ ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxBigAlloc, VecZnxDftAdd, VecZnxDftAddInplace, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftSub, VecZnxDftSubInplace, VecZnxDftSubNegateInplace, VecZnxIdftApply, VecZnxIdftApplyTmpA, VecZnxIdftApplyTmpBytes, }, layouts::{Backend, DataViewMut, Module, ScratchOwned, VecZnx, VecZnxBig, VecZnxDft}, source::Source, }; pub fn bench_vec_znx_dft_add(c: &mut Criterion, label: &str) where Module: VecZnxDftAdd + ModuleNew + VecZnxDftAlloc, { let group_name: String = format!("vec_znx_dft_add::{label}"); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxDftAdd + ModuleNew + VecZnxDftAlloc, { let n: usize = params[0]; let cols: usize = params[1]; let size: usize = params[2]; let module: Module = Module::::new(n as u64); let mut source: Source = Source::new([0u8; 32]); let mut a: VecZnxDft, B> = module.vec_znx_dft_alloc(cols, size); let mut b: VecZnxDft, B> = module.vec_znx_dft_alloc(cols, size); let mut c: VecZnxDft, B> = module.vec_znx_dft_alloc(cols, size); source.fill_bytes(a.data_mut()); source.fill_bytes(b.data_mut()); source.fill_bytes(c.data_mut()); move || { for i in 0..cols { module.vec_znx_dft_add(&mut c, i, &a, i, &b, i); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_dft_add_inplace(c: &mut Criterion, label: &str) where Module: VecZnxDftAddInplace + ModuleNew + VecZnxDftAlloc, { let group_name: String = format!("vec_znx_dft_add_inplace::{label}"); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxDftAddInplace + ModuleNew + VecZnxDftAlloc, { let n: usize = params[0]; let cols: usize = params[1]; let size: usize = params[2]; let module: Module = Module::::new(n as u64); let mut source: Source = Source::new([0u8; 32]); let mut a: VecZnxDft, B> = module.vec_znx_dft_alloc(cols, size); let mut c: VecZnxDft, B> = module.vec_znx_dft_alloc(cols, size); // Fill a with random i64 source.fill_bytes(a.data_mut()); source.fill_bytes(c.data_mut()); move || { for i in 0..cols { module.vec_znx_dft_add_inplace(&mut c, i, &a, i); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_dft_apply(c: &mut Criterion, label: &str) where Module: VecZnxDftApply + ModuleNew + VecZnxDftAlloc, { let group_name: String = format!("vec_znx_dft_apply::{label}"); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxDftApply + ModuleNew + VecZnxDftAlloc, { let n: usize = params[0]; let cols: usize = params[1]; let size: usize = params[2]; let module: Module = Module::::new(n as u64); let mut source: Source = Source::new([0u8; 32]); let mut res: VecZnxDft, B> = module.vec_znx_dft_alloc(cols, size); let mut a: VecZnx> = VecZnx::alloc(n, cols, size); source.fill_bytes(res.data_mut()); source.fill_bytes(a.data_mut()); move || { for i in 0..cols { module.vec_znx_dft_apply(1, 0, &mut res, i, &a, i); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_idft_apply(c: &mut Criterion, label: &str) where Module: VecZnxIdftApply + ModuleNew + VecZnxIdftApplyTmpBytes + VecZnxDftAlloc + VecZnxBigAlloc, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, { let group_name: String = format!("vec_znx_idft_apply::{label}"); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxIdftApply + ModuleNew + VecZnxIdftApplyTmpBytes + VecZnxDftAlloc + VecZnxBigAlloc, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, { let n: usize = params[0]; let cols: usize = params[1]; let size: usize = params[2]; let module: Module = Module::::new(n as u64); let mut source: Source = Source::new([0u8; 32]); let mut res: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); let mut a: VecZnxDft, B> = module.vec_znx_dft_alloc(cols, size); source.fill_bytes(res.data_mut()); source.fill_bytes(a.data_mut()); let mut scratch = ScratchOwned::alloc(module.vec_znx_idft_apply_tmp_bytes()); move || { for i in 0..cols { module.vec_znx_idft_apply(&mut res, i, &a, i, scratch.borrow()); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_idft_apply_tmpa(c: &mut Criterion, label: &str) where Module: VecZnxIdftApplyTmpA + ModuleNew + VecZnxDftAlloc + VecZnxBigAlloc, { let group_name: String = format!("vec_znx_idft_apply_tmpa::{label}"); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxIdftApplyTmpA + ModuleNew + VecZnxDftAlloc + VecZnxBigAlloc, { let module: Module = Module::::new(1 << params[0]); let cols: usize = params[1]; let size: usize = params[2]; let mut source: Source = Source::new([0u8; 32]); let mut res: VecZnxBig, B> = module.vec_znx_big_alloc(cols, size); let mut a: VecZnxDft, B> = module.vec_znx_dft_alloc(cols, size); source.fill_bytes(res.data_mut()); source.fill_bytes(a.data_mut()); move || { for i in 0..cols { module.vec_znx_idft_apply_tmpa(&mut res, i, &mut a, i); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_dft_sub(c: &mut Criterion, label: &str) where Module: VecZnxDftSub + ModuleNew + VecZnxDftAlloc, { let group_name: String = format!("vec_znx_dft_sub::{label}"); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxDftSub + ModuleNew + VecZnxDftAlloc, { let n: usize = params[0]; let cols: usize = params[1]; let size: usize = params[2]; let module: Module = Module::::new(n as u64); let mut source: Source = Source::new([0u8; 32]); let mut a: VecZnxDft, B> = module.vec_znx_dft_alloc(cols, size); let mut b: VecZnxDft, B> = module.vec_znx_dft_alloc(cols, size); let mut c: VecZnxDft, B> = module.vec_znx_dft_alloc(cols, size); source.fill_bytes(a.data_mut()); source.fill_bytes(b.data_mut()); source.fill_bytes(c.data_mut()); move || { for i in 0..cols { module.vec_znx_dft_sub(&mut c, i, &a, i, &b, i); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_dft_sub_inplace(c: &mut Criterion, label: &str) where Module: VecZnxDftSubInplace + ModuleNew + VecZnxDftAlloc, { let group_name: String = format!("vec_znx_dft_sub_inplace::{label}"); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxDftSubInplace + ModuleNew + VecZnxDftAlloc, { let n: usize = params[0]; let cols: usize = params[1]; let size: usize = params[2]; let module: Module = Module::::new(n as u64); let mut source: Source = Source::new([0u8; 32]); let mut a: VecZnxDft, B> = module.vec_znx_dft_alloc(cols, size); let mut c: VecZnxDft, B> = module.vec_znx_dft_alloc(cols, size); // Fill a with random i64 source.fill_bytes(a.data_mut()); source.fill_bytes(c.data_mut()); move || { for i in 0..cols { module.vec_znx_dft_sub_inplace(&mut c, i, &a, i); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); } pub fn bench_vec_znx_dft_sub_negate_inplace(c: &mut Criterion, label: &str) where Module: VecZnxDftSubNegateInplace + ModuleNew + VecZnxDftAlloc, { let group_name: String = format!("vec_znx_dft_sub_negate_inplace::{label}"); let mut group = c.benchmark_group(group_name); fn runner(params: [usize; 3]) -> impl FnMut() where Module: VecZnxDftSubNegateInplace + ModuleNew + VecZnxDftAlloc, { let n: usize = params[0]; let cols: usize = params[1]; let size: usize = params[2]; let module: Module = Module::::new(n as u64); let mut source: Source = Source::new([0u8; 32]); let mut a: VecZnxDft, B> = module.vec_znx_dft_alloc(cols, size); let mut c: VecZnxDft, B> = module.vec_znx_dft_alloc(cols, size); // Fill a with random i64 source.fill_bytes(a.data_mut()); source.fill_bytes(c.data_mut()); move || { for i in 0..cols { module.vec_znx_dft_sub_negate_inplace(&mut c, i, &a, i); } black_box(()); } } for params in [[10, 2, 2], [11, 2, 4], [12, 2, 8], [13, 2, 16], [14, 2, 32]] { let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2],)); let mut runner = runner::(params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } group.finish(); }