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

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

问题描述

有一个名为SCNNode(SIMD)SCNNode类,它声明了一些属性,如simdPositionsimdRotation等等.似乎这些是原始/正常属性 positionrotation 的重复属性.

@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));

positionsimdPosition 有什么区别?前缀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

为了执行前面的 2 个 sums,CPU 需要

<块引用>

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

所以结果是通过2次操作得到的.

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

<块引用>

第一步

<块引用>

第 2 步

SIMD

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

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

如你所见,前面的 xy 是向量.事实上,xy 都包含 2 个组件.

现在我们可以写

让总和 = x + y

让我们看看 CPU 做了什么来执行前面的操作

<块引用>

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

就是这样!

x 的两个组件和 y 的两个组件同时处理.

并行编程

我们不是谈论并发编程,而是真正的并行编程.

正如您可以想象的那样,在某些操作中,SIMD 方法比串行方法要快得多.

场景套件

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

我们想将 10 添加到所有直接后代的 xyz 组件中场景节点.

使用经典的串行方法我们可以编写

用于scene.rootNode.childNodes中的节点{node.position.x += 10node.position.y += 10节点.位置.z += 10}

<块引用>

这里一共执行了childNodes.count * 3次操作.

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

let delta = simd_float3(10)对于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天全站免登陆