Contiguous Access and Data Layout
Reading neighboring elements in memory can use storage and cache behavior differently from visiting values separated by a large stride.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace a simple grid
A row-major layout makes two neighboring columns consecutive. Stepping down rows in one column can jump by the row width. Flattened row-major indexing supplies the precise offsets.
Compare like work
Reordering traversal can also change computed groups or update order. Verify output equality under the declared invariants before interpreting timing.[1]
Compare offset patterns from two traversal orders
A row-major matrix with four columns stores adjacent entries in one row at offsets differing by one. Moving down a column changes the offset by four. Choosing loops that fit layout can improve locality without changing mathematical work. See row-major storage.
Storage row width 4
Traverse row 1 → offsets 4,5,6,7
Traverse column 1 → offsets 1,5,9 across three rows
Test the shape and operation that benefit
Transposing data can make one traversal convenient while adding preprocessing and memory expense. Measure sizes that matter and test non-square matrices to detect accidentally changed indexing.