Benchmarks

SIMD benchmarks

1 programs measured with axle bench: simd_sort. Run time, peak RAM at compile and run, and binary size, with the exact source for each language.

Benchmarks for v0.23.0

SIMD

simd_sort

Counting sort over a quantized f64 domain, identical algorithm in all three languages — only Axle's max-scan / key pre-scale uses explicit f64x8 SIMD lanes.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
Axle -O19 ms45.5 MB86 ms123.4 MB313 KB
Axle -O29 ms45.5 MB101 ms123.5 MB314 KB
Axle -O39 ms45.5 MB102 ms122.7 MB314 KB
C++ -O111 ms45.5 MB369 ms115.6 MB16 KB
C++ -O211 ms45.5 MB384 ms116.2 MB16 KB
C++ -O311 ms45.5 MB384 ms116.2 MB16 KB
Rust -O211 ms45.5 MB129 ms148.6 MB3864 KB
Rust -O311 ms45.5 MB129 ms152.7 MB3864 KB
Rust -O112 ms45.5 MB120 ms147.3 MB3864 KB
Axle -O020 ms45.5 MB76 ms124.5 MB314 KB
C++ -O044 ms45.5 MB352 ms112.0 MB30 KB
Rust -O084 ms45.5 MB102 ms146.3 MB3880 KB
▸ Source
// SIMD-accelerated sort on 1_048_576 f64 values — counting sort variant.
//
// Strategy: the LCG produces integer keys in `[0, 1_048_576)` (= 2^20),
// stored as `buf[i] = m_i / 1000.0` (float view); the sort reconstructs
// the key via `round(buf[i] * 1000)`. Counting sort is O(N + K); with
// K = 2^20 the bucket array is 4 MiB — outside L1 but resident in L2/L3
// throughout the histogram and emit passes, so every access is still
// cache-bound rather than DRAM-bound.
//
// SIMD usage (one fused pass over 131_072 f64x8 chunks):
//   * Global max via `f64x8` horizontal-reduce. f64x8 is NOT in
//     `axle_runtime::simd_dispatch::has_runtime_kernel`, so the
//     reduction lowers inline as `llvm.vector.reduce.fmax.v8f64` —
//     ~5 native AVX instructions per chunk, no indirect call.
//   * Key pre-scaling via `f64x8.fma(v, 1000, 0.5)` → `llvm.fma.v8f64`
//     inline (single-rounded), stored back in place. This lifts the
//     per-element float math out of the latency-bound histogram
//     scatter so that loop only truncates + increments.
//
// What is deliberately NOT SIMD: the histogram increment and the emit
// pass are scatters (random `counts[key]++` / replicated stores).
// Axle exposes no gather/scatter/conflict-detect builtin, and on this
// input (N = K, ~1 element per bucket) a vectorised fill never reaches
// its 8-lane threshold — so both stay scalar by design.
//
// The C++ and Rust siblings (`simd_sort.cpp` / `simd_sort.rs`) run the
// identical O(N) counting sort — same max-scan, same `fma`-based bucket
// prescale, same histogram + emit passes — so this bench reads as a
// language-speed comparison on one algorithm, not counting sort vs.
// introsort. All three print byte-identical output.

use std::numeric::math;

