Add the following to your Package.swift:
.package(url: "https://github.com/PawScale/kernels.git", from: "1.0.0")Then add it as a dependency to your target:
.target(
name: "YourTarget",
dependencies: ["Kernels"]
)Create a .metal file with your compute kernel:
kernel void vector_add(
device const float* a [[buffer(0)]],
device const float* b [[buffer(1)]],
device float* c [[buffer(2)]],
uint id [[thread_position_in_grid]]
) {
c[id] = a[id] + b[id];
}import Kernels
let kernel = MetalKernel(name: "vector_add")
let result = kernel.execute(inputs: [vectorA, vectorB])The framework provides:
- MetalKernel: Main class for loading and executing Metal compute kernels
- GPUBuffer: Wrapper for GPU memory management
- ComputeContext: GPU device and command queue management
- Batch operations to reduce kernel launch overhead
- Use appropriate grid and threadgroup sizes
- Minimize data transfer between CPU and GPU
- Profile your kernels using Xcode's Metal debugger
See the Examples directory for complete working examples.
Ensure your Metal library is properly bundled and the kernel name matches exactly.
Check that buffer sizes match your kernel's expectations and that you're not exceeding GPU memory limits.
Use Xcode's Metal profiler to identify bottlenecks and optimize accordingly.