Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Trixi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Trixi
using Preferences: @load_preference, set_preferences!
const _PREFERENCE_SQRT = @load_preference("sqrt", "sqrt_Trixi_NaN")
const _PREFERENCE_LOG = @load_preference("log", "log_Trixi_NaN")
const _PREFERENCE_POLYESTER = @load_preference("polyester", true)
const _PREFERENCE_THREADING = @load_preference("backend", :polyester)
const _PREFERENCE_LOOPVECTORIZATION = @load_preference("loop_vectorization", true)

# Include other packages that are used in Trixi.jl
Expand Down
18 changes: 16 additions & 2 deletions src/auxiliary/auxiliary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,13 @@ and [https://discourse.julialang.org/t/threads-threads-with-one-thread-how-to-re
macro threaded(expr)
# !!! danger "Heisenbug"
# Look at the comments for `wrap_array` when considering to change this macro.
expr = if _PREFERENCE_POLYESTER
expr = @static if _PREFERENCE_THREADING === :polyester
# Currently using `@batch` from Polyester.jl is more efficient,
# bypasses the Julia task scheduler and provides parallelization with less overhead.
quote
$Trixi.@batch $(expr)
end
else
elseif _PREFERENCE_THREADING === :static
# The following code is a simple version using only `Threads.@threads` from the
# standard library with an additional check whether only a single thread is used
# to reduce some overhead (and allocations) for serial execution.
Expand All @@ -225,6 +225,20 @@ macro threaded(expr)
end
end
end
elseif _PREFERENCE_THREADING === :dynamic
quote
let
if $Threads.nthreads() == 1
$(expr)
else
$Threads.@threads :dynamic $(expr)
end
end
end
Comment on lines +228 to +237
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will introduce bugs for code like

@threaded for mortar in eachmortar(dg, cache)
# Choose thread-specific pre-allocated container
fstar_primary_upper = fstar_primary_upper_threaded[Threads.threadid()]
fstar_primary_lower = fstar_primary_lower_threaded[Threads.threadid()]
fstar_secondary_upper = fstar_secondary_upper_threaded[Threads.threadid()]
fstar_secondary_lower = fstar_secondary_lower_threaded[Threads.threadid()]

won't it? @vchuravy

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oof, yes it will. @benegee and I were just talking about #2212 and the fact that creating or adapting the cache is a weird operation due to these backend specific containers. We are baking quite a bit of structure that is dependent on how we are performing the computation into the cache object.

So eventually we might need something like create_cache(..., backend), but even that is fraught since with #2212 I am able to change the storage type and thus the backend.

Maybe, we need struct StaticThreadIDCache end and struct GPUCache end...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think we will need to remove the "dynamic" option for now from this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
elseif _PREFERENCE_THREADING === :dynamic
quote
let
if $Threads.nthreads() == 1
$(expr)
else
$Threads.@threads :dynamic $(expr)
end
end
end

elseif _PREFERENCE_THREADING === :serial
quote
$(expr)
end
end
# Use `esc(quote ... end)` for nested macro calls as suggested in
# https://github.com/JuliaLang/julia/issues/23221
Expand Down
30 changes: 19 additions & 11 deletions src/auxiliary/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@
const TRIXI_UUID = UUID("a7f1ee26-1774-49b1-8366-f1abc58fbfcb")

"""
Trixi.set_polyester!(toggle::Bool; force = true)

Toggle the usage of [Polyester.jl](https://github.com/JuliaSIMD/Polyester.jl) for multithreading.
By default, Polyester.jl is enabled, but it can
be useful for performance comparisons to switch to the Julia core backend.

This does not fully disable Polyester.jl,
but only its use as part of Trixi.jl's [`@threaded`](@ref) macro.
Trixi.set_threading_backend!(backend::Symbol; force = true)

Toggle and/or switch backend behavior used in multithreaded loops inside Trixi.jl.
The selected backend affects the behavior of Trixi.jl's [`@threaded`](@ref) macro, which is used
throughout the codebase for parallel loops. By default, Polyester.jl is enabled for
optimal performance, but switching backends can be useful for comparisons or debugging.

# Available backends
- `:polyester`: Uses the default [Polyester.jl](https://github.com/JuliaSIMD/Polyester.jl)
- `:static`: Uses Julia's built-in static thread scheduling via `Threads.@threads :static`
- `:dynamic`: Uses Julia's built-in dynamic thread scheduling via `Threads.@threads :dynamic`
- `:serial`: Disables threading, executing loops serially
"""
function set_polyester!(toggle::Bool; force = true)
set_preferences!(TRIXI_UUID, "polyester" => toggle, force = force)
@info "Please restart Julia and reload Trixi.jl for the `polyester` change to take effect"
function set_threading_backend!(backend::Symbol = :polyester; force = true)
valid_backends = (:polyester, :static, :dynamic, :serial)
if !(backend in valid_backends)
throw(ArgumentError("Invalid threading backend: $(backend). Current options are: $(join(valid_backends, ", "))"))
end
set_preferences!(TRIXI_UUID, "backend" => backend, force = force)
@info "Please restart Julia and reload Trixi.jl for the `backend` change to take effect"
end

"""
Expand Down
2 changes: 1 addition & 1 deletion src/callbacks_step/summary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ function initialize_summary_callback(cb::DiscreteCallback, u, t, integrator;

# technical details
setup = Pair{String, Any}["#threads" => Threads.nthreads()]
if !_PREFERENCE_POLYESTER
if _PREFERENCE_THREADING !== :polyester
push!(setup, "Polyester" => "disabled")
end
if !_PREFERENCE_LOOPVECTORIZATION
Expand Down
2 changes: 1 addition & 1 deletion src/solvers/dg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ end
# since LoopVectorization does not support `ForwardDiff.Dual`s. Hence, we use
# optimized `PtrArray`s whenever possible and fall back to plain `Array`s
# otherwise.
if _PREFERENCE_POLYESTER && LoopVectorization.check_args(u_ode)
if _PREFERENCE_THREADING === :polyester && LoopVectorization.check_args(u_ode)
# This version using `PtrArray`s from StrideArrays.jl is very fast and
# does not result in allocations.
#
Expand Down