“simd”是什么? SceneKit中的前缀是什么意思? [英] What does the "simd" prefix mean in SceneKit?

查看:543
本文介绍了“simd”是什么? SceneKit中的前缀是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个名为 SCNNode(SIMD) SCNNode 类别,它声明了一些属性,如 simdPosition simdRotation 等等。看起来这些是原始/普通属性的重复属性位置轮换

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

位置和<$之间有什么区别? C $ C> simdPosition ?前缀simd到底是什么意思?

解决方案

SIMD:单指令多数据



SIMD指令允许您在同时对多个值执行相同的操作。



让我们看一个示例



串行方式(无SIMD)



我们有这4个Int32值

 让x0:Int32 = 10 
让y0:Int32 = 20

让x1:Int32 = 30
让y1:Int32 = 40

现在我们想要求和2 x 和2 y 值,所以我们写

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

为了执行前两个总和,CPU需要



  1. 在内存中加载x0和x1并添加它们

  2. 在内存中加载y0和y1并添加它们




所以结果是通过2次操作获得的。



我创建了一些图形来更好地向你展示这个想法


第1步



第2步




SIMD



现在让我们看看SIMD是如何做的工作。
首先,我们需要以正确的SIMD格式存储输入值,所以

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

正如你可以看到的那样 x y 是向量。事实上, x y 包含2个组件。



现在我们可以写

 让sum = x + y 

让我们看看CPU为了执行前面的操作所做的工作



  1. 在内存中加载x和y并添加它们




就是这样!



x <的两个组成部分/ code>并且 y 的两个组件同时处理



并行编程



我们 NOT 谈论并发编程,而这是真正的并行编程



正如您在某些操作中可以想象的那样,SIMD方法比ser更快ial one。



场景套件



现在让我们看一下SceneKit中的一个例子



我们要将 10 添加到 x y z 场景节点的所有直接后代的组件。



使用经典串行方法我们可以为scene.rootNode.childNodes {
节点中的节点写

  .position.x + = 10 
node.position.y + = 10
node.position.z + = 10
}




此处共执行 childNodes.count * 3 操作。


现在让我们看看如何在SIMD指令中转换以前的代码

  let delta = simd_float3(10)
for scene.rootNode.childNodes中的节点{
node.simdPosition + = delta
}

此代码比前一代码快得多。我不确定2倍或3倍的速度,但相信我,它会更好。



总结



如果您需要对不同的值执行多次相同的操作,只需使用SIMD属性:)


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?

解决方案

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

Step 2

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

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

这篇关于“simd”是什么? SceneKit中的前缀是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