A structure of arrays (SoA) stores each record field in its own contiguous array. For particles, x, y, z, vx, vy, and vz each have a separate array; position i across those arrays belongs to the same particle. In an array of structures (AoS), each particle’s fields are stored together instead. section 10.9
Choose the layout according to which fields your code reads:
- When neighboring GPU threads read the same field from different records, SoA puts those values next to each other, supporting coalesced, stride-1 access. It can be 3–5× faster for memory-bound kernels that read only one or two fields per element. section 10.9
- AoS is more natural and can suit CPU loops that use every field of each record. SoA is less convenient for that access pattern, so neither layout is best for every workload. section 10.9
- For whole-element loads, padding float3 data to float4 can help alignment; for kernels reading only one field, use separate arrays instead. section 10.13
Updating particle positions
Assign each thread a particle index i, then update x[i] using vx[i], y[i] using vy[i], and z[i] using vz[i]. Neighboring threads access consecutive values in each field array. section 10.9
// Structure of Arrays float x[N], y[N], z[N]; float vx[N], vy[N], vz[N];
See the separate arrays for particle coordinates and velocities. section 10.9
