mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
58 lines
2.3 KiB
C++
58 lines
2.3 KiB
C++
// Benchmarks for symmetric rank-1 update (SYR).
|
|
//
|
|
// Tests C.selfadjointView<Lower>().rankUpdate(v, alpha) which computes
|
|
// C += alpha * v * v^T, updating only the lower (or upper) triangle.
|
|
// Exercises SelfadjointProduct.h / selfadjoint_rank1_update.
|
|
|
|
#include <benchmark/benchmark.h>
|
|
#include <Eigen/Core>
|
|
|
|
using namespace Eigen;
|
|
|
|
template <typename Scalar>
|
|
double syrFlops(Index n) {
|
|
// SYR: n*(n+1)/2 multiply-adds ~ n^2
|
|
return (NumTraits<Scalar>::IsComplex ? 8.0 : 2.0) * n * (n + 1) / 2;
|
|
}
|
|
|
|
template <typename Scalar>
|
|
static void BM_SYR_Lower(benchmark::State& state) {
|
|
const Index n = state.range(0);
|
|
using Mat = Matrix<Scalar, Dynamic, Dynamic>;
|
|
using Vec = Matrix<Scalar, Dynamic, 1>;
|
|
Vec v = Vec::Random(n);
|
|
Mat C = Mat::Zero(n, n);
|
|
Scalar alpha(1);
|
|
for (auto _ : state) {
|
|
C.template selfadjointView<Lower>().rankUpdate(v, alpha);
|
|
benchmark::DoNotOptimize(C.data());
|
|
benchmark::ClobberMemory();
|
|
}
|
|
state.counters["GFLOPS"] = benchmark::Counter(syrFlops<Scalar>(n), benchmark::Counter::kIsIterationInvariantRate,
|
|
benchmark::Counter::kIs1000);
|
|
}
|
|
|
|
template <typename Scalar>
|
|
static void BM_SYR_Upper(benchmark::State& state) {
|
|
const Index n = state.range(0);
|
|
using Mat = Matrix<Scalar, Dynamic, Dynamic>;
|
|
using Vec = Matrix<Scalar, Dynamic, 1>;
|
|
Vec v = Vec::Random(n);
|
|
Mat C = Mat::Zero(n, n);
|
|
Scalar alpha(1);
|
|
for (auto _ : state) {
|
|
C.template selfadjointView<Upper>().rankUpdate(v, alpha);
|
|
benchmark::DoNotOptimize(C.data());
|
|
benchmark::ClobberMemory();
|
|
}
|
|
state.counters["GFLOPS"] = benchmark::Counter(syrFlops<Scalar>(n), benchmark::Counter::kIsIterationInvariantRate,
|
|
benchmark::Counter::kIs1000);
|
|
}
|
|
|
|
// clang-format off
|
|
BENCHMARK(BM_SYR_Lower<float>)->Arg(8)->Arg(16)->Arg(32)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024)->Arg(2048)->Name("SYR_Lower_float");
|
|
BENCHMARK(BM_SYR_Lower<double>)->Arg(8)->Arg(16)->Arg(32)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024)->Arg(2048)->Name("SYR_Lower_double");
|
|
BENCHMARK(BM_SYR_Upper<float>)->Arg(8)->Arg(16)->Arg(32)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024)->Arg(2048)->Name("SYR_Upper_float");
|
|
BENCHMARK(BM_SYR_Upper<double>)->Arg(8)->Arg(16)->Arg(32)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024)->Arg(2048)->Name("SYR_Upper_double");
|
|
// clang-format on
|