Skip to content

[KernelInterface] barrier: add optional memory flags - #983

Merged
christiangnrd merged 5 commits into
mainfrom
granularbarrier
Nov 20, 2026
Merged

christiangnrd merged 5 commits into
mainfrom
granularbarrier

Conversation

@christiangnrd

@christiangnrd christiangnrd commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Backends with support for these flags (OpenCL, Metal) can implement it, while those that don't (AMDGPU, CUDA) keep the full barrier.

The goal for this implementation was to mimic OpenCL's interface. Open to suggestions for improvements.

The KA version will be a follow-up

@github-actions

Copy link
Copy Markdown
Contributor

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark results

Compared with 301c8f3b8, the latest measured ancestor of the merge base (1 commit before it).
The environment differs from the baseline's: GPUCompiler 2.23.3 → 2.23.4, Hwloc_jll 2.15.0+3 → 2.16.0+0, KernelInterface 0.4.6 → 0.4.7, LLVM 10.4.3 → 10.6.0, LLVMExtra_jll 0.0.56+0 → 0.0.59+0, Libiconv_jll 1.18.0+0 → 1.18.1, SPIRVIntrinsics 1.6.0 → 1.6.1.

Times are the minimum time of every benchmark. A change is unusual when it exceeds both three times the benchmark's typical change between consecutive runs on the default branch and a minimum per category (performance 3%, latency 5%, applications 3%, other 3%). A benchmark that starts or stops allocating is listed however few the allocations. A single run can still be a fluke, so rerun before drawing conclusions.

Geometric mean Unusual: slower Unusual: faster Compared
Performance −0.5% 0 0 32
Latency +1.5% 1 0 13
Applications −0.5% 0 0 5
Benchmark Baseline This PR Change
Latency
host/import/ka 602 ms 704 ms +17.0% 🔴 slower
All benchmarks
Benchmark Baseline This PR Change
Performance
julia/serial/arithmetic/muladd-reference 655 µs 655 µs +0.0%
julia/serial/math/exp-f64-reference 283 µs 291 µs +2.9%
julia/serial/narrow/masked-reference 708 µs 707 µs −0.1%
pocl/parallel/atomics/histogram 5.25 ms 5.24 ms −0.2%
pocl/parallel/groupreduce/add-f64 1.09 ms 1.09 ms +0.2%
pocl/parallel/groupreduce/fallback 4.39 ms 4.38 ms −0.2%
pocl/parallel/groupscan/add-i32 391 µs 392 µs +0.1%
pocl/parallel/indexing/cartesian3d 40.2 µs 40.6 µs +1.0%
pocl/parallel/indexing/linear3d 40.2 µs 40.7 µs +1.2%
pocl/parallel/indexing/ntuple3d 39.8 µs 39.4 µs −1.0%
pocl/parallel/indexing/ntuple4d 129 µs 127 µs −1.2%
pocl/parallel/localmem/transpose-naive 438 µs 434 µs −1.0%
pocl/parallel/localmem/transpose-tiled 66.4 µs 59.8 µs −10.0%
pocl/parallel/private/across-barrier 42.3 µs 42.2 µs −0.2%
pocl/parallel/scheduling/default 117 µs 117 µs −0.0%
pocl/parallel/scheduling/explicit 117 µs 117 µs +0.1%
pocl/parallel/stream/copy/cache 700 ns 657 ns −6.1%
pocl/parallel/stream/copy/dram 6.50 ms 6.25 ms −3.7%
pocl/parallel/stream/triad/cache 1.16 µs 1.13 µs −2.3%
pocl/parallel/stream/triad/dram 9.55 ms 9.55 ms −0.0%
pocl/parallel/util/copyto-small 25.0 ns 24.0 ns −4.0%
pocl/parallel/util/zeros-large 915 µs 949 µs +3.8%
pocl/parallel/util/zeros-small 631 ns 655 ns +3.8%
pocl/serial/arithmetic/contract 1.85 ms 1.85 ms +0.0%
pocl/serial/arithmetic/muladd 705 µs 705 µs +0.0%
pocl/serial/const/marked 668 µs 668 µs +0.0%
pocl/serial/const/unmarked 859 µs 860 µs +0.1%
pocl/serial/math/exp-f64 123 µs 123 µs +0.1%
pocl/serial/math/sin-f64 167 µs 170 µs +2.0%
pocl/serial/narrow/masked 633 µs 634 µs +0.2%
pocl/serial/narrow/unmasked 629 µs 626 µs −0.5%
pocl/serial/stream/triad/dram 8.51 ms 8.56 ms +0.6%
Latency
host/import/ka 602 ms 704 ms +17.0% 🔴 slower
host/precompile/ka 27.0 s 27.1 s +0.3%
host/startup 128 ms 129 ms +0.6%
pocl/parallel/launch/dynamic-default 171 ns 164 ns −4.1%
pocl/parallel/launch/groupreduce-f64 773 ns 771 ns −0.3%
pocl/parallel/launch/many-args 677 ns 660 ns −2.5%
pocl/parallel/launch/partial-workgroup 219 ns 233 ns +6.4%
pocl/serial/compile/groupreduce 1.49 s 1.52 s +1.8%
pocl/serial/compile/localmem-barrier 675 ms 701 ms +3.9%
pocl/serial/compile/new-kernel 128 ms 127 ms −0.9%
pocl/serial/compile/new-workgroupsize 8.66 ms 8.82 ms +1.9%
pocl/serial/first-kernel/cold-cache 407 ms 403 ms −1.0%
pocl/serial/first-kernel/warm-cache 331 ms 325 ms −2.0%
Applications
pocl/parallel/dgsem/fluxdiff-node 253 µs 245 µs −3.5%
pocl/parallel/dgsem/weak-element 90.2 µs 92.4 µs +2.4%
pocl/parallel/dgsem/weak-node-localmem 92.8 µs 94.7 µs +2.0%
pocl/parallel/stencil/timestep 7.38 ms 7.28 ms −1.4%
pocl/serial/stencil/timestep 10.5 ms 10.3 ms −1.7%

