Skip to content

Commit cd745f3

Browse files
committed
Add cached exp to benchmark
1 parent 8b4780b commit cd745f3

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed

cpp/bench/exps.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
#include "CachedExponentials.hpp"
12
#include "Exponentials.hpp"
23

34
#include "benchmark-util.hpp"
45
#include <iostream>
56

67
using namespace ahr;
78
using namespace ahr::exp;
8-
template <typename Exp> static void BM_ExpKXKY(benchmark::State &state) {
9+
template <space_like Exp> static void BM_ExpKXKY(benchmark::State &state) {
910
Dim const X = state.range(0);
1011
Dim const Y = state.range(1);
1112

@@ -21,7 +22,7 @@ template <typename Exp> static void BM_ExpKXKY(benchmark::State &state) {
2122
}
2223
}
2324

24-
template <typename Exp> static void BM_ExpM(benchmark::State &state) {
25+
template <moment_like Exp> static void BM_ExpM(benchmark::State &state) {
2526
Dim const M = state.range(0);
2627
Dim const X = state.range(1);
2728
Dim const Y = state.range(2);
@@ -52,3 +53,16 @@ BENCHMARK_WMIN(BM_ExpKXKY<NuG>)
5253
BENCHMARK_WMIN(BM_ExpM<GM>)
5354
->ArgsProduct({{2, 4, 10}, {2048, 4096}, {2048, 4096}})
5455
->Unit(benchmark::kMillisecond);
56+
57+
BENCHMARK_WMIN(BM_ExpKXKY<CachedKXKY<Eta>>)
58+
->ArgsProduct({{2048, 4096, 8192}, {2048, 4096, 8192}})
59+
->Unit(benchmark::kMillisecond);
60+
BENCHMARK_WMIN(BM_ExpKXKY<CachedKXKY<Nu>>)
61+
->ArgsProduct({{2048, 4096, 8192}, {2048, 4096, 8192}})
62+
->Unit(benchmark::kMillisecond);
63+
BENCHMARK_WMIN(BM_ExpKXKY<CachedKXKY<NuG>>)
64+
->ArgsProduct({{2048, 4096, 8192}, {2048, 4096, 8192}})
65+
->Unit(benchmark::kMillisecond);
66+
BENCHMARK_WMIN(BM_ExpM<CachedM<GM>>)
67+
->ArgsProduct({{2, 4, 10}, {2048, 4096}, {2048, 4096}})
68+
->Unit(benchmark::kMillisecond);

cpp/lib/CachedExponentials.hpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#pragma once
2+
3+
#include "Exponentials.hpp"
4+
5+
namespace ahr::exp {
6+
7+
template <typename Exp>
8+
concept space_like = requires(Exp exp, Dim kx, Dim ky) {
9+
{ exp(kx, ky) } -> std::same_as<Real>;
10+
};
11+
12+
template <typename Exp>
13+
concept moment_like = requires(Exp exp, Dim m) {
14+
{ exp(m) } -> std::same_as<Real>;
15+
};
16+
17+
template <typename Exp>
18+
concept updatable = requires(Exp exp, Real dt) {
19+
{ exp.update(HyperCoefficients{}, dt) };
20+
};
21+
22+
template <updatable Exp>
23+
requires space_like<Exp>
24+
struct CachedKXKY : Exp {
25+
explicit CachedKXKY(Grid const &grid) : Exp(grid), factors(grid.KX, grid.KY) {}
26+
27+
void update(HyperCoefficients hyper, Real dt) {
28+
Exp::update(hyper, dt);
29+
this->grid.for_each_kxky([&](Dim kx, Dim ky) { factors(kx, ky) = Exp::operator()(kx, ky); });
30+
}
31+
32+
Real operator()(Dim kx, Dim ky) { return factors(kx, ky); }
33+
34+
protected:
35+
Grid::Buf::R_XY factors; ///< Real but of size (KX,KY)
36+
};
37+
38+
template <updatable Exp>
39+
requires moment_like<Exp>
40+
struct CachedM : Exp {
41+
explicit CachedM(Grid const &grid) : Exp(grid), factors(grid.M) {}
42+
43+
void update(HyperCoefficients hyper, Real dt) {
44+
Exp::update(hyper, dt);
45+
for (Dim m = 0; m < this->grid.M; ++m) {
46+
factors[m] = Exp::operator()(m);
47+
}
48+
}
49+
50+
Real operator()(Dim m) { return factors[m]; }
51+
52+
protected:
53+
std::vector<Real> factors;
54+
};
55+
} // namespace ahr::exp

cpp/lib/Exponentials.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Base {
2020
Real dt{-1};
2121
};
2222

23-
class Eta : Base {
23+
class Eta : protected Base {
2424
public:
2525
using Base::Base;
2626
using Base::update;
@@ -32,7 +32,7 @@ class Eta : Base {
3232
}
3333
};
3434

35-
class Nu : Base {
35+
class Nu : protected Base {
3636
public:
3737
using Base::Base;
3838
using Base::update;
@@ -43,7 +43,7 @@ class Nu : Base {
4343
}
4444
};
4545

46-
class NuG : Base {
46+
class NuG : protected Base {
4747
public:
4848
using Base::Base;
4949
using Base::update;
@@ -54,7 +54,7 @@ class NuG : Base {
5454
}
5555
};
5656

57-
class GM : Base {
57+
class GM : protected Base {
5858
public:
5959
using Base::Base;
6060
using Base::update;

0 commit comments

Comments
 (0)