Ref. + AVX code & generic tests + benches (#85)

This commit is contained in:
Jean-Philippe Bossuat
2025-09-15 16:16:11 +02:00
committed by GitHub
parent 99b9e3e10e
commit 56dbd29c59
286 changed files with 27797 additions and 7270 deletions

View File

@@ -0,0 +1,5 @@
pub mod svp;
pub mod vec_znx;
pub mod vec_znx_big;
pub mod vec_znx_dft;
pub mod vmp;

View File

@@ -0,0 +1,237 @@
use std::hint::black_box;
use criterion::{BenchmarkId, Criterion};
use rand::RngCore;
use crate::{
api::{
ModuleNew, SvpApplyDft, SvpApplyDftToDft, SvpApplyDftToDftAdd, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPrepare,
VecZnxDftAlloc,
},
layouts::{Backend, DataViewMut, FillUniform, Module, ScalarZnx, SvpPPol, VecZnx, VecZnxDft},
source::Source,
};
pub fn bench_svp_prepare<B>(c: &mut Criterion, label: &str)
where
Module<B>: SvpPrepare<B> + SvpPPolAlloc<B> + ModuleNew<B>,
B: Backend<ScalarPrep = f64>,
{
let group_name: String = format!("svp_prepare::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B>(log_n: usize) -> impl FnMut()
where
Module<B>: SvpPrepare<B> + SvpPPolAlloc<B> + ModuleNew<B>,
B: Backend<ScalarPrep = f64>,
{
let module: Module<B> = Module::<B>::new(1 << log_n);
let cols: usize = 2;
let mut svp: SvpPPol<Vec<u8>, B> = module.svp_ppol_alloc(cols);
let mut a: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(module.n(), cols);
let mut source = Source::new([0u8; 32]);
a.fill_uniform(50, &mut source);
move || {
module.svp_prepare(&mut svp, 0, &a, 0);
black_box(());
}
}
for log_n in [10, 11, 12, 13, 14] {
let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}", 1 << log_n));
let mut runner = runner::<B>(log_n);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_svp_apply_dft<B>(c: &mut Criterion, label: &str)
where
Module<B>: SvpApplyDft<B> + SvpPPolAlloc<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
B: Backend<ScalarPrep = f64>,
{
let group_name: String = format!("svp_apply_dft::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: SvpApplyDft<B> + SvpPPolAlloc<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
B: Backend<ScalarPrep = f64>,
{
let n: usize = 1 << params[0];
let cols: usize = params[1];
let size: usize = params[2];
let module: Module<B> = Module::<B>::new(n as u64);
let mut svp: SvpPPol<Vec<u8>, B> = module.svp_ppol_alloc(cols);
let mut res: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(cols, size);
let mut a: VecZnx<Vec<u8>> = VecZnx::alloc(n, cols, size);
let mut source = Source::new([0u8; 32]);
source.fill_bytes(svp.data_mut());
source.fill_bytes(res.data_mut());
source.fill_bytes(a.data_mut());
move || {
for j in 0..cols {
module.svp_apply_dft(&mut res, j, &svp, j, &a, j);
}
black_box(());
}
}
for params in [[10, 2, 2], [11, 2, 4], [12, 2, 7], [13, 2, 15], [14, 2, 31]] {
let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2]));
let mut runner = runner::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_svp_apply_dft_to_dft<B>(c: &mut Criterion, label: &str)
where
Module<B>: SvpApplyDftToDft<B> + SvpPPolAlloc<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
B: Backend<ScalarPrep = f64>,
{
let group_name: String = format!("svp_apply_dft_to_dft::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: SvpApplyDftToDft<B> + SvpPPolAlloc<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
B: Backend<ScalarPrep = f64>,
{
let n: usize = 1 << params[0];
let cols: usize = params[1];
let size: usize = params[2];
let module: Module<B> = Module::<B>::new(n as u64);
let mut svp: SvpPPol<Vec<u8>, B> = module.svp_ppol_alloc(cols);
let mut res: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(cols, size);
let mut a: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(cols, size);
let mut source = Source::new([0u8; 32]);
source.fill_bytes(svp.data_mut());
source.fill_bytes(res.data_mut());
source.fill_bytes(a.data_mut());
move || {
for j in 0..cols {
module.svp_apply_dft_to_dft(&mut res, j, &svp, j, &a, j);
}
black_box(());
}
}
for params in [[10, 2, 2], [11, 2, 4], [12, 2, 7], [13, 2, 15], [14, 2, 31]] {
let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2]));
let mut runner = runner::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_svp_apply_dft_to_dft_add<B>(c: &mut Criterion, label: &str)
where
Module<B>: SvpApplyDftToDftAdd<B> + SvpPPolAlloc<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
B: Backend<ScalarPrep = f64>,
{
let group_name: String = format!("svp_apply_dft_to_dft_add::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: SvpApplyDftToDftAdd<B> + SvpPPolAlloc<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
B: Backend<ScalarPrep = f64>,
{
let n: usize = 1 << params[0];
let cols: usize = params[1];
let size: usize = params[2];
let module: Module<B> = Module::<B>::new(n as u64);
let mut svp: SvpPPol<Vec<u8>, B> = module.svp_ppol_alloc(cols);
let mut res: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(cols, size);
let mut a: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(cols, size);
let mut source = Source::new([0u8; 32]);
source.fill_bytes(svp.data_mut());
source.fill_bytes(res.data_mut());
source.fill_bytes(a.data_mut());
move || {
for j in 0..cols {
module.svp_apply_dft_to_dft_add(&mut res, j, &svp, j, &a, j);
}
black_box(());
}
}
for params in [[10, 2, 2], [11, 2, 4], [12, 2, 7], [13, 2, 15], [14, 2, 31]] {
let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2]));
let mut runner = runner::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_svp_apply_dft_to_dft_inplace<B>(c: &mut Criterion, label: &str)
where
Module<B>: SvpApplyDftToDftInplace<B> + SvpPPolAlloc<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
B: Backend<ScalarPrep = f64>,
{
let group_name: String = format!("svp_apply_dft_to_dft_inplace::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: SvpApplyDftToDftInplace<B> + SvpPPolAlloc<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
B: Backend<ScalarPrep = f64>,
{
let n: usize = 1 << params[0];
let cols: usize = params[1];
let size: usize = params[2];
let module: Module<B> = Module::<B>::new(n as u64);
let mut svp: SvpPPol<Vec<u8>, B> = module.svp_ppol_alloc(cols);
let mut res: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(cols, size);
let mut source = Source::new([0u8; 32]);
source.fill_bytes(svp.data_mut());
source.fill_bytes(res.data_mut());
move || {
for j in 0..cols {
module.svp_apply_dft_to_dft_inplace(&mut res, j, &svp, j);
}
black_box(());
}
}
for params in [[10, 2, 2], [11, 2, 4], [12, 2, 7], [13, 2, 15], [14, 2, 31]] {
let id: BenchmarkId = BenchmarkId::from_parameter(format!("{}x({}x{})", 1 << params[0], params[1], params[2]));
let mut runner = runner::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1,641 @@
use std::hint::black_box;
use criterion::{BenchmarkId, Criterion};
use rand::RngCore;
use crate::{
api::{
ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxBigAdd, VecZnxBigAddInplace, VecZnxBigAddSmall,
VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAutomorphism, VecZnxBigAutomorphismInplace,
VecZnxBigAutomorphismInplaceTmpBytes, VecZnxBigNegate, VecZnxBigNegateInplace, VecZnxBigNormalize,
VecZnxBigNormalizeTmpBytes, VecZnxBigSub, VecZnxBigSubABInplace, VecZnxBigSubBAInplace, VecZnxBigSubSmallA,
VecZnxBigSubSmallB,
},
layouts::{Backend, DataViewMut, Module, ScratchOwned, VecZnx, VecZnxBig},
source::Source,
};
pub fn bench_vec_znx_big_add<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxBigAdd<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let group_name: String = format!("vec_znx_big_add::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxBigAdd<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let n: usize = params[0];
let cols: usize = params[1];
let size: usize = params[2];
let module: Module<B> = Module::<B>::new(n as u64);
let mut source: Source = Source::new([0u8; 32]);
let mut a: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
let mut b: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
let mut c: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
// Fill a with random bytes
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_big_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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_big_add_inplace<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxBigAddInplace<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let group_name: String = format!("vec_znx_big_add_inplace::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxBigAddInplace<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let n: usize = params[0];
let cols: usize = params[1];
let size: usize = params[2];
let module: Module<B> = Module::<B>::new(n as u64);
let mut source: Source = Source::new([0u8; 32]);
let mut a: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
let mut c: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_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_big_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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_big_add_small<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxBigAddSmall<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let group_name: String = format!("vec_znx_big_add_small::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxBigAddSmall<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let n: usize = params[0];
let cols: usize = params[1];
let size: usize = params[2];
let module: Module<B> = Module::<B>::new(n as u64);
let mut source: Source = Source::new([0u8; 32]);
let mut a: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
let mut b: VecZnx<Vec<u8>> = VecZnx::alloc(n, cols, size);
let mut c: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
// Fill a with random i64
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_big_add_small(&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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_big_add_small_inplace<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxBigAddSmallInplace<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let group_name: String = format!("vec_znx_big_add_small_inplace::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxBigAddSmallInplace<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let n: usize = params[0];
let cols: usize = params[1];
let size: usize = params[2];
let module: Module<B> = Module::<B>::new(n as u64);
let mut source: Source = Source::new([0u8; 32]);
let mut a: VecZnx<Vec<u8>> = VecZnx::alloc(n, cols, size);
let mut c: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_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_big_add_small_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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_big_automorphism<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxBigAutomorphism<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let group_name: String = format!("vec_znx_big_automorphism::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxBigAutomorphism<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let n: usize = params[0];
let cols: usize = params[1];
let size: usize = params[2];
let module: Module<B> = Module::<B>::new(n as u64);
let mut source: Source = Source::new([0u8; 32]);
let mut a: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
let mut res: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
// Fill a with random i64
source.fill_bytes(a.data_mut());
source.fill_bytes(res.data_mut());
move || {
for i in 0..cols {
module.vec_znx_big_automorphism(-7, &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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_automorphism_inplace<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxBigAutomorphismInplace<B> + VecZnxBigAutomorphismInplaceTmpBytes + ModuleNew<B> + VecZnxBigAlloc<B>,
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
{
let group_name: String = format!("vec_znx_automorphism_inplace::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxBigAutomorphismInplace<B> + ModuleNew<B> + VecZnxBigAutomorphismInplaceTmpBytes + VecZnxBigAlloc<B>,
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
{
let n: usize = params[0];
let cols: usize = params[1];
let size: usize = params[2];
let module: Module<B> = Module::<B>::new(n as u64);
let mut source: Source = Source::new([0u8; 32]);
let mut res: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(module.vec_znx_big_automorphism_inplace_tmp_bytes());
// Fill a with random i64
source.fill_bytes(res.data_mut());
move || {
for i in 0..cols {
module.vec_znx_big_automorphism_inplace(-7, &mut res, 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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_big_negate<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxBigNegate<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let group_name: String = format!("vec_znx_big_negate::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxBigNegate<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let module: Module<B> = Module::<B>::new(1 << params[0]);
let cols: usize = params[1];
let size: usize = params[2];
let mut source: Source = Source::new([0u8; 32]);
let mut a: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
let mut b: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
// Fill a with random i64
source.fill_bytes(a.data_mut());
source.fill_bytes(b.data_mut());
move || {
for i in 0..cols {
module.vec_znx_big_negate(&mut b, 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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_big_negate_inplace<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxBigNegateInplace<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let group_name: String = format!("vec_znx_negate_big_inplace::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxBigNegateInplace<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let module: Module<B> = Module::<B>::new(1 << params[0]);
let cols: usize = params[1];
let size: usize = params[2];
let mut source: Source = Source::new([0u8; 32]);
let mut a: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
// Fill a with random i64
source.fill_bytes(a.data_mut());
move || {
for i in 0..cols {
module.vec_znx_big_negate_inplace(&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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_normalize<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxBigNormalize<B> + ModuleNew<B> + VecZnxBigNormalizeTmpBytes + VecZnxBigAlloc<B>,
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
{
let group_name: String = format!("vec_znx_big_normalize::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxBigNormalize<B> + ModuleNew<B> + VecZnxBigNormalizeTmpBytes + VecZnxBigAlloc<B>,
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
{
let n: usize = params[0];
let cols: usize = params[1];
let size: usize = params[2];
let module: Module<B> = Module::<B>::new(n as u64);
let basek: usize = 50;
let mut source: Source = Source::new([0u8; 32]);
let mut a: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
let mut res: VecZnx<Vec<u8>> = VecZnx::alloc(module.n(), cols, size);
// Fill a with random i64
source.fill_bytes(a.data_mut());
source.fill_bytes(res.data_mut());
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(module.vec_znx_big_normalize_tmp_bytes());
move || {
for i in 0..cols {
module.vec_znx_big_normalize(basek, &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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_big_sub<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxBigSub<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let group_name: String = format!("vec_znx_big_sub::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxBigSub<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let module: Module<B> = Module::<B>::new(1 << params[0]);
let cols: usize = params[1];
let size: usize = params[2];
let mut source: Source = Source::new([0u8; 32]);
let mut a: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
let mut b: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
let mut c: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
// Fill a with random bytes
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_big_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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_big_sub_ab_inplace<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxBigSubABInplace<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let group_name: String = format!("vec_znx_big_sub_inplace::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxBigSubABInplace<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let module: Module<B> = Module::<B>::new(1 << params[0]);
let cols: usize = params[1];
let size: usize = params[2];
let mut source: Source = Source::new([0u8; 32]);
let mut a: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
let mut c: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
// Fill a with random bytes
source.fill_bytes(a.data_mut());
source.fill_bytes(c.data_mut());
move || {
for i in 0..cols {
module.vec_znx_big_sub_ab_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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_big_sub_ba_inplace<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxBigSubBAInplace<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let group_name: String = format!("vec_znx_big_sub_inplace::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxBigSubBAInplace<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let module: Module<B> = Module::<B>::new(1 << params[0]);
let cols: usize = params[1];
let size: usize = params[2];
let mut source: Source = Source::new([0u8; 32]);
let mut a: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
let mut c: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
// Fill a with random bytes
source.fill_bytes(a.data_mut());
source.fill_bytes(c.data_mut());
move || {
for i in 0..cols {
module.vec_znx_big_sub_ba_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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_big_sub_small_a<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxBigSubSmallA<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let group_name: String = format!("vec_znx_big_sub_small_a::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxBigSubSmallA<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let module: Module<B> = Module::<B>::new(1 << params[0]);
let cols: usize = params[1];
let size: usize = params[2];
let mut source: Source = Source::new([0u8; 32]);
let mut a: VecZnx<Vec<u8>> = VecZnx::alloc(module.n(), cols, size);
let mut b: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
let mut c: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
// Fill a with random bytes
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_big_sub_small_a(&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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_big_sub_small_b<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxBigSubSmallB<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let group_name: String = format!("vec_znx_big_sub_small_b::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxBigSubSmallB<B> + ModuleNew<B> + VecZnxBigAlloc<B>,
{
let module: Module<B> = Module::<B>::new(1 << params[0]);
let cols: usize = params[1];
let size: usize = params[2];
let mut source: Source = Source::new([0u8; 32]);
let mut a: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
let mut b: VecZnx<Vec<u8>> = VecZnx::alloc(module.n(), cols, size);
let mut c: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
// Fill a with random bytes
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_big_sub_small_b(&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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}

View File

@@ -0,0 +1,365 @@
use std::hint::black_box;
use criterion::{BenchmarkId, Criterion};
use rand::RngCore;
use crate::{
api::{
ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxBigAlloc, VecZnxDftAdd, VecZnxDftAddInplace, VecZnxDftAlloc,
VecZnxDftApply, VecZnxDftSub, VecZnxDftSubABInplace, VecZnxDftSubBAInplace, VecZnxIdftApply, VecZnxIdftApplyTmpA,
VecZnxIdftApplyTmpBytes,
},
layouts::{Backend, DataViewMut, Module, ScratchOwned, VecZnx, VecZnxBig, VecZnxDft},
source::Source,
};
pub fn bench_vec_znx_dft_add<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxDftAdd<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
{
let group_name: String = format!("vec_znx_dft_add::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxDftAdd<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
{
let n: usize = params[0];
let cols: usize = params[1];
let size: usize = params[2];
let module: Module<B> = Module::<B>::new(n as u64);
let mut source: Source = Source::new([0u8; 32]);
let mut a: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(cols, size);
let mut b: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(cols, size);
let mut c: VecZnxDft<Vec<u8>, 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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_dft_add_inplace<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxDftAddInplace<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
{
let group_name: String = format!("vec_znx_dft_add_inplace::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxDftAddInplace<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
{
let n: usize = params[0];
let cols: usize = params[1];
let size: usize = params[2];
let module: Module<B> = Module::<B>::new(n as u64);
let mut source: Source = Source::new([0u8; 32]);
let mut a: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(cols, size);
let mut c: VecZnxDft<Vec<u8>, 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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_dft_apply<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxDftApply<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
{
let group_name: String = format!("vec_znx_dft_apply::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxDftApply<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
{
let n: usize = params[0];
let cols: usize = params[1];
let size: usize = params[2];
let module: Module<B> = Module::<B>::new(n as u64);
let mut source: Source = Source::new([0u8; 32]);
let mut res: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(cols, size);
let mut a: VecZnx<Vec<u8>> = 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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_idft_apply<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxIdftApply<B> + ModuleNew<B> + VecZnxIdftApplyTmpBytes + VecZnxDftAlloc<B> + VecZnxBigAlloc<B>,
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
{
let group_name: String = format!("vec_znx_idft_apply::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxIdftApply<B> + ModuleNew<B> + VecZnxIdftApplyTmpBytes + VecZnxDftAlloc<B> + VecZnxBigAlloc<B>,
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
{
let n: usize = params[0];
let cols: usize = params[1];
let size: usize = params[2];
let module: Module<B> = Module::<B>::new(n as u64);
let mut source: Source = Source::new([0u8; 32]);
let mut res: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
let mut a: VecZnxDft<Vec<u8>, 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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_idft_apply_tmpa<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxIdftApplyTmpA<B> + ModuleNew<B> + VecZnxDftAlloc<B> + VecZnxBigAlloc<B>,
{
let group_name: String = format!("vec_znx_idft_apply_tmpa::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxIdftApplyTmpA<B> + ModuleNew<B> + VecZnxDftAlloc<B> + VecZnxBigAlloc<B>,
{
let module: Module<B> = Module::<B>::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<Vec<u8>, B> = module.vec_znx_big_alloc(cols, size);
let mut a: VecZnxDft<Vec<u8>, 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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_dft_sub<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxDftSub<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
{
let group_name: String = format!("vec_znx_dft_sub::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxDftSub<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
{
let n: usize = params[0];
let cols: usize = params[1];
let size: usize = params[2];
let module: Module<B> = Module::<B>::new(n as u64);
let mut source: Source = Source::new([0u8; 32]);
let mut a: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(cols, size);
let mut b: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(cols, size);
let mut c: VecZnxDft<Vec<u8>, 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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_dft_sub_ab_inplace<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxDftSubABInplace<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
{
let group_name: String = format!("vec_znx_dft_sub_ab_inplace::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxDftSubABInplace<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
{
let n: usize = params[0];
let cols: usize = params[1];
let size: usize = params[2];
let module: Module<B> = Module::<B>::new(n as u64);
let mut source: Source = Source::new([0u8; 32]);
let mut a: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(cols, size);
let mut c: VecZnxDft<Vec<u8>, 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_ab_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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vec_znx_dft_sub_ba_inplace<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: VecZnxDftSubBAInplace<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
{
let group_name: String = format!("vec_znx_dft_sub_ba_inplace::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 3]) -> impl FnMut()
where
Module<B>: VecZnxDftSubBAInplace<B> + ModuleNew<B> + VecZnxDftAlloc<B>,
{
let n: usize = params[0];
let cols: usize = params[1];
let size: usize = params[2];
let module: Module<B> = Module::<B>::new(n as u64);
let mut source: Source = Source::new([0u8; 32]);
let mut a: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(cols, size);
let mut c: VecZnxDft<Vec<u8>, 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_ba_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::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}

View File

@@ -0,0 +1,259 @@
use std::hint::black_box;
use criterion::{BenchmarkId, Criterion};
use rand::RngCore;
use crate::{
api::{
ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxDftAlloc, VmpApplyDft, VmpApplyDftTmpBytes, VmpApplyDftToDft,
VmpApplyDftToDftAdd, VmpApplyDftToDftAddTmpBytes, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, VmpPrepareTmpBytes,
},
layouts::{Backend, DataViewMut, MatZnx, Module, ScratchOwned, VecZnx, VecZnxDft, VmpPMat},
source::Source,
};
pub fn bench_vmp_prepare<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: ModuleNew<B> + VmpPMatAlloc<B> + VmpPrepare<B> + VmpPrepareTmpBytes,
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
{
let group_name: String = format!("vmp_prepare::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 5]) -> impl FnMut()
where
Module<B>: ModuleNew<B> + VmpPMatAlloc<B> + VmpPrepare<B> + VmpPrepareTmpBytes,
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
{
let module: Module<B> = Module::<B>::new(1 << params[0]);
let rows: usize = params[1];
let cols_in: usize = params[2];
let cols_out: usize = params[3];
let size: usize = params[4];
let mut source: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(module.vmp_prepare_tmp_bytes(rows, cols_in, cols_out, size));
let mut mat: MatZnx<Vec<u8>> = MatZnx::alloc(module.n(), rows, cols_in, cols_out, size);
let mut pmat: VmpPMat<Vec<u8>, B> = module.vmp_pmat_alloc(rows, cols_in, cols_out, size);
source.fill_bytes(mat.data_mut());
source.fill_bytes(pmat.data_mut());
move || {
module.vmp_prepare(&mut pmat, &mat, scratch.borrow());
black_box(());
}
}
for params in [
[10, 2, 1, 2, 3],
[11, 4, 1, 2, 5],
[12, 7, 1, 2, 8],
[13, 15, 1, 2, 16],
[14, 31, 1, 2, 32],
] {
let id = BenchmarkId::from_parameter(format!(
"{}x({}x{})x({}x{})",
1 << params[0],
params[2],
params[1],
params[3],
params[4]
));
let mut runner = runner::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vmp_apply_dft<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: ModuleNew<B> + VmpApplyDftTmpBytes + VmpApplyDft<B> + VmpPMatAlloc<B> + VecZnxDftAlloc<B>,
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
{
let group_name: String = format!("vmp_apply_dft::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 5]) -> impl FnMut()
where
Module<B>: ModuleNew<B> + VmpApplyDftTmpBytes + VmpApplyDft<B> + VmpPMatAlloc<B> + VecZnxDftAlloc<B>,
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
{
let module: Module<B> = Module::<B>::new(1 << params[0]);
let rows: usize = params[1];
let cols_in: usize = params[2];
let cols_out: usize = params[3];
let size: usize = params[4];
let mut source: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(1 << 20);
let mut res: VecZnxDft<Vec<u8>, _> = module.vec_znx_dft_alloc(cols_out, size);
let mut a: VecZnx<Vec<u8>> = VecZnx::alloc(module.n(), cols_in, size);
let mut pmat: VmpPMat<Vec<u8>, B> = module.vmp_pmat_alloc(rows, cols_in, cols_out, size);
source.fill_bytes(pmat.data_mut());
source.fill_bytes(res.data_mut());
source.fill_bytes(a.data_mut());
move || {
module.vmp_apply_dft(&mut res, &a, &pmat, scratch.borrow());
black_box(());
}
}
for params in [
[10, 2, 1, 2, 3],
[11, 4, 1, 2, 5],
[12, 7, 1, 2, 8],
[13, 15, 1, 2, 16],
[14, 31, 1, 2, 32],
] {
let id = BenchmarkId::from_parameter(format!(
"{}x({}x{})x({}x{})",
1 << params[0],
params[2],
params[1],
params[3],
params[4]
));
let mut runner = runner::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vmp_apply_dft_to_dft<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: ModuleNew<B> + VecZnxDftAlloc<B> + VmpPMatAlloc<B> + VmpApplyDftToDft<B> + VmpApplyDftToDftTmpBytes,
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
{
let group_name: String = format!("vmp_apply_dft_to_dft::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 5]) -> impl FnMut()
where
Module<B>: ModuleNew<B> + VecZnxDftAlloc<B> + VmpPMatAlloc<B> + VmpApplyDftToDft<B> + VmpApplyDftToDftTmpBytes,
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
{
let module: Module<B> = Module::<B>::new(1 << params[0]);
let rows: usize = params[1];
let cols_in: usize = params[2];
let cols_out: usize = params[3];
let size: usize = params[4];
let mut source: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> =
ScratchOwned::alloc(module.vmp_apply_dft_to_dft_tmp_bytes(size, size, rows, cols_in, cols_out, size));
let mut res: VecZnxDft<Vec<u8>, _> = module.vec_znx_dft_alloc(cols_out, size);
let mut a: VecZnxDft<Vec<u8>, _> = module.vec_znx_dft_alloc(cols_in, size);
let mut pmat: VmpPMat<Vec<u8>, B> = module.vmp_pmat_alloc(rows, cols_in, cols_out, size);
source.fill_bytes(pmat.data_mut());
source.fill_bytes(res.data_mut());
source.fill_bytes(a.data_mut());
move || {
module.vmp_apply_dft_to_dft(&mut res, &a, &pmat, scratch.borrow());
black_box(());
}
}
for params in [
[10, 2, 1, 2, 3],
[11, 4, 1, 2, 5],
[12, 7, 1, 2, 8],
[13, 15, 1, 2, 16],
[14, 31, 1, 2, 32],
] {
let id = BenchmarkId::from_parameter(format!(
"{}x({}x{})x({}x{})",
1 << params[0], // n
params[2], // cols_in
params[1], // size_in (=rows)
params[3], // cols_out
params[4] // size_out
));
let mut runner = runner::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}
pub fn bench_vmp_apply_dft_to_dft_add<B: Backend>(c: &mut Criterion, label: &str)
where
Module<B>: ModuleNew<B> + VecZnxDftAlloc<B> + VmpPMatAlloc<B> + VmpApplyDftToDftAdd<B> + VmpApplyDftToDftAddTmpBytes,
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
{
let group_name: String = format!("vmp_apply_dft_to_dft_add::{}", label);
let mut group = c.benchmark_group(group_name);
fn runner<B: Backend>(params: [usize; 5]) -> impl FnMut()
where
Module<B>: ModuleNew<B> + VecZnxDftAlloc<B> + VmpPMatAlloc<B> + VmpApplyDftToDftAdd<B> + VmpApplyDftToDftAddTmpBytes,
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
{
let module: Module<B> = Module::<B>::new(1 << params[0]);
let rows: usize = params[1];
let cols_in: usize = params[2];
let cols_out: usize = params[3];
let size: usize = params[4];
let mut source: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> =
ScratchOwned::alloc(module.vmp_apply_dft_to_dft_add_tmp_bytes(size, size, rows, cols_in, cols_out, size));
let mut res: VecZnxDft<Vec<u8>, _> = module.vec_znx_dft_alloc(cols_out, size);
let mut a: VecZnxDft<Vec<u8>, _> = module.vec_znx_dft_alloc(cols_in, size);
let mut pmat: VmpPMat<Vec<u8>, B> = module.vmp_pmat_alloc(rows, cols_in, cols_out, size);
source.fill_bytes(pmat.data_mut());
source.fill_bytes(res.data_mut());
source.fill_bytes(a.data_mut());
move || {
module.vmp_apply_dft_to_dft_add(&mut res, &a, &pmat, 1, scratch.borrow());
black_box(());
}
}
for params in [
[10, 2, 1, 2, 3],
[11, 4, 1, 2, 5],
[12, 7, 1, 2, 8],
[13, 15, 1, 2, 16],
[14, 31, 1, 2, 32],
] {
let id = BenchmarkId::from_parameter(format!(
"{}x({}x{})x({}x{})",
1 << params[0],
params[2],
params[1],
params[3],
params[4]
));
let mut runner = runner::<B>(params);
group.bench_with_input(id, &(), |b, _| b.iter(&mut runner));
}
group.finish();
}