Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7b5ca4b
Add a cuDSS GPU solver for Maxwell frequency sweeps
Nov 5, 2026
bf270d5
Merge maxwell/3-sweep-abc into maxwell/6-cudss
Nov 5, 2026
b53a948
Rename the cuDSS test group to MaxwellGPU and run it by hand
Nov 5, 2026
160336f
Merge maxwell/3-sweep-abc into maxwell/6-cudss
Nov 5, 2026
96cd680
Merge maxwell/3-sweep-abc into maxwell/6-cudss
Nov 5, 2026
ca9927d
Turn the csr_layout and csr_values! docstrings into comments
Nov 5, 2026
fc4459e
Merge maxwell/3-sweep-abc into maxwell/6-cudss (PR workflow triggers)
Nov 6, 2026
5c20966
Merge forge/mwsync-3-sweep-abc into maxwell/6-cudss
Nov 10, 2026
26a2a3a
Document the cuDSS version that resolves
Nov 11, 2026
42ed15c
Merge forge/mwsync-3-sweep-abc into 6-cudss
Nov 11, 2026
c3a849d
Merge forge/mwsync-3-sweep-abc into 6-cudss
Nov 11, 2026
6047b84
Merge forge/mwsync-3-sweep-abc into 6-cudss
Nov 12, 2026
5a94689
Merge forge/mwsync-3-sweep-abc into 6-cudss
Nov 12, 2026
f86f8ea
Merge forge/mwsync-3-sweep-abc into 6-cudss
Nov 12, 2026
5f14564
Merge forge/mwsync-3-sweep-abc into 6-cudss
Nov 12, 2026
070c615
Merge forge/mwsync-3-sweep-abc into 6-cudss
Nov 12, 2026
2daf6ca
Merge forge/mwsync-3-sweep-abc into 6-cudss
Nov 12, 2026
b9e59a9
Merge forge/mwsync-3-sweep-abc into 6-cudss
Nov 12, 2026
2ad532f
Merge forge/mwsync-3-sweep-abc into 6-cudss
Nov 12, 2026
f1a042f
Merge forge/mwsync-3-sweep-abc into 6-cudss
Nov 12, 2026
99d2941
Merge forge/mwsync-3-sweep-abc into 6-cudss
Nov 12, 2026
57ae636
Merge forge/mwsync-3-sweep-abc into 6-cudss
Nov 12, 2026
c422fdc
Merge forge/mwsync-3-sweep-abc into 6-cudss
Nov 12, 2026
aae76be
Merge forge/mwsync-3-sweep-abc into 6-cudss
Nov 12, 2026
176ecc8
Merge forge/mwsync-3-sweep-abc into 6-cudss
Nov 13, 2026
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
5 changes: 5 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,21 @@ SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"

[weakdeps]
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
CUDSS = "45b445bb-4962-46a0-9369-b4df9d0f772e"
FerriteGmsh = "4f95f4f8-b27c-4ae5-9a39-ea55e634e36b"
Meshes = "eacbb407-ea5a-433e-ab97-5258b1ca43fa"

[extensions]
FiniteElementMethodCUDSSExt = ["CUDA", "CUDSS"]
FiniteElementMethodFerriteGmshExt = "FerriteGmsh"
FiniteElementMethodMeshesExt = "Meshes"

[compat]
ADTypes = "1.24"
AllocCheck = "0.2"
CUDA = "6"
CUDSS = "0.8, 1"
DiffEqBase = "7.21"
DomainSets = "0.7, 0.8"
Ferrite = "1.7"
Expand Down
25 changes: 25 additions & 0 deletions docs/src/maxwell.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ or a non-positive `mu_r` at a port is rejected, because the power-wave normalisa
impedance and the outward power of the TE10 mode is `Re(β/μr) > 0` only for positive `μr` and `εr`. Put loss behind the port, not in its face, and a
facet cannot belong to two ports or to both a port and an `absorbing` set.

### Solving on an NVIDIA GPU

