Skip to content

Instantly share code, notes, and snippets.

@SteveBronder
Created July 3, 2020 21:18
Show Gist options
  • Save SteveBronder/47d5e5e7c6642018835bc01d21e95f29 to your computer and use it in GitHub Desktop.
Save SteveBronder/47d5e5e7c6642018835bc01d21e95f29 to your computer and use it in GitHub Desktop.
#include <benchmark/benchmark.h>
#include <stan/math/mix.hpp>
#include <Eigen/Dense>
#include <utility>
static bool run_once = true;
// Just to fill up the stack allocator
static void toss_me(benchmark::State& state) {
using stan::math::var;
using stan::math::var_value;
using stan::math::sum;
using stan::math::multiply;
if (run_once) {
using mat_dbl = Eigen::Matrix<double, -1, -1>;
using mat_var = Eigen::Matrix<var, -1, -1>;
mat_var x(mat_dbl::Random(20, 20));
mat_var y(mat_dbl::Random(20, 20));
var lp = 0;
puts("Setup Mats");
lp -= sum(multiply(x, y) + x);
puts("Ran Sum");
lp.grad();
puts("Grad");
run_once = false;
benchmark::DoNotOptimize(lp.vi_);
stan::math::set_zero_all_adjoints();
benchmark::ClobberMemory();
stan::math::recover_memory();
puts("Cleanup");
}
for (auto _ : state) {
var lp2 = 0;
var foo = lp2 + lp2;
//lp.grad();
stan::math::recover_memory();
}
}
// Just to kick off the stack allocation
static void stat_create_destory(benchmark::State& state) {
using stan::math::var_value;
using stan::math::var;
using stan::math::sum;
Eigen::MatrixXd rando = Eigen::MatrixXd::Random(state.range(0), state.range(0));
for (auto _ : state) {
var_value<Eigen::Matrix<double, -1, -1>> a(rando);
var_value<Eigen::Matrix<double, -1, -1>> b(rando);
var_value<Eigen::Matrix<double, -1, -1>> c(rando);
var_value<Eigen::Matrix<double, -1, -1>> d(rando);
var_value<Eigen::Matrix<double, -1, -1>> e(rando);
var_value<Eigen::Matrix<double, -1, -1>> f(rando);
var_value<Eigen::Matrix<double, -1, -1>> g(rando);
var_value<Eigen::Matrix<double, -1, -1>> h(rando);
benchmark::DoNotOptimize(a.val().data());
benchmark::DoNotOptimize(b.val().data());
benchmark::DoNotOptimize(c.val().data());
benchmark::DoNotOptimize(d.val().data());
benchmark::DoNotOptimize(e.val().data());
benchmark::DoNotOptimize(f.val().data());
benchmark::DoNotOptimize(g.val().data());
benchmark::DoNotOptimize(h.val().data());
benchmark::ClobberMemory();
stan::math::recover_memory();
benchmark::ClobberMemory();
}
}
// The start and ending sizes for the benchmark
int start_val = 2;
int end_val = 2048;
BENCHMARK(toss_me);
BENCHMARK(stat_create_destory)->RangeMultiplier(2)->Range(start_val, end_val);
BENCHMARK_MAIN();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment