σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
BLAS and LAPACK skill›Module 3 · Speed and implementations›Topic 3.2·~40 minAdvanced

Choosing an implementation from a benchmark table

Skill positionModule 3 of 4 · Topic 2 of 2
3.2 Choosing an implementation from a benchmark table
—Lab score— / 100Total XP—Streak—
Lecture0% read

4 min readCode examples: 3

  1. 01The idea in plain words
  2. 02How much of the machine is the result using?
  3. 03What the tasks ask

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?

01

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.

main.pyPython
# 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
  1. 1No library is the best everywhere: OpenBLAS at the smallest size on one thread, MKL once there is enough work or enough threads.
main.pyPython
# 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
  1. 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.
02

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.

main.pyPython
# 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%
  1. 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.
What a benchmark table leaves out

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.

03

What the tasks ask

  1. 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. 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. 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.

Key takeaways

  • Read the table by operation, size and threads There is no best BLAS in general. The winner changes with the three, so look up your own case.
  • Ask how much of the peak is used A winner far below the peak tells you the library is not the bottleneck; memory or call size is.
  • Now call it from C Whichever library you pick is reached through the same interface. The next topic shows its argument list and what goes wrong when it is misread.
Quick check
A library tops the table for 4096 by 4096 dgemm on 8 threads. Your job multiplies a 12 by 250 matrix by a 250 by 12 one, once. What should you conclude?
Pick an answer.

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
Explorer · lab-8-1
▾ lab-8-1
…
bash · in-browser
learner@quantcraft:~/lab-8-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-8-1 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes