Sub topic 12
Associativity and conflict misses from power-of-two strides
This chapter assumes you know what a cache line is and the difference between capacity and conflict misses. It also assumes you can do binary arithmetic on addresses.
How a cache chooses where a line goes
A cache is divided into sets. Each set holds a fixed number of lines, called ways. A cache with 8 ways is 8-way set-associative.
The hardware picks the set from a few address bits just above the line offset. Take a typical 32 KiB L1 data cache with 64-byte lines and 8 ways:
- 32 KiB / 64 B = 512 lines, and 512 / 8 = 64 sets.
- Bits 0-5 are the offset within the line.
- Bits 6-11 are the set index.
- Higher bits form the tag.
Two addresses 4096 bytes apart have the same set index. A single set can hold only 8 such lines, even though the cache has 512 lines in total.
The power-of-two stride problem
If your access stride is a multiple of 4096 bytes, every access lands in the same set. That happens with a matrix whose row length is 1024 doubles (8192 bytes) when you walk down a column. After 8 rows, each new line evicts an older one. The data is small enough to fit in the cache, but the lines still miss on every pass. These are conflict misses.
The program below walks 16 rows of a column many times. It runs once with a row stride of 1024 doubles and once with 1032 doubles. The second stride is 8192 + 64 bytes, so each row moves to the next set.
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define ROWS 16
#define REPS (1 << 20)
static double now(void) {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return (double)t.tv_sec + (double)t.tv_nsec * 1e-9;
}
static void run(size_t stride) {
double *buf;
if (posix_memalign((void **)&buf, 4096, ROWS * stride * sizeof(double)) != 0)
exit(1);
memset(buf, 0, ROWS * stride * sizeof(double));
double start = now();
for (long r = 0; r < REPS; r++)
for (size_t i = 0; i < ROWS; i++)
buf[i * stride] += 1.0; /* read-modify-write: not hoistable */
double elapsed = now() - start;
printf("stride %zu doubles: %.3f s (check %.0f)\n",
stride, elapsed, buf[0]);
free(buf);
}
int main(void) {
run(1024); /* 8192-byte stride: all rows share one set */
run(1032); /* 8256-byte stride: rows spread across sets */
return 0;
}Compile with gcc -O2. On many x86 machines the first run is noticeably slower. The exact ratio depends on your cache geometry.
Fixes
- Pad the row length so it is not a multiple of the set-mapping period. One extra cache line per row is often enough.
- Avoid hot arrays whose sizes are exact powers of two when you access them at the same index. Examples are
a[i],b[i], andc[i]in separate 1 MiB arrays. - Use blocking so the working set stays small.
Exercise
A 16 KiB, 4-way cache has 64-byte lines. Which stride in bytes makes every access map to the same set, and how many lines can stay cached?
Show answer
There are 16384 / 64 = 256 lines and 256 / 4 = 64 sets. Set index repeats every 64 * 64 = 4096 bytes, so any stride that is a multiple of 4096 bytes maps to one set. Only 4 lines can stay cached.