Skip to content
systemcoding.dev
Performance

Measure Memory Bandwidth With the STREAM Benchmark

Build and run the STREAM benchmark with gcc and OpenMP to measure sustainable memory bandwidth, size the arrays so they miss cache, and read the four kernels.

6 min read
Summarize in AI
Flat illustration of a CPU core with its cache levels and main memory, with the C, AMD and Intel logos.

Before you can say a program is limited by memory bandwidth, you need to know how much bandwidth the machine actually delivers. The data sheet number, channels times transfer rate times width, is a ceiling that real code never reaches. STREAM, written by John McCalpin at the University of Virginia, is the standard way to find the sustainable figure: four simple loops over arrays far larger than any cache, timed, and reported in megabytes per second. This post builds STREAM with GCC and OpenMP, explains what each kernel counts, shows how to size the arrays, and reads the output. It sits under the guide to memory bandwidth, NUMA and huge pages.

What STREAM measures

STREAM runs four kernels over three arrays a, b and c of double:

KernelOperationBytes per element
Copyc[i] = a[i]16 (one read, one write)
Scaleb[i] = scalar * c[i]16
Addc[i] = a[i] + b[i]24 (two reads, one write)
Triada[i] = b[i] + scalar * c[i]24

Each kernel does almost no arithmetic per byte, so its time is set by how fast memory can supply and absorb data. The reported rate counts only the bytes the source code names, so it does not include the extra read a write-allocate cache performs before storing to a line. Real traffic on the bus is therefore somewhat higher than the number STREAM prints, which is a documented convention of the benchmark and the reason to compare numbers only against other STREAM runs. The reference description and source are on the STREAM home page.

Get and build it

The benchmark is one C file, stream.c, downloadable from the STREAM site. Two compile-time macros matter: STREAM_ARRAY_SIZE, the number of elements per array, and NTIMES, how many repetitions run.

Text
gcc -O3 -march=native -fopenmp \
    -DSTREAM_ARRAY_SIZE=80000000 -DNTIMES=20 \
    stream.c -o stream

-fopenmp enables the OpenMP pragmas in the source, which spread each loop across threads. A single core usually cannot saturate memory, so a one-thread run reports what that core can pull, and a multi-thread run reports the machine. The -march=native flag lets the compiler use the widest vector loads and stores of the host, and the effect on portability is discussed in the post on choosing between -march=native and x86-64-v3. These flags are written for GCC 15 on Linux.

Size the arrays to miss every cache

The rule in the STREAM documentation is that each array should be at least four times the size of the largest cache level, so the loops measure memory and not cache. With the last-level cache shared by the socket, add the sizes across the sockets you use. Three arrays of double occupy 3 * 8 * STREAM_ARRAY_SIZE bytes:

Text
STREAM_ARRAY_SIZE = 80,000,000
one array  = 80,000,000 * 8  = 640,000,000 bytes  (about 610 MiB)
three arrays                 = 1,920,000,000 bytes (about 1.8 GiB)

With 80 million elements, each array is about 610 MiB, which exceeds four times any cache that exists on a mainstream server. The program prints the actual footprint at start, so check that the line is what you expect and that the machine has enough free memory. Do not shrink the arrays to make the run faster, because a result that fits in cache measures cache.

Pin threads and run it

Thread placement changes the answer. Set the thread count and pin threads so they do not migrate.

Text
OMP_NUM_THREADS=16 OMP_PROC_BIND=close OMP_PLACES=cores ./stream

OMP_PROC_BIND and OMP_PLACES are standard OpenMP environment variables. close packs threads onto neighboring cores, and spread distributes them across the machine. On a multi-socket system, spread is what you want when you intend to use every memory controller. A typical result on a single socket looks like this. The figures are illustrative of the format and will differ on your hardware.

Text
-------------------------------------------------------------
STREAM version $Revision: 5.10 $
-------------------------------------------------------------
This system uses 8 bytes per array element.
-------------------------------------------------------------
Array size = 80000000 (elements), Offset = 0 (elements)
Memory per array = 610.4 MiB (= 0.6 GiB).
Total memory required = 1831.1 MiB (= 1.8 GiB).
Each kernel will be executed 20 times.
 The *best* time for each kernel (excluding the first iteration)
 will be used to compute the reported bandwidth.
-------------------------------------------------------------
Number of Threads requested = 16
Number of Threads counted = 16
-------------------------------------------------------------
Function    Best Rate MB/s  Avg time     Min time     Max time
Copy:          101234.5     0.012702     0.012644     0.012811
Scale:          99876.1     0.012901     0.012816     0.013002
Add:           108765.4     0.017799     0.017653     0.017944
Triad:         109012.8     0.017750     0.017614     0.017905
-------------------------------------------------------------
Solution Validates: avg error less than 1.000000e-13 on all three arrays
-------------------------------------------------------------

The numbers in the table are placeholders to show the layout; run it yourself for real values. The first iteration is excluded because it pays page-fault costs, and the best of the rest is reported, so the value is a best case for sustained transfer. Triad is the figure most people quote, since it mixes reads and writes and resembles real numerical code. The Solution Validates line is a correctness check on the arithmetic, and a failure there means a compiler or hardware problem, not a slow memory system.

Read the result against the hardware

Compute the theoretical peak and compare. For a DDR5 channel, peak is the transfer rate in mega-transfers per second times 8 bytes, times the number of populated channels. A socket with eight channels at a given transfer rate has a peak of eight times that single-channel figure. STREAM Triad on a well-configured machine typically lands below that ceiling, and the gap reflects refresh, bank conflicts and write turnaround. A result far lower usually means a configuration problem: unpopulated channels, a slower DIMM speed than expected, threads all on one socket, or a power setting. The related post on memory channels, ranks and interleaving explains how to count what should be there.

Scale the thread count to see the curve. Run with 1, 2, 4 and up to all cores and note where Triad stops improving. The knee is the number of cores needed to saturate memory, and beyond it extra threads add nothing for bandwidth-bound code. That number is valuable for sizing thread pools.

Common mistakes

Arrays that fit in cache, which inflate the result. Forgetting -fopenmp, which silently runs one thread. Letting threads migrate between runs, which makes results jump. Running on a machine with other busy workloads. And first-touch effects: STREAM initializes the arrays inside a parallel loop so each thread's pages land on its own NUMA node, and changing that to serial initialization puts all the memory on one node and lowers the result on multi-socket systems, the effect explained in first-touch page placement.

A checklist before you trust the number

Run through these checks every time, because a bad STREAM run is easy to produce and hard to spot. Confirm the array size line in the output: the benchmark prints the memory per array, and it must be at least four times your largest cache, which on a two-socket machine means adding the last-level caches of both sockets. Confirm the thread count line, and that it equals the number you asked for. Check that no other job is running with uptime, because a busy neighbor steals bandwidth and the benchmark cannot tell. Check the CPU frequency governor: a machine in a power-saving mode can report a lower result than the same hardware in a performance mode. Run the whole thing three times and compare the Triad lines. A spread of more than a few percent means something on the machine is changing between runs, such as thermal limits, background services or threads that are not pinned.

Finally, run it once with the threads bound to a single NUMA node and once spread across all of them. The first number is what a node-local service can reach, and the second is the whole machine. On a two-socket server the second is roughly twice the first when both sockets have full channel population, and a ratio far below two points to an unbalanced configuration, such as DIMMs missing on one socket.

Also inHardware
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.