1
0
mirror of https://github.com/golang/go synced 2024-10-03 22:31:21 -06:00

math/big: improved karatsuba calibration code, better mul benchmark

An attempt to profit from CL 6176043 (fix to superpolinomial
runtime of karatsuba multiplication) and determine a better
karatsuba threshold. The result indicates that 32 is still
a reasonable value. Left the threshold as is (== 32), but
made some minor changes to the calibrate code which are
worthwhile saving (use of existing benchmarking code for
better results, better use of package time).

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6260062
This commit is contained in:
Robert Griesemer 2012-06-04 09:48:27 -07:00
parent de0c1c9cf5
commit cc1890cbe3
2 changed files with 31 additions and 34 deletions

View File

@ -21,15 +21,17 @@ import (
var calibrate = flag.Bool("calibrate", false, "run calibration test") var calibrate = flag.Bool("calibrate", false, "run calibration test")
// measure returns the time to run f func karatsubaLoad(b *testing.B) {
func measure(f func()) time.Duration { BenchmarkMul(b)
const N = 100 }
start := time.Now()
for i := N; i > 0; i-- { // measureKaratsuba returns the time to run a Karatsuba-relevant benchmark
f() // given Karatsuba threshold th.
} func measureKaratsuba(th int) time.Duration {
stop := time.Now() th, karatsubaThreshold = karatsubaThreshold, th
return stop.Sub(start) / N res := testing.Benchmark(karatsubaLoad)
karatsubaThreshold = th
return time.Duration(res.NsPerOp())
} }
func computeThresholds() { func computeThresholds() {
@ -37,35 +39,33 @@ func computeThresholds() {
fmt.Printf("(run repeatedly for good results)\n") fmt.Printf("(run repeatedly for good results)\n")
// determine Tk, the work load execution time using basic multiplication // determine Tk, the work load execution time using basic multiplication
karatsubaThreshold = 1e9 // disable karatsuba Tb := measureKaratsuba(1e9) // th == 1e9 => Karatsuba multiplication disabled
Tb := measure(benchmarkMulLoad) fmt.Printf("Tb = %10s\n", Tb)
fmt.Printf("Tb = %dns\n", Tb)
// thresholds // thresholds
n := 8 // any lower values for the threshold lead to very slow multiplies th := 4
th1 := -1 th1 := -1
th2 := -1 th2 := -1
var deltaOld time.Duration var deltaOld time.Duration
for count := -1; count != 0; count-- { for count := -1; count != 0 && th < 128; count-- {
// determine Tk, the work load execution time using Karatsuba multiplication // determine Tk, the work load execution time using Karatsuba multiplication
karatsubaThreshold = n // enable karatsuba Tk := measureKaratsuba(th)
Tk := measure(benchmarkMulLoad)
// improvement over Tb // improvement over Tb
delta := (Tb - Tk) * 100 / Tb delta := (Tb - Tk) * 100 / Tb
fmt.Printf("n = %3d Tk = %8dns %4d%%", n, Tk, delta) fmt.Printf("th = %3d Tk = %10s %4d%%", th, Tk, delta)
// determine break-even point // determine break-even point
if Tk < Tb && th1 < 0 { if Tk < Tb && th1 < 0 {
th1 = n th1 = th
fmt.Print(" break-even point") fmt.Print(" break-even point")
} }
// determine diminishing return // determine diminishing return
if 0 < delta && delta < deltaOld && th2 < 0 { if 0 < delta && delta < deltaOld && th2 < 0 {
th2 = n th2 = th
fmt.Print(" diminishing return") fmt.Print(" diminishing return")
} }
deltaOld = delta deltaOld = delta
@ -74,10 +74,10 @@ func computeThresholds() {
// trigger counter // trigger counter
if th1 >= 0 && th2 >= 0 && count < 0 { if th1 >= 0 && th2 >= 0 && count < 0 {
count = 20 // this many extra measurements after we got both thresholds count = 10 // this many extra measurements after we got both thresholds
} }
n++ th++
} }
} }

View File

@ -6,6 +6,7 @@ package big
import ( import (
"io" "io"
"math/rand"
"strings" "strings"
"testing" "testing"
) )
@ -135,26 +136,22 @@ func TestMulRangeN(t *testing.T) {
} }
} }
var mulArg, mulTmp nat var rnd = rand.New(rand.NewSource(0x43de683f473542af))
var mulx = rndNat(1e4)
var muly = rndNat(1e4)
func init() { func rndNat(n int) nat {
const n = 1000 x := make(nat, n)
mulArg = make(nat, n)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
mulArg[i] = _M x[i] = Word(rnd.Int63()<<1 + rnd.Int63n(2))
}
}
func benchmarkMulLoad() {
for j := 1; j <= 10; j++ {
x := mulArg[0 : j*100]
mulTmp.mul(x, x)
} }
return x
} }
func BenchmarkMul(b *testing.B) { func BenchmarkMul(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
benchmarkMulLoad() var z nat
z.mul(mulx, muly)
} }
} }