1. Background

In a prior posting, I presented how to use Swift combined with Metal for general-purpose GPU-based processing; this posting presents related benchmarks — compared to using the Accelerate Framework — on iPhone 5S, iPhone 6, iPad Air, and iPad Mini.

All Swift/Metal and Accelerate code and benchmarks can be found in the GitHub repository:

2. GPGPU Performance on Swift/Metal vs Accelerate

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. The performance results compare (1) Metal + Swift performance with (2) the Accelerate framework (numerical methods such as BLAS that are highly optimized), where both approaches calculate the Sigmoid function (frequently occurring in machine learning settings, e.g., for Deep Learning and Kernel Methods / Support Vector Machines).

Performance data for the curves below can be found in simplebenchmark.

2.1 Swift+Metal Throughput — Sigmoid Vector Elements per Second

Swift+Metal Sigmoid throughput across iPhone 5S, iPhone 6, iPad Air, and iPad Mini

Observation: iPhone 6 (with A8 CPU/GPU) has almost twice the performance of A7-based devices for larger arrays (of length 221–222).

2.2 Swift+Metal Compared to Accelerate-Based Approach

Relative speedup of Swift+Metal compared to Apple Accelerate framework

Observation: All devices are from 2 to 20 times more processing efficient with Swift+Metal compared to using Accelerate, and speedup increases with larger amounts of data.

3. Swift Code for Sigmoid with Metal and Accelerate

See ViewController.swift for the primary code.

3.1 Sigmoid Function in Metal Shader Language

kernel void sigmoid(const device float *inVector [[ buffer(0) ]],
                    device float *outVector [[ buffer(1) ]],
                    uint id [[ thread_position_in_grid ]]) {
    outVector[id] = 1.0 / (1.0 + exp(-inVector[id]));
}

3.2 Sigmoid Function Using Accelerate and Swift

vvexpf(&expMinusX, &mynegativeVector, &localcount)
cblas_saxpy(Int32(oneVec.count), 1.0, &expMinusX, 1, &oneVec, 1)
vvpowf(&finalResultVector, &negOneVec, &oneVec, &localcount)

3.3 Sigmoid Function Implemented in Pure Swift

for j in 0..<ccount {
    fra[j] = 1.0 / (1.0 + exp(-myvector[j]))
}

4. Conclusion

Metal combined with Swift delivers substantial performance for non-graphical processing as well: once set up, it is easier to use than Accelerate and can give significant speedups (up to 20× in this example). Compared to CPU-based processing in pure Swift, the performance gains are even higher — up to 75× faster on iPhone 6.

Amund Tveit — Memkite Team