So the idea of the data layout transformation
is to reorganize the way data is laid out in memory to facilitate better memory performance.
So a really common example of this data layout transformation
is something we've seen before where we take data,
which is laid out as an array of structures, like so.
An array of structures.
And transform it into a structure of arrays.
Here's what those layouts would look like in memory.
In the first case,
the different fields in the structure are laid out one after another.
Each structure is adjacent.
In the next case, they're all laid out adjacently.
So as a quick quiz,
which of these 2 layouts: the array of structures or AOS or the structure of arrays, SOA,
will perform better on the little code snippets I've given here?
So you set i equal to the thread index.
And then we access field a for a given thread.
And then we access field b, c, and d.
And then over here I've done the same code,
but I've transformed it to use this data layout.
So the question is to check off, which of these 2 data layouts you think will perform better?
所以数据布局转换就是重新组织数据
在内存分布的方式,以促进更好的内存性能。一个很常见的
这种数据布局转换的例子,就是我们之前看到过的,
我们取以结构数组形式分布的数据,像这样。一个结构数组,
把它转换成数组结构。
这就是这些布局在内存中的样子。在第一个例子中,
结构中的字段一个接一个地分布,每个结构是相邻的。
在下一个例子中,他们全都相邻分布。做个快速测验,
在两种布局结构数组(AOS)和数组结构(SOA) 中,
哪一种在运行我这里提供的小代码片段时性能更好?
你将 i 设为线程索引。然后我们在给定线程存取字段 a。
我们再存取字段 b、c 和 d。接着来到这儿,
我编写了同样的代码,但是我已把它转换成这种数据布局。所以问题是,检查一下,
这两种数据布局中,你认为哪一种性能更好。