简体   繁体   English

结构数组总是比数组的结构更快?

[英]Array of Structs are always faster than Structs of arrays?

I was wondering if the data layout Structs of Arrays ( SoA ) is always faster than an Array of Structs ( AoS ) or Array of Pointers ( AoP ) for problems with inputs that only fits in RAM programmed in C/JAVA . 我想知道如果数据布局Structs of Arrays (SOA)总是比更快Array of Structs (AOS)Array of Pointers用于与输入,只配合在问题(AOP) RAM在编程C/JAVA

Some days ago I was improving the performance of a Molecular Dynamic algorithm (in C), summarizing in this algorithm it is calculated the force interaction among particles based on their force and position. 几天前,我正在改进分子动力学算法(在C中)的性能,总结在该算法中,基于它们的力和位置计算粒子之间的力相互作用。

Original the particles were represented by a struct containing 9 different doubles, 3 for particles forces (Fx,Fy,Fz) , 3 for positions and 3 for velocity. 原始粒子由包含9个不同双精度的结构表示,3表示粒子力(Fx,Fy,Fz),3表示位置,3表示速度。 The algorithm had an array containing pointers to all the particles ( AoP ). 该算法有一个包含指向所有粒子( AoP )的指针的数组。 I decided to change the layout from AoP to SoA to improve the cache use. 我决定从AOP布局更改为SOA来提高缓存的使用。

So, now I have a Struct with 9 array where each array stores forces, velocity and positions (x,y,z) of each particle. 所以,现在我有一个带有9个数组的数组,其中每个数组存储每个粒子的力,速度和位置(x,y,z)。 Each particle is accessed by it own array index. 每个粒子都由它自己的数组索引访问。

I had a gain in performance (for an input that only fits in RAM) of about 1.9x , so I was wondering if typically changing from AoP or AoS to SoA it will always performance better, and if not in which types of algorithms this do not occurs. 我在性能增益(对于只在RAM适合的输入)的1.9倍左右,所以我在想,如果一般从AOPAOS更改为永远的性能更好的SOA它,如果不是在哪种类型的算法做这不会发生。

Much depends of how useful all fields are. 很大程度上取决于所有领域的有用性。 If you have a data structure where using one fields means you are likely to use all of them, then an array of struct is more efficient as it keeps together all the things you are likely to need. 如果你有一个数据结构,其中使用一个字段意味着你可能会使用所有这些,那么一个struct数组更有效,因为它将你可能需要的所有东西保持在一起。

Say you have time series data where you only need a small selection of the possible fields you have. 假设您有时间序列数据,您只需要选择一小部分可能的字段。 You might have all sorts of data about an event or point in time, but you only need say 3-5 of them. 您可能拥有关于某个事件或时间点的各种数据,但您只需要说3-5个。 In this case a structure of arrays is more efficient because a) you don't need to cache the fields you don't use b) you often access values in order ie caching a field, its next value and its next is useful. 在这种情况下,数组的结构更有效,因为a)您不需要缓存您不使用的字段b)您经常按顺序访问值,即缓存字段,其下一个值和下一个值是有用的。

For this reason, time-series information is often stored as a collection of columns. 因此,时间序列信息通常存储为列的集合。

This will depend on how exactly you access the data. 这取决于您访问数据的准确程度。 Try to imagine, what exactly happens in the hardware when you access your data, in either SoA or AoS. 试着想象一下,当你访问数据时,在SoA或AoS中,硬件究竟会发生什么。

To reason about your question, you must consider following things - 要推断您的问题,您必须考虑以下事项 -

  1. If the cache is absent, the performance should be the same, assuming that memory access latency is equal for all the elements of the data. 如果缓存不存在,则性能应该相同,假设数据的所有元素的内存访问延迟相等。
  2. Now with the cache, if you access consecutive address locations, definitely you will get performance improvement. 现在使用缓存,如果您访问连续的地址位置,肯定会获得性能提升。 This is exactly valid in your case. 这在您的情况下完全有效。 When you have AoS, The locations are not consecutive in the memory, so you must lose some performance there. 当你有AoS时,内存中的位置不连续,所以你必须在那里失去一些性能。
  3. You must be accessing in for loops your data like for(int i=0;i<1000000;i++) Fx[i] = 0 . 你必须访问for循环你的数据,如for(int i=0;i<1000000;i++) Fx[i] = 0 So if the data is huge in quantity, you will easily see the small performance benefits. 因此,如果数据量很大,您将很容易看到小的性能优势。 If your data was small, this would not matter much. 如果您的数据很小,这无关紧要。
  4. Finally, you also don't know about the DRAM that you are using. 最后,您还不了解您正在使用的DRAM。 It will have some benefits when you access consecutive data. 访问连续数据时,它会带来一些好处。 For example to understand why it is like that you can refer to wiki . 例如,要理解为什么你可以参考wiki

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM