code wiki / _hdl_build / nx_f32_hw_matmul_perf.nx

nx_f32_hw_matmul_perf.nx source

↩ module page · 58 lines · 2507 B

1// nx_f32_hw_matmul_perf.nx -- MEASURED head-to-head: software vs hardware f32 GEMM (both sovereign). 2// 3// Times nx_f32_matmul (bits-up SOFTWARE f32) vs nx_f32_hw_matmul (SSE addss/mulss) on the SAME inputs, 4// clock read IN-ORGAN (sys_now_ms). Reports MFLOP/s for each + the speedup. This is a legitimate 5// measured comparison (both are OURS -- no 3rd-party); it quantifies how much the SSE hardware rung 6// buys, and what gap to BLAS remains (packed SIMD addps/mulps + blocking = the next rung). 7// Sovereign: imports nx_f32_matmul + nx_f32_hw_matmul + nx_syscalls. license_tier: ORIGINAL 8import "nx_f32_matmul.nx" 9import "nx_f32_hw_matmul.nx" 10import "nx_syscalls.nx" 11const K_MAGIC_32768: i64 = 32768 12 13// which: 0 = software nx_f32_matmul, 1 = hardware nx_f32_hw_matmul. returns MFLOP/s. 14func time_mm(which: i64, A: *i64, B: *i64, C: *i64, N: i64) -> i64 { 15 var reps: i64 = 1; var dt: i64 = 0; var go: i64 = 1 16 while go == 1 { 17 let t0: i64 = sys_now_ms() 18 var r: i64 = 0 19 while r < reps { 20 if which == 0 { nx_f32_matmul(A, B, C, N, N, N) } else { nx_f32_hw_matmul(A, B, C, N, N, N) } 21 r = r + 1 22 } 23 let t1: i64 = sys_now_ms() 24 dt = t1 - t0 25 if dt >= 50 { go = 0 } else { if reps >= K_MAGIC_32768 { go = 0 } else { reps = reps * 2 } } 26 } 27 if dt <= 0 { dt = 1 } 28 let macs: i64 = N * N * N * reps 29 let flops: i64 = 2 * macs 30 return flops / (dt * 1000) 31} 32 33func run_pair(N: i64) -> i64 { 34 let A: *i64 = sys_mmap(N * N * 8) as *i64 35 let B: *i64 = sys_mmap(N * N * 8) as *i64 36 let C: *i64 = sys_mmap(N * N * 8) as *i64 37 var i: i64 = 0 38 while i < N * N { A[i] = f32_of(2); B[i] = f32_of(2); i = i + 1 } 39 let sw: i64 = time_mm(0, A, B, C, N) 40 let hw: i64 = time_mm(1, A, B, C, N) 41 var speed: i64 = 0 42 if sw > 0 { speed = hw / sw } 43 hmm_puts(" N="); hmm_putn(N) 44 hmm_puts(" software="); hmm_putn(sw); hmm_puts(" MFLOP/s") 45 hmm_puts(" hardware="); hmm_putn(hw); hmm_puts(" MFLOP/s") 46 hmm_puts(" speedup="); hmm_putn(speed); hmm_puts("x\n") 47 return 0 48} 49 50func main() -> i64 { 51 hmm_puts("=== f32 GEMM head-to-head: software (bits-up) vs hardware (SSE), both sovereign ===\n") 52 run_pair(32) 53 run_pair(64) 54 run_pair(128) 55 hmm_puts(" HONEST: both are OUR sovereign matmuls (no BLAS/cuBLAS run). The remaining gap to\n") 56 hmm_puts(" single-thread BLAS (~tens of GFLOP/s) = packed SIMD (addps/mulps, 4-wide) + blocking = next rung.\n") 57 sys_exit(0); return 0 58}