Skip to content

Commit

Permalink
Add benchmark for clad::matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
vaithak committed Jul 31, 2023
1 parent a36f54f commit b22eb3b
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions benchmark/Matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "benchmark/benchmark.h"

#include "clad/Differentiator/Differentiator.h"

// Benchmarking clad::matrix initialization as identity matrix.
static void BM_MatrixIdentity(benchmark::State& state) {
unsigned N = state.range(0);
for (auto _ : state) {
clad::matrix<double> m = clad::identity_matrix<double>(N, N);
}
}

BENCHMARK(BM_MatrixIdentity)
->RangeMultiplier(2)
->Range(4, 1024);

// Benchmarking scalar operation on a row of clad::matrix.
static void BM_MatrixRowOp(benchmark::State& state) {
unsigned N = state.range(0);
clad::matrix<double> m(N, N);
unsigned mid = N / 2;
for (auto _ : state) {
benchmark::DoNotOptimize(
m[mid] *= 2
);
}
}

BENCHMARK(BM_MatrixRowOp)
->RangeMultiplier(2)
->Range(4, 1024);

// Benchmarking sum of two rows of a clad::matrix.
static void BM_MatrixRowSum(benchmark::State& state) {
unsigned N = state.range(0);
clad::matrix<double> m(N, N);
clad::array<double> res(N);
unsigned mid = N / 2;
for (auto _ : state) {
benchmark::DoNotOptimize(
res = m[0] + m[mid]
);
}
}

BENCHMARK(BM_MatrixRowSum)
->RangeMultiplier(2)
->Range(4, 1024);

// Benchmarking sum of all rows of a clad::matrix.
static void BM_MatrixCompleteRowSum(benchmark::State& state) {
unsigned N = state.range(0);
clad::matrix<double> m(N, N);
clad::array<double> res(N);
for (auto _ : state) {
for (unsigned i = 0; i < N; ++i) {
benchmark::DoNotOptimize(res += m[i]);
}
}
}

BENCHMARK(BM_MatrixRowSum)
->RangeMultiplier(2)
->Range(4, 1024);

BENCHMARK_MAIN();

0 comments on commit b22eb3b

Please sign in to comment.