Sample CodemacOSReviewed 2026-07-21View on Apple Developer

Performing calculations on a GPU

At a glance

Item Summary
Purpose Use Metal to find GPUs and perform calculations on them.
App architecture Objective-C host code with Metal shaders; Command-line main hands work to MetalAdder, which owns command-line MetalAdder orchestration before parallel add compute kernel.
Main patterns Command object, Compute pipeline, Host-shader data contract
Scope High-level review of 4 scanned source files and 2 detected declarations; build assets are omitted.

Project structure

Source bundle/
└── MetalComputeBasic/
    ├── main.m
    ├── add.metal
    ├── MetalAdder.m
    └── MetalAdder.h

Structure observations

  • The entry/composition boundary and renderer or operation boundary are separate in the source; MetalAdder is the principal feature coordinator.
  • GPU-specific logic stays in Metal shader files; shared headers bridge host/shader layouts where present.
  • The tree above is intentionally pruned to composition, resource, and shader files.

Overall architecture

Reference code

MetalComputeBasic/main.m:32 — feature handoff or setup anchor

        MetalAdder* adder = [[MetalAdder alloc] initWithDevice:device];
        
        // Create buffers to hold data
        [adder prepareData];
        
        // Send a command to the GPU to perform the calculation.
        [adder sendComputeCommand];

Interpretation

This is the dominant control/data path: platform code composes MetalAdder; that object controls command-line MetalAdder orchestration; GPU-visible work ends in parallel add compute kernel. The arrows summarize responsibility transfer, not a claim that every node directly calls the next.

Ownership and state

Ownership evidence

MetalComputeBasic/main.m:32main creates for a local scope adder

        MetalAdder* adder = [[MetalAdder alloc] initWithDevice:device];
        
        // Create buffers to hold data
        [adder prepareData];
        
        // Send a command to the GPU to perform the calculation.
        [adder sendComputeCommand];
Owner Object or state Relationship Mutation authority
main adder / MetalAdder Creates for a local scope; the diagram uses composition because construction is source-visible. The declaring scope performs setup and replacement.
MetalAdder Feature-specific framework and Metal resources Operation-local calls or stored state; exclusive lifetime is not assumed beyond cited evidence. Feature setup/encoding code controls mutation and command submission.

Class and protocol design

Type Responsibility Depends on or conforms to Source
MetalAdder encapsulates the feature operation and its framework/GPU resources. NSObject MetalComputeBasic/MetalAdder.h:13

No local protocol abstraction is present; the sample uses concrete framework, operation, or model types at this boundary.

Access control

Symbol Access Verified effect Design reason
MetalAdder header-visible available to translation units that import the header; this is not Swift public. Keep the usable surface no wider than the collaboration requires. (MetalComputeBasic/MetalAdder.h:13)
MetalAdder implementation details implementation-only native helpers, stored state, or registration code stays out of the imported header contract. Hide native implementation details from importing translation units. (MetalComputeBasic/MetalAdder.m:1)
add entry points Metal library boundary host code resolves named shader entry points; Swift access modifiers do not apply. Expose only named shader entry points needed by pipeline creation. (MetalComputeBasic/add.metal:11)

This sample does not use Swift private, fileprivate, or public for its native boundary. Objective-C/Objective-C++ use header versus implementation placement, C++ uses access specifiers/linkage, Python uses module conventions, and Metal entry points cross a compiled-library boundary; these are not Swift access levels.

Logic ownership and placement

Logic Owning type or file Why it lives there
Encapsulates the feature operation and its framework/GPU resources MetalAdderMetalComputeBasic/MetalAdder.h:13 The type’s callbacks and stored state align with this responsibility.
Command-line MetalAdder orchestration MetalAdder / MetalComputeBasic/main.m:32 Keeps Metal/framework setup and encoding out of entry or lifecycle code.
Parallel add compute kernel MetalComputeBasic/add.metal:11 GPU-parallel code remains in the Metal compilation boundary.

Shader boundary reference

MetalComputeBasic/add.metal:11 — representative GPU entry/helper

kernel void add_arrays(device const float* inA,
                       device const float* inB,
                       device float* result,
                       uint index [[thread_position_in_grid]])
{
    // the for-loop is replaced with a collection of threads, each of which
    // calls this function.
    result[index] = inA[index] + inB[index];
}

Design patterns

Pattern Source evidence Purpose or tradeoff
Command object MetalComputeBasic/main.m:32 Makes the sample’s command-line MetalAdder orchestration an explicit, reviewable boundary.
Compute pipeline MetalComputeBasic/main.m:32 Makes the sample’s command-line MetalAdder orchestration an explicit, reviewable boundary.
Host-shader data contract MetalComputeBasic/main.m:1 Shared indices/structs and matching bindings couple host encoding to shader signatures deliberately.

Naming conventions

  • Role suffixes make ownership visible: MetalAdder.
  • Method names describe setup or encoding actions: initWithDevice, prepareData, sendComputeCommand, encodeAddCommand, generateRandomFloatData, verifyResults.
  • Shader entry points use stage/operation names: add_arrays.
  • Names favor concrete domain roles and target-local types; no broad public library namespace is introduced.

Architecture takeaways

  • Keep Command-line main focused on composition; MetalAdder is the owner of command-line MetalAdder orchestration.
  • Treat parallel add compute kernel as a separate execution/compilation boundary with explicit resource and data-layout contracts.
  • The verified ownership edge is mainadder; broader exclusive ownership is not inferred.
  • Access-control rationale follows concrete language boundaries rather than translating every header or shader symbol into Swift terms.

Source map

Source file Architectural role
MetalComputeBasic/main.m entry point or feature implementation
MetalComputeBasic/add.metal entry point or feature implementation
MetalComputeBasic/MetalAdder.m MetalAdder
MetalComputeBasic/MetalAdder.h MetalAdder