Surge is a Swift library, similar to numpy, that uses the Accelerate framework to provide high-performance functions for matrix math, digital signal processing, and image manipulation.
Surge aims to bring Accelerate to the mainstream, making it as easy (and nearly as fast, in most cases) to perform computation over a set of numbers as for a single member.
Curious about the name Surge? Back in the mid 90's, Apple, IBM, and Motorola teamed up to create AltiVec (a.k.a the Velocity Engine), which provided a SIMD instruction set for the PowerPC architecture. When Apple made the switch to Intel CPUs, AltiVec was ported to the x86 architecture and rechristened Accelerate. The derivative of Accelerate (and second derivative of Velocity) is known as either jerk, jolt, surge, or lurch, hence the name of this library.
Use Swift Package Manager
Surge functions are named according to their corresponding "Math.h" functions, where applicable (omitting
f
andd
affixes, since type information is communicated and enforced by the language's type system).
sum
asum
max
min
mean
meamg
measq
add
sub
mul
div
mod
remainder
sqrt
sum of squared values
abs
ceil
copysign
floor
rec
round
trunc
conv
xcorr
exp
exp2
log
log2
log10
logb
fft
sinh
cosh
tanh
asinh
acosh
atanh
add
mul
inv
transpose
negate
You can also initialize matrices with generator functions for the elements, thus enabling the user to create matrices from gaussian distributions for example.
pow
sincos
sin
cos
tan
asin
acos
atan
rad2deg
deg2rad
import Surge
let n = [1.0, 2.0, 3.0, 4.0, 5.0]
let sum = Surge.sum(n) // 15.0
import Surge
let a = [1.0, 3.0, 5.0, 7.0]
let b = [2.0, 4.0, 6.0, 8.0]
let product = Surge.mul(a, b) // [2.0, 12.0, 30.0, 56.0]
Initial benchmarks on iOS devices and the iOS simulator indicate significant performance improvements over a conventional Swift implementation.
import Surge
let numbers: [Double] = ...
var sum: Double = 0.0
// Naïve Swift Implementation
sum = reduce(numbers, 0.0, +)
// Surge Implementation
sum = Surge.sum(numbers)
(Time in milliseconds, Optimization Level -Ofast
)
n | Swift | Surge | Δ |
---|---|---|---|
100 | 0.269081 | 0.004453 | ~60x |
100000 | 251.037254 | 0.028687 | ~9000x |
100000000 | 239474.689326 | 57.009841 | ~4000x |
Surge's performance characteristics have not yet been thoroughly evaluated, though initial benchmarks show incredible promise. Further investigation is definitely warranted.
Surge is available under the MIT license. See the LICENSE file for more info.