From 8d90d8106d0dc9ef425c3d34bb6aed2fbafb8527 Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Mon, 31 Aug 2020 22:58:46 +0200 Subject: [PATCH] add benches for sparse matrix multiplication --- sbp/benches/sbpoperators.rs | 66 +++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/sbp/benches/sbpoperators.rs b/sbp/benches/sbpoperators.rs index 19b9eae..4da115c 100644 --- a/sbp/benches/sbpoperators.rs +++ b/sbp/benches/sbpoperators.rs @@ -45,6 +45,72 @@ fn performance_benchmark(c: &mut Criterion) { b.iter(|| operators::SBP8.diffeta(x.view(), res.view_mut())) }); + #[cfg(feature = "sparse")] + { + use sbp::utils::kronecker_product; + + fn make_operators( + op: impl SbpOperator2d, + nx: usize, + ny: usize, + ) -> (sprs::CsMat, sprs::CsMat) { + let dx = op.op_xi().diff_matrix(nx); + let dx = kronecker_product(sprs::CsMat::eye(ny).view(), dx.view()); + + let dy = op.op_eta().diff_matrix(ny); + let dy = kronecker_product(dy.view(), sprs::CsMat::eye(nx).view()); + + (dy, dx) + } + + { + let (dy, dx) = make_operators(operators::Upwind4, w, h); + group.bench_function("upwind4 diffxi matrix", |b| { + let mut res = ndarray::Array2::zeros(x.raw_dim()); + b.iter(|| { + sprs::prod::mul_acc_mat_vec_csr( + dx.view(), + x.as_slice().unwrap(), + res.as_slice_mut().unwrap(), + ) + }) + }); + group.bench_function("upwind4 diffeta matrix", |b| { + let mut res = ndarray::Array2::zeros(x.raw_dim()); + b.iter(|| { + sprs::prod::mul_acc_mat_vec_csr( + dy.view(), + x.as_slice().unwrap(), + res.as_slice_mut().unwrap(), + ) + }) + }); + } + { + let (dy, dx) = make_operators(operators::Upwind9, w, h); + group.bench_function("upwind9 diffxi matrix", |b| { + let mut res = ndarray::Array2::zeros(x.raw_dim()); + b.iter(|| { + sprs::prod::mul_acc_mat_vec_csr( + dx.view(), + x.as_slice().unwrap(), + res.as_slice_mut().unwrap(), + ) + }) + }); + group.bench_function("upwind9 diffeta matrix", |b| { + let mut res = ndarray::Array2::zeros(x.raw_dim()); + b.iter(|| { + sprs::prod::mul_acc_mat_vec_csr( + dy.view(), + x.as_slice().unwrap(), + res.as_slice_mut().unwrap(), + ) + }) + }); + } + } + group.finish(); }