fn main() : void {
    let mut buf : f64[1048576] = f64[1048576];

    // ── Deterministic LCG (Knuth MMIX) → buf[i] = m_i / 1000, ──
    // ── m_i ∈ [0, 2^20). Keys fit in 20 bits so K = 2^20.      ──
    let mut state : i64 = 12345;
    let mul : i64 = 6364136223846793005;
    let add : i64 = 1442695040888963407;
    let mut m : i64 = 0;
    let mut sum_in : i64 = 0;
    for (i of 0..1048576) {
        state = state * mul + add;
        m = state % 1048576;
        if (m < 0) {
            m = m + 1048576;
        }
        sum_in = sum_in + m;
        buf[i] = (m as f64) / 1000.0;
    }

    // ── Fused SIMD pass over 131_072 f64x8 chunks (no tail):       ──
    // ──   1. max-scan — `f64x8.max` lowers to                      ──
    // ──      `llvm.vector.reduce.fmax.v8f64` inline;               ──
    // ──   2. key pre-scale — `scaled = v*1000 + 0.5` via           ──
    // ──      `f64x8.fma` (f64x8 ∉ has_runtime_kernel, so it        ──
    // ──      lowers inline as `llvm.fma.v8f64`, single-rounded),   ──
    // ──      stored back in place so the histogram only truncates. ──
    // ── Both reuse the one loaded vector `v`; the FMA store is the ──
    // ── only added traffic and it streams sequentially.            ──
    let mut max_val : f64 = -1000000000000000000000000000000.0;
    let mut chunk_hi : f64 = 0.0;
    let mut v : f64x8 = f64x8.splat(0.0);
    let mut scaled : f64x8 = f64x8.splat(0.0);
    let scale : f64x8 = f64x8.splat(1000.0);
    let half : f64x8 = f64x8.splat(0.5);
    let mut c : i32 = 0;
    while (c < 131072) {
        v = f64x8.load(buf, c * 8);
        chunk_hi = v.max();
        if (chunk_hi > max_val) {
            max_val = chunk_hi;
        }
        scaled = v.fma(scale, half);
        scaled.store(buf, c * 8);
        c = c + 1;
    }

    // ── Counting sort.                                         ──
    // ── K bound derived from `max_val`; cap is 2^20 - 1 so the ──
    // ── 4 MiB count array fits comfortably in L2/L3.           ──
    let mut key : i32 = 0;
    let bucket_sz : i32 = (max_val * 1000.0 + 0.5).truncToInt() as i32 + 1;
    let mut counts : i32[1048576] = i32[1048576];
    let mut i : i32 = 0;
    while (i < 1048576) {
        // buf[i] already holds m+0.5 from the SIMD FMA pass, so a bare
        // truncation lands in bucket m (the rounding bias that would
        // otherwise sink m into bucket m-1 was paid once, vectorised).
        key = buf[i].truncToInt() as i32;
        counts[key] = counts[key] + 1;
        i = i + 1;
    }

    // ── Emit sorted output (scan histogram, replicate each      ──
    // ── reconstructed value `count` times). Empty buckets are   ──
    // ── skipped with a single branch — most buckets are hit on  ──
    // ── this input (N = K, so the histogram is dense).          ──
    let mut out_pos : i32 = 0;
    let mut k : i32 = 0;
    let mut n : i32 = 0;
    let mut j : i32 = 0;
    let mut val : f64 = 0.0;
    let mut sum_out : i64 = 0;
    while (k < bucket_sz) {
        n = counts[k];
        if (n > 0) {
            sum_out = sum_out + (k as i64) * (n as i64);
            val = (k as f64) / 1000.0;
            j = 0;
            while (j < n) {
                buf[out_pos] = val;
                out_pos = out_pos + 1;
                j = j + 1;
            }
        }
        k = k + 1;
    }

    // ── Multiset invariant: a correct sort is a permutation of the ──
    // ── input, so Σ keys must be preserved. Monotonicity is NOT a  ──
    // ── valid check here — a counting sort emits buckets in        ──
    // ── ascending k by construction, so the output is always       ──
    // ── monotone even when keys are mis-bucketed. The checksum is  ──
    // ── what actually catches truncation/bucketing bugs.           ──
    // ── On success prints nothing (stdout stays byte-for-byte      ──
    // ── identical to the Rust reference); on mismatch flags it.    ──
    if (sum_out < sum_in) {
        println("CORRUPTED");
    }
    if (sum_out > sum_in) {
        println("CORRUPTED");
    }
    if (out_pos < 1048576) {
        println("MISSING ELEMENTS");
    }

    // ── Cross-impl sanity prints — first / mid / last sorted   ──
    // ── slot, in micro-units so println-of-i64 matches the      ──
    // ── Rust reference output byte-for-byte.                    ──
    println((buf[0] * 1000000.0).truncToInt());
    println((buf[524288] * 1000000.0).truncToInt());
    println((buf[1048575] * 1000000.0).truncToInt());
    println("Done!");
}