Skip to content

Commit a66a18f

Browse files
authored
Add preference to disable LoopVectorization (#2295)
1 parent fdbc2dd commit a66a18f

File tree

5 files changed

+69
-7
lines changed

5 files changed

+69
-7
lines changed

src/Trixi.jl

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ See also: [trixi-framework/Trixi.jl](https://github.com/trixi-framework/Trixi.jl
1515
"""
1616
module Trixi
1717

18+
using Preferences: @load_preference, set_preferences!
19+
const _PREFERENCE_SQRT = @load_preference("sqrt", "sqrt_Trixi_NaN")
20+
const _PREFERENCE_LOG = @load_preference("log", "log_Trixi_NaN")
21+
const _PREFERENCE_POLYESTER = @load_preference("polyester", true)
22+
const _PREFERENCE_LOOPVECTORIZATION = @load_preference("loop_vectorization", true)
23+
1824
# Include other packages that are used in Trixi.jl
1925
# (standard library packages first, other packages next, all of them sorted alphabetically)
2026

@@ -53,7 +59,13 @@ using FillArrays: Ones, Zeros
5359
using ForwardDiff: ForwardDiff
5460
using HDF5: HDF5, h5open, attributes, create_dataset, datatype, dataspace
5561
using LinearMaps: LinearMap
56-
using LoopVectorization: LoopVectorization, @turbo, indices
62+
if _PREFERENCE_LOOPVECTORIZATION
63+
using LoopVectorization: LoopVectorization, @turbo, indices
64+
else
65+
using LoopVectorization: LoopVectorization, indices
66+
include("auxiliary/mock_turbo.jl")
67+
end
68+
5769
using StaticArrayInterface: static_length # used by LoopVectorization
5870
using MuladdMacro: @muladd
5971
using Octavian: Octavian, matmul!
@@ -81,11 +93,6 @@ using SimpleUnPack: @pack!
8193
using DataStructures: BinaryHeap, FasterForward, extract_all!
8294

8395
using UUIDs: UUID
84-
using Preferences: @load_preference, set_preferences!
85-
86-
const _PREFERENCE_SQRT = @load_preference("sqrt", "sqrt_Trixi_NaN")
87-
const _PREFERENCE_LOG = @load_preference("log", "log_Trixi_NaN")
88-
const _PREFERENCE_POLYESTER = @load_preference("polyester", true)
8996

9097
# finite difference SBP operators
9198
using SummationByPartsOperators: AbstractDerivativeOperator,

src/auxiliary/math.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ function set_polyester!(toggle::Bool; force = true)
2222
@info "Please restart Julia and reload Trixi.jl for the `polyester` change to take effect"
2323
end
2424

25+
"""
26+
Trixi.set_loop_vectorization!(toggle::Bool; force = true)
27+
28+
Toggle the usage of [LoopVectorization.jl](https://github.com/JuliaSIMD/LoopVectorization.jl).
29+
By default, LoopVectorization.jl is enabled, but it can
30+
be useful for performance comparisons to switch to the Julia core backend.
31+
32+
This does not fully disable LoopVectorization.jl,
33+
but only its internal use as part of Trixi.jl.
34+
"""
35+
function set_loop_vectorization!(toggle::Bool; force = true)
36+
set_preferences!(TRIXI_UUID, "loop_vectorization" => toggle, force = force)
37+
@info "Please restart Julia and reload Trixi.jl for the `loop_vectorization` change to take effect"
38+
end
39+
2540
"""
2641
Trixi.set_sqrt_type!(type; force = true)
2742

src/auxiliary/mock_turbo.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copies some of the LoopVectorization functionality,
2+
# but solely using Julia base functionality. It is equivalent to `@simd`
3+
# at every loop level
4+
macro turbo(exprs...)
5+
# Find the outermost for loop
6+
body = nothing
7+
for expr in exprs
8+
if Meta.isexpr(expr, :for)
9+
body = expr
10+
end
11+
end
12+
@assert body !== nothing
13+
14+
# We want to visit each nested for loop and insert a `Loopinfo` expression at every level.
15+
function insert_loopinfo!(expr)
16+
recurse = Meta.isexpr(expr, :for) || Meta.isexpr(expr, :block) ||
17+
Meta.isexpr(expr, :let)
18+
if recurse
19+
foreach(insert_loopinfo!, expr.args)
20+
end
21+
if Meta.isexpr(expr, :for)
22+
# We could insert additional LLVM loopinfo or `julia.ivdep`.
23+
# For now we just encourage vectorization.
24+
# `Expr(:loopinfo)` corresponds to https://llvm.org/docs/LangRef.html#llvm-loop with two additional nodes
25+
# `julia.simdloop` & `julia.ivdep`
26+
# x-ref: https://github.com/JuliaLang/julia/pull/31376
27+
push!(expr.args, Expr(:loopinfo, Symbol("julia.simdloop")))
28+
end
29+
end
30+
insert_loopinfo!(body)
31+
32+
body = Expr(:block,
33+
Expr(:inbounds, true),
34+
body,
35+
Expr(:inbounds, :pop))
36+
return esc(body)
37+
end

src/callbacks_step/summary.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ function initialize_summary_callback(cb::DiscreteCallback, u, t, integrator;
212212
if !_PREFERENCE_POLYESTER
213213
push!(setup, "Polyester" => "disabled")
214214
end
215+
if !_PREFERENCE_LOOPVECTORIZATION
216+
push!(setup, "LoopVectorization" => "disabled")
217+
end
215218
if mpi_isparallel()
216219
push!(setup,
217220
"#MPI ranks" => mpi_nranks())

src/solvers/dg.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ end
672672
nvariables(equations) * nnodes(dg)^ndims(mesh) * nelements(dg, cache)
673673
end
674674
# See comments on the DGSEM version above
675-
if LoopVectorization.check_args(u_ode)
675+
if _PREFERENCE_POLYESTER && LoopVectorization.check_args(u_ode)
676676
# Here, we do not specialize on the number of nodes using `StaticInt` since
677677
# - it will not be type stable (SBP operators just store it as a runtime value)
678678
# - FD methods tend to use high node counts

0 commit comments

Comments
 (0)