Skip to content

Commit 25d2b56

Browse files
authored
refactor: improve StencilIterator and the schemes are not const anymore (#263)
## Description - A `StencilAnalyzer` class has been added to gather information about the stencil. This way, the analysis of the stencil is done only once instead of at every interval, thus improving the performance of the `StencilIterator`. - The scheme object is no longer `const`, so that temporary variables can be stored in the class and reused upon application of the scheme. This change prepares the possibility of applying a scheme by batches. ## Code of Conduct By submitting this PR, you agree to follow our [Code of Conduct](https://github.com/hpc-maths/samurai/blob/master/docs/CODE_OF_CONDUCT.md) - [x] I agree to follow this project's Code of Conduct
1 parent 2000714 commit 25d2b56

28 files changed

+445
-312
lines changed

include/samurai/bc.hpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -816,8 +816,11 @@ namespace samurai
816816
// BC Types //
817817
//////////////
818818
template <class Field, class Subset, std::size_t stencil_size, class Vector>
819-
void
820-
__apply_bc_on_subset(Bc<Field>& bc, Field& field, Subset& subset, const Stencil<stencil_size, Field::dim>& stencil, const Vector& direction)
819+
void __apply_bc_on_subset(Bc<Field>& bc,
820+
Field& field,
821+
Subset& subset,
822+
const StencilAnalyzer<stencil_size, Field::dim>& stencil,
823+
const Vector& direction)
821824
{
822825
auto apply_bc = bc.get_apply_function(std::integral_constant<std::size_t, stencil_size>(), direction);
823826
if (bc.get_value_type() == BCVType::constant)
@@ -833,14 +836,13 @@ namespace samurai
833836
}
834837
else if (bc.get_value_type() == BCVType::function)
835838
{
836-
int origin_index = find_stencil_origin(stencil);
837-
assert(origin_index >= 0);
839+
assert(stencil.has_origin);
838840
for_each_stencil(field.mesh(),
839841
subset,
840842
stencil,
841-
[&, origin_index](auto& cells)
843+
[&](auto& cells)
842844
{
843-
auto& cell_in = cells[static_cast<std::size_t>(origin_index)];
845+
auto& cell_in = cells[stencil.origin_index];
844846
auto face_coords = cell_in.face_center(direction);
845847
auto value = bc.value(direction, cell_in, face_coords);
846848
apply_bc(field, cells, value);
@@ -884,13 +886,14 @@ namespace samurai
884886

885887
if (is_cartesian_direction)
886888
{
887-
auto stencil = convert_for_direction(stencil_0, direction[d]);
889+
auto stencil = convert_for_direction(stencil_0, direction[d]);
890+
auto stencil_analyzer = make_stencil_analyzer(stencil);
888891

889892
// 1. Inner cells in the boundary region
890893
auto bdry_cells = intersection(mesh[mesh_id_t::cells][level], lca[d]).on(level);
891894
if (level >= mesh.min_level()) // otherwise there is no cells
892895
{
893-
__apply_bc_on_subset(bc, field, bdry_cells, stencil, direction[d]);
896+
__apply_bc_on_subset(bc, field, bdry_cells, stencil_analyzer, direction[d]);
894897
}
895898

896899
// 2. Inner ghosts in the boundary region that have a neighbouring ghost outside the domain
@@ -901,7 +904,7 @@ namespace samurai
901904
auto inner_cells_and_ghosts = intersection(potential_inner_cells_and_ghosts, mesh[mesh_id_t::reference][level]).on(level);
902905
auto inner_ghosts_with_outer_nghbr = difference(inner_cells_and_ghosts, bdry_cells).on(level);
903906

904-
__apply_bc_on_subset(bc, field, inner_ghosts_with_outer_nghbr, stencil, direction[d]);
907+
__apply_bc_on_subset(bc, field, inner_ghosts_with_outer_nghbr, stencil_analyzer, direction[d]);
905908
}
906909
}
907910
}
@@ -920,8 +923,9 @@ namespace samurai
920923

921924
auto& mesh = field.mesh();
922925

923-
auto stencil_0 = bc.get_stencil(std::integral_constant<std::size_t, stencil_size>());
924-
auto stencil = convert_for_direction(stencil_0, direction);
926+
auto stencil_0 = bc.get_stencil(std::integral_constant<std::size_t, stencil_size>());
927+
auto stencil = convert_for_direction(stencil_0, direction);
928+
auto stencil_analyzer = make_stencil_analyzer(stencil);
925929

926930
// 1. Inner cells in the boundary region
927931
if (!only_fill_ghost_neighbours && level >= mesh.min_level())
@@ -931,7 +935,7 @@ namespace samurai
931935
auto translated_outer_nghbr = translate(mesh[mesh_id_t::reference][level], -(stencil_size / 2) * direction);
932936
auto cells = intersection(translated_outer_nghbr, bdry_cells).on(level);
933937

934-
__apply_bc_on_subset(bc, field, cells, stencil, direction);
938+
__apply_bc_on_subset(bc, field, cells, stencil_analyzer, direction);
935939
}
936940

937941
// 2. Inner ghosts in the boundary region that have a neighbouring ghost outside the domain
@@ -942,7 +946,7 @@ namespace samurai
942946
auto inner_cells_and_ghosts = intersection(potential_inner_cells_and_ghosts, mesh[mesh_id_t::reference][level]).on(level);
943947
auto inner_ghosts_with_outer_nghbr = difference(inner_cells_and_ghosts, bdry_cells).on(level);
944948

945-
__apply_bc_on_subset(bc, field, inner_ghosts_with_outer_nghbr, stencil, direction);
949+
__apply_bc_on_subset(bc, field, inner_ghosts_with_outer_nghbr, stencil_analyzer, direction);
946950
}
947951
}
948952

include/samurai/boundary.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace samurai
2020
template <class Mesh, class Subset, std::size_t stencil_size, class GetCoeffsFunc, class Func>
2121
void for_each_stencil_on_boundary(const Mesh& mesh,
2222
const Subset& boundary_region,
23-
const Stencil<stencil_size, Mesh::dim>& stencil,
23+
const StencilAnalyzer<stencil_size, Mesh::dim>& stencil,
2424
GetCoeffsFunc&& get_coefficients,
2525
Func&& func)
2626
{
@@ -44,7 +44,7 @@ namespace samurai
4444
template <class Mesh, class Subset, std::size_t stencil_size, class Equation, std::size_t nb_equations, class Func>
4545
void for_each_stencil_on_boundary(const Mesh& mesh,
4646
const Subset& boundary_region,
47-
const Stencil<stencil_size, Mesh::dim>& stencil,
47+
const StencilAnalyzer<stencil_size, Mesh::dim>& stencil,
4848
std::array<Equation, nb_equations> equations,
4949
Func&& func)
5050
{

0 commit comments

Comments
 (0)