Skip to content

Add atomic memory orderings for Metal#79

Open
VEZY wants to merge 1 commit into
JuliaConcurrent:mainfrom
VEZY:codex/metal-atomic-ordering
Open

Add atomic memory orderings for Metal#79
VEZY wants to merge 1 commit into
JuliaConcurrent:mainfrom
VEZY:codex/metal-atomic-ordering

Conversation

@VEZY

@VEZY VEZY commented Jun 30, 2026

Copy link
Copy Markdown

Note: I used Codex with GPT-5.5 High to implement this feature (both on Metal.jl and here). The original issue for context is linked to building a TLAS on GPU with RayCore.jl (JuliaGeometry/Raycore.jl#19). The PR on Metal is here: JuliaGPU/Metal.jl#870. Please wait for this PR to be merged before merging this one.

Summary

This updates AtomixMetalExt so atomic operations on Metal arrays preserve the memory ordering requested through Atomix/UnsafeAtomics.

The extension previously dispatched to Metal atomic operations without forwarding the requested ordering. That makes relaxed counter-style atomics work, but it prevents acquire/release synchronization patterns from being represented correctly on Metal.

Changes:

  • Map Atomix/UnsafeAtomics orderings to Metal memory orders:
    • unordered / monotonic -> relaxed
    • acquire -> acquire
    • release -> release
    • acq_rel / acquire_release -> acq_rel
    • seq_cst / sequentially_consistent -> seq_cst
  • Pass the mapped ordering through modify!.
  • Pass success and failure orderings through replace!.
  • Implement Metal-backed get and set! using ordered Metal load/store operations.
  • Preserve relaxed Metal atomics for unordered/monotonic requests.
  • Add Metal extension tests covering get/set and ordering propagation.

Why

Atomix exposes memory ordering as part of its atomic API, and downstream GPU code relies on those orderings for synchronization-sensitive algorithms. In particular, algorithms that publish ordinary device-memory writes through an atomic operation need acquire/release or stronger semantics to make the writes visible to another workitem. This was my case when building a TLAS on GPU for performing all hits computations with a ray tracing algorithm (using RayCore).

Forwarding the requested ordering to Metal fixes those synchronization cases while keeping unordered/monotonic atomics mapped to relaxed Metal operations for fast counters and reductions.

Validation

  • Atomix Metal extension tests passed against a patched local Metal checkout (available here: Add ordered atomics JuliaGPU/Metal.jl#870).
  • Downstream RayCore.jl TLAS/BLAS Metal repro passed with Atomix orderings wired through to Metal.
  • ArchimedLight (my package) derived static TLAS comparison passed: CPU-built/adapted and direct Metal-built TLAS match for nodes, instances, all BLAS buffers, descriptors, and root AABB.

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.

1 participant