Skip to content

Commit cd68b81

Browse files
authored
Remove 2nd LevelCache Constructor (#144)
1 parent 6d77e40 commit cd68b81

File tree

5 files changed

+33
-182
lines changed

5 files changed

+33
-182
lines changed

include/Level/level.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ class LevelCache
114114
explicit LevelCache(const PolarGrid& grid, const DensityProfileCoefficients& density_profile_coefficients,
115115
const DomainGeometry& domain_geometry, const bool cache_density_profile_coefficients,
116116
const bool cache_domain_geometry);
117-
explicit LevelCache(const Level& previous_level, const PolarGrid& current_grid);
118117

119118
const DomainGeometry& domainGeometry() const;
120119
const DensityProfileCoefficients& densityProfileCoefficients() const;
@@ -132,18 +131,8 @@ class LevelCache
132131
inline void obtainValues(const int i_r, const int i_theta, const int global_index, double r, double theta,
133132
double& coeff_beta, double& arr, double& att, double& art, double& detDF) const
134133
{
135-
if (cache_density_profile_coefficients_)
136-
coeff_beta = coeff_beta_[global_index];
137-
else
138-
coeff_beta = density_profile_coefficients_.beta(r, theta);
139-
140-
double coeff_alpha;
141-
if (!cache_domain_geometry_) {
142-
if (cache_density_profile_coefficients_)
143-
coeff_alpha = coeff_alpha_[global_index];
144-
else
145-
coeff_alpha = density_profile_coefficients_.alpha(r, theta);
146-
}
134+
coeff_beta = cache_density_profile_coefficients_ ? coeff_beta_[global_index]
135+
: density_profile_coefficients_.beta(r, theta);
147136

148137
if (cache_domain_geometry_) {
149138
arr = arr_[global_index];
@@ -152,6 +141,9 @@ class LevelCache
152141
detDF = detDF_[global_index];
153142
}
154143
else {
144+
double coeff_alpha = cache_density_profile_coefficients_ ? coeff_alpha_[global_index]
145+
: density_profile_coefficients_.alpha(r, theta);
146+
155147
compute_jacobian_elements(domain_geometry_, r, theta, coeff_alpha, arr, att, art, detDF);
156148
}
157149
}
@@ -160,7 +152,7 @@ class LevelCache
160152
const DomainGeometry& domain_geometry_;
161153
const DensityProfileCoefficients& density_profile_coefficients_;
162154

163-
bool cache_density_profile_coefficients_; // cache alpha(r_i), beta(r_i)
155+
bool cache_density_profile_coefficients_; // cache alpha(r, theta), beta(r, theta)
164156
Vector<double> coeff_alpha_;
165157
Vector<double> coeff_beta_;
166158

src/ExtrapolatedSmoother/ExtrapolatedSmootherGive/smootherSolver.cpp

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -826,30 +826,8 @@ void ExtrapolatedSmootherGive::applyAscOrthoCircleSection(const int i_r, const S
826826
const double theta = grid_.theta(i_theta);
827827
const int index = grid_.index(i_r, i_theta);
828828

829-
double coeff_beta;
830-
if (level_cache_.cacheDensityProfileCoefficients())
831-
coeff_beta = level_cache_.coeff_beta()[index];
832-
else
833-
coeff_beta = density_profile_coefficients_.beta(r, theta);
834-
835-
double coeff_alpha;
836-
if (!level_cache_.cacheDomainGeometry()) {
837-
if (level_cache_.cacheDensityProfileCoefficients())
838-
coeff_alpha = level_cache_.coeff_alpha()[index];
839-
else
840-
coeff_alpha = density_profile_coefficients_.alpha(r, theta);
841-
}
842-
843-
double arr, att, art, detDF;
844-
if (level_cache_.cacheDomainGeometry()) {
845-
arr = level_cache_.arr()[index];
846-
att = level_cache_.att()[index];
847-
art = level_cache_.art()[index];
848-
detDF = level_cache_.detDF()[index];
849-
}
850-
else {
851-
compute_jacobian_elements(domain_geometry_, r, theta, coeff_alpha, arr, att, art, detDF);
852-
}
829+
double coeff_beta, arr, att, art, detDF;
830+
level_cache_.obtainValues(i_r, i_theta, index, r, theta, coeff_beta, arr, att, art, detDF);
853831

854832
// Apply Asc Ortho at the current node
855833
NODE_APPLY_ASC_ORTHO_CIRCLE_GIVE(i_r, i_theta, r, theta, grid_, DirBC_Interior_, smoother_color, x, rhs, temp,
@@ -868,30 +846,8 @@ void ExtrapolatedSmootherGive::applyAscOrthoRadialSection(const int i_theta, con
868846
const double r = grid_.radius(i_r);
869847
const int index = grid_.index(i_r, i_theta);
870848

871-
double coeff_beta;
872-
if (level_cache_.cacheDensityProfileCoefficients())
873-
coeff_beta = level_cache_.coeff_beta()[index];
874-
else
875-
coeff_beta = density_profile_coefficients_.beta(r, theta);
876-
877-
double coeff_alpha;
878-
if (!level_cache_.cacheDomainGeometry()) {
879-
if (level_cache_.cacheDensityProfileCoefficients())
880-
coeff_alpha = level_cache_.coeff_alpha()[index];
881-
else
882-
coeff_alpha = density_profile_coefficients_.alpha(r, theta);
883-
}
884-
885-
double arr, att, art, detDF;
886-
if (level_cache_.cacheDomainGeometry()) {
887-
arr = level_cache_.arr()[index];
888-
att = level_cache_.att()[index];
889-
art = level_cache_.art()[index];
890-
detDF = level_cache_.detDF()[index];
891-
}
892-
else {
893-
compute_jacobian_elements(domain_geometry_, r, theta, coeff_alpha, arr, att, art, detDF);
894-
}
849+
double coeff_beta, arr, att, art, detDF;
850+
level_cache_.obtainValues(i_r, i_theta, index, r, theta, coeff_beta, arr, att, art, detDF);
895851

896852
// Apply Asc Ortho at the current node
897853
NODE_APPLY_ASC_ORTHO_RADIAL_GIVE(i_r, i_theta, r, theta, grid_, DirBC_Interior_, smoother_color, x, rhs, temp,

src/GMGPolar/setup.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ void GMGPolar::setup()
6161
levels_.emplace_back(level_depth, std::move(finest_grid), std::move(finest_levelCache), extrapolation_, FMG_);
6262

6363
for (level_depth = 1; level_depth < number_of_levels_; level_depth++) {
64-
auto current_grid = std::make_unique<PolarGrid>(coarseningGrid(levels_[level_depth - 1].grid()));
65-
auto current_levelCache = std::make_unique<LevelCache>(levels_[level_depth - 1], *current_grid);
64+
auto current_grid = std::make_unique<PolarGrid>(coarseningGrid(levels_[level_depth - 1].grid()));
65+
auto current_levelCache =
66+
std::make_unique<LevelCache>(*current_grid, density_profile_coefficients_, domain_geometry_,
67+
cache_density_profile_coefficients_, cache_domain_geometry_);
6668
levels_.emplace_back(level_depth, std::move(current_grid), std::move(current_levelCache), extrapolation_, FMG_);
6769
}
6870

src/Level/levelCache.cpp

Lines changed: 15 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ LevelCache::LevelCache(const PolarGrid& grid, const DensityProfileCoefficients&
1616
, art_("art", cache_domain_geometry ? grid.numberOfNodes() : 0)
1717
, detDF_("detDF", cache_domain_geometry ? grid.numberOfNodes() : 0)
1818
{
19+
// Pre-compute and store alpha/beta coefficients at all grid nodes to avoid
20+
// repeated expensive evaluations during runtime computations
1921
if (cache_density_profile_coefficients_) {
2022
#pragma omp parallel for
2123
for (int i_r = 0; i_r < grid.nr(); i_r++) {
@@ -31,15 +33,20 @@ LevelCache::LevelCache(const PolarGrid& grid, const DensityProfileCoefficients&
3133
}
3234
}
3335

36+
// Pre-compute and store Jacobian matrix elements (arr, att, art, detDF) at all grid nodes
37+
// to avoid repeated coordinate transformation calculations during domain operations
3438
if (cache_domain_geometry_) {
39+
// We split the loops into two regions to better respect the
40+
// access patterns of the smoother and improve cache locality
41+
42+
// Circular Indexing Section
3543
#pragma omp parallel for
3644
for (int i_r = 0; i_r < grid.numberSmootherCircles(); i_r++) {
3745
const double r = grid.radius(i_r);
3846
for (int i_theta = 0; i_theta < grid.ntheta(); i_theta++) {
39-
const double theta = grid.theta(i_theta);
40-
const int index = grid.index(i_r, i_theta);
41-
42-
double coeff_alpha = density_profile_coefficients.alpha(r, theta);
47+
const double theta = grid.theta(i_theta);
48+
const int index = grid.index(i_r, i_theta);
49+
const double coeff_alpha = density_profile_coefficients.alpha(r, theta);
4350

4451
double arr, att, art, detDF;
4552
compute_jacobian_elements(domain_geometry_, r, theta, coeff_alpha, arr, att, art, detDF);
@@ -49,21 +56,14 @@ LevelCache::LevelCache(const PolarGrid& grid, const DensityProfileCoefficients&
4956
art_(index) = art;
5057
}
5158
}
52-
59+
// Radial Indexing Section
5360
#pragma omp parallel for
5461
for (int i_theta = 0; i_theta < grid.ntheta(); i_theta++) {
5562
const double theta = grid.theta(i_theta);
5663
for (int i_r = grid.numberSmootherCircles(); i_r < grid.nr(); i_r++) {
57-
const double r = grid.radius(i_r);
58-
const int index = grid.index(i_r, i_theta);
59-
60-
double coeff_alpha;
61-
if (cache_density_profile_coefficients_ && !cache_domain_geometry_) {
62-
coeff_alpha = coeff_alpha_(index);
63-
}
64-
else {
65-
coeff_alpha = density_profile_coefficients.alpha(r, theta);
66-
}
64+
const double r = grid.radius(i_r);
65+
const int index = grid.index(i_r, i_theta);
66+
const double coeff_alpha = density_profile_coefficients.alpha(r, theta);
6767

6868
double arr, att, art, detDF;
6969
compute_jacobian_elements(domain_geometry_, r, theta, coeff_alpha, arr, att, art, detDF);
@@ -76,62 +76,6 @@ LevelCache::LevelCache(const PolarGrid& grid, const DensityProfileCoefficients&
7676
}
7777
}
7878

79-
LevelCache::LevelCache(const Level& previous_level, const PolarGrid& current_grid)
80-
: domain_geometry_(previous_level.levelCache().domainGeometry())
81-
, density_profile_coefficients_(previous_level.levelCache().densityProfileCoefficients())
82-
, cache_density_profile_coefficients_(previous_level.levelCache().cacheDensityProfileCoefficients())
83-
, coeff_alpha_("coeff_alpha",
84-
previous_level.levelCache().coeff_alpha().size() > 0 ? current_grid.numberOfNodes() : 0)
85-
, coeff_beta_("coeff_beta", previous_level.levelCache().coeff_beta().size() > 0 ? current_grid.numberOfNodes() : 0)
86-
, cache_domain_geometry_(previous_level.levelCache().cacheDomainGeometry())
87-
, arr_("arr", previous_level.levelCache().arr().size() > 0 ? current_grid.numberOfNodes() : 0)
88-
, att_("att", previous_level.levelCache().att().size() > 0 ? current_grid.numberOfNodes() : 0)
89-
, art_("art", previous_level.levelCache().art().size() > 0 ? current_grid.numberOfNodes() : 0)
90-
, detDF_("detDF", previous_level.levelCache().detDF().size() > 0 ? current_grid.numberOfNodes() : 0)
91-
{
92-
const auto& previous_level_cache = previous_level.levelCache();
93-
94-
if (previous_level_cache.cacheDensityProfileCoefficients()) {
95-
#pragma omp parallel for
96-
for (int i_r = 0; i_r < current_grid.nr(); i_r++) {
97-
for (int i_theta = 0; i_theta < current_grid.ntheta(); i_theta++) {
98-
const int current_index = current_grid.index(i_r, i_theta);
99-
const int previous_index = previous_level.grid().index(2 * i_r, 2 * i_theta);
100-
101-
if (!previous_level_cache.cacheDomainGeometry()) {
102-
coeff_alpha_[current_index] = previous_level_cache.coeff_alpha()[previous_index];
103-
}
104-
coeff_beta_[current_index] = previous_level_cache.coeff_beta()[previous_index];
105-
}
106-
}
107-
}
108-
109-
if (previous_level_cache.cacheDomainGeometry()) {
110-
#pragma omp parallel for
111-
for (int i_r = 0; i_r < current_grid.numberSmootherCircles(); i_r++) {
112-
for (int i_theta = 0; i_theta < current_grid.ntheta(); i_theta++) {
113-
const int current_index = current_grid.index(i_r, i_theta);
114-
const int previous_index = previous_level.grid().index(2 * i_r, 2 * i_theta);
115-
arr_[current_index] = previous_level_cache.arr()[previous_index];
116-
att_[current_index] = previous_level_cache.att()[previous_index];
117-
art_[current_index] = previous_level_cache.art()[previous_index];
118-
detDF_[current_index] = previous_level_cache.detDF()[previous_index];
119-
}
120-
}
121-
#pragma omp parallel for
122-
for (int i_theta = 0; i_theta < current_grid.ntheta(); i_theta++) {
123-
for (int i_r = current_grid.numberSmootherCircles(); i_r < current_grid.nr(); i_r++) {
124-
const int current_index = current_grid.index(i_r, i_theta);
125-
const int previous_index = previous_level.grid().index(2 * i_r, 2 * i_theta);
126-
arr_[current_index] = previous_level_cache.arr()[previous_index];
127-
att_[current_index] = previous_level_cache.att()[previous_index];
128-
art_[current_index] = previous_level_cache.art()[previous_index];
129-
detDF_[current_index] = previous_level_cache.detDF()[previous_index];
130-
}
131-
}
132-
}
133-
}
134-
13579
const DensityProfileCoefficients& LevelCache::densityProfileCoefficients() const
13680
{
13781
return density_profile_coefficients_;

src/Smoother/SmootherGive/smootherSolver.cpp

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -334,30 +334,8 @@ void SmootherGive::applyAscOrthoCircleSection(const int i_r, const SmootherColor
334334
const double theta = grid_.theta(i_theta);
335335
const int index = grid_.index(i_r, i_theta);
336336

337-
double coeff_beta;
338-
if (level_cache_.cacheDensityProfileCoefficients())
339-
coeff_beta = level_cache_.coeff_beta()[index];
340-
else
341-
coeff_beta = density_profile_coefficients_.beta(r, theta);
342-
343-
double coeff_alpha;
344-
if (!level_cache_.cacheDomainGeometry()) {
345-
if (level_cache_.cacheDensityProfileCoefficients())
346-
coeff_alpha = level_cache_.coeff_alpha()[index];
347-
else
348-
coeff_alpha = density_profile_coefficients_.alpha(r, theta);
349-
}
350-
351-
double arr, att, art, detDF;
352-
if (level_cache_.cacheDomainGeometry()) {
353-
arr = level_cache_.arr()[index];
354-
att = level_cache_.att()[index];
355-
art = level_cache_.art()[index];
356-
detDF = level_cache_.detDF()[index];
357-
}
358-
else {
359-
compute_jacobian_elements(domain_geometry_, r, theta, coeff_alpha, arr, att, art, detDF);
360-
}
337+
double coeff_beta, arr, att, art, detDF;
338+
level_cache_.obtainValues(i_r, i_theta, index, r, theta, coeff_beta, arr, att, art, detDF);
361339

362340
// Apply Asc Ortho at the current node
363341
NODE_APPLY_ASC_ORTHO_CIRCLE_GIVE(i_r, i_theta, r, theta, grid_, DirBC_Interior_, smoother_color, x, rhs, temp,
@@ -375,30 +353,9 @@ void SmootherGive::applyAscOrthoRadialSection(const int i_theta, const SmootherC
375353
const double r = grid_.radius(i_r);
376354
const int index = grid_.index(i_r, i_theta);
377355

378-
double coeff_beta;
379-
if (level_cache_.cacheDensityProfileCoefficients())
380-
coeff_beta = level_cache_.coeff_beta()[index];
381-
else
382-
coeff_beta = density_profile_coefficients_.beta(r, theta);
383-
384-
double coeff_alpha;
385-
if (!level_cache_.cacheDomainGeometry()) {
386-
if (level_cache_.cacheDensityProfileCoefficients())
387-
coeff_alpha = level_cache_.coeff_alpha()[index];
388-
else
389-
coeff_alpha = density_profile_coefficients_.alpha(r, theta);
390-
}
356+
double coeff_beta, arr, att, art, detDF;
357+
level_cache_.obtainValues(i_r, i_theta, index, r, theta, coeff_beta, arr, att, art, detDF);
391358

392-
double arr, att, art, detDF;
393-
if (level_cache_.cacheDomainGeometry()) {
394-
arr = level_cache_.arr()[index];
395-
att = level_cache_.att()[index];
396-
art = level_cache_.art()[index];
397-
detDF = level_cache_.detDF()[index];
398-
}
399-
else {
400-
compute_jacobian_elements(domain_geometry_, r, theta, coeff_alpha, arr, att, art, detDF);
401-
}
402359
// Apply Asc Ortho at the current node
403360
NODE_APPLY_ASC_ORTHO_RADIAL_GIVE(i_r, i_theta, r, theta, grid_, DirBC_Interior_, smoother_color, x, rhs, temp,
404361
arr, att, art, detDF, coeff_beta);

0 commit comments

Comments
 (0)