Apple describes Metal as:

"Metal provides the lowest-overhead access to the GPU, enabling you to maximize the graphics and compute potential of your iOS 8 app. With a streamlined API, precompiled shaders, and support for efficient multi-threading, Metal can take your game or graphics app to the next level of performance and capability." — developer.apple.com/metal

1. Calculating the Sigmoid Function with Metal and Swift

Metal is an alternative to OpenGL for graphics processing, and for general data-parallel programming on GPUs it is an alternative to OpenCL and CUDA. This example shows how to use Metal with Swift to calculate the Sigmoid function, which frequently occurs in machine learning settings such as Deep Learning and Kernel Methods / Support Vector Machines.

If you want to read up on Metal, check out developer.apple.com/metal (Metal Programming Guide, Metal Shading Language, and Metal Framework Reference).

2. Metal and Swift Code for Calculating Sigmoid

The implementation is divided into 8 parts:

  1. Function for setting up Metal
  2. Sigmoid function in Shaders.metal
  3. Prepare original input data — a Swift array
  4. Prepare Sigmoid function to run on GPU
  5. Create GPU input and output data
  6. Configure GPU threads
  7. Wrap up encoding setup, start processing, and wait until finished
  8. Get result data out from GPU and into Swift

2.1 Function for Setting Up Metal

func initMetal() -> (MTLDevice, MTLCommandQueue, MTLLibrary, MTLCommandBuffer,
                     MTLComputeCommandEncoder) {
    // Get access to iPhone or iPad GPU
    var device = MTLCreateSystemDefaultDevice()

    // Queue to handle an ordered list of command buffers
    var commandQueue = device.newCommandQueue()

    // Access to Metal functions that are stored in Shaders.metal file, e.g. sigmoid()
    var defaultLibrary = device.newDefaultLibrary()

    // Buffer for storing encoded commands that are sent to GPU
    var commandBuffer = commandQueue.commandBuffer()

    // Encoder for GPU commands
    var computeCommandEncoder = commandBuffer.computeCommandEncoder()

    return (device, commandQueue, defaultLibrary!, commandBuffer, computeCommandEncoder)
}

2.2 Sigmoid Function in Shaders.metal

kernel void sigmoid(const device float *inVector [[ buffer(0) ]],
                    device float *outVector [[ buffer(1) ]],
                    uint id [[ thread_position_in_grid ]]) {
    // This calculates sigmoid for _one_ position (=id) in a vector per call on the GPU
    outVector[id] = 1.0 / (1.0 + exp(-inVector[id]));
}

2.3 Prepare Original Input Data — a Swift Array

var myvector = [Float](count: 123456, repeatedValue: 0)
for (index, value) in enumerate(myvector) {
    myvector[index] = Float(index)
}

2.4 Prepare Sigmoid Function to Run on GPU

// a. initialize Metal
var (device, commandQueue, defaultLibrary, commandBuffer, computeCommandEncoder) = initMetal()

// b. set up a compute pipeline with Sigmoid function and add it to encoder
let sigmoidProgram = defaultLibrary.newFunctionWithName("sigmoid")
var pipelineErrors = NSErrorPointer()
var computePipelineFilter = device.newComputePipelineStateWithFunction(sigmoidProgram!, error: pipelineErrors)
computeCommandEncoder.setComputePipelineState(computePipelineFilter!)

2.5 Create GPU Input and Output Data

// a. calculate byte length of input data - myvector
var myvectorByteLength = myvector.count * sizeofValue(myvector[0])

// b. create a MTLBuffer - input data that the GPU and Metal consume
var inVectorBuffer = device.newBufferWithBytes(&myvector, length: myvectorByteLength, options: nil)

// c. set the input vector for the Sigmoid() function, e.g. inVector
//    atIndex: 0 here corresponds to buffer(0) in the Sigmoid function
computeCommandEncoder.setBuffer(inVectorBuffer, offset: 0, atIndex: 0)

// d. create the output vector for the Sigmoid() function, e.g. outVector
//    atIndex: 1 here corresponds to buffer(1) in the Sigmoid function
var resultdata = [Float](count: myvector.count, repeatedValue: 0)
var outVectorBuffer = device.newBufferWithBytes(&resultdata, length: myvectorByteLength, options: nil)
computeCommandEncoder.setBuffer(outVectorBuffer, offset: 0, atIndex: 1)

2.6 Configure GPU Threads

// hardcoded to 32 for now (recommendation: read about threadExecutionWidth)
var threadsPerGroup = MTLSize(width: 32, height: 1, depth: 1)
var numThreadgroups = MTLSize(width: (myvector.count + 31) / 32, height: 1, depth: 1)
computeCommandEncoder.dispatchThreadgroups(numThreadgroups, threadsPerThreadgroup: threadsPerGroup)

2.7 Wrap Up Encoding Setup, Start Processing, and Wait Until Finished

computeCommandEncoder.endEncoding()
commandBuffer.commit()
commandBuffer.waitUntilCompleted()

2.8 Get Result Data Out from GPU and into Swift

// a. Get GPU data
// outVectorBuffer.contents() returns UnsafeMutablePointer roughly equivalent to char* in C
var data = NSData(bytesNoCopy: outVectorBuffer.contents(),
                  length: myvector.count * sizeof(Float), freeWhenDone: false)

// b. prepare Swift array large enough to receive data from GPU
var finalResultArray = [Float](count: myvector.count, repeatedValue: 0)

// c. get data from GPU into Swift array
data.getBytes(&finalResultArray, length: myvector.count * sizeof(Float))

// d. YOU'RE ALL SET!

Hope you find it useful.

Amund Tveit (@atveit) — Memkite Team