Skip to content

Commit 6149104

Browse files
authored
Fix type conversion and type instability in MortarL2 (trixi-framework#2242)
* Fix * Fix
1 parent 6349413 commit 6149104

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

src/solvers/dgsem/basis_lobatto_legendre.jl

+8-12
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,15 @@ function MortarL2(basis::LobattoLegendreBasis)
159159
RealT = real(basis)
160160
nnodes_ = nnodes(basis)
161161

162-
forward_upper_ = calc_forward_upper(nnodes_, RealT)
163-
forward_lower_ = calc_forward_lower(nnodes_, RealT)
164-
reverse_upper_ = calc_reverse_upper(nnodes_, Val(:gauss), RealT)
165-
reverse_lower_ = calc_reverse_lower(nnodes_, Val(:gauss), RealT)
162+
forward_upper = calc_forward_upper(nnodes_, RealT)
163+
forward_lower = calc_forward_lower(nnodes_, RealT)
164+
reverse_upper = calc_reverse_upper(nnodes_, Val(:gauss), RealT)
165+
reverse_lower = calc_reverse_lower(nnodes_, Val(:gauss), RealT)
166166

167-
# type conversions to get the requested real type and enable possible
168-
# optimizations of runtime performance and latency
169-
170-
# Usually as fast as `SMatrix` but better for latency
171-
forward_upper = Matrix{RealT}(forward_upper_)
172-
forward_lower = Matrix{RealT}(forward_lower_)
167+
# We keep the matrices above stored using the standard `Matrix` type
168+
# since this is usually as fast as `SMatrix`
169+
# (when using `let` in the volume integral/`@threaded`)
170+
# and reduces latency
173171

174172
# TODO: Taal performance
175173
# Check the performance of different implementations of `mortar_fluxes_to_elements!`
@@ -179,8 +177,6 @@ function MortarL2(basis::LobattoLegendreBasis)
179177
# `@tullio` when the matrix sizes are not necessarily static.
180178
# reverse_upper = SMatrix{nnodes_, nnodes_, RealT, nnodes_^2}(reverse_upper_)
181179
# reverse_lower = SMatrix{nnodes_, nnodes_, RealT, nnodes_^2}(reverse_lower_)
182-
reverse_upper = Matrix{RealT}(reverse_upper_)
183-
reverse_lower = Matrix{RealT}(reverse_lower_)
184180

185181
LobattoLegendreMortarL2{RealT, nnodes_, typeof(forward_upper),
186182
typeof(reverse_upper)}(forward_upper, forward_lower,

src/solvers/dgsem/l2projection.jl

+10-10
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ function calc_forward_upper(n_nodes, RealT = Float64)
2929
wbary = barycentric_weights(nodes)
3030

3131
# Calculate projection matrix (actually: interpolation)
32-
operator = zeros(n_nodes, n_nodes)
32+
operator = zeros(RealT, n_nodes, n_nodes)
3333
for j in 1:n_nodes
34-
poly = lagrange_interpolating_polynomials(1 / 2 * (nodes[j] + 1), nodes, wbary)
34+
poly = lagrange_interpolating_polynomials(0.5f0 * (nodes[j] + 1), nodes, wbary)
3535
for i in 1:n_nodes
3636
operator[j, i] = poly[i]
3737
end
@@ -49,9 +49,9 @@ function calc_forward_lower(n_nodes, RealT = Float64)
4949
wbary = barycentric_weights(nodes)
5050

5151
# Calculate projection matrix (actually: interpolation)
52-
operator = zeros(n_nodes, n_nodes)
52+
operator = zeros(RealT, n_nodes, n_nodes)
5353
for j in 1:n_nodes
54-
poly = lagrange_interpolating_polynomials(1 / 2 * (nodes[j] - 1), nodes, wbary)
54+
poly = lagrange_interpolating_polynomials(0.5f0 * (nodes[j] - 1), nodes, wbary)
5555
for i in 1:n_nodes
5656
operator[j, i] = poly[i]
5757
end
@@ -70,12 +70,12 @@ function calc_reverse_upper(n_nodes, ::Val{:gauss}, RealT = Float64)
7070
gauss_wbary = barycentric_weights(gauss_nodes)
7171

7272
# Calculate projection matrix (actually: discrete L2 projection with errors)
73-
operator = zeros(n_nodes, n_nodes)
73+
operator = zeros(RealT, n_nodes, n_nodes)
7474
for j in 1:n_nodes
75-
poly = lagrange_interpolating_polynomials(1 / 2 * (gauss_nodes[j] + 1),
75+
poly = lagrange_interpolating_polynomials(0.5f0 * (gauss_nodes[j] + 1),
7676
gauss_nodes, gauss_wbary)
7777
for i in 1:n_nodes
78-
operator[i, j] = 1 / 2 * poly[i] * gauss_weights[j] / gauss_weights[i]
78+
operator[i, j] = 0.5f0 * poly[i] * gauss_weights[j] / gauss_weights[i]
7979
end
8080
end
8181

@@ -97,12 +97,12 @@ function calc_reverse_lower(n_nodes, ::Val{:gauss}, RealT = Float64)
9797
gauss_wbary = barycentric_weights(gauss_nodes)
9898

9999
# Calculate projection matrix (actually: discrete L2 projection with errors)
100-
operator = zeros(n_nodes, n_nodes)
100+
operator = zeros(RealT, n_nodes, n_nodes)
101101
for j in 1:n_nodes
102-
poly = lagrange_interpolating_polynomials(1 / 2 * (gauss_nodes[j] - 1),
102+
poly = lagrange_interpolating_polynomials(0.5f0 * (gauss_nodes[j] - 1),
103103
gauss_nodes, gauss_wbary)
104104
for i in 1:n_nodes
105-
operator[i, j] = 1 / 2 * poly[i] * gauss_weights[j] / gauss_weights[i]
105+
operator[i, j] = 0.5f0 * poly[i] * gauss_weights[j] / gauss_weights[i]
106106
end
107107
end
108108

0 commit comments

Comments
 (0)