liveBook · Manning

Array of structs

As explained in Grokking GPU Programming

An array of structures stores records as an array in which each element contains multiple fields belonging to one record.

An array of structures stores records one after another, with each record holding several related fields. For particles, each record might contain position (x, y, z) and velocity (vx, vy, vz). section 10.9

struct Particle {
    float x, y, z;     #A
    float vx, vy, vz;    #B
};
 
Particle particles[N];    #C

A particle structure shows how six fields are grouped into each record before the records are placed in an array. section 10.9

This layout is intuitive, but GPU threads reading the same field from consecutive particles jump between records. With six floats per particle, neighboring threads’ x values are six floats apart, making access strided rather than coalesced. A structure of arrays (SoA) instead puts each field in its own contiguous array, which can be faster when a kernel reads only one or two fields. section 10.9 section 10.14

Updating particle positions

A kernel that updates x, y, and z using the corresponding velocities accesses several fields per particle. Whether AoS or SoA performs better depends on the fields the kernel reads; SoA can be 3–5× faster for memory-bound kernels that read only one or two fields per element. section 10.9

How Grokking GPU Programming covers it

How deep it goes
Explains record layout, thread strides, and coalescing, then compares SoA performance for selected workloads.
Examples
Uses particle position and velocity records to contrast AoS and SoA layouts and show particle updates.
What you can do after
Evaluate whether AoS or SoA better suits a kernel’s field-access pattern.
How it connects
Compares AoS with structure of arrays and connects layout choices to memory coalescing and data-oriented design.

Also explained in

Compare how 2 books explain array of structs →

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.