SIMD: multiple instructions with multiple instructions
SIMD instructions allow you to perform the same operation with multiple values ββat the same time .
Let's see an example
Sequential Approach (NO SIMD)
We have these 4 Int32 values.
let x0: Int32 = 10 let y0: Int32 = 20 let x1: Int32 = 30 let y1: Int32 = 40
Now we want to sum the values ββof 2 x and 2 y , so we write
let sumX = x0 + x1 // 40 let sumY = y0 + y1 // 60
To complete the 2 previous sums CPU must
- load x0 and x1 into memory and add them
- load y0 and y1 into memory and add them
Thus, the result is obtained using two operations.
SIMD
Now let's see how SIMD works. First of all, we need input values ββstored in the correct SIMD format, therefore
let x = simd_int2(10, 20) let y = simd_int2(30, 40)
As you can see, the previous x and y are vectors. Infact and x and y contain 2 components.
Now we can write
let sum = x + y
See what the processor does to complete the previous steps.
- load x and y into memory and add them
What is it!
Both components x and both components y processed simultaneously .
Concurrent programming
We are NOT talking about parallel programming, but this is real parallel programming .
As you can imagine in a certain operation, the SIMD approach is faster than the serial one.
Scene set
Now let's see an example in SceneKit
We want to add 10 to the x , y and z components of all the direct descendants of the node scene.
Using the classic sequential approach , we can write
for node in scene.rootNode.childNodes { node.position.x += 10 node.position.y += 10 node.position.z += 10 }
All childNodes.count * 3 operations are performed here.
Let's see how we can convert the previous code into SIMD instructions
let delta = simd_float3(10) for node in scene.rootNode.childNodes { node.simdPosition += delta }
This code is much faster than the previous one. Iβm not sure that itβs 2 or 3 times faster, but believe me, this is better.
Wrap up
If you need to perform the same operation several times with a different value, just use the SIMD properties :)