Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1f91eda
Use preferences to switch between SIMD and KernelAbstractions
vchuravy Jun 20, 2024
4b7a846
fixup! Use preferences to switch between SIMD and KernelAbstractions
vchuravy Jun 20, 2024
e77bca4
fixup! Use preferences to switch between SIMD and KernelAbstractions
vchuravy Jun 20, 2024
b01cdce
don't limit workgroups to 64
vchuravy Jun 20, 2024
5ecb979
Merge branch 'master' into vc/backends
b-fg May 21, 2025
f0b351f
Updated loop macro and cleaned up Preferences.jl routines.
b-fg May 21, 2025
2369279
Small bug fix.
b-fg May 21, 2025
2c629c2
Cleaned up -t 1 warning message.
b-fg May 21, 2025
5f17ede
testing 64,64 workgroup size
b-fg May 21, 2025
f1621be
Workgroup size is 64, like master.
b-fg May 22, 2025
6f9aa99
Wrapped SIMD kernels in a fuction too, as single-threaded test were 4…
b-fg May 22, 2025
fb85fd1
First attempt at adjusting CI to work with LocalPreferences
b-fg May 22, 2025
2c7c41e
CI fix.
b-fg May 22, 2025
4ea8e73
Added automatic specialization for kernel functions generated with lo…
b-fg May 22, 2025
85f0855
Merge branch 'master' into vc/backends
b-fg May 22, 2025
eea319b
Cleaned up CI.
b-fg May 22, 2025
1239610
Adding pkg Random in tests.
b-fg May 22, 2025
a50deb1
Trying to fix Random pkg error in CI.
b-fg May 22, 2025
d2b3ade
Bumping CI to Julia 1.11, trying to fix Random pkg error during tests
b-fg May 22, 2025
87cddb8
Removed x86 architecture from CI.
b-fg May 22, 2025
f8633ea
Moved nthreads check outside of __init__ so it is only checked once a…
b-fg May 24, 2025
7da0526
Added test for new routines used in automatic kernel generation.
b-fg May 24, 2025
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
15 changes: 11 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ jobs:
fail-fast: false
matrix:
version:
- '1.10'
- '1.11'
os:
- ubuntu-latest
- macOS-latest
- windows-latest
arch:
- x64
- x86
nthreads:
- '1'
- 'auto'
Expand All @@ -56,10 +55,18 @@ jobs:
${{ runner.os }}-test-${{ env.cache-name }}-
${{ runner.os }}-test-
${{ runner.os }}-
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- name: WaterLily tests
env:
JULIA_NUM_THREADS: ${{ matrix.nthreads }}
shell: bash
run: |
if [ "${{ matrix.nthreads }}" = "auto" ]
then
printf "[WaterLily]\nbackend = \"KernelAbstractions\"" > LocalPreferences.toml
else
printf "[WaterLily]\nbackend = \"SIMD\"" > LocalPreferences.toml
fi
julia --proj --color=yes -e "using Pkg; Pkg.instantiate(); Pkg.test(; coverage=true);"
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v5
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.vscode/
.DS_Store
Manifest.toml
LocalPreferences.toml
/docs/build/

# gfx
Expand Down
5 changes: 4 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Expand All @@ -19,8 +21,8 @@ TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
[weakdeps]
AMDGPU = "21141c5a-9bdb-4563-92ae-f87d6854732e"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
Meshing = "e6723b4c-ebff-59f1-b4b7-d97aa5274f73"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Expand All @@ -43,6 +45,7 @@ EllipsisNotation = "1.8"
ForwardDiff = "^0.10.18"
KernelAbstractions = "0.9.1"
LoggingExtras = "1.1"
Random = "1.11.0"
Reexport = "^1.2.2"
Requires = "1.3"
StaticArrays = "^1.1.0"
Expand Down
23 changes: 14 additions & 9 deletions src/WaterLily.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using DocStringExtensions

include("util.jl")
export L₂,BC!,@inside,inside,δ,apply!,loc,@log
export L₂,BC!,@inside,inside,δ,apply!,loc,@log,set_backend,backend

using Reexport
@reexport using KernelAbstractions: @kernel,@index,get_backend
Expand Down Expand Up @@ -166,15 +166,21 @@

# Check number of threads when loading WaterLily
"""
check_nthreads(::Val{1})
check_nthreads()

Check the number of threads available for the Julia session that loads WaterLily.
A warning is shown when running in serial (`JULIA_NUM_THREADS=1`).
"""
check_nthreads(::Val{1}) = @warn("\nUsing WaterLily in serial (ie. JULIA_NUM_THREADS=1) is not recommended because \
it disables the GPU backend and defaults to serial CPU."*
"\nUse JULIA_NUM_THREADS=auto, or any number of threads greater than 1, to allow multi-threading in CPU or GPU backends.")
check_nthreads(_) = nothing
A warning is shown when running in serial (JULIA_NUM_THREADS=1) with KernelAbstractions enabled.
"""
function check_nthreads()
if backend == "KernelAbstractions" && Threads.nthreads() == 1
@warn """

Check warning on line 176 in src/WaterLily.jl

View check run for this annotation

Codecov / codecov/patch

src/WaterLily.jl#L174-L176

