Reorganize benchmarks into subdirectories and clean up Eigen sources

libeigen/eigen!2176

Co-authored-by: Rasmus Munk Larsen <rmlarsen@gmail.com>
This commit is contained in:
Rasmus Munk Larsen
2026-02-21 17:46:55 -08:00
parent 832b940976
commit d4077a6e99
34 changed files with 49 additions and 37 deletions

View File

@@ -0,0 +1,3 @@
eigen_add_benchmark(bench_spmv bench_spmv.cpp)
eigen_add_benchmark(bench_spmm bench_spmm.cpp)
eigen_add_benchmark(bench_sparse_transpose bench_sparse_transpose.cpp)

View File

@@ -0,0 +1,45 @@
#include <benchmark/benchmark.h>
#include <Eigen/Sparse>
#include <set>
using namespace Eigen;
typedef double Scalar;
typedef SparseMatrix<Scalar> SpMat;
static void fillMatrix(float density, int rows, int cols, SpMat& dst) {
dst.resize(rows, cols);
dst.reserve(static_cast<int>(rows * cols * density));
for (int j = 0; j < cols; ++j) {
for (int i = 0; i < rows; ++i) {
if (internal::random<float>(0, 1) < density) {
dst.insert(i, j) = internal::random<Scalar>();
}
}
}
dst.makeCompressed();
}
static void BM_SparseTranspose(benchmark::State& state) {
int n = state.range(0);
float density = state.range(1) / 10000.0f;
SpMat sm(n, n), result(n, n);
fillMatrix(density, n, n, sm);
for (auto _ : state) {
result = sm.transpose();
benchmark::DoNotOptimize(result.valuePtr());
}
state.counters["nnz"] = sm.nonZeros();
state.counters["density%"] = density * 100;
}
static void TransposeSizes(::benchmark::Benchmark* b) {
// Args: {size, density*10000}
for (int n : {1000, 10000}) {
for (int d : {100, 50, 10, 4}) { // 1%, 0.5%, 0.1%, 0.04%
b->Args({n, d});
}
}
}
BENCHMARK(BM_SparseTranspose)->Apply(TransposeSizes);

View File

@@ -0,0 +1,49 @@
#include <benchmark/benchmark.h>
#include <Eigen/Sparse>
#include <set>
using namespace Eigen;
typedef double Scalar;
typedef SparseMatrix<Scalar> SpMat;
static void fillMatrix(int nnzPerCol, int rows, int cols, SpMat& dst) {
dst.resize(rows, cols);
dst.reserve(VectorXi::Constant(cols, nnzPerCol));
for (int j = 0; j < cols; ++j) {
std::set<int> used;
for (int i = 0; i < nnzPerCol; ++i) {
int row;
do {
row = internal::random<int>(0, rows - 1);
} while (used.count(row));
used.insert(row);
dst.insert(row, j) = internal::random<Scalar>();
}
}
dst.makeCompressed();
}
static void BM_SparseMM(benchmark::State& state) {
int n = state.range(0);
int nnzPerCol = state.range(1);
SpMat sm1(n, n), sm2(n, n), sm3(n, n);
fillMatrix(nnzPerCol, n, n, sm1);
fillMatrix(nnzPerCol, n, n, sm2);
for (auto _ : state) {
sm3 = sm1 * sm2;
benchmark::DoNotOptimize(sm3.valuePtr());
}
state.counters["nnz_A"] = sm1.nonZeros();
state.counters["nnz_B"] = sm2.nonZeros();
}
static void SpMMSizes(::benchmark::Benchmark* b) {
for (int n : {1000, 10000}) {
for (int nnz : {4, 6, 10}) {
b->Args({n, nnz});
}
}
}
BENCHMARK(BM_SparseMM)->Apply(SpMMSizes);

View File

@@ -0,0 +1,64 @@
#include <benchmark/benchmark.h>
#include <Eigen/Sparse>
using namespace Eigen;
typedef double Scalar;
typedef SparseMatrix<Scalar> SpMat;
typedef Matrix<Scalar, Dynamic, 1> DenseVec;
static void fillMatrix(int nnzPerCol, int rows, int cols, SpMat& dst) {
dst.resize(rows, cols);
dst.reserve(VectorXi::Constant(cols, nnzPerCol));
for (int j = 0; j < cols; ++j) {
std::set<int> used;
for (int i = 0; i < nnzPerCol; ++i) {
int row;
do {
row = internal::random<int>(0, rows - 1);
} while (used.count(row));
used.insert(row);
dst.insert(row, j) = internal::random<Scalar>();
}
}
dst.makeCompressed();
}
static void BM_SpMV(benchmark::State& state) {
int n = state.range(0);
int nnzPerCol = state.range(1);
SpMat sm(n, n);
fillMatrix(nnzPerCol, n, n, sm);
DenseVec v = DenseVec::Random(n);
DenseVec res(n);
for (auto _ : state) {
res.noalias() = sm * v;
benchmark::DoNotOptimize(res.data());
}
state.counters["nnz"] = sm.nonZeros();
}
static void BM_SpMV_Transpose(benchmark::State& state) {
int n = state.range(0);
int nnzPerCol = state.range(1);
SpMat sm(n, n);
fillMatrix(nnzPerCol, n, n, sm);
DenseVec v = DenseVec::Random(n);
DenseVec res(n);
for (auto _ : state) {
res.noalias() = sm.transpose() * v;
benchmark::DoNotOptimize(res.data());
}
state.counters["nnz"] = sm.nonZeros();
}
static void SpMVSizes(::benchmark::Benchmark* b) {
for (int n : {1000, 10000, 100000}) {
for (int nnz : {7, 20, 50}) {
b->Args({n, nnz});
}
}
}
BENCHMARK(BM_SpMV)->Apply(SpMVSizes);
BENCHMARK(BM_SpMV_Transpose)->Apply(SpMVSizes);