Skip to content

(feat): Metal.jl (Apple Silicon GPU) backend support#34

Merged
mgyoo86 merged 4 commits intomasterfrom
feat/Metal_support
Mar 14, 2026
Merged

(feat): Metal.jl (Apple Silicon GPU) backend support#34
mgyoo86 merged 4 commits intomasterfrom
feat/Metal_support

Conversation

@mgyoo86
Copy link
Member

@mgyoo86 mgyoo86 commented Mar 14, 2026

Summary

Adds Metal.jl backend support for AdaptiveArrayPools, enabling zero-allocation GPU array pooling on Apple Silicon. Same checkpoint/rewind semantics and @with_pool macro support as CUDA.

What's new

  • @with_pool :metal — pool GPU arrays on Apple Silicon with the same API as :cuda
  • Shared GPU backend layer (src/gpu_backend_common.jl) — extracted common algorithms (_gpu_claim_slot!, _gpu_get_cached_array!, _gpu_cached_reshape!, _gpu_invalidate_released_slots!) used by both CUDA and Metal extensions
  • Julia 1.11+ requirement for GPU extensions — both CUDA and Metal extensions now gate on VERSION >= v"1.11-" (requires setfield!-based Array internals)
  • 6 fixed-slot types: Float32, Float16, Int32, Int64, ComplexF32, Bool (Float64/ComplexF64 not supported on Metal hardware)
  • Full safety parity: poisoning, invalidation, escape detection, borrow tracking
  • Lazy & typed-lazy mode support with Float16 tracked via bit 7

Usage

using AdaptiveArrayPools, Metal

@with_pool :metal p begin
    A = acquire!(p, Float32, 1024, 1024)
    A .= 1.0f0
    # A is automatically returned to pool at end of block
end

Known limitations

  • No Float64/ComplexF64: Apple Silicon GPUs do not natively support 64-bit floating point
  • Single-device only: tested on single Apple GPU (multi-device untested)
  • Metal.jl internals: depends on MtlArray internal fields (:data, :maxsize, :offset, :dims, .data.rc), guarded with @static checks

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Metal.jl (Apple Silicon GPU) backend support for AdaptiveArrayPools, mirroring the existing CUDA extension. Also extracts shared GPU pooling algorithms into a common layer (gpu_backend_common.jl) used by both CUDA and Metal extensions, and gates both GPU extensions on Julia 1.11+.

Changes:

  • New Metal extension (ext/AdaptiveArrayPoolsMetalExt/) with types, acquire, state management, safety, display, and convenience functions for MtlArray pooling
  • Extracted shared GPU backend common layer (src/gpu_backend_common.jl) with _gpu_claim_slot!, _gpu_get_cached_array!, _gpu_cached_reshape!, _gpu_invalidate_released_slots!, refactoring CUDA to use the same shared code
  • Added Julia 1.11+ version gate to both CUDA and Metal GPU extensions

Reviewed changes

Copilot reviewed 35 out of 36 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
Project.toml Add Metal weak dependency and compat
src/AdaptiveArrayPools.jl Include gpu_backend_common.jl, export Metal stubs
src/gpu_backend_common.jl New shared GPU pooling algorithms (hooks + implementations)
src/task_local_pool.jl Add Metal task-local pool function stubs
src/utils.jl Add _show_wrapper_cache, loop over GPU backends in pool_stats()
ext/AdaptiveArrayPoolsMetalExt/*.jl Full Metal extension (types, acquire, state, debug, macros, convenience, utils, task_local_pool)
ext/AdaptiveArrayPoolsCUDAExt/*.jl Refactored to use shared GPU backend hooks, added Julia 1.11 gate
test/metal/*.jl Comprehensive Metal test suite
test/runtests.jl, test/cuda/runtests.jl Version gates and Metal test inclusion
docs/ Metal documentation and navigation updates
README.md Add Metal to supported backends

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

@codecov
Copy link

codecov bot commented Mar 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.53%. Comparing base (6cffe33) to head (13c2cf2).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           master      #34   +/-   ##
=======================================
  Coverage   96.53%   96.53%           
=======================================
  Files          14       14           
  Lines        2566     2568    +2     
=======================================
+ Hits         2477     2479    +2     
  Misses         89       89           
Files with missing lines Coverage Δ
src/AdaptiveArrayPools.jl 100.00% <ø> (ø)
src/task_local_pool.jl 100.00% <ø> (ø)
src/utils.jl 93.68% <100.00%> (+0.13%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

mgyoo86 added 3 commits March 13, 2026 23:22
Standalone Metal extension with full parity to CUDA: checkpoint/rewind,
lazy mode, typed-lazy mode, safety (poisoning/escape detection/borrow
tracking), @with_pool :metal, DisabledPool{:metal}, and task-local
multi-device pools. No shared GPU common layer — each backend is
self-contained to preserve type stability and zero-alloc guarantees.
CUDA extension imports modern-path-only functions (_store_arr_wrapper!,
_reshape_impl!, _check_pool_growth) that don't exist on Julia 1.10 LTS.
Wrap all includes in @static if VERSION >= v"1.11-" to prevent
precompilation failure on older Julia.
Replace `return`-based early exit (unreliable inside @static if on 1.10)
with if-else branching to properly skip GPU tests on Julia LTS.
@mgyoo86 mgyoo86 force-pushed the feat/Metal_support branch from 974ec5e to 733a41f Compare March 14, 2026 06:35
- Add docs/src/features/metal-support.md with full Metal API reference
- Fix cuda-support.md: replace incorrect N-way cache description with
  actual arr_wrappers-based direct-index caching mechanism
- Add Metal backend to README supported backends and doc links
- Add Metal page to docs/make.jl navigation and path mappings
@mgyoo86 mgyoo86 merged commit 2d870d3 into master Mar 14, 2026
14 checks passed
@mgyoo86 mgyoo86 deleted the feat/Metal_support branch March 14, 2026 06:50
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