For a 16 by 16 product the fastest setting in our table is one thread of OpenBLAS at 8.0 GFLOP/s, and eight threads of the best library reach only 3.4. For a 2,048 by 2,048 product eight threads of MKL reach 205.6. Which BLAS should a covariance job use, and what do you have to know before the table can answer?
The idea in plain words
Where we are. The earlier topics built the calls, the cache model, the layouts, the solvers, conditioning and the threading model. This topic is the practical step before writing code against any of them: choosing which BLAS to link. It comes after threading because the best library depends on how many threads you run, and before the last two topics, which call BLAS from C and run a whole pipeline.
BLAS is a specification: a list of function names and what they must compute. Several libraries implement it. OpenBLAS is free and runs almost anywhere. MKL is tuned by Intel for its chips. BLIS is a free framework built for easy tuning. Apple's Accelerate ships with macOS. They all return the same numbers up to rounding, so the choice is about speed, portability and licence.
Our table has the columns library, op, n, threads, gflops. The numbers are invented for the course with a formula that gives them the shapes real tables have (a library that is strong at small sizes, one that scales best across threads), so you can practise reading without anyone's marketing attached. rows is the parsed table.
# Who wins dgemm at the size of 12 stocks (n=16), 500 stocks (n=512) and 2,000 stocks (n=2048)?for n in (16, 512, 2048): print(f"n={n:5}", "1 thread:", best_library(rows, "dgemm", n, 1), "| 8 threads:", best_library(rows, "dgemm", n, 8))1
n= 16 1 thread: openblas | 8 threads: mkl n= 512 1 thread: mkl | 8 threads: mkl n= 2048 1 thread: mkl | 8 threads: mkl
- 1No library is the best everywhere: OpenBLAS at the smallest size on one thread, MKL once there is enough work or enough threads.
# Where does MKL overtake OpenBLAS on one thread, and on eight?print("one thread:", crossover(rows, "dgemm", "openblas", "mkl", 1))print("eight threads:", crossover(rows, "dgemm", "openblas", "mkl", 8))1print("BLIS first beats OpenBLAS at:", crossover(rows, "dgemm", "openblas", "blis", 1))
one thread: 512 eight threads: 8 BLIS first beats OpenBLAS at: 256
- 1On eight threads MKL is ahead from the first size in the table (8), because it scales best; on one thread it needs n = 512.
How much of the machine is the result using?
A table of winners hides what is left to gain. efficiency divides a result by the peak of the threads used. We take the peak of one core to be 36 GFLOP/s (say, a 2.25 GHz core that can do 16 operations per cycle). dgemm on MKL with eight threads reaches three quarters of the machine. dgemv reaches under 5%: it moves one number for every two operations (the intensity of topic 1), so it is limited by memory and no BLAS can change that.
# What fraction of the machine's peak does each result use? (peak of one core: 36 GFLOP/s)for library in ("mkl", "openblas"): big = efficiency(rows, library, "dgemm", 4096, 8, 36.0) small = efficiency(rows, library, "dgemm", 16, 8, 36.0) vector = efficiency(rows, library, "dgemv", 4096, 8, 36.0) print(f"{library:9} dgemm 4096: {big:5.1%} | dgemm 16: {small:5.1%} | dgemv 4096: {vector:5.1%}")1
mkl dgemm 4096: 75.5% | dgemm 16: 1.2% | dgemv 4096: 4.8% openblas dgemm 4096: 56.0% | dgemm 16: 0.9% | dgemv 4096: 4.5%
- 1Even the best library idles on a tiny matrix (n = 16 uses about 1% of eight cores), and the matrix-vector product never gets near the peak.
Sizes are square, but your covariance job multiplies a 12 by 250 matrix by a 250 by 12 one. The machine in the table is not yours: a library that wins on one CPU can lose on another. Results are best-case runs of a hot cache. And a library has costs outside the table: MKL runs on x86 only, Accelerate on Apple hardware only, and licences differ. Use a table to pick candidates, then time your own job on your own machine.
What the tasks ask
- 1`parse_table`Read the CSV text into typed rows, skipping blank and comment lines, and reject a bad header or a bad line with the exact message, line number included.
- 2`best_library` and `crossover`The winner at one exact setting, and the first size where one library strictly overtakes another. Ties are handled in a fixed way so the answer never depends on file order.
- 3`efficiency`A result as a fraction of the peak of the threads it used, which shows how much of the machine is left unused.
dgemm on 8 threads. Your job multiplies a 12 by 250 matrix by a 250 by 12 one, once. What should you conclude?Lab · what's graded
4 tasks · 100 pts- Read the table with
parse_table25 pts - Pick the winner with
best_library25 pts - Find the turning point with
crossover25 pts - Measure against the peak with
efficiency25 pts