Skip to content

Commit 3c198c3

Browse files
vchuravyranochabenegee
authored
Add synchronization statements to ensure timer output correctness on the GPU (#2892)
* Add synchronization statements to ensure timer output correctness on the GPU * use trixi_timit macro with a backend * don't commit piracy * Update src/Trixi.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fixup! Add synchronization statements to ensure timer output correctness on the GPU Co-authored-by: Benedict <135045760+benegee@users.noreply.github.com> * only synchronize when timers are enabled --------- Co-authored-by: Hendrik Ranocha <ranocha@users.noreply.github.com> Co-authored-by: Benedict <135045760+benegee@users.noreply.github.com>
1 parent cc71756 commit 3c198c3

2 files changed

Lines changed: 35 additions & 12 deletions

File tree

src/auxiliary/auxiliary.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,26 @@ end
8282
return ncalls_first
8383
end
8484

85+
"""
86+
@trixi_timeit_ext backend timer() "some label" expression
87+
88+
This macro is an extension of [`@trixi_timeit`](@ref) that also synchronizes the given `backend` after executing the given `expression`.
89+
This is useful to get accurate timing measurements for GPU backends, where the execution of kernels is asynchronous.
90+
The synchronization ensures that all GPU operations are completed before the timer is stopped.
91+
92+
See also [`@trixi_timeit`](@ref).
93+
"""
94+
macro trixi_timeit_ext(backend, timer_output, label, expr)
95+
expr = quote
96+
local val = $(esc(expr))
97+
if $(esc(backend)) !== nothing && $(TrixiBase).timeit_debug_enabled()
98+
$(KernelAbstractions.synchronize)($(esc(backend)))
99+
end
100+
val
101+
end
102+
return :(@trixi_timeit($(esc(timer_output)), $(esc(label)), $(expr)))
103+
end
104+
85105
"""
86106
examples_dir()
87107

src/solvers/dgsem_tree/dg_2d.jl

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,63 +113,66 @@ function rhs!(du, u, t,
113113
backend = trixi_backend(u)
114114

115115
# Reset du
116-
@trixi_timeit timer() "reset ∂u/∂t" set_zero!(du, dg, cache)
116+
@trixi_timeit_ext backend timer() "reset ∂u/∂t" begin
117+
set_zero!(du, dg, cache)
118+
end
117119

118120
# Calculate volume integral
119-
@trixi_timeit timer() "volume integral" begin
121+
@trixi_timeit_ext backend timer() "volume integral" begin
120122
calc_volume_integral!(backend, du, u, mesh,
121123
have_nonconservative_terms(equations), equations,
122124
dg.volume_integral, dg, cache)
123125
end
124126

125127
# Prolong solution to interfaces
126-
@trixi_timeit timer() "prolong2interfaces" begin
128+
@trixi_timeit_ext backend timer() "prolong2interfaces" begin
127129
prolong2interfaces!(backend, cache, u, mesh, equations, dg)
128130
end
129131

130132
# Calculate interface fluxes
131-
@trixi_timeit timer() "interface flux" begin
133+
@trixi_timeit_ext backend timer() "interface flux" begin
132134
calc_interface_flux!(backend, cache.elements.surface_flux_values, mesh,
133135
have_nonconservative_terms(equations), equations,
134136
dg.surface_integral, dg, cache)
135137
end
136138

137139
# Prolong solution to boundaries
138-
@trixi_timeit timer() "prolong2boundaries" begin
140+
@trixi_timeit_ext backend timer() "prolong2boundaries" begin
139141
prolong2boundaries!(cache, u, mesh, equations, dg)
140142
end
141143

142144
# Calculate boundary fluxes
143-
@trixi_timeit timer() "boundary flux" begin
145+
@trixi_timeit_ext backend timer() "boundary flux" begin
144146
calc_boundary_flux!(cache, t, boundary_conditions, mesh, equations,
145147
dg.surface_integral, dg)
146148
end
147149

148150
# Prolong solution to mortars
149-
@trixi_timeit timer() "prolong2mortars" begin
151+
@trixi_timeit_ext backend timer() "prolong2mortars" begin
150152
prolong2mortars!(cache, u, mesh, equations,
151153
dg.mortar, dg)
152154
end
153155

154156
# Calculate mortar fluxes
155-
@trixi_timeit timer() "mortar flux" begin
157+
@trixi_timeit_ext backend timer() "mortar flux" begin
156158
calc_mortar_flux!(cache.elements.surface_flux_values, mesh,
157159
have_nonconservative_terms(equations), equations,
158160
dg.mortar, dg.surface_integral, dg, cache)
159161
end
160162

161163
# Calculate surface integrals
162-
@trixi_timeit timer() "surface integral" begin
164+
@trixi_timeit_ext backend timer() "surface integral" begin
163165
calc_surface_integral!(backend, du, u, mesh, equations,
164166
dg.surface_integral, dg, cache)
165167
end
166168

167169
# Apply Jacobian from mapping to reference element
168-
@trixi_timeit timer() "Jacobian" apply_jacobian!(backend, du, mesh, equations, dg,
169-
cache)
170+
@trixi_timeit_ext backend timer() "Jacobian" begin
171+
apply_jacobian!(backend, du, mesh, equations, dg, cache)
172+
end
170173

171174
# Calculate source terms
172-
@trixi_timeit timer() "source terms" begin
175+
@trixi_timeit_ext backend timer() "source terms" begin
173176
calc_sources!(du, u, t, source_terms, equations, dg, cache)
174177
end
175178

0 commit comments

Comments
 (0)