Skip to content

Commit 2798877

Browse files
committed
Add update and M scaling for benchmarks, save dt in cached
1 parent cd745f3 commit 2798877

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

cpp/bench/exps.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@
77
using namespace ahr;
88
using namespace ahr::exp;
99
template <space_like Exp> static void BM_ExpKXKY(benchmark::State &state) {
10-
Dim const X = state.range(0);
11-
Dim const Y = state.range(1);
10+
Dim const M = state.range(0);
11+
Dim const X = state.range(1);
12+
Dim const Y = state.range(2);
1213

13-
Grid grid{1, X, Y};
14+
Grid grid{M, X, Y};
1415
Real dt = 1.0;
1516
HyperCoefficients hyper = HyperCoefficients::calculate(dt, grid);
16-
1717
Exp exp{grid};
18-
exp.update(hyper, dt);
1918

2019
for (auto _ : state) {
21-
grid.for_each_kxky([&](Dim kx, Dim ky) { benchmark::DoNotOptimize(exp(kx, ky)); });
20+
dt *= 1.2;
21+
exp.update(hyper, dt);
22+
for (int m = 0; m < M; ++m) {
23+
grid.for_each_kxky([&](Dim kx, Dim ky) { benchmark::DoNotOptimize(exp(kx, ky)); });
24+
}
2225
}
2326
}
2427

@@ -32,37 +35,38 @@ template <moment_like Exp> static void BM_ExpM(benchmark::State &state) {
3235
HyperCoefficients hyper = HyperCoefficients::calculate(dt, grid);
3336

3437
Exp exp{grid};
35-
exp.update(hyper, dt);
3638

3739
for (auto _ : state) {
40+
dt *= 1.2;
41+
exp.update(hyper, dt);
3842
for (Dim m = 0; m < M; ++m) {
3943
grid.for_each_kxky([&](Dim kx, Dim ky) { benchmark::DoNotOptimize(exp(m)); });
4044
}
4145
}
4246
}
4347

4448
BENCHMARK_WMIN(BM_ExpKXKY<Eta>)
45-
->ArgsProduct({{2048, 4096, 8192}, {2048, 4096, 8192}})
49+
->ArgsProduct({{2, 4, 16}, {2048, 4096}, {2048, 4096}})
4650
->Unit(benchmark::kMillisecond);
4751
BENCHMARK_WMIN(BM_ExpKXKY<Nu>)
48-
->ArgsProduct({{2048, 4096, 8192}, {2048, 4096, 8192}})
52+
->ArgsProduct({{2, 4, 16}, {2048, 4096}, {2048, 4096}})
4953
->Unit(benchmark::kMillisecond);
5054
BENCHMARK_WMIN(BM_ExpKXKY<NuG>)
51-
->ArgsProduct({{2048, 4096, 8192}, {2048, 4096, 8192}})
55+
->ArgsProduct({{2, 4, 16}, {2048, 4096}, {2048, 4096}})
5256
->Unit(benchmark::kMillisecond);
5357
BENCHMARK_WMIN(BM_ExpM<GM>)
54-
->ArgsProduct({{2, 4, 10}, {2048, 4096}, {2048, 4096}})
58+
->ArgsProduct({{4, 8, 16}, {2048, 4096}, {2048, 4096}})
5559
->Unit(benchmark::kMillisecond);
5660

5761
BENCHMARK_WMIN(BM_ExpKXKY<CachedKXKY<Eta>>)
58-
->ArgsProduct({{2048, 4096, 8192}, {2048, 4096, 8192}})
62+
->ArgsProduct({{2, 4, 16}, {2048, 4096}, {2048, 4096}})
5963
->Unit(benchmark::kMillisecond);
6064
BENCHMARK_WMIN(BM_ExpKXKY<CachedKXKY<Nu>>)
61-
->ArgsProduct({{2048, 4096, 8192}, {2048, 4096, 8192}})
65+
->ArgsProduct({{2, 4, 16}, {2048, 4096}, {2048, 4096}})
6266
->Unit(benchmark::kMillisecond);
6367
BENCHMARK_WMIN(BM_ExpKXKY<CachedKXKY<NuG>>)
64-
->ArgsProduct({{2048, 4096, 8192}, {2048, 4096, 8192}})
68+
->ArgsProduct({{2, 4, 16}, {2048, 4096}, {2048, 4096}})
6569
->Unit(benchmark::kMillisecond);
6670
BENCHMARK_WMIN(BM_ExpM<CachedM<GM>>)
67-
->ArgsProduct({{2, 4, 10}, {2048, 4096}, {2048, 4096}})
71+
->ArgsProduct({{4, 8, 16}, {2048, 4096}, {2048, 4096}})
6872
->Unit(benchmark::kMillisecond);

cpp/lib/CachedExponentials.hpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ concept updatable = requires(Exp exp, Real dt) {
2222
template <updatable Exp>
2323
requires space_like<Exp>
2424
struct CachedKXKY : Exp {
25-
explicit CachedKXKY(Grid const &grid) : Exp(grid), factors(grid.KX, grid.KY) {}
25+
explicit CachedKXKY(Grid const &grid) : Exp(grid), factors(grid.KX, grid.KY), dt(-1) {}
2626

2727
void update(HyperCoefficients hyper, Real dt) {
28+
// skip update if dt hasn't changed
29+
if (this->dt == dt) { return; }
30+
this->dt = dt;
31+
2832
Exp::update(hyper, dt);
2933
this->grid.for_each_kxky([&](Dim kx, Dim ky) { factors(kx, ky) = Exp::operator()(kx, ky); });
3034
}
@@ -33,14 +37,19 @@ struct CachedKXKY : Exp {
3337

3438
protected:
3539
Grid::Buf::R_XY factors; ///< Real but of size (KX,KY)
40+
Real dt;
3641
};
3742

3843
template <updatable Exp>
3944
requires moment_like<Exp>
4045
struct CachedM : Exp {
41-
explicit CachedM(Grid const &grid) : Exp(grid), factors(grid.M) {}
46+
explicit CachedM(Grid const &grid) : Exp(grid), factors(grid.M), dt(-1) {}
4247

4348
void update(HyperCoefficients hyper, Real dt) {
49+
// skip update if dt hasn't changed
50+
if (this->dt == dt) { return; }
51+
this->dt = dt;
52+
4453
Exp::update(hyper, dt);
4554
for (Dim m = 0; m < this->grid.M; ++m) {
4655
factors[m] = Exp::operator()(m);
@@ -51,5 +60,6 @@ struct CachedM : Exp {
5160

5261
protected:
5362
std::vector<Real> factors;
63+
Real dt;
5464
};
5565
} // namespace ahr::exp

0 commit comments

Comments
 (0)