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