liveBook · Manning

Struct of arrays

As explained in Grokking GPU Programming

A structure of arrays stores each record field in a separate array, with corresponding positions belonging to the same record.

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

How Grokking GPU Programming covers it

How deep it goes
Compares particle layouts, shows update kernels, and notes alignment pitfalls.
Examples
Uses particle position and velocity arrays to contrast layouts and illustrate updates.
What you can do after
Apply it by storing fields separately when threads read the same field across records.
How it connects
Connects array of structures, memory coalescing, GPU memory bandwidth, and CPU cache locality.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking GPU Programming. The section links point to the parts of the book it draws on.