Structure of Arrays vs Array of Structs
Choosing layouts that coalesce.
Structure of Arrays vs Array of Structs is a free CUDA Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CUDA Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Ways to Store Records
Say each particle has x, y, and z. You can keep them together or split them apart, and that choice changes how every warp coalesces.
Array of Structs
The natural C++ layout packs each record's fields side by side. This is an array of structs, often shortened to AoS.
struct Particle { float x, y, z; };
Particle p[N];Why AoS Scatters
When a warp reads only x from each record, the x values sit 12 bytes apart. That built-in stride wastes most of every line it pulls.
float xi = p[i].x; // x values are spaced by sizeof(Particle)Structure of Arrays
Flip it: store all the x values together, all the y values together, all the z values together. This is a structure of arrays, or SoA.
float x[N];
float y[N];
float z[N];Why SoA Coalesces
Now reading x for 32 threads touches 32 contiguous floats. That is one aligned line and one tidy transaction per field.
float xi = x[i]; // neighbors land in one lineThe Field-Access Test
Ask how your kernel reads data. If threads sweep one field across many records, SoA almost always wins on the GPU.
When AoS Is Fine
If each thread uses all of a record's fields together, AoS keeps them in one line and stays friendly to the cache.
Bandwidth, Not Just Style
This is not about taste. Switching from AoS to SoA can multiply real throughput for the same math, simply by cutting wasted bytes.
A Hybrid Middle Ground
You can group a few records, then split fields inside each group. This AoSoA layout balances coalescing with cache locality.
Design Before You Optimize
Layout is hard to change late, since it touches every kernel and copy. Pick your SoA or AoS shape early, guided by access patterns. 🧩
The Takeaway Mapping
GPU rule of thumb: separate fields into arrays so each warp reads a contiguous run. Favor SoA for field-wise kernels.
Quick Check
Choose the layout that coalesces this access.
Recap
You compared AoS and SoA: pack fields together for whole-record use, but split them into arrays when warps sweep one field at a time. 🎉
Frequently asked questions
Is the “Structure of Arrays vs Array of Structs” lesson free?
Yes — the full text of “Structure of Arrays vs Array of Structs” is free to read here on the web, and the CUDA Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CUDA Academy course, upgrade to CoddyKit PRO.
What will I learn in “Structure of Arrays vs Array of Structs”?
Choosing layouts that coalesce. You practise CUDA Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start CUDA Academy?
No prior experience is required. CUDA Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Structure of Arrays vs Array of Structs” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this CUDA Academy lesson?
Yes. Every CUDA Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What a Memory Transaction Is
- Coalesced vs Strided Reads
- Structure of Arrays vs Array of Structs
- Measuring Effective Bandwidth