Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

ios - What does the "simd" prefix mean in SceneKit?

There is a SCNNode category named SCNNode(SIMD), which declares some properties like simdPosition, simdRotation and so on. It seems these are duplicated properties of the original/normal properties position and rotation.

@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's the difference between position and simdPosition? What does the prefix "simd" mean exactly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

SIMD: Single Instruction Multiple Data

SIMD instructions allow you to perform the same operation on multiple values at the same time.

Let's see an example

Serial 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 2 x and the 2 y values, so we write

let sumX = x0 + x1 // 40
let sumY = y0 + y1 // 60

In order to perform the 2 previous sums the CPU needs to

  1. load x0 and x1 in memory and add them
  2. load y0 and y1 in memory and add them

So the result is obtained with 2 operations.

I created some graphics to better show you the idea

Step 1

enter image description here

Step 2

enter image description here

SIMD

Let's see now how SIMD does work. First of all we need the input values stored in the proper SIMD format so

let x = simd_int2(10, 20)
let y = simd_int2(30, 40)

As you can see the previous x and y are vectors. Infact both x and y contain 2 components.

Now we can write

let sum = x + y

Let's see what the CPU does in order to perform the previous operations

  1. load x and y in memory and add them

enter image description here

That's it!

Both components of x and both components of y are processed at the same time.

Parallel Programming

We are NOT talking about concurrent programming, instead this is real parallel programming.

As you can imagine in certain operation the SIMD approach is way faster then the serial one.

Scene Kit

Let's see now an example in SceneKit

We want to add 10 to the x, y and z components of all the direct descendants of the scene node.

Using the classic serial approach we can write

for node in scene.rootNode.childNodes {
    node.position.x += 10
    node.position.y += 10
    node.position.z += 10
}

Here a total of childNodes.count * 3 operations is executed.

Let's see now how we can convert the previous code in SIMD instructions

let delta = simd_float3(10)
for node in scene.rootNode.childNodes {
    node.simdPosition += delta
}

This code is much faster then the previous one. I am not sure whether 2x or 3x faster but, believe me, it's way better.

Wrap up

If you need to perform several times the same operation on different value, just use the SIMD properties :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...