Running inline ML operations in a shader with Metal 4
At a glance
| Item | Summary |
|---|---|
| Purpose | Multiply matrices across multiple GPU cores with inline tensor operations. |
| App architecture | A C/Objective-C header, Metal, Objective-C sample centered on main, with direct use of Metal, MetalPerformancePrimitives, metal_stdlib. |
| Main patterns | No named application pattern supported by the extracted structure |
| Project style | 8 scanned source file(s) across C/Objective-C header, Metal, Objective-C, organized around ranked entry, type, and file boundaries. |
| Execution model | No structured execution marker indexed; callback threading requires source review. |
| State/event model | No structured observation or publisher-scheduling marker indexed. |
| Key frameworks/packages | Foundation, Metal, metal_stdlib, MetalPerformancePrimitives; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── Entry point/
│ └── main.m
├── Matrix/
│ ├── Matrix.m
│ ├── Matrix+TensorUtilities.h
│ ├── Matrix+TensorUtilities.m
│ └── Matrix.h
├── Matrix multiplier/
│ ├── MatrixMultiplier.h
│ ├── MatrixMultiplier.m
│ └── Shaders.metal
├── Configuration/
│ └── SampleCode.xcconfig
└── Shader Inline ML.xcodeproj/
├── .xcodesamplecode.plist
└── project.pbxproj
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: C/Objective-C header, Metal, Objective-C.
- The verified tree contains 3 project/configuration file(s) and 2 source declaration(s).
Overall architecture
flowchart LR
N1["main"]
N2["Metal / MetalPerformancePrimitives APIs"]
N1 --> N2
Reference code
Entry point/main.m:73 — architecture anchor
int main(void) {
@autoreleasepool {
Matrix *matrix1 = createRandomMatrix(512, 512);
Matrix *matrix2 = createRandomMatrix(512, 512);
[matrix1 printWithName:@"Matrix 1"];
[matrix2 printWithName:@"Matrix 2"];
Matrix *cpuProduct = multiplyMatricesOnCPU(matrix1, matrix2);
Matrix *gpuProduct = multiplyMatricesOnGPU(matrix1, matrix2);
if (cpuProduct == nil || gpuProduct == nil) {
return EXIT_FAILURE;
}
[cpuProduct printWithName:@"CPU Product"];
[gpuProduct printWithName:@"GPU Product"];
BOOL productsMatch = compareResults(gpuProduct, cpuProduct);
return productsMatch ? EXIT_SUCCESS : EXIT_FAILURE;
}
}Interpretation
The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into Metal.
Ownership and state
classDiagram
Matrix o-- NSInteger : rows
Matrix o-- NSInteger : columns
Ownership evidence
Matrix/Matrix.h:19 — stored dependency or nearest verified ownership anchor
@interface Matrix : NSObject
// ...
@property (nonatomic, readonly) NSInteger rows;
// ...
@end| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
Matrix |
NSInteger (rows) |
stores or receives | The declaring implementation writes; property clients read |
Matrix |
NSInteger (columns) |
stores or receives | The declaring implementation writes; property clients read |
Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.
Concurrency, scheduling, and thread safety
Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.
No source-visible execution, scheduling, or synchronization boundary was found in the indexed source.
@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.
State propagation, frameworks, and dependencies
Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.
| Category | Mechanism or module | Verified role | Evidence |
|---|---|---|---|
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | Entry point/main.m:10 |
| Source import | Metal |
The cited file imports this module; runtime use and architectural role are not inferred. | Matrix multiplier/MatrixMultiplier.h:9 |
| Source import | metal_stdlib |
The cited file imports this module; runtime use and architectural role are not inferred. | Matrix multiplier/Shaders.metal:8 |
| Source import | MetalPerformancePrimitives |
The cited file imports this module; runtime use and architectural role are not inferred. | Matrix multiplier/Shaders.metal:10 |
receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.
Class and protocol design
Matrix multiplier/MatrixMultiplier.h:19 — representative type boundary
@interface MatrixMultiplier : NSObject
// ...
@end| Type | Responsibility | Depends on or conforms to |
|---|---|---|
MatrixMultiplier |
Defines a feature-specific type boundary | NSObject |
Matrix |
Defines a feature-specific type boundary | NSObject |
No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
rows (Matrix/Matrix.h:19) |
header-visible |
The declaration is exposed to translation units that import the header. | Inference: declare a contract needed by other Objective-C/C translation units. |
columns (Matrix/Matrix.h:22) |
header-visible |
The declaration is exposed to translation units that import the header. | Inference: declare a contract needed by other Objective-C/C translation units. |
Matrix (Matrix/Matrix.m:11) |
language/file boundary |
Visibility follows header/implementation and language linkage rules. | Inference: the language’s file or module boundary is sufficient for this sample collaboration. |
Reference code
Matrix/Matrix.h:19 — representative boundary
@interface Matrix : NSObject
// ...
@property (nonatomic, readonly) NSInteger rows;
// ...
@endSwift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.
Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Feature and framework orchestration | main |
The sample keeps the demonstrated path in its primary concrete type. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| No named application pattern | Entry point/main.m:73 |
The verified source directly composes concrete framework types; this document avoids forcing a pattern name. |
Main application flow
sequenceDiagram
participant MatrixMultiplier
participant device
participant commandBuffer
participant encoder
participant outputTensordimensions
participant queue
participant event
MatrixMultiplier->>device: newCommandBuffer()
MatrixMultiplier->>commandBuffer: beginCommandBufferWithAllocator()
MatrixMultiplier->>commandBuffer: useResidencySet()
MatrixMultiplier->>commandBuffer: computeCommandEncoder()
MatrixMultiplier->>encoder: setComputePipelineState()
MatrixMultiplier->>outputTensordimensions: extentAtDimensionIndex()
MatrixMultiplier->>queue: commit()
MatrixMultiplier->>event: waitUntilSignaledValue()
Reference code
Matrix multiplier/MatrixMultiplier.m:176 — encodeAndRunComputation()
- (void)encodeAndRunComputation {
// ...
id<MTL4CommandBuffer> commandBuffer = [device newCommandBuffer];
[commandBuffer beginCommandBufferWithAllocator:commandAllocator];
[commandBuffer useResidencySet:residencySet];
id<MTL4ComputeCommandEncoder> encoder = [commandBuffer computeCommandEncoder];
[encoder setComputePipelineState:pipelineState];
[encoder setArgumentTable:argumentTable];
NSInteger tileSizeY = 64;
NSInteger tileSizeX = 64;
NSInteger numSIMDGroups = 4;
NSInteger outputWidth = [outputTensor.dimensions extentAtDimensionIndex:0];
NSInteger outputHeight = [outputTensor.dimensions extentAtDimensionIndex:1];
MTLSize threadgroups = MTLSizeMake((outputWidth + tileSizeX - 1) / tileSizeX,
(outputHeight + tileSizeY - 1) / tileSizeY,
1);
NSUInteger simdGroupSize = pipelineState.threadExecutionWidth;
[encoder dispatchThreadgroups:threadgroups
threadsPerThreadgroup:MTLSizeMake(numSIMDGroups * simdGroupSize, 1, 1)];
[encoder endEncoding];
[commandBuffer endCommandBuffer];
[queue commit:&commandBuffer count:1];
signaledValue++;
[queue signalEvent:event value:signaledValue];
[event waitUntilSignaledValue:signaledValue timeoutMS:1000];
}Naming conventions
- Types: feature-specific names rather than reusable layer suffixes.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
data,matrixWithRandomRows,initWithRows,initWithData,initFromTensor,multiplyWithMatrix,isEqualToMatrix,printWithName. - Files:
Matrix/Matrix.m,Matrix multiplier/MatrixMultiplier.h,Matrix multiplier/MatrixMultiplier.m,Matrix/Matrix.h.
Architecture takeaways
mainis the main source-visible entry or composition anchor for this sample.- Framework work reaches Metal, MetalPerformancePrimitives, metal_stdlib through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
- Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
- Access-control conclusions separate verified language visibility from the likely design rationale.
- The source does not justify labeling the design protocol-oriented.
Source map
| Source file | Relevant symbols |
|---|---|
Entry point/main.m |
Cited implementation, Foundation, Feature implementation |
Matrix/Matrix.h |
Cited implementation, rows, columns, Matrix |
Matrix multiplier/MatrixMultiplier.h |
MatrixMultiplier, Metal |
Matrix/Matrix.m |
Matrix |
Matrix multiplier/Shaders.metal |
metal_stdlib, MetalPerformancePrimitives, Feature implementation |
Matrix multiplier/MatrixMultiplier.m |
MatrixMultiplier |
Matrix/Matrix+TensorUtilities.h |
Matrix |
Matrix/Matrix+TensorUtilities.m |
Matrix |