Skip to content

Commit 75a442e

Browse files
Merge pull request #1351 from PrincetonUniversity/issue-1350
Fix bug in coupled interfaces
2 parents 51be740 + 3b074d7 commit 75a442e

File tree

19 files changed

+148
-69
lines changed

19 files changed

+148
-69
lines changed

core/specfem/assembly/edge_types/dim2/edge_types.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ specfem::assembly::edge_types<specfem::dimension::type::dim2>::edge_types(
7272
std::vector<specfem::mesh_entity::edge<dimension_tag> >
7373
coupled_collect;
7474

75+
int edge_index = 0;
7576
for (const auto &edge :
7677
boost::make_iterator_range(boost::edges(nc_graph))) {
7778
const int ispec1 = boost::source(edge, nc_graph);
@@ -95,8 +96,11 @@ specfem::assembly::edge_types<specfem::dimension::type::dim2>::edge_types(
9596
count++;
9697
// we do not need orientation flipping -- that's handled by
9798
// the transfer function
98-
self_collect.push_back({ ispec1, self_orientation, false });
99-
coupled_collect.push_back({ ispec2, coupled_orientation, false });
99+
self_collect.push_back(
100+
{ ispec1, edge_index, self_orientation, false });
101+
coupled_collect.push_back(
102+
{ ispec2, edge_index, coupled_orientation, false });
103+
edge_index++;
100104
}
101105
}
102106

@@ -137,6 +141,7 @@ specfem::assembly::edge_types<specfem::dimension::type::dim2>::edge_types(
137141
coupled_interfaces.template get<self_medium, coupled_medium>();
138142
const int nedges =
139143
interface_container.num_interfaces; // number of edges
144+
int edge_index = 0;
140145
for (int iedge = 0; iedge < nedges; ++iedge) {
141146
const int ispec1_mesh =
142147
interface_container.medium1_index_mapping(iedge);
@@ -152,13 +157,14 @@ specfem::assembly::edge_types<specfem::dimension::type::dim2>::edge_types(
152157
connection_mapping.flip_orientation(edge1, edge2);
153158
_h_self_edges_(index) =
154159
specfem::mesh_entity::edge<specfem::dimension::type::dim2>{
155-
ispec1, edge1, false
160+
ispec1, edge_index, edge1, false
156161
};
157162
_h_coupled_edges_(index) =
158163
specfem::mesh_entity::edge<specfem::dimension::type::dim2>{
159-
ispec2, edge2, flip
164+
ispec2, edge_index, edge2, flip
160165
};
161166
index++;
167+
edge_index++;
162168
}
163169
}
164170
Kokkos::deep_copy(_self_edges_, _h_self_edges_);

core/specfem/timescheme/newmark.tpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ int corrector_phase_impl(
4141

4242
specfem::execution::for_all(
4343
"specfem::TimeScheme::Newmark::corrector_phase_impl", range,
44-
KOKKOS_LAMBDA(const IndexType &index) {
44+
KOKKOS_LAMBDA(const typename decltype(range)::base_index_type &iterator_index) {
45+
const auto index = iterator_index.get_index();
4546
PointAccelerationType acceleration;
4647
PointVelocityType velocity;
4748

@@ -99,7 +100,8 @@ int predictor_phase_impl(
99100

100101
specfem::execution::for_all(
101102
"specfem::TimeScheme::Newmark::corrector_phase_impl", range,
102-
KOKKOS_LAMBDA(const IndexType &index) {
103+
KOKKOS_LAMBDA(const typename decltype(range)::base_index_type &iterator_index) {
104+
const auto index = iterator_index.get_index();
103105
PointDisplacementType displacement;
104106
PointVelocityType velocity;
105107
PointAccelerationType acceleration;

include/enumerations/dim2/mesh_entities.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,14 @@ template <typename T> bool contains(const T &list, const dim2::type &value) {
142142
template <> struct edge<specfem::dimension::type::dim2> {
143143
specfem::mesh_entity::dim2::type edge_type;
144144
int ispec;
145+
int iedge;
145146
bool reverse_orientation;
146147

147148
KOKKOS_INLINE_FUNCTION
148-
edge(const int ispec, const specfem::mesh_entity::dim2::type edge_type,
149+
edge(const int ispec, const int iedge,
150+
const specfem::mesh_entity::dim2::type edge_type,
149151
const bool reverse_orientation = false)
150-
: edge_type(edge_type), ispec(ispec),
152+
: edge_type(edge_type), ispec(ispec), iedge(iedge),
151153
reverse_orientation(reverse_orientation) {}
152154

153155
KOKKOS_INLINE_FUNCTION

include/execution/chunked_domain_iterator.hpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -501,15 +501,14 @@ class ChunkedDomainIterator : public TeamPolicy<ParallelConfig> {
501501
///< specfem::execution::for_each_level
502502
using execution_space =
503503
typename base_type::execution_space; ///< Execution space type.
504-
using base_index_type = specfem::point::index<
505-
ParallelConfig::dimension,
506-
ParallelConfig::simd::using_simd>; ///< Index type
507-
///< to be used
508-
///< when calling
509-
///< @ref
510-
///< specfem::execution::for_all
511-
///< with this
512-
///< iterator.
504+
using base_index_type = PointIndex<
505+
dimension_tag, typename ViewType::value_type,
506+
ParallelConfig::simd::using_simd,
507+
typename base_type::execution_space>; ///< Index type to be
508+
///< used when calling
509+
///< @ref
510+
///< specfem::execution::for_all
511+
///< with this iterator.
513512

514513
/**
515514
* @brief Construct a new Chunked Domain Iterator object

include/execution/chunked_edge_iterator.hpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
* // Create chunked iterator and process edges
3434
* specfem::execution::ChunkedEdgeIterator iterator(ParallelConfig(), edges,
3535
* num_points); specfem::execution::for_all("process_edges", iterator,
36-
* KOKKOS_LAMBDA(const auto& index) {
36+
* KOKKOS_LAMBDA(const auto& iterator_index) {
37+
* const auto index = iterator_index.get_index();
3738
* // Access edge point data
3839
* int ispec = index.ispec; // Element index
3940
* int ipoint = index.ipoint; // Point index along edge
@@ -80,7 +81,8 @@ namespace specfem::execution {
8081
* @code{.cpp}
8182
* // Typically used within chunked edge iterator lambdas
8283
* specfem::execution::for_all("process_edges", iterator,
83-
* KOKKOS_LAMBDA(const EdgePointIndex<dim2, int, ExecutionSpace>& index) {
84+
* KOKKOS_LAMBDA(const auto & iterator_index) {
85+
* const auto index = iterator_index.get_index();
8486
* int element_id = index.ispec; // Element containing this edge point
8587
* int z_coord = index.iz; // Local z-coordinate within element
8688
* int x_coord = index.ix; // Local x-coordinate within element
@@ -121,6 +123,11 @@ class EdgePointIndex {
121123
KOKKOS_INLINE_FUNCTION
122124
constexpr const index_type &get_index() const { return this->index; }
123125

126+
KOKKOS_INLINE_FUNCTION
127+
constexpr const index_type &get_local_index() const {
128+
return this->local_index; ///< Returns the local point index
129+
}
130+
124131
/**
125132
* @brief Constructor for EdgePointIndex
126133
*
@@ -130,8 +137,10 @@ class EdgePointIndex {
130137
*/
131138
KOKKOS_INLINE_FUNCTION
132139
EdgePointIndex(const specfem::point::index<DimensionTag, false> &index,
133-
const int ipoint, const KokkosIndexType &kokkos_index)
134-
: index(index.ispec, kokkos_index, ipoint, index.iz, index.ix),
140+
const int gedge, const int iedge, const int ipoint,
141+
const KokkosIndexType &kokkos_index)
142+
: index(index.ispec, gedge, ipoint, index.iz, index.ix),
143+
local_index(index.ispec, iedge, ipoint, index.iz, index.ix),
135144
kokkos_index(kokkos_index) {}
136145

137146
/**
@@ -144,7 +153,8 @@ class EdgePointIndex {
144153
constexpr const iterator_type get_iterator() const { return iterator_type{}; }
145154

146155
private:
147-
index_type index; ///< Local element coordinates of the edge point
156+
index_type index; ///< Local element coordinates of the edge point
157+
index_type local_index; ///< Local element coordinates relative to chunk
148158

149159
KokkosIndexType kokkos_index; ///< Kokkos index type
150160
};
@@ -201,6 +211,7 @@ class ChunkEdgeIterator : public TeamThreadRangePolicy<TeamMemberType, int> {
201211
const int iedge = i % nedges;
202212
const int ipoint = i / nedges;
203213
const int ispec = edges(iedge).ispec;
214+
const int gedge = edges(iedge).iedge;
204215
const specfem::mesh_entity::dim2::type edge_type = edges(iedge).edge_type;
205216
const auto index =
206217
(edges(iedge).reverse_orientation)
@@ -209,7 +220,7 @@ class ChunkEdgeIterator : public TeamThreadRangePolicy<TeamMemberType, int> {
209220
: ChunkEdgeIterator::compute_index(ispec, ipoint, num_points,
210221
edge_type);
211222

212-
return index_type{ index, ipoint, iedge };
223+
return index_type{ index, gedge, iedge, ipoint, i };
213224
}
214225

215226
/**
@@ -463,10 +474,15 @@ class ChunkedEdgeIterator : public TeamPolicy<ParallelConfig> {
463474
///< specfem::execution::for_each_level
464475
using execution_space =
465476
typename base_type::execution_space; ///< Execution space type.
466-
using base_index_type = specfem::point::edge_index<
467-
ParallelConfig::dimension>; ///< Index type to be used when calling
468-
///< @ref specfem::execution::for_all
469-
///< with this iterator.
477+
using base_index_type = EdgePointIndex<
478+
ParallelConfig::dimension, int,
479+
typename base_type::execution_space>; ///< Index type
480+
///< to be used
481+
///< when calling
482+
///< @ref
483+
///< specfem::execution::for_all
484+
///< with this
485+
///< iterator.
470486

471487
/**
472488
* @brief Constructor with explicit edge view and point count

include/execution/chunked_intersection_iterator.hpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ class InterfacePointIndex {
129129
///< the interface
130130
}
131131

132+
KOKKOS_INLINE_FUNCTION
133+
constexpr const index_type &get_local_index() const {
134+
return this->local_index; ///< Returns the local point index
135+
}
136+
132137
/**
133138
* @brief Constructor for InterfacePointIndex
134139
*
@@ -158,6 +163,7 @@ class InterfacePointIndex {
158163

159164
private:
160165
index_type index; ///< Index of the GLL point on the interface
166+
index_type local_index; ///< Index of the GLL point relative to chunk
161167
KokkosIndexType kokkos_index; ///< Kokkos index type
162168
};
163169

@@ -552,10 +558,15 @@ class ChunkedIntersectionIterator : public TeamPolicy<ParallelConfig> {
552558

553559
using execution_space =
554560
typename base_type::execution_space; ///< Execution space type.
555-
using base_index_type = specfem::point::interface_index<
556-
ParallelConfig::dimension>; ///< Index type to be used when calling @c
557-
///< specfem::execution::for_all with this
558-
///< iterator.
561+
using base_index_type = InterfacePointIndex<
562+
ParallelConfig::dimension, int,
563+
typename base_type::execution_space>; ///< Index type
564+
///< to be used
565+
///< when calling
566+
///< @ref
567+
///< specfem::execution::for_all
568+
///< with this
569+
///< iterator.
559570

560571
/**
561572
* @brief Constructor with explicit edge views and point count

include/execution/for_all.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ std::enable_if_t<
1717
Kokkos::DefaultHostExecutionSpace>)),
1818
void>
1919
impl_for_all(const IndexType &index, const ClosureType &closure) {
20-
const auto i = index.get_index();
21-
closure(i);
20+
closure(index);
2221
}
2322

2423
#if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP)
@@ -32,8 +31,7 @@ KOKKOS_FORCEINLINE_FUNCTION std::enable_if_t<
3231
Kokkos::DefaultExecutionSpace>)),
3332
void>
3433
impl_for_all(const IndexType &index, const ClosureType &closure) {
35-
const auto i = index.get_index();
36-
closure(i);
34+
closure(index);
3735
}
3836

3937
#endif

include/execution/mapped_chunked_domain_iterator.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ class MappedChunkedDomainIterator
169169
using execution_space =
170170
typename base_type::execution_space; ///< Execution space type
171171

172+
using base_index_type =
173+
MappedPointIndex<DimensionTag, typename ViewType::value_type,
174+
ParallelConfig::simd::using_simd, execution_space>;
175+
172176
MappedChunkedDomainIterator(
173177
const ViewType indices, const ViewType mapping,
174178
const specfem::mesh_entity::element_grid<DimensionTag> &element_grid)

include/execution/range_iterator.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ class RangeIterator : public RangePolicy<ParallelConfig> {
130130
///< calling @ref
131131
///< specfem::execution::for_each_level
132132

133+
using base_index_type = RangeIndex<
134+
typename base_type::index_type, ParallelConfig::simd::using_simd,
135+
typename base_type::execution_space>; ///< Index type to be
136+
///< used when calling
137+
///< @ref
138+
///< specfem::execution::for_all
139+
///< with this iterator.
140+
133141
using execution_space =
134142
typename base_type::execution_space; ///< Execution space type.
135143

include/kokkos_kernels/impl/compute_coupling.tpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,34 +63,32 @@ void specfem::kokkos_kernels::impl::compute_coupling(
6363

6464
specfem::execution::for_all(
6565
"specfem::kokkos_kernels::impl::compute_coupling", chunk,
66-
KOKKOS_LAMBDA(const typename decltype(chunk)::base_index_type &index) {
67-
const auto self_index = index.self_index;
68-
const auto coupled_index = index.coupled_index;
66+
KOKKOS_LAMBDA(const typename decltype(chunk)::base_index_type &iterator_index) {
67+
const auto index = iterator_index.get_index();
6968

7069
specfem::point::coupled_interface<dimension_tag, connection_tag,
7170
interface_tag, boundary_tag>
7271
point_interface_data;
73-
specfem::assembly::load_on_device(self_index, coupled_interfaces,
72+
specfem::assembly::load_on_device(index.self_index, coupled_interfaces,
7473
point_interface_data);
7574

7675
CoupledFieldType coupled_field;
77-
specfem::assembly::load_on_device(coupled_index, field, coupled_field);
78-
76+
specfem::assembly::load_on_device(index.coupled_index, field, coupled_field);
7977
SelfFieldType self_field;
8078

8179
specfem::medium::compute_coupling(point_interface_data, coupled_field,
8280
self_field);
8381

8482
PointBoundaryType point_boundary;
85-
specfem::assembly::load_on_device(self_index, boundaries,
83+
specfem::assembly::load_on_device(index.self_index, boundaries,
8684
point_boundary);
8785
if constexpr (BoundaryTag ==
8886
specfem::element::boundary_tag::acoustic_free_surface) {
8987
specfem::boundary_conditions::apply_boundary_conditions(
9088
point_boundary, self_field);
9189
}
9290

93-
specfem::assembly::atomic_add_on_device(self_index, field, self_field);
91+
specfem::assembly::atomic_add_on_device(index.self_index, field, self_field);
9492
});
9593

9694
return;

0 commit comments

Comments
 (0)