Added lines #L174 - L176 were not covered by tests
Using WaterLily in serial (ie. JULIA_NUM_THREADS=1) is not recommended because it defaults to serial CPU execution.
Use JULIA_NUM_THREADS=auto, or any number of threads greater than 1, to allow multi-threading in CPU backends.
For a low-overhead single-threaded CPU only backend set: WaterLily.set_backend("SIMD")
"""
end
end
check_nthreads()

# Backward compatibility for extensions
if !isdefined(Base, :get_extension)
Expand All @@ -191,7 +197,6 @@
@require Meshing = "e6723b4c-ebff-59f1-b4b7-d97aa5274f73" include("../ext/WaterLilyMeshingExt.jl")
@require JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" include("../ext/WaterLilyJLD2Ext.jl")
end
check_nthreads(Val{Threads.nthreads()}())
end

end # module
55 changes: 40 additions & 15 deletions src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@
end |> esc
end

# Could also use ScopedValues in Julia 1.11+
using Preferences
const backend = @load_preference("backend", "KernelAbstractions")
function set_backend(new_backend::String)
if !(new_backend in ("SIMD", "KernelAbstractions"))
throw(ArgumentError("Invalid backend: \"$(new_backend)\""))
end

# Set it in our runtime values, as well as saving it to disk
@set_preferences!("backend" => new_backend)
@info("New backend set; restart your Julia session for this change to take effect!")
end

"""
@loop <expr> over <I ∈ R>

Expand Down Expand Up @@ -118,26 +131,34 @@
"""
macro loop(args...)
ex,_,itr = args
_,I,R = itr.args; sym = []
_,I,R = itr.args
sym = []
grab!(sym,ex) # get arguments and replace composites in `ex`
setdiff!(sym,[I]) # don't want to pass I as an argument
symT = symtypes(sym) # generate a list of types for each symbol
@gensym(kern, kern_) # generate unique kernel function names for serial and KA execution
return quote
function $kern($(rep.(sym)...),::Val{1})
@simd for $I ∈ $R
@static if backend == "KernelAbstractions"

Check warning on line 140 in src/util.jl

View check run for this annotation

Codecov / codecov/patch

src/util.jl#L140

Added line #L140 was not covered by tests
return quote
@kernel function $kern_($(rep.(sym)...),@Const(I0)) # replace composite arguments
$I = @index(Global,Cartesian)

Check warning on line 143 in src/util.jl

View check run for this annotation

Codecov / codecov/patch

src/util.jl#L143

Added line #L143 was not covered by tests
$I += I0
@fastmath @inbounds $ex
end
end
@kernel function $kern_($(rep.(sym)...),@Const(I0)) # replace composite arguments
$I = @index(Global,Cartesian)
$I += I0
@fastmath @inbounds $ex
end
function $kern($(rep.(sym)...),_)
$kern_(get_backend($(sym[1])),64)($(sym...),$R[1]-oneunit($R[1]),ndrange=size($R))
end
$kern($(sym...),Val{Threads.nthreads()}()) # dispatch to SIMD for -t 1, or KA otherwise
end |> esc
function $kern($(joinsymtype(rep.(sym),symT)...)) where {$(symT...)}
$kern_(get_backend($(sym[1])),64)($(sym...),$R[1]-oneunit($R[1]),ndrange=size($R))
end
$kern($(sym...))
end |> esc
else # backend == "SIMD"
return quote
function $kern($(joinsymtype(rep.(sym),symT)...)) where {$(symT...)}
@simd for $I ∈ $R
@fastmath @inbounds $ex
end

Check warning on line 157 in src/util.jl

View check run for this annotation

Codecov / codecov/patch

src/util.jl#L157

Added line #L157 was not covered by tests
end
$kern($(sym...))
end |> esc
end
end
function grab!(sym,ex::Expr)
ex.head == :. && return union!(sym,[ex]) # grab composite name and return
Expand All @@ -149,6 +170,10 @@
grab!(sym,ex) = nothing
rep(ex) = ex
rep(ex::Expr) = ex.head == :. ? Symbol(ex.args[2].value) : ex
using Random
symtypes(sym) = [Symbol.(Random.randstring('A':'Z',4)) for _ in 1:length(sym)]
joinsymtype(sym::Symbol,symT::Symbol) = Expr(:(::), sym, symT)
joinsymtype(sym,symT) = zip(sym,symT) .|> x->joinsymtype(x...)

using StaticArrays
"""
Expand Down
1 change: 1 addition & 0 deletions test/alloctest.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BenchmarkTools, Printf

backend != "SIMD" && throw(ArgumentError("KernelAbstractions backend not allowed to run allocations tests, use SIMD backend"))
@testset "mom_step! allocations" begin
function Sim(θ;L=32,U=1,Re=100,perdir=())
function map(x,t)
Expand Down
7 changes: 6 additions & 1 deletion test/maintests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using GPUArrays
using ReadVTK, WriteVTK, JLD2
using ReadVTK, WriteVTK, JLD2, Random

backend != "KernelAbstractions" && throw(ArgumentError("SIMD backend not allowed to run main tests, use KernelAbstractions backend"))
@info "Test backends: $(join(arrays,", "))"
@testset "util.jl" begin
I = CartesianIndex(1,2,3,4)
Expand All @@ -17,6 +18,10 @@ using ReadVTK, WriteVTK, JLD2
WaterLily.grab!(sym,ex)
@test ex == :(a[I, i] = Math.add(b[I], func(I, q)))
@test sym == [:a, :I, :i, :(p.b), :q]
sym = [:a,:b,:c]
Random.seed!(99)
symT = WaterLily.symtypes(sym)
@test WaterLily.joinsymtype(sym,symT) == Expr[:(a::MDFZ), :(b::AXYU), :(c::RFIB)]

for f ∈ arrays
p = zeros(4,5) |> f
Expand Down
Loading