# GPGPU Performance of Swift/Metal vs Accelerate on iPhone 6 & 5S, iPad Air and iPad Mini

- Canonical URL: https://amund.blog/archive-memkite-swift-metal-gpgpu-performance/
- Author: Amund Tveit (https://amund.blog/about/)
- Published: 2014-12-18
- Tags: Apple Silicon, GPU, Swift, Metal
- Original Source: memkite.com (http://memkite.com/blog/2014/12/18/gpgpu-performance-of-swiftmetal-vs-accelerate-on-iphone-6-5s-ipad-air-and-ipad-mini/)

---

## 1. Background

In a [prior posting](/archive-memkite-data-parallel-metal-swift/), 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](https://developer.apple.com/library/ios/documentation/Accelerate/Reference/AccelerateFWRef/_index.html) — 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:

- [github.com/atveit/SwiftMetalGPUParallelProcessing](https://github.com/atveit/SwiftMetalGPUParallelProcessing)

## 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](http://www.netlib.org/blas/) that are highly optimized), where both approaches calculate the [Sigmoid function](http://en.wikipedia.org/wiki/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](https://github.com/atveit/SwiftMetalGPUParallelProcessing/tree/master/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](./metal-throughput.png)

*Observation: iPhone 6 (with A8 CPU/GPU) has almost twice the performance of A7-based devices for larger arrays (of length 2<sup>21</sup>–2<sup>22</sup>).*

### 2.2 Swift+Metal Compared to Accelerate-Based Approach

![Relative speedup of Swift+Metal compared to Apple Accelerate framework](./metal-relative-speedup.png)

*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](https://github.com/atveit/SwiftMetalGPUParallelProcessing/blob/master/SwiftMetalProcessing/ViewController.swift) for the primary code.

### 3.1 Sigmoid Function in Metal Shader Language

```metal
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

```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

```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*
