import MetalKit //---------------------------------------------------------------------- // Setup let device = MTLCreateSystemDefaultDevice()! let commandQueue = device.makeCommandQueue()! let library = try device.makeLibrary(filepath: "compute.metallib") //---------------------------------------------------------------------- // pipeline let commandBuffer = commandQueue.makeCommandBuffer()! let encoder = commandBuffer.makeComputeCommandEncoder()! encoder.setComputePipelineState(try device.makeComputePipelineState(function: library.makeFunction(name: "add")!)) //---------------------------------------------------------------------- // Set Data let input: [Float] = [1.0, 2.0] encoder.setBuffer(device.makeBuffer(bytes: input as [Float], length: MemoryLayout.stride * input.count, options: []), offset: 0, index: 0) let outputBuffer = device.makeBuffer(length: MemoryLayout.stride, options: [])! encoder.setBuffer(outputBuffer, offset: 0, index: 1) //---------------------------------------------------------------------- // Run Kernel let numThreadgroups = MTLSize(width: 1, height: 1, depth: 1) let threadsPerThreadgroup = MTLSize(width: 1, height: 1, depth: 1) encoder.dispatchThreadgroups(numThreadgroups, threadsPerThreadgroup: threadsPerThreadgroup) encoder.endEncoding() commandBuffer.commit() commandBuffer.waitUntilCompleted() //---------------------------------------------------------------------- // Results let result = outputBuffer.contents().load(as: Float.self) print(String(format: "%f + %f = %f", input[0], input[1], result))