History: https://juliagpu.org/perf/?pkg=kernelabstractions

@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.27%. Comparing base (b81493f) to head (3d60ed9).
⚠️ Report is 12 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #983      +/-   ##
==========================================
+ Coverage   85.61%   89.27%   +3.65%     
==========================================
  Files          31       33       +2     
  Lines        2969     3012      +43     
==========================================
+ Hits         2542     2689     +147     
+ Misses        427      323     -104     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@christiangnrd
christiangnrd force-pushed the granularbarrier branch 2 times, most recently from 12e5d9b to 3b7f428 Compare November 19, 2026 14:02
@christiangnrd christiangnrd linked an issue Nov 19, 2026 that may be closed by this pull request
@maleadt

maleadt commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

After a quick look: seems worth it. Local-only barriers give 8–11% on the base matmul and ~15% on @groupreduce-style trees on M1, and 8–12% on trees on an Arc A770 (IGC emits a fence per flag). AMDGPU could honour them too (HIP does). CUDA can't, and PoCL ignores them. So I'm in favour of barrier(flags).

Not so sure about NO_MEM_FENCE: it's no faster than LOCAL_MEM_FENCE on Metal, gives wrong results on Intel, and silently works on CUDA/PoCL.

Also not a fan of sub_group_barrier(flags) for now. Nothing calls sub_group_barrier yet, and the form tiles will use, sub_group_barrier(width), doesn't get flags here. That one needs more thought: on Metal/oneAPI/OpenCL a narrow-segment barrier is only a fence, so the flags would be the whole operation. Maybe leave it out and add (flags) and (width, flags) together once tiles want them?

This also conflicts with #990 (KI 0.5), so we need to pick a merge order and version, obviously. oneAPI.jl can get the same two-line override as JuliaGPU/OpenCL.jl#617.

@christiangnrd

christiangnrd commented Nov 20, 2026 •

Copy link
Copy Markdown
Member Author

So I'm in favour of barrier(flags).

Yay!

Not so sure about NO_MEM_FENCE: it's no faster than LOCAL_MEM_FENCE on Metal, gives wrong results on Intel, and silently works on CUDA/PoCL.

I had claude add this so it could test on Metal that no flags gave errors and with flags worked, but f you'd rather I remove it, lmk. Edit: I've removed it since giving wrong results on intel is not desirable behaviour

Also not a fan of sub_group_barrier(flags) for now. ... Maybe leave it out and add (flags) and (width, flags) together once tiles want them?

Yeah I wasn't sure about this one either. I'll stick to barrier for this PR and we can revisit the sub-group barrier memory flags later. Edit: subgroup memory flags removed

This also conflicts with #990 (KI 0.5), so we need to pick a merge order and version

If we can get this out quickly, I'd like to have this as 0.4.7 since it's non-breaking, and once this is in, I can deprecate Metal.jl's scalar algorithm and have it point to GPUArrays implementation that'll be either identical or faster.

@christiangnrd
christiangnrd force-pushed the granularbarrier branch 2 times, most recently from 79c9773 to 125c1a8 Compare November 20, 2026 17:45
christiangnrd and others added 5 commits November 20, 2026 14:20
Add `KI.barrier(flags)`, which orders only the memory in `flags`:
`KI.LOCAL_MEM_FENCE`, `KI.GLOBAL_MEM_FENCE` or both.
Backends may implement it; the fallback is the full `barrier()`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The barriers of `@groupreduce` and `@groupscan` only order accesses to their
`KI.localmemory` storage, so they use `KI.barrier(KI.LOCAL_MEM_FENCE)`.
This requires KernelInterface 0.4.7.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The barriers of the histogram and matrix multiplication examples only order
accesses to local memory.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@christiangnrd
christiangnrd merged commit 0a9aaa5 into main Nov 20, 2026
31 of 32 checks passed
@christiangnrd
christiangnrd deleted the granularbarrier branch November 20, 2026 19:58
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.

KernelInterface: Add memory flag options to barrier

2 participants