When do non-temporal stores pay off?
1Two paths to memory
Figure 1 shows the two routes a store can take. A regular store needs the line in cache first, so writing a line the core does not have costs a read from DRAM (the read-for-ownership1) and, later, a write back. A non-temporal store goes to a write-combining buffer and leaves as one full-line write.
2A simple model
For a fill of bytes that misses every cache level, with memory bandwidth , the regular path moves each byte twice and the non-temporal path once:
Equation (1) is an upper bound on the speedup for a memory-bound fill. It assumes the buffer is much larger than the last-level cache; below that, the regular path keeps the data in cache and wins. Averaged over a run of fills, the measured speedup lands somewhere in .
3In code
In C, the AVX2 intrinsic is _mm256_stream_si256, which needs a 32-byte-aligned destination:
#include <immintrin.h>
// Fill n bytes without polluting the cache.
// dst must be 32-byte aligned; n a multiple of 32.
void fill_nt(void *dst, int value, size_t n) {
__m256i v = _mm256_set1_epi8((char)value);
for (size_t i = 0; i < n; i += 32)
_mm256_stream_si256((__m256i *)((char *)dst + i), v);
_mm_sfence(); // make the stores visible to other cores
}
Using it safely
- Align the destination to 32 bytes, or handle the unaligned head with regular stores.
- Handle any tail shorter than 32 bytes with regular stores.
- Issue
_mm_sfence()before another thread reads the data.
4The instructions
x86 has a non-temporal store for each register width (Table 1).
| Instruction | Width | Source | Extension |
|---|---|---|---|
MOVNTI | 32/64-bit | general-purpose register | SSE2 |
MOVNTPS / MOVNTPD | 128-bit | XMM, float / double | SSE / SSE2 |
MOVNTDQ | 128-bit | XMM | SSE2 |
VMOVNTDQ | 256-bit | YMM | AVX |
VMOVNTDQ | 512-bit | ZMM | AVX-512F |
5Where the lines cross
Putting the two regimes together gives the shape in Figure 2: regular stores are cheaper while the buffer fits in cache, and non-temporal stores win once it spills to DRAM. Exactly where the lines cross depends on the machine, which is what the next post measures.
rep stosb, which narrows the gap.