• <legend id='GzhHt'><style id='GzhHt'><dir id='GzhHt'><q id='GzhHt'></q></dir></style></legend><tfoot id='GzhHt'></tfoot>

      <bdo id='GzhHt'></bdo><ul id='GzhHt'></ul>

      1. <i id='GzhHt'><tr id='GzhHt'><dt id='GzhHt'><q id='GzhHt'><span id='GzhHt'><b id='GzhHt'><form id='GzhHt'><ins id='GzhHt'></ins><ul id='GzhHt'></ul><sub id='GzhHt'></sub></form><legend id='GzhHt'></legend><bdo id='GzhHt'><pre id='GzhHt'><center id='GzhHt'></center></pre></bdo></b><th id='GzhHt'></th></span></q></dt></tr></i><div id='GzhHt'><tfoot id='GzhHt'></tfoot><dl id='GzhHt'><fieldset id='GzhHt'></fieldset></dl></div>
      2. <small id='GzhHt'></small><noframes id='GzhHt'>

        数组结构和结构数组 - 性能差异

        时间:2023-09-18

                • <bdo id='ZFSez'></bdo><ul id='ZFSez'></ul>
                • <i id='ZFSez'><tr id='ZFSez'><dt id='ZFSez'><q id='ZFSez'><span id='ZFSez'><b id='ZFSez'><form id='ZFSez'><ins id='ZFSez'></ins><ul id='ZFSez'></ul><sub id='ZFSez'></sub></form><legend id='ZFSez'></legend><bdo id='ZFSez'><pre id='ZFSez'><center id='ZFSez'></center></pre></bdo></b><th id='ZFSez'></th></span></q></dt></tr></i><div id='ZFSez'><tfoot id='ZFSez'></tfoot><dl id='ZFSez'><fieldset id='ZFSez'></fieldset></dl></div>
                  <legend id='ZFSez'><style id='ZFSez'><dir id='ZFSez'><q id='ZFSez'></q></dir></style></legend>

                  <tfoot id='ZFSez'></tfoot>
                    <tbody id='ZFSez'></tbody>

                  <small id='ZFSez'></small><noframes id='ZFSez'>

                  本文介绍了数组结构和结构数组 - 性能差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一堂这样的课:

                  //Array of Structures
                  class Unit
                  {
                    public:
                      float v;
                      float u;
                      //And similarly many other variables of float type, upto 10-12 of them.
                      void update()
                      {
                         v+=u;
                         v=v*i*t;
                         //And many other equations
                      }
                  };
                  

                  我创建了一个 Unit 类型的对象数组.并调用更新.

                  I create an array of objects of Unit type. And call update on them.

                  int NUM_UNITS = 10000;
                  void ProcessUpdate()
                  {
                    Unit *units = new Unit[NUM_UNITS];
                    for(int i = 0; i < NUM_UNITS; i++)
                    {
                      units[i].update();
                    }
                  }
                  

                  为了加快速度,并可能自动向量化循环,我将 AoS 转换为数组结构.

                  In order to speed up things, and possibly autovectorize the loop, I converted AoS to structure of arrays.

                  //Structure of Arrays:
                  class Unit
                  {
                    public:
                    Unit(int NUM_UNITS)
                    {
                      v = new float[NUM_UNITS];
                    }
                    float *v;
                    float *u;
                    //Mnay other variables
                    void update()
                    {
                      for(int i = 0; i < NUM_UNITS; i++)
                      {
                        v[i]+=u[i];
                        //Many other equations
                      }
                    }
                  };
                  

                  当循环无法自动向量化时,数组结构的性能非常差.对于50个单元,SoA的更新比AoS略快.但是从100个单元开始,SoA比AoS慢.在 300 个单位时,SoA 几乎差了两倍.以 100K 为单位,SoA 比 AoS 慢 4 倍.虽然缓存可能是 SoA 的一个问题,但我没想到性能差异会如此之大.对 cachegrind 的分析显示两种方法的未命中数相似.Unit 对象的大小为 48 字节.L1 缓存为 256K,L2 为 1MB,L3 为 8MB.我在这里错过了什么?这真的是缓存问题吗?

                  When the loop fails to autovectorize, i am getting a very bad performance for structure of arrays. For 50 units, SoA's update is slightly faster than AoS.But then from 100 units onwards, SoA is slower than AoS. At 300 units, SoA is almost twice as worse. At 100K units, SoA is 4x slower than AoS. While cache might be an issue for SoA, i didnt expect the performance difference to be this high. Profiling on cachegrind shows similar number of misses for both approach. Size of a Unit object is 48 bytes. L1 cache is 256K, L2 is 1MB and L3 is 8MB. What am i missing here? Is this really a cache issue?

                  我正在使用 gcc 4.5.2.编译器选项是 -o3 -msse4 -ftree-vectorize.

                  I am using gcc 4.5.2. Compiler options are -o3 -msse4 -ftree-vectorize.

                  我在 SoA 中做了另一个实验.我没有动态分配数组,而是在编译时分配了v"和u".当有 100K 单元时,这提供了比具有动态分配阵列的 SoA 快 10 倍的性能.这里发生了什么事?为什么静态分配的内存和动态分配的内存会有这么大的性能差异?

                  I did another experiment in SoA. Instead of dynamically allocating the arrays, i allocated "v" and "u" in compile time. When there are 100K units, this gives a performance which is 10x faster than the SoA with dynamically allocated arrays. Whats happening here? Why is there such a performance difference between static and dynamically allocated memory?

                  推荐答案

                  在这种情况下,数组结构对缓存不友好.

                  Structure of arrays is not cache friendly in this case.

                  您同时使用 uv,但是如果它们有 2 个不同的数组,它们将不会同时加载到一个缓存行中,缓存未命中将花费巨大性能损失.

                  You use both u and v together, but in case of 2 different arrays for them they will not be loaded simultaneously into one cache line and cache misses will cost huge performance penalty.

                  _mm_prefetch 可用于使 AoS 表示更快.

                  这篇关于数组结构和结构数组 - 性能差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:std::bind 与 lambda 性能 下一篇:在 Intel Kaby Lake 架构上获取最后一级缓存未命中数

                  相关文章

                  最新文章

                • <i id='Uiwyj'><tr id='Uiwyj'><dt id='Uiwyj'><q id='Uiwyj'><span id='Uiwyj'><b id='Uiwyj'><form id='Uiwyj'><ins id='Uiwyj'></ins><ul id='Uiwyj'></ul><sub id='Uiwyj'></sub></form><legend id='Uiwyj'></legend><bdo id='Uiwyj'><pre id='Uiwyj'><center id='Uiwyj'></center></pre></bdo></b><th id='Uiwyj'></th></span></q></dt></tr></i><div id='Uiwyj'><tfoot id='Uiwyj'></tfoot><dl id='Uiwyj'><fieldset id='Uiwyj'></fieldset></dl></div>
                  • <bdo id='Uiwyj'></bdo><ul id='Uiwyj'></ul>

                  <small id='Uiwyj'></small><noframes id='Uiwyj'>

                • <tfoot id='Uiwyj'></tfoot>

                    1. <legend id='Uiwyj'><style id='Uiwyj'><dir id='Uiwyj'><q id='Uiwyj'></q></dir></style></legend>