Skip to content

Assemble the linear operators on a KernelAbstractions device - #73

Merged
xtalax merged 7 commits into
gpu/kafrom
gpu/device-assembly
Nov 8, 2026
Merged

xtalax merged 7 commits into
gpu/kafrom
gpu/device-assembly

Conversation

@xtalax-droid

@xtalax-droid xtalax-droid commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Targets the long-lived feature branch gpu/ka. Nothing here merges to master until a Ferrite release contains Ferrite-FEM/Ferrite.jl#1496.

Builds on #70 (device kernel preparation, merged into gpu/ka) and on #69, #71 and #72 (merged to master, and merged into gpu/ka). The diff shows only the device-assembly layer.

What

FEMDiscretization(Ω, t; backend, precision, assembly) assembles the mass matrix M, the stiffness matrix K and the constant load F on a KernelAbstractions backend. discretize (and NonlinearProblem) then returns the same host DAEProblem with Float64 SparseMatrixCSC operators, so the solve paths are untouched. The default backend = nothing is today's host path.

  • Core: the backend, precision (Float32/Float64, default Float64) and assembly (:colored, the deterministic default, or :atomic) keywords. They are validated at construction and never forwarded to the problem. The core holds only stubs, whose error says what to load when the extension is missing. precision without backend is an error.

  • FiniteElementMethodKAExt: a DeviceAssemblyContext built from the host AssemblyContext:

    • the DofHandler adapted to the device, keeping the host dof numbering;
    • per-field CellValues and CellCaches distributed to workers;
    • element buffers as worker slabs of 3D device arrays.

    Each SubDofHandler is scattered with one @kernel launch per color, or one atomic launch. The kernel calls the same element routine as the host driver (_linear_element! from Separate the per-cell element routines from the op structs #72), with kernels prepared by device_kernel from Add KernelAbstractions extension: device kernel preparation #70 and taken from the host functions, so there is no second codegen pass. The device matrices share the host sparsity pattern, so only their values are copied back. Dirichlet rows are replaced on the host as before. The extension also checks that Ferrite's FerriteKAExt is loaded.

  • FiniteElementMethodAMDGPUExt: maps ROCBackend to ROCSparseMatrixCSC.

  • FiniteElementMethodGenericSparseArraysExt: maps other GPU backends, such as JLArrays, to GenericSparseMatrixCSC.

  • Not supported on the device yet, each raising an ArgumentError that names it: facet terms (Neumann and Robin conditions), nonlinear terms, sources that depend on time, SUPG, and dirichlet = :eliminate.

Documentation says "assembly runs on the device" and makes no speedup claim. Support status: KA.CPU and JLArrays are tested in CI, AMDGPU is tested on a Radeon 8060S (gfx1151), and CUDA, oneAPI and Metal are untested.

Tests

  • test/GPU/assembly.jl, through discretize on KA.CPU and JLArrays, covers three problems:

    • a 3D P1 hexahedron heat problem;
    • a 2D P2 triangle heat problem;
    • a two-field problem with P2 and P1 fields coupled by linear terms.

    Each runs with constant and time-dependent Dirichlet data, in Float32 and Float64, with colored and atomic assembly. Two checks per run:

    • M, K and F equal the host assembly within 1e-5 relative (Float32) and 1e-12 (Float64);
    • sol(t, x...) equals the host solution.

    It also checks that colored assembly is deterministic, and that a steady problem solves.

  • test/GPU/extension.jl checks that both extensions load, the keyword validation, the error for a Ferrite without GPU support, and the out-of-scope errors.

  • test/Frontend/backend.jl checks the error text when the extension is absent.

Results: GPU group 557/557, QA 21/21, Perf 28/28, Assembly 136/136, Frontend 778/778. The docs build cleanly.

CI: since gpu/ka dcb0e17 the PR workflows (Tests, Downgrade, Benchmarks) run for every base branch, so this PR now gets the full test matrix. Local evidence before that: GPU group 557/557 at head 2aee507 (after merging gpu/ka, which contains current master), and QA, Perf, Assembly and Frontend as listed above.

AMDGPU (by hand, Radeon 8060S gfx1151, ROCm/HIP 7.2, AMDGPU.jl 2.8.0, KA 0.9.45, Julia 1.12.1, Ferrite do/gpu-ci a8db9b1a): 59/59 kernel tests and 131/131 device-assembly tests. Largest relative errors against the host Float64 assembly:

Quantity Float32 Float64
M 1.4e-7 3e-16
K 3.0e-7 3e-16
F 1.9e-7 1e-16
solution 2.1e-7 2e-10

Timing (report only, no claim)

3D P1 hexahedron heat problem on Radeon 8060S, run alone. Device columns are Float32 and include the copy back to the host. The host column is the existing serial Float64 path; there is no Float32 host path to compare with. Best of 5 runs.

cells dofs host serial (Float64) device colored device atomic device setup (colored)
10^3 1331 1.55 ms 6.07 ms 0.48 ms 9584 ms (first compile)
20^3 9261 12.42 ms 6.63 ms 0.87 ms 7.46 ms
36^3 50653 76.28 ms 9.39 ms 5.50 ms 29.18 ms
57^3 195112 329.41 ms 37.51 ms 18.87 ms 95.19 ms

M and K are assembled once per problem, so this does not change end-to-end solve time materially.

Known limitations

  • Ferrite's KA API is unreleased and may change: distribute_to_workers, start_assemble(...; atomic), the device CellCache, and the assembler's adapt rules.
  • The Float32 grid copy rebuilds a DofHandler and relies on its layout matching the host one; an internal error is raised if it does not.
  • CPU CI cannot catch user functions that fail to compile for a GPU; on a real device the launch error names them.
  • Regions with several SubDofHandlers go through the same loop but have no dedicated test yet.
  • Worker counts and workgroup sizes are first choices, not tuned.

Merge gate

The package keeps Ferrite = "1.7". test/GPU and test/GPU/vendor/amdgpu take Ferrite from do/gpu-ci through [sources].


🤖 Posted by an AI agent · harness: Claude Code 2.1.318 · model: claude-opus-5-5
Review: unreviewed
Session: f7b46bdf-7cc3-44dd-b8a4-9205616ff6ac

t and others added 5 commits November 5, 2026 21:51
…embly

Agent-Harness: Claude Code 2.1.318
Agent-Model: claude-opus-5-5
Agent-Session: f7b46bdf-7cc3-44dd-b8a4-9205616ff6ac
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
…mbly

Agent-Harness: Claude Code 2.1.318
Agent-Model: claude-opus-5-5
Agent-Session: f7b46bdf-7cc3-44dd-b8a4-9205616ff6ac
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
…embly

Agent-Harness: Claude Code 2.1.318
Agent-Model: claude-opus-5-5
Agent-Session: f7b46bdf-7cc3-44dd-b8a4-9205616ff6ac
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Device assembly is chosen on the discretization like every other option (design D1): backend = nothing keeps the host path unchanged, the keywords are validated at construction and never forwarded to the problem, and the core holds only stubs whose error says what to load. discretize refuses the features device assembly does not cover yet before assembling anything.

Agent-Harness: Claude Code 2.1.318
Agent-Model: claude-opus-5-5
Agent-Session: f7b46bdf-7cc3-44dd-b8a4-9205616ff6ac
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
FiniteElementMethodKAExt builds a DeviceAssemblyContext from the host AssemblyContext (DofHandler adapted with the host numbering, per-field CellValues and CellCaches distributed to workers, element buffers as slabs of 3D device arrays) and scatters each SubDofHandler with one launch per color, or one atomic launch, calling the host driver's element routine. The device matrices share the host pattern, so only the values are copied back into the Float64 operators; Dirichlet rows are then replaced on the host as before. The AMDGPU extension maps ROCBackend to ROCSparseMatrixCSC; GenericSparseArrays serves backends without a sparse library (JLArrays). The GPU environments take Ferrite from the PR #1496 branch.

Agent-Harness: Claude Code 2.1.318
Agent-Model: claude-opus-5-5
Agent-Session: f7b46bdf-7cc3-44dd-b8a4-9205616ff6ac
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
@xtalax-droid
xtalax-droid changed the base branch from gpu/devicepass to gpu/ka November 6, 2026 15:35
Agent-Harness: Claude Code 2.1.319
Agent-Model: claude-opus-5-5
Agent-Session: f7b46bdf-7cc3-44dd-b8a4-9205616ff6ac
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
@xtalax-droid
xtalax-droid marked this pull request as ready for review November 6, 2026 16:38
Agent-Harness: Claude Code 2.1.319
Agent-Model: claude-sonnet-5-5
Agent-Session: 43e03fde-3384-4386-bae9-03e81f19ba7c
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Benchmark Results (Julia v1)

Time benchmarks
master 8e4e923... master / 8e4e923...
construction/heat P1 10x10 8.05 ± 1.2 ms 7.4 ± 0.59 ms 1.09 ± 0.18
construction/heat P1 20x20 10.8 ± 1 ms 9.72 ± 0.58 ms 1.12 ± 0.12
time_to_load 5.94 ± 0.26 s 5.41 ± 0.11 s 1.1 ± 0.052
Memory benchmarks
master 8e4e923... master / 8e4e923...
construction/heat P1 10x10 0.0397 M allocs: 2.13 MB 0.0397 M allocs: 2.13 MB 1
construction/heat P1 20x20 0.0706 M allocs: 4.75 MB 0.0706 M allocs: 4.75 MB 1
time_to_load 0.201 k allocs: 12.2 kB 0.201 k allocs: 12.2 kB 1

@xtalax
xtalax merged commit 02044c6 into gpu/ka Nov 8, 2026
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants