forked from stan-dev/perf-math
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checks_vs_mult.cpp
123 lines (111 loc) · 3.66 KB
/
checks_vs_mult.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <benchmark/benchmark.h>
#include <Eigen/Dense>
#include <stan/math/rev/core.hpp>
static void escape(void *p) {
asm volatile("" : : "g"(p) : "memory");
}
static void clobber() {
asm volatile("" : : : "memory");
}
template <typename T, typename S, typename M>
static void BM_Check(benchmark::State& state) {
const int N = state.range(0);
auto sigma = Eigen::Matrix<S, -1, -1>::Identity(N, N).eval();
auto mu = Eigen::Matrix<M, -1, -1>::Zero(N, 1).eval();
auto x = Eigen::Matrix<T, -1, -1>::Random(N, 1).eval();
for (auto _ : state) {
escape(sigma.data());
escape(mu.data());
escape(x.data());
// Actual task
if (sigma == Eigen::Matrix<S, -1, -1>::Identity(N, N)) {
if (mu == Eigen::Matrix<M, -1, 1>::Zero(N, 1))
escape(x.data());
else {
auto res = (mu + x).eval();
escape(res.data());
}
} else {
auto res = mu + sigma * x;
auto res_eval = res.eval();
escape(res_eval.data());
}
clobber();
}
}
template <typename T>
inline bool is_identity(Eigen::Matrix<T, -1, -1> x) {
for (int j = 0; j < x.rows(); ++j) {
for (int i = 0; i < j; ++i) {
if (x(i, j) != 0) return false;
}
if (x(j, j) != 1) return false;
for (int i = j + 1; i < x.rows(); ++i) {
if (x(i, j) != 0) return false;
}
}
return true;
}
template <typename T, int R, int C>
inline bool is_zero(Eigen::Matrix<T, R, C> x) {
for (int i = 0; i < x.size(); ++i) {
if (x.coeff(i) != 0) return false;
}
return true;
}
template <typename T, typename S, typename M>
static void BM_BobCheck(benchmark::State& state) {
const int N = state.range(0);
auto sigma = Eigen::Matrix<S, -1, -1>::Identity(N, N).eval();
auto mu = Eigen::Matrix<M, -1, -1>::Zero(N, 1).eval();
auto x = Eigen::Matrix<T, -1, -1>::Random(N, 1).eval();
for (auto _ : state) {
escape(sigma.data());
escape(mu.data());
escape(x.data());
// Actual task
if (is_identity(sigma)) {
if (is_zero(mu))
escape(x.data());
else {
auto res = (mu + x).eval();
escape(res.data());
}
} else {
auto res = mu + sigma * x;
auto res_eval = res.eval();
escape(res_eval.data());
}
clobber();
}
}
template <typename T, typename S, typename M>
static void BM_Mult(benchmark::State& state) {
const int N = state.range(0);
auto sigma = Eigen::Matrix<S, -1, -1>::Identity(N, N).eval();
auto mu = Eigen::Matrix<M, -1, -1>::Zero(N, 1).eval();
auto x = Eigen::Matrix<T, -1, -1>::Random(N, 1).eval();
for (auto _ : state) {
escape(sigma.data());
escape(mu.data());
escape(x.data());
// Actual task
auto res = mu + sigma * x;
auto res_eval = res.eval();
escape(res_eval.data());
clobber();
}
}
BENCHMARK_TEMPLATE(BM_Check, double, double, double)->Arg(25);
BENCHMARK_TEMPLATE(BM_BobCheck, double, double, double)->Arg(25);
BENCHMARK_TEMPLATE(BM_Mult, double, double, double)->Arg(25);
BENCHMARK_TEMPLATE(BM_Check, double, double, double)->Arg(500);
BENCHMARK_TEMPLATE(BM_BobCheck, double, double, double)->Arg(500);
BENCHMARK_TEMPLATE(BM_Mult, double, double, double)->Arg(500);
BENCHMARK_TEMPLATE(BM_Check, stan::math::var, stan::math::var, stan::math::var)->Arg(25);
BENCHMARK_TEMPLATE(BM_BobCheck, stan::math::var, stan::math::var, stan::math::var)->Arg(25);
BENCHMARK_TEMPLATE(BM_Mult, stan::math::var, stan::math::var, stan::math::var)->Arg(25);
BENCHMARK_TEMPLATE(BM_Check, stan::math::var, stan::math::var, stan::math::var)->Arg(500);
BENCHMARK_TEMPLATE(BM_BobCheck, stan::math::var, stan::math::var, stan::math::var)->Arg(500);
BENCHMARK_TEMPLATE(BM_Mult, stan::math::var, stan::math::var, stan::math::var)->Arg(500);
BENCHMARK_MAIN();