Add native Metal backend for macOS on Apple silicon#3709
Open
saarlemo wants to merge 2 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request adds a native Metal compute backend for ArrayFire on Apple silicon using Apple's metal-cpp headers. This integrates Metal with the backend-specific and Unified APIs, with Metal preferred over OpenCL on macOS.
Description
This is a new backend feature for macOS arm64 systems, plus one small portability fix in shared API code.
The PR adds a native Metal backend targeting macOS 12 or later on Apple silicon. It introduces the
AF_BUILD_METALbuild option, theArrayFire::afmetalCMake target, the publicAF_BACKEND_METALbackend enum value, CMake package export support, and Unified API loading/selection support. The Unified backend priority is updated to:as OpenCL support is deprecated on MacOS devices. The Metal backend implementation uses modular
.metalsource files that are embedded and compiled through ArrayFire's source-generation infrastructure. It covers core ArrayFire functionality including array operations and JIT evaluation, reductions, scans, sorting, indexing, FFT and convolution, BLAS and linear algebra, image processing and computer vision, sparse arrays, and random-number generation.The PR also updates Metal build, usage, priority, and limitation documentation. Building the backend requires macOS 12 or later, Apple silicon (
arm64, M1 or newer), Xcode or Xcode Command Line Tools, C++17, and Apple's header-only metal-cpp distribution.The shared confidence-connected implementation is adjusted to rely on standard
std::absoverload resolution instead of explicit template arguments. This keeps the code portable across Apple clang/libc++ and GCC/libstdc++ without changing the intended numerical behavior.Initial version of code included in this PR was written manually, with the final version having been reviewed and formatted with assistance of the GPT-5.5 large language model.
Current Metal backend limitations:
Resolves #2878.
Changes to Users
AF_BUILD_METALCMake option.ArrayFire::afmetalCMake target and Metal backend library.AF_BACKEND_METALto the public backend enumeration.METALCPP_INCLUDE_DIR.Checklist