Array of Structures vs. Structure of Arrays

  1. Ordering for Cache
    1. Well known that it is better to access arrays in row-major order
      • Show timing test of row vs. column major
  2. Array of Structures
    1. Smoothed Particle Hydrodynamics
      1. Particle
        1. Mass
        2. Position
        3. Velocity
        4. Force
        5. Density
        6. DensityDerivative
        7. Alpha
        8. K
        9. Neighbors
      2. Simulation (Divergence-Free Smoothed Particle Hydrodynamics)
      3. One cache miss per particle
    2. Game (example from book)
      1. Array of Characters
        1. Position, Up, Forward, Right
        2. Velocity
        3. Inventory
        4. Health
        5. BulletCount
        6. Controller
        7. PlayerModel
        8. AnimationGoal
        9. CurrentAnimation
        10. CurrentSound
        11. controllable
        12. visible
        13. XP
        14. Speed, Jump, Strength, Dodge
        15. cheat
      2. Processing positions = 1 cache miss per character
  3. Structure of arrays
    1. Characters object
      1. Position[], Up[], Forward[], Right[]
      2. Velocity[]
      3. ...
    2. Processing position: use two lists, Position & Velocity
      1. Predictable (prefetch)
      2. Full cache utilization for both arrays
    3. Need to know the data
      • Position[] vs. x[], y[], z[]
    4. SIMD/SSE
      1. Same operation applied to multiple data
        1. MMX = 32-bit registers treated as 4 x 8-bit
        2. SSE = 128-bit registers treated as 2 x 64-bit; 4 x 32-bit; 8 x 16-bit; 16 x 8-bit
        3. AVX = 256-bit registers treated as 4 x 64-bit; 8 x 32-bit; 16 x 16-bit; 32 x 8-bit
      2. Much of performance is lost in packing & unpacking into vector registers
      3. SoA already in right format