Skip to content

Add KernelAbstractions extension: device kernel preparation - #70

Merged
xtalax merged 2 commits into
gpu/kafrom
gpu/devicepass
Nov 6, 2026
Merged

xtalax merged 2 commits into
gpu/kafrom
gpu/devicepass

Conversation

@xtalax-droid

Copy link
Copy Markdown
Collaborator

Targets the long-lived feature branch gpu/ka. It merges to master only after a Ferrite release contains Ferrite-FEM/Ferrite.jl#1496.

This is the first layer of GPU support: preparing FEM's generated kernels so they can run inside a KernelAbstractions kernel. There is no device assembly yet; that comes in a later PR.

What changes

  • New weak dependencies KernelAbstractions, Adapt, GPUArrays and GPUArraysCore, with compat 0.9, 4, 11 and 0.2 (KernelAbstractions matches Ferrite #1496's pin). They trigger the extension FiniteElementMethodKAExt. There is no version bump and no other compat change.
  • The extension takes a kernel Expr from emit_kernels(...; expression = Val(true)) and prepares it for precision T:
    • Float64 and Rational literals are retyped to T. The kernel's own time argument, which is named T, is never touched.
    • NaNMath.pow is rewritten to an FEM-owned nanpow, a copy of the NaNMath 1.1.5 pure-Julia body. It gives NaN for a negative base with a non-integer exponent instead of throwing. Integer powers through Base.literal_pow are left as they are.
    • NaNMath.lgamma, eight verified ccall-backed SpecialFunctions names, closures and ccalls are refused with an ArgumentError when the kernel is prepared.
    • The function is built with RuntimeGeneratedFunction(FEM, FEM, ex), the cache Symbolics uses, and then drop_expr. The result is asserted to be isbits.
  • foreign_callees and launch_checked turn a compile failure in a user-registered function into an error that names the function.
  • Core gains only the internal stub device_kernel(ex, T). Without the extension it says which packages to load.

Tests

New opt-in group GPU (in_all = false) on Julia 1 only, with its own test/GPU/Project.toml that develops FEM through [sources].

The kernels come from real PDESystems: heat with a parameter and a Neumann facet kernel, SUPG with CellCoeffs, nonlinear f and tangent, NaNMath.pow with constant and symbolic exponents, time-dependent Dirichlet data, and a registered function.

  • devicepass.jl (host, 169 tests):
    • After the pass, every emitted kernel has no Float64 literal, closure, ccall or NaNMath.pow.
    • Every prepared kernel is isbits, and so is a LinearBlock of prepared kernels; a KernelSpec is not.
    • Float32 inputs give no Float64 output, within 1e-6 relative of the Float64 kernel.
    • nanpow matches NaNMath, while ^ throws a DomainError on the host.
    • lgamma, closures and ccalls are refused.
    • The callee listing and the error message are checked.
  • backends.jl (113 tests): every kernel inside a @kernel on KernelAbstractions.CPU() and on JLArrays matches the host Float64 kernel, in Float32 and Float64. A small check shows that GenericSparseMatrixCSC with JLArray storage works.

The repository requires an end-to-end test for every code path. For this layer that test is: real PDESystem, then the emitted kernels, then the device pass, then a KernelAbstractions kernel whose result equals the host kernel. End-to-end tests through discretize arrive with device assembly.

JLArrays and KA.CPU test host discipline only, as test/GPU/README.md explains. A kernel that would not compile for a GPU passes on both.

Other groups: GPU 169/169 and 113/113, QA 21/21, Kernels 346/346. Runic 1.13 is clean on every touched file.

AMDGPU (by hand: test/GPU/vendor/amdgpu)

Tested on Radeon 8060S (gfx1151). Julia 1.12.1, AMDGPU.jl 2.8.0, KernelAbstractions 0.9.45, HIP 7.2.53211. The memory pool is capped at a 6 GiB soft and 8 GiB hard limit.

  • 59/59 pass: 28 kernels × {Float32, Float64}, a registered function that GPU code can compile, and the negative test.
  • Max relative error against the host Float64 kernels: Float32 1.5e-7, Float64 2.1e-16.
  • Negative test: a registered function that allocates a Dict fails with
    ArgumentError: a generated kernel failed to compile for ROCBackend; it calls the user functions Main.badf, which must be GPU-compilable (no allocation, Dict, String or ccall). Original error: InvalidIRError: ...

CUDA, oneAPI and Metal are untested.

Known limitations

  • The test helper in test/GPU/common/cases.jl copies the start of FEM._lower and calls internals, so it can drift if _lower changes. End-to-end tests through discretize will remove that risk. Its Dirichlet Exprs copy the body of emit_dirichlet, which Add expression keyword to emit_dirichlet #69 replaces with an expression keyword.
  • launch_checked attributes any first-launch error to the listed user functions; the original error is kept in the message.
  • Kernels with constant Dirichlet data still return Int. The device driver converts values when it stores them.

🤖 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 2 commits November 5, 2026 20:30
FiniteElementMethodKAExt (triggers KernelAbstractions, Adapt, GPUArrays, GPUArraysCore) prepares an emitted kernel Expr for a device: Float64 and Rational literals are retyped to the working precision, NaNMath.pow becomes a FEM-owned nanpow, library ccalls such as NaNMath.lgamma are refused, and the function is built in FEM's RuntimeGeneratedFunctions cache with drop_expr so it is isbits. Core gains only the device_kernel stub, which names the packages to load when the extension is absent.

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>
Kernels of real PDESystems go through the device pass and a KernelAbstractions kernel on KA.CPU() and JLArrays, and must match the host Float64 kernels (Float32 within 1e-6). test/GPU/vendor/amdgpu runs the same files on ROCBackend by hand, including a Dict-allocating registered function whose compile failure must name it.

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
xtalax merged commit fd5867d into gpu/ka Nov 6, 2026
6 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