What does the "simd" prefix mean in SceneKit?

There is an SCNNode category called SCNNode(SIMD) that declares some properties, such as simdPosition , simdRotation , etc. These seem to be duplicated properties of the original / normal position and rotation properties.

 @property(nonatomic) simd_float3 simdPosition API_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0)); @property(nonatomic) simd_float4 simdRotation API_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0)); 

What is the difference between position and simdPosition ? What does the prefix "simd" mean?

+11
source share
2 answers

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 :)

+24
source

SIMD is a small library built on top of vector types that you can import from <simd/simd.h> . This allows you to make more expressive and more efficient code.

For example, using SIMD, you can write

 simd_float3 result = a + 2.0 * b; 

instead

 SCNVector3 result = SCNVector3Make(ax + 2.0 * bx, ay + 2.0 * by, az + 2.0 * bz); 

In Objective-C, you cannot overload methods. That is, you cannot have both

 @property(nonatomic) SCNVector3 position; @property(nonatomic) simd_float3 position API_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0)); 

The new SIMD-based interface needed a different name and why SceneKit provides simdPosition .

+6
source

Source: https://habr.com/ru/post/989088/


All Articles