`solver = CuDSSSolver()` moves the factorisation and solves of [`solve_sweep`](@ref) to an NVIDIA GPU through
[cuDSS](https://developer.nvidia.com/cudss); it needs `using CUDA, CUDSS` and a functional CUDA device.
Assembly stays on the CPU. The first frequency uploads the operator and runs the cuDSS analysis and
factorisation; later frequencies copy only the new nonzero values and refactorise, reusing the analysis, and
every port excitation is solved in one call.

```julia
using CUDA, CUDSS
sweep = solve_sweep(model, 2π .* (8.0e9:0.5e9:12.0e9); solver = CuDSSSolver())
```

The default is cuDSS's general LU (`structure = "G"`). `CuDSSSolver(; structure = "S")` uses its complex
symmetric LDLᵀ instead (the Maxwell operator is complex symmetric, not Hermitian), with about half the factor
storage; CUDSS.jl documents complex symmetric matrices only from its version 1.0. CUDSS.jl 1.0 currently cannot be
installed next to this package (its ArrayInterface bound conflicts with the one of DiffEqBase), so use CUDSS.jl 0.8
with the default general LU until it resolves; the extension accepts both.

The CPU side of this solver (the CSR layout and the per-frequency value update) is tested in the Maxwell test
group, and the `MaxwellGPU` group checks that the extension loads and infers. The comparison of the GPU sweep with
UMFPACK on a WR-90 guide runs only where `CUDA.functional()` holds; CI has no NVIDIA GPU, so run it by hand on a
CUDA machine with `GROUP=MaxwellGPU julia --project=. -e 'using Pkg; Pkg.test()'`.

```@docs
MaxwellModel
MaxwellMaterial
Expand All @@ -86,6 +110,7 @@ FiniteElementMethod.WavePortData
solve_sweep
MaxwellSweep
FiniteElementMethod.umfpack_solver
CuDSSSolver
FiniteElementMethod.tangential_mass
FiniteElementMethod.port_excitations
FiniteElementMethod.s_parameters
Expand Down
40 changes: 40 additions & 0 deletions ext/FiniteElementMethodCUDSSExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module FiniteElementMethodCUDSSExt

using FiniteElementMethod: CuDSSSolver, csr_layout, csr_values!
using CUDA: CuMatrix, CuVector, cuSPARSE
using CUDSS: CudssSolver, cudss, cudss_update
using SparseArrays: SparseMatrixCSC, nonzeros

const CudssFloat = Union{Float32, Float64, ComplexF32, ComplexF64}

# What a CuDSSSolver keeps between frequencies: the host CSR value buffer and its gather permutation, the
# device matrix (whose value array cuDSS reads in place) and the cuDSS analysis and factors.
struct CuDSSCache{T, S}
perm::Vector{Int}
vals::Vector{T}
dA::cuSPARSE.CuSparseMatrixCSR{T, Int32}
solver::S
end

function (s::CuDSSSolver)(A::SparseMatrixCSC{T}, B::AbstractMatrix{T}, cache) where {T <: CudssFloat}
dB = CuMatrix{T}(B)
dX = similar(dB)
if cache === nothing
rowptr, colval, perm = csr_layout(A)
vals = nonzeros(A)[perm]
dA = cuSPARSE.CuSparseMatrixCSR{T, Int32}(CuVector(rowptr), CuVector(colval), CuVector(vals), size(A))
solver = CudssSolver(dA, s.structure, 'F')
cudss("analysis", solver, dX, dB)
cudss("factorization", solver, dX, dB)
cache = CuDSSCache(perm, vals, dA, solver)
else
csr_values!(cache.vals, A, cache.perm)
copyto!(cache.dA.nzVal, cache.vals)
cudss_update(cache.solver, cache.dA)
cudss("refactorization", cache.solver, dX, dB)
end
cudss("solve", cache.solver, dX, dB; asynchronous = false)
return Array(dX), cache
end

end # module
3 changes: 2 additions & 1 deletion src/FiniteElementMethod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ include("maxwell/model.jl")
include("maxwell/driven.jl")
include("maxwell/sweep.jl")
include("maxwell/adjoint.jl")
include("maxwell/cudss.jl")
export MaxwellMaterial, MaxwellModel, MaxwellTerm, maxwell_operator, maxwell_operator!
export MaxwellSolution, driven_problem, solve_driven, field_values
export WavePort, MaxwellSweep, solve_sweep
export WavePort, MaxwellSweep, solve_sweep, CuDSSSolver
export MaxwellSensitivities, s_parameter_sensitivities, adjoint_gradient

include("precompile.jl")
Expand Down
59 changes: 59 additions & 0 deletions src/maxwell/cudss.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# The cuDSS sparse direct solver of `solve_sweep`, implemented by the FiniteElementMethodCUDSSExt extension. The
# host-side pieces (the CSR layout of a CSC matrix and the per-frequency value gather) live here so that they are
# testable without a GPU.

"""
CuDSSSolver(; structure = "G")

A [`solve_sweep`](@ref) solver that factorises `A` on an NVIDIA GPU with cuDSS: `solve_sweep(model, omegas;
solver = CuDSSSolver())`. It needs `using CUDA, CUDSS` (the FiniteElementMethodCUDSSExt extension) and a
functional CUDA device.

Assembly stays on the CPU. The first frequency uploads `A` in CSR layout and runs the cuDSS analysis
(reordering and symbolic factorisation) and the numerical factorisation; every later frequency copies only the
new nonzero values to the device and refactorises. All port excitations (the columns of `B`) are solved at once
on the device and returned to the host.

`structure = "G"` (the default) is cuDSS's general LDU factorisation. `structure = "S"` is its symmetric LDLᵀ,
which for complex values treats `A` as complex symmetric (`transpose(A) == A`, true of the Maxwell operator);
it halves the factor storage but is documented for complex values only from CUDSS.jl 1.0 (cuDSS 1.0).
"""
struct CuDSSSolver
structure::String
function CuDSSSolver(; structure::AbstractString = "G")
structure in ("G", "S") ||
throw(ArgumentError("CuDSSSolver structure must be \"G\" or \"S\", got \"$structure\""))
return new(String(structure))
end
end

function (::CuDSSSolver)(A, B, cache)
loaded = Base.get_extension(@__MODULE__, :FiniteElementMethodCUDSSExt) !== nothing
msg = loaded ?
"CuDSSSolver solves a SparseMatrixCSC with Float32, Float64, ComplexF32 or ComplexF64 values and a dense right-hand side of the same element type; got $(typeof(A)) and $(typeof(B))" :
"CuDSSSolver needs the CUDA and CUDSS packages: run `using CUDA, CUDSS` first"
throw(ArgumentError(msg))
end

# csr_layout(A::SparseMatrixCSC) -> (rowptr, colval, perm)
#
# The compressed-sparse-row arrays of `A` with `Int32` indices (1-based, as cuSPARSE's `CuSparseMatrixCSR` stores
# them) and the permutation with `A.nzval[perm]` the values in that row order. The pattern of the sweep operator
# never changes, so `perm` is computed once and every later frequency only gathers its values.
function csr_layout(A::SparseMatrixCSC)
nnz(A) < typemax(Int32) ||
throw(ArgumentError("CuDSSSolver uses Int32 indices; the matrix has $(nnz(A)) stored entries"))
positions = SparseMatrixCSC(size(A)..., A.colptr, A.rowval, collect(1:nnz(A)))
byrow = copy(transpose(positions))
return Int32.(byrow.colptr), Int32.(byrow.rowval), byrow.nzval
end

# csr_values!(vals, A::SparseMatrixCSC, perm)
#
# Overwrite `vals` with the nonzero values of `A` in the row order of `csr_layout`.
function csr_values!(vals, A::SparseMatrixCSC, perm)
length(perm) == nnz(A) ||
throw(DimensionMismatch("the sparsity pattern changed: $(nnz(A)) stored entries, $(length(perm)) expected"))
vals .= view(nonzeros(A), perm)
return vals
end
56 changes: 56 additions & 0 deletions test/Maxwell/cudss.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# The device-independent pieces of the cuDSS sweep solver: the CSR layout of the sweep operator, the
# per-frequency value gather, and the error without the extension. The GPU solve itself is in test/GPU.
using FiniteElementMethod, Ferrite, SparseArrays, LinearAlgebra, Test
using FiniteElementMethod: csr_layout, csr_values!

include(joinpath(@__DIR__, "..", "shared", "wr90.jl"))

# The CSR arrays of `A` (rows of the transpose's columns), built independently of csr_layout.
function reference_csr(A)
rowptr = ones(Int, size(A, 1) + 1)
colval, vals = Int[], eltype(A)[]
for i in 1:size(A, 1)
for j in 1:size(A, 2)
if any(==(i), rowvals(A)[nzrange(A, j)])
push!(colval, j)
push!(vals, A[i, j])
end
end
rowptr[i + 1] = length(colval) + 1
end
return rowptr, colval, vals
end

@testset "CSR layout of a nonsymmetric matrix with stored zeros" begin
A = sparse([1, 3, 2, 1, 3, 2], [1, 1, 2, 3, 3, 3], ComplexF64[1 + 2im, 3, 0, 4im, 5, 6], 3, 3)
rowptr, colval, perm = csr_layout(A)
@test rowptr isa Vector{Int32} && colval isa Vector{Int32}
ref_rowptr, ref_colval, ref_vals = reference_csr(A)
@test rowptr == ref_rowptr && colval == ref_colval
@test nonzeros(A)[perm] == ref_vals
@test length(colval) == nnz(A) # the stored zero A[2, 2] stays in the pattern
vals = similar(nonzeros(A))
A.nzval .*= 2im
@test csr_values!(vals, A, perm) == 2im .* ref_vals
@test_throws DimensionMismatch csr_values!(vals, sparse(A[1:2, 1:2]), perm)
end

@testset "CSR layout of the WR-90 sweep operator" begin
model = wr90(5.0, 20.0)
A = maxwell_operator(model, F10)
Ferrite.apply!(A, model.ch)
rowptr, colval, perm = csr_layout(A)
csr = SparseMatrixCSC(size(A)..., Int.(rowptr), Int.(colval), nonzeros(A)[perm]) # CSC of transpose(A)
@test transpose(csr) == A
vals = similar(nonzeros(A))
maxwell_operator!(A, model, 1.1 * F10) # a new frequency, the same pattern
Ferrite.apply!(A, model.ch)
@test transpose(SparseMatrixCSC(size(A)..., Int.(rowptr), Int.(colval), csr_values!(vals, A, perm))) == A
end

@testset "CuDSSSolver without CUDA" begin
@test CuDSSSolver().structure == "G"
@test CuDSSSolver(; structure = "S").structure == "S"
@test_throws "must be \"G\" or \"S\"" CuDSSSolver(; structure = "H")
@test_throws "using CUDA, CUDSS" solve_sweep(wr90(5.0, 20.0), [F10]; solver = CuDSSSolver())
end
19 changes: 1 addition & 18 deletions test/Maxwell/ports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,7 @@
# against closed forms (exp(+iωt), ports at z = 0 and z = L, broad wall along x).
using FiniteElementMethod, Ferrite, SparseArrays, LinearAlgebra, Test

const WR90_A, WR90_B = 22.86, 10.16 # mm
const F10 = 2π * 10.0e9
const BETA10 = 158.2383 # rad/m, TE10 at 10 GHz
const K010 = F10 / FiniteElementMethod.C0

# A WR-90 section of length `len` mm with cells of about `h` mm; the bottom face z = 0 is a wave port, the top
# face a wave port too unless `top` names another termination.
function wr90(h, len; top = :port, kwargs...)
n = ceil.(Int, (WR90_A, WR90_B, len) ./ h)
grid = generate_grid(Tetrahedron, Tuple(n), Vec(0.0, 0.0, 0.0), Vec(WR90_A, WR90_B, float(len)))
walls = ["left", "right", "front", "back"]
ports = [WavePort("bottom"; a_direction = (1, 0, 0), kwargs...)]
top === :port && push!(ports, WavePort("top"; a_direction = (1, 0, 0), kwargs...))
return MaxwellModel(
grid; pec = top === :pec ? [walls; "top"] : walls, absorbing = top === :abc ? ["top"] : String[],
ports, length_scale = 1.0e-3,
)
end
include(joinpath(@__DIR__, "..", "shared", "wr90.jl"))

phase_deg(z) = rad2deg(angle(z))
wrap_deg(x) = mod(x + 180, 360) - 180
Expand Down
20 changes: 20 additions & 0 deletions test/MaxwellGPU/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[deps]
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
CUDSS = "45b445bb-4962-46a0-9369-b4df9d0f772e"
Ferrite = "c061ca5d-56c9-439f-9c0e-210fe06d3992"
FiniteElementMethod = "e08948db-0ae8-498a-9b98-73fc7f237ce3"
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
CUDA = "6"
CUDSS = "0.8, 1"
Ferrite = "1.7"
JET = "0.9, 0.10, 0.11, 0.12"
LinearAlgebra = "1.10"
SafeTestsets = "0.1"
SparseArrays = "1.10"
Test = "1"
14 changes: 14 additions & 0 deletions test/MaxwellGPU/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# MaxwellGPU

Tests of `CuDSSSolver`, the cuDSS GPU solver of `solve_sweep` (the `FiniteElementMethodCUDSSExt` extension). The
group has its own environment (CUDA, CUDSS, JET) and is not part of `All` or of CI, which has no NVIDIA GPU.

Run it by hand on a machine with an NVIDIA GPU and a working CUDA driver, from the repository root:

```sh
GROUP=MaxwellGPU julia --project=. -e 'using Pkg; Pkg.test()'
```

Without a functional CUDA device (`CUDA.functional() == false`) the group only checks that the extension loads
and infers (JET), and the WR-90 sweep against UMFPACK is reported as skipped (one `Broken` test). On a CUDA
machine that comparison runs for `structure = "G"`, and also for `"S"` with CUDSS.jl 1.x.
46 changes: 46 additions & 0 deletions test/MaxwellGPU/cudss.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# The cuDSS sweep solver against UMFPACK on the WR-90 sweep. Without a functional CUDA device only the
# extension's loading and inference are checked.
using FiniteElementMethod, Ferrite, SparseArrays, LinearAlgebra, Test
using CUDA, CUDSS, JET

include(joinpath(@__DIR__, "..", "shared", "wr90.jl"))

const EXT = Base.get_extension(FiniteElementMethod, :FiniteElementMethodCUDSSExt)

@testset "extension loads and infers" begin
@test EXT !== nothing
model = wr90(5.0, 20.0)
A = maxwell_operator(model, F10)
B = FiniteElementMethod.port_excitations(model, F10)
solver = CuDSSSolver()
@test_opt target_modules = (EXT,) solver(A, B, nothing)
@test_call target_modules = (EXT,) solver(A, B, nothing)
Cache = EXT.CuDSSCache{ComplexF64, CUDSS.CudssSolver{ComplexF64, Int32}}
@test only(Base.return_types(solver, (typeof(A), typeof(B), Nothing))) == Tuple{Matrix{ComplexF64}, Cache}
@test only(Base.return_types(solver, (typeof(A), typeof(B), Cache))) == Tuple{Matrix{ComplexF64}, Cache}
@test_throws "SparseMatrixCSC" solver(Matrix(A), B, nothing) # dense A is not for cuDSS
end

# The complex symmetric LDLᵀ is documented from CUDSS.jl 1.0 on; the supported resolvable version today is 0.8,
# where only the general LU runs, so the gate skips "S" there.
const STRUCTURES = pkgversion(CUDSS) >= v"1" ? ["G", "S"] : ["G"]

if CUDA.functional()
@testset "cuDSS sweep equals UMFPACK ($structure)" for structure in STRUCTURES
model = wr90(3.0, 30.0)
ωs = 2π .* [9.0e9, 10.0e9, 11.0e9]
cpu = solve_sweep(model, ωs; fields = true)
calls = Ref(0)
cudss_solver = CuDSSSolver(; structure)
counting(A, B, cache) = (calls[] += 1; cudss_solver(A, B, cache))
gpu = solve_sweep(model, ωs; fields = true, solver = counting)
@test calls[] == length(ωs)
@test gpu.S ≈ cpu.S rtol = 1.0e-8
for k in eachindex(ωs)
@test gpu.fields[k] ≈ cpu.fields[k] rtol = 1.0e-8
end
end
else
@info "CUDA is not functional here; the cuDSS solve against UMFPACK is skipped"
@test_skip "cuDSS sweep equals UMFPACK"
end
22 changes: 22 additions & 0 deletions test/shared/wr90.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# The WR-90 waveguide model shared by the Maxwell and MaxwellGPU groups (exp(+iωt), ports at z = 0 and z = L,
# broad wall along x).
using FiniteElementMethod, Ferrite

const WR90_A, WR90_B = 22.86, 10.16 # mm
const F10 = 2π * 10.0e9
const BETA10 = 158.2383 # rad/m, TE10 at 10 GHz
const K010 = F10 / FiniteElementMethod.C0

# A WR-90 section of length `len` mm with cells of about `h` mm; the bottom face z = 0 is a wave port, the top
# face a wave port too unless `top` names another termination.
function wr90(h, len; top = :port, kwargs...)
n = ceil.(Int, (WR90_A, WR90_B, len) ./ h)
grid = generate_grid(Tetrahedron, Tuple(n), Vec(0.0, 0.0, 0.0), Vec(WR90_A, WR90_B, float(len)))
walls = ["left", "right", "front", "back"]
ports = [WavePort("bottom"; a_direction = (1, 0, 0), kwargs...)]
top === :port && push!(ports, WavePort("top"; a_direction = (1, 0, 0), kwargs...))
return MaxwellModel(
grid; pec = top === :pec ? [walls; "top"] : walls, absorbing = top === :abc ? ["top"] : String[],
ports, length_scale = 1.0e-3,
)
end
6 changes: 6 additions & 0 deletions test/test_groups.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@ versions = ["lts", "1", "pre"]

[Maxwell]
versions = ["lts", "1", "pre"]

# The cuDSS sweep solver: a CUDA sub-env, run by hand on a CUDA machine (see test/MaxwellGPU/README.md). Without a
# functional CUDA device it only checks that the extension loads and infers; the GPU solve is skipped.
[MaxwellGPU]
in_all = false
versions = ["1"]
Loading