Skip to content

Instantly share code, notes, and snippets.

@SteveBronder
Created December 16, 2020 01:55
Show Gist options
  • Save SteveBronder/f14df4b040b5d6d3e4afb03fecbf8888 to your computer and use it in GitHub Desktop.
Save SteveBronder/f14df4b040b5d6d3e4afb03fecbf8888 to your computer and use it in GitHub Desktop.
#ifndef CALLBACK_TOSS_ME_HPP
#define CALLBACK_TOSS_ME_HPP
#include <benchmark/benchmark.h>
#include <stan/math.hpp>
#include <utility>
static bool needs_done = true;
// Just to fill up the stack allocator
template <int max_alloc>
static void toss_me(benchmark::State& state) {
using stan::math::var;
if (needs_done) {
needs_done = false;
using stan::math::var;
using stan::math::sum;
Eigen::Matrix<var, -1, -1> x(Eigen::MatrixXd::Random(max_alloc, max_alloc));
Eigen::Matrix<var, -1, -1> y(Eigen::MatrixXd::Random(max_alloc, max_alloc));
Eigen::Matrix<var, -1, -1> z = elt_multiply(x, y);
var lp = sum(z);
benchmark::DoNotOptimize(lp.vi_);
for (auto _ : state) {
lp.grad();
stan::math::set_zero_all_adjoints();
}
stan::math::recover_memory();
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment