Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

Cache Lines Touched: AoS vs SoA

Prerequisites: Cache Lines, Locality, and Pointer Chasing, Memory Layout of Vectors and Linked Lists, The Cost Model: What Big-O Hides

The cost model's MarketUpdate loop reads only a few fields of each update. Decide how to lay out records for such a loop by counting the cache lines each of two layouts makes it touch.

  • Array of structs (AoS): the records one after another in a single array, the way std::vector<MarketUpdate> does.
  • Struct of arrays (SoA): one dense array per field, such as a price array and a qty array. Record r's fields are element r of each array.
// Array of structs: one array of whole records.
std::vector<MarketUpdate> updates;      // record r's price is updates[r].price

// Struct of arrays: one array per field.
struct MarketUpdates {
    std::vector<std::uint64_t> sequence;
    std::vector<std::int64_t> price;    // record r's price is price[r]
    std::vector<std::int32_t> qty;
};

What to implement

  • count_cache_lines(fields, n, commands) returns one line per SCAN, STRIDE, or GATHER, in command order.
  • fields lists the record's fields in declaration order, each with a size in bytes. Both layouts hold n records, numbered 0 to n - 1.
  • On stdin: the field count, then one name size line per field, then n, then the command count and one command per line.
  • The problem has three parts. Each part adds commands, and one solution must pass all eleven tests.
CommandLoop it describesPrints
SCAN fieldsReads fields of every record, r = 0, 1, ..., n - 1aos soa winner
STRIDE fields kReads fields of records 0, k, 2k, ... below n (Part 2)aos soa winner
GATHER fields i1 i2 ...Reads fields of the listed records (Part 2)aos soa winner
OFFSET mMoves the start of every array m bytes past a line boundary (Part 3)nothing

fields in a command is a comma-separated list of declared field names with no spaces, such as price,qty. Each name appears at most once, in any order, and the order does not change what the loop reads.

Memory model

  • Memory is divided into 64-byte cache lines. Byte address a lies in line a / 64 (integer division).
  • The record is packed: the first field starts at offset 0 and each later field starts where the previous one ends. The record size R is the sum of the field sizes.
  • A record that needs padding lists it as a field of its own that no loop reads, as pad is in the example.
LayoutField f of record r is the size(f) bytes starting at
AoSr * R + offset(f) (the array starts at address 0, record r at r * R)
SoAr * size(f) bytes after the start of field f's own array

Every array starts at the first byte of a cache line. The arrays lie far apart, so no two of them share a line.

Part 1: Full scans

Part 1 uses only SCAN, which reads the named fields of record 0, then record 1, and so on through record n - 1.

  1. Reading a field touches every line that holds at least one of its bytes. A field that crosses a line boundary touches two lines, and one longer than a line can touch more.
  2. A loop's count is the number of distinct lines it touches. A line touched several times counts once, as if the cache kept every line it loaded.
  3. Each loop command is counted on its own, starting from no lines touched.
  4. The SoA count is the sum over the field arrays.

When R does not divide 64, records straddle line boundaries, so a field can cross a boundary even though every array starts on one.

Output: one line per loop.

aos soa winner
  • aos is the AoS count; soa is the SoA count.
  • winner is AOS when the AoS count is smaller, SOA when the SoA count is smaller, and TIE when they are equal.

Limits

  • Field names are distinct and use lowercase letters and underscores.
  • At most 16 fields, each between 1 and 256 bytes long.
  • n is between 1 and 100,000. At most 100 commands. A GATHER lists between 1 and 1,000 record indices.
  • Every address stays below 231, though the reference solutions compute addresses in 64-bit integers (std::int64_t).

Tests 1–3 cover this part.

Part 2: Strides and gathers

STRIDE fields k reads every record whose index is a multiple of k and below n.

  • k is between 1 and 109.
  • STRIDE fields 1 is the same loop as SCAN fields. A stride of n or more reads only record 0.

GATHER fields i1 i2 ... reads the named fields of the listed records, in the listed order.

  • Each index is between 0 and n - 1. Indices may repeat and come in any order.
  • A record read a second time adds no lines, because its lines are already counted.

Tests 4–7 and 11 cover this part.

Part 3: A misaligned base

OFFSET m, with m between 0 and 63, moves the start of every array (the AoS array and each SoA array) to m bytes past a line boundary.

LayoutField f of record r then starts at
AoSaddress m + r * R + offset(f)
SoAm + r * size(f) bytes after the line boundary where its array begins
  • The SoA arrays still never share a line.
  • The offset starts at 0 and applies to every later loop until the next OFFSET. OFFSET 0 restores the aligned layout.

Tests 8–10 cover this part.

Example
Input
5
sequence 8
symbol 8
price 8
qty 4
pad 4
8
4
SCAN price
SCAN qty,price
SCAN sequence,symbol,price,qty
SCAN sequence,symbol,price,qty,pad
Output
4 1 SOA
4 2 SOA
4 4 TIE
4 5 AOS
Explanation

The record is the course's MarketUpdate with its 4 bytes of tail padding declared as pad. R = 32, so two records fill each line.

AoS: the 8 records fill 256 bytes, lines 0 to 3, and every loop reads a field in each of them. Every AoS count is 4.

SoA: the 8 price values fill exactly one line, and the 8 qty values use 32 bytes of one. So price touches 1 line and qty,price touches 2.

The four real fields touch one line each, a tie. Adding pad costs SoA a fifth line, while the AoS lines already held those bytes.