Skip to content

Commit dffcd46

Browse files
authored
Merge pull request #1 from afilogo/branch1
commit 1
2 parents aeff5a0 + 0f10be1 commit dffcd46

File tree

5 files changed

+38
-16
lines changed

5 files changed

+38
-16
lines changed

src/Trixi.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Trixi
1818
using Preferences: @load_preference, set_preferences!
1919
const _PREFERENCE_SQRT = @load_preference("sqrt", "sqrt_Trixi_NaN")
2020
const _PREFERENCE_LOG = @load_preference("log", "log_Trixi_NaN")
21-
const _PREFERENCE_POLYESTER = @load_preference("polyester", true)
21+
const _PREFERENCE_THREADING = @load_preference("backend", :polyester)
2222
const _PREFERENCE_LOOPVECTORIZATION = @load_preference("loop_vectorization", true)
2323

2424
# Include other packages that are used in Trixi.jl

src/auxiliary/auxiliary.jl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,13 @@ and [https://discourse.julialang.org/t/threads-threads-with-one-thread-how-to-re
206206
macro threaded(expr)
207207
# !!! danger "Heisenbug"
208208
# Look at the comments for `wrap_array` when considering to change this macro.
209-
expr = if _PREFERENCE_POLYESTER
209+
expr = @static if _PREFERENCE_THREADING == :polyester
210210
# Currently using `@batch` from Polyester.jl is more efficient,
211211
# bypasses the Julia task scheduler and provides parallelization with less overhead.
212212
quote
213213
$Trixi.@batch $(expr)
214214
end
215-
else
215+
elseif _PREFERENCE_THREADING == :static
216216
# The following code is a simple version using only `Threads.@threads` from the
217217
# standard library with an additional check whether only a single thread is used
218218
# to reduce some overhead (and allocations) for serial execution.
@@ -225,6 +225,20 @@ macro threaded(expr)
225225
end
226226
end
227227
end
228+
elseif _PREFERENCE_THREADING == :dynamic
229+
quote
230+
let
231+
if $Threads.nthreads() == 1
232+
$(expr)
233+
else
234+
$Threads.@threads :dynamic $(expr)
235+
end
236+
end
237+
end
238+
elseif _PREFERENCE_THREADING == :serial
239+
quote
240+
$(expr)
241+
end
228242
end
229243
# Use `esc(quote ... end)` for nested macro calls as suggested in
230244
# https://github.com/JuliaLang/julia/issues/23221

src/auxiliary/math.jl

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,26 @@
88
const TRIXI_UUID = UUID("a7f1ee26-1774-49b1-8366-f1abc58fbfcb")
99

1010
"""
11-
Trixi.set_polyester!(toggle::Bool; force = true)
12-
13-
Toggle the usage of [Polyester.jl](https://github.com/JuliaSIMD/Polyester.jl) for multithreading.
14-
By default, Polyester.jl is enabled, but it can
15-
be useful for performance comparisons to switch to the Julia core backend.
16-
17-
This does not fully disable Polyester.jl,
18-
but only its use as part of Trixi.jl's [`@threaded`](@ref) macro.
11+
Trixi.set_threading_backend!(backend::Symbol; force = true)
12+
13+
Toggle and/or switch backend behavior used in multithreaded loops inside Trixi.jl.
14+
The selected backend affects the behavior of Trixi.jl's [`@threaded`](@ref) macro, which is used
15+
throughout the codebase for parallel loops. By default, Polyester.jl is enabled for
16+
optimal performance, but switching backends can be useful for comparisons or debugging.
17+
18+
# Available backends
19+
- `:polyester`: Uses the default [Polyester.jl](https://github.com/JuliaSIMD/Polyester.jl)
20+
- `:static`: Uses Julia's built-in static thread scheduling via `Threads.@threads :static`
21+
- `:dynamic`: Uses Julia's built-in dynamic thread scheduling via `Threads.@threads :dynamic`
22+
- `:serial`: Disables threading, executing loops serially
1923
"""
20-
function set_polyester!(toggle::Bool; force = true)
21-
set_preferences!(TRIXI_UUID, "polyester" => toggle, force = force)
22-
@info "Please restart Julia and reload Trixi.jl for the `polyester` change to take effect"
24+
function set_threading_backend!(backend::Symbol = :polyester; force = true)
25+
valid_backends = (:polyester, :static, :dynamic, :serial)
26+
if !(backend in valid_backends)
27+
throw(ArgumentError("Invalid threading backend: $(backend). Current options are: $(join(valid_backends, ", "))"))
28+
end
29+
set_preferences!(TRIXI_UUID, "backend" => backend, force = force)
30+
@info "Please restart Julia and reload Trixi.jl for the `backend` change to take effect"
2331
end
2432

2533
"""

src/callbacks_step/summary.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ function initialize_summary_callback(cb::DiscreteCallback, u, t, integrator;
209209

210210
# technical details
211211
setup = Pair{String, Any}["#threads" => Threads.nthreads()]
212-
if !_PREFERENCE_POLYESTER
212+
if _PREFERENCE_THREADING !== :polyester
213213
push!(setup, "Polyester" => "disabled")
214214
end
215215
if !_PREFERENCE_LOOPVECTORIZATION

src/solvers/dg.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ end
638638
# since LoopVectorization does not support `ForwardDiff.Dual`s. Hence, we use
639639
# optimized `PtrArray`s whenever possible and fall back to plain `Array`s
640640
# otherwise.
641-
if _PREFERENCE_POLYESTER && LoopVectorization.check_args(u_ode)
641+
if _PREFERENCE_THREADING === :polyester && LoopVectorization.check_args(u_ode)
642642
# This version using `PtrArray`s from StrideArrays.jl is very fast and
643643
# does not result in allocations.
644644
#

0 commit comments

Comments
 (0)