|
| 1 | +using DelimitedFiles, Statistics, Printf |
| 2 | + |
| 3 | +const CSV = joinpath(@__DIR__, "results.csv") |
| 4 | + |
| 5 | +# Read raw data |
| 6 | +raw, header = readdlm(CSV, ','; header = true) |
| 7 | +header = vec(string.(header)) |
| 8 | +function col(name) |
| 9 | + j = findfirst(==(name), header) |
| 10 | + j === nothing && error("no column $name") |
| 11 | + return j |
| 12 | +end |
| 13 | + |
| 14 | +const STRATS = ["LinearScan", "SIMDLinearScan", "BracketGallop", "ExpFromLeft", "InterpolationSearch"] |
| 15 | + |
| 16 | +function row_winner(row) |
| 17 | + times = Float64[parse(Float64, string(row[col(s)])) for s in STRATS] |
| 18 | + j = argmin(times) |
| 19 | + return STRATS[j], times[j] |
| 20 | +end |
| 21 | + |
| 22 | +function ratio_to(row, strat_name) |
| 23 | + times = Float64[parse(Float64, string(row[col(s)])) for s in STRATS] |
| 24 | + best = minimum(times) |
| 25 | + return parse(Float64, string(row[col(strat_name)])) / best |
| 26 | +end |
| 27 | + |
| 28 | +println("==> Where does SIMDLinearScan win?") |
| 29 | +simd_wins = [] |
| 30 | +for i in axes(raw, 1) |
| 31 | + local row = raw[i, :] |
| 32 | + local winner, _ = row_winner(row) |
| 33 | + if winner == "SIMDLinearScan" |
| 34 | + push!( |
| 35 | + simd_wins, ( |
| 36 | + eltype = string(row[col("eltype")]), |
| 37 | + v = string(row[col("v_kind")]), |
| 38 | + q = string(row[col("q_kind")]), |
| 39 | + n = parse(Int, string(row[col("n")])), |
| 40 | + m = parse(Int, string(row[col("m")])), |
| 41 | + ) |
| 42 | + ) |
| 43 | + end |
| 44 | +end |
| 45 | +n_simd_wins = length(simd_wins) |
| 46 | +println(" SIMDLinearScan wins in $n_simd_wins cells") |
| 47 | +println() |
| 48 | + |
| 49 | +# Tabulate where SIMD wins by m (gap proxy) |
| 50 | +println("==> SIMDLinearScan wins by m (proxy for batched-gap regime):") |
| 51 | +by_m = Dict{Int, Int}() |
| 52 | +for c in simd_wins |
| 53 | + by_m[c.m] = get(by_m, c.m, 0) + 1 |
| 54 | +end |
| 55 | +for m in sort(collect(keys(by_m))) |
| 56 | + @printf(" m=%5d: %d cells\n", m, by_m[m]) |
| 57 | +end |
| 58 | +println() |
| 59 | + |
| 60 | +# By eltype |
| 61 | +println("==> SIMDLinearScan wins by eltype:") |
| 62 | +by_eltype = Dict{String, Int}() |
| 63 | +for c in simd_wins |
| 64 | + by_eltype[c.eltype] = get(by_eltype, c.eltype, 0) + 1 |
| 65 | +end |
| 66 | +for k in sort(collect(keys(by_eltype))) |
| 67 | + println(" $k: $(by_eltype[k]) cells") |
| 68 | +end |
| 69 | +println() |
| 70 | + |
| 71 | +# Now show: for sorted-batched cells where SIMDLinearScan wins, what's the |
| 72 | +# ratio of LinearScan/ExpFromLeft to SIMDLinearScan? This tells us the |
| 73 | +# magnitude of the improvement. |
| 74 | +println("==> SIMDLinearScan win margin over LinearScan and ExpFromLeft:") |
| 75 | +println(" (Higher = bigger SIMD speedup over the second-best Auto candidate)") |
| 76 | +margins = [] |
| 77 | +for i in axes(raw, 1) |
| 78 | + row = raw[i, :] |
| 79 | + winner, best = row_winner(row) |
| 80 | + if winner == "SIMDLinearScan" |
| 81 | + t_simd = best |
| 82 | + t_lin = parse(Float64, string(row[col("LinearScan")])) |
| 83 | + t_exp = parse(Float64, string(row[col("ExpFromLeft")])) |
| 84 | + push!( |
| 85 | + margins, ( |
| 86 | + ratio_lin = t_lin / t_simd, |
| 87 | + ratio_exp = t_exp / t_simd, |
| 88 | + n = parse(Int, string(row[col("n")])), |
| 89 | + m = parse(Int, string(row[col("m")])), |
| 90 | + ) |
| 91 | + ) |
| 92 | + end |
| 93 | +end |
| 94 | +println(" SIMD vs LinearScan: median $(median(m.ratio_lin for m in margins))x, max $(maximum(m.ratio_lin for m in margins))x") |
| 95 | +println(" SIMD vs ExpFromLeft: median $(median(m.ratio_exp for m in margins))x, max $(maximum(m.ratio_exp for m in margins))x") |
| 96 | +println() |
| 97 | + |
| 98 | +# What does Auto pick in the cells where SIMDLinearScan would win? |
| 99 | +# Find the m, n, gap regime where SIMDLinearScan beats LinearScan AND ExpFromLeft. |
| 100 | +println("==> Best regime for SIMDLinearScan (cells where SIMD wins by >20%):") |
| 101 | +significant_simd_wins = filter(c -> c.ratio_lin > 1.2 && c.ratio_exp > 1.2, margins) |
| 102 | +println(" $(length(significant_simd_wins)) cells where SIMD beats both LinearScan and ExpFromLeft by >20%") |
| 103 | +n_by_m_n = Dict{Tuple{Int, Int}, Int}() |
| 104 | +for c in significant_simd_wins |
| 105 | + n_by_m_n[(c.n, c.m)] = get(n_by_m_n, (c.n, c.m), 0) + 1 |
| 106 | +end |
| 107 | +for (n, m) in sort(collect(keys(n_by_m_n))) |
| 108 | + @printf(" n=%5d m=%5d: %d cells\n", n, m, n_by_m_n[(n, m)]) |
| 109 | +end |
| 110 | +println() |
| 111 | + |
| 112 | +# Compute: in each cell, the GAP. gap = n * span(queries)/span(v) / m |
| 113 | +# (the same heuristic Auto uses). Use n/m as a rough proxy since exact gap |
| 114 | +# depends on the query distribution which isn't recoverable from CSV alone. |
| 115 | +println("==> n/m ratio for SIMD-winning cells (rough gap proxy):") |
| 116 | +nm_buckets = Dict{Int, Int}() |
| 117 | +for c in simd_wins |
| 118 | + bucket = c.n ÷ c.m |
| 119 | + # Round to a power-of-2 bucket |
| 120 | + log_bucket = bucket == 0 ? 0 : floor(Int, log2(bucket)) |
| 121 | + nm_buckets[log_bucket] = get(nm_buckets, log_bucket, 0) + 1 |
| 122 | +end |
| 123 | +for b in sort(collect(keys(nm_buckets))) |
| 124 | + @printf(" n/m in [2^%d, 2^%d): %d cells\n", b, b + 1, nm_buckets[b]) |
| 125 | +end |
| 126 | +println() |
| 127 | + |
| 128 | +println("==> What strategy does Auto pick in cells where SIMD would win?") |
| 129 | +auto_picks_when_simd = Dict{String, Int}() |
| 130 | +for i in axes(raw, 1) |
| 131 | + row = raw[i, :] |
| 132 | + winner, _ = row_winner(row) |
| 133 | + if winner == "SIMDLinearScan" |
| 134 | + # We don't know Auto's pick from the CSV, but we know Auto's time. |
| 135 | + # The closest strategy time to Auto's time tells us the pick. |
| 136 | + t_auto = parse(Float64, string(row[col("Auto")])) |
| 137 | + candidates = [(s, parse(Float64, string(row[col(s)]))) for s in STRATS] |
| 138 | + # Pick the strategy whose time is closest to Auto's |
| 139 | + # (within 20% — heuristic). |
| 140 | + closest = argmin(c -> abs(c[2] - t_auto), candidates) |
| 141 | + auto_picks_when_simd[closest[1]] = get(auto_picks_when_simd, closest[1], 0) + 1 |
| 142 | + end |
| 143 | +end |
| 144 | +for (s, n) in sort(collect(pairs(auto_picks_when_simd)); by = x -> -x[2]) |
| 145 | + println(" Auto -> $s: $n cells (out of $(length(simd_wins)))") |
| 146 | +end |
0 commit comments