From 5bd0436884503537f1d37b8aa2d8d5e03ea4ce03 Mon Sep 17 00:00:00 2001 From: Naseer Dari Date: Sat, 11 Nov 2017 09:28:14 -0600 Subject: [PATCH] Add benchmarking script This PR will add a bench.sh script which will run the benchmarking tests for base, sse, and avx implementations. Also, the dep package manager is used to resolve dependencies. --- .gitignore | 3 + Gopkg.lock | 33 ++++++++++ Gopkg.toml | 7 ++ arith_bench_test.go | 151 ++++++++++++++++++++++++++++++++++++++++++++ arith_test.go | 4 +- bench.sh | 17 +++++ 6 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 Gopkg.lock create mode 100644 Gopkg.toml create mode 100644 arith_bench_test.go create mode 100755 bench.sh diff --git a/.gitignore b/.gitignore index daf913b..350ebcb 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,6 @@ _testmain.go *.exe *.test *.prof + +vendor +vendor/* diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..b27da88 --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,33 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + name = "github.com/chewxy/math32" + packages = ["."] + revision = "d1e7b22839c693f54edf7811dd9487623abf2cd2" + version = "v1.0.0" + +[[projects]] + name = "github.com/davecgh/go-spew" + packages = ["spew"] + revision = "346938d642f2ec3594ed81d874461961cd0faa76" + version = "v1.1.0" + +[[projects]] + name = "github.com/pmezard/go-difflib" + packages = ["difflib"] + revision = "792786c7400a136282c1664665ae0a8db921c6c2" + version = "v1.0.0" + +[[projects]] + name = "github.com/stretchr/testify" + packages = ["assert"] + revision = "69483b4bd14f5845b5a1e55bca19e954e827f1d0" + version = "v1.1.4" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "efdc54437fbcbccc06963a55dfe8a20ed1debec74f99044bbdeee793399ab451" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..bc85eb6 --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,7 @@ +[[constraint]] + name = "github.com/chewxy/math32" + version = "1.0.0" + +[[constraint]] + name = "github.com/stretchr/testify" + version = "1.1.4" diff --git a/arith_bench_test.go b/arith_bench_test.go new file mode 100644 index 0000000..2d9698d --- /dev/null +++ b/arith_bench_test.go @@ -0,0 +1,151 @@ +// +build !sse,!avx + +package vecf32 + +import ( + "testing" + + "github.com/chewxy/math32" +) + +/* BENCHMARKS */ + +func _vanillaVecAdd(a, b []float32) { + for i := range a { + a[i] += b[i] + } +} + +func BenchmarkVecAdd(b *testing.B) { + x := Range(0, niceprime) + y := Range(niceprime, 2*niceprime) + + for n := 0; n < b.N; n++ { + Add(x, y) + } +} + +func BenchmarkVanillaVecAdd(b *testing.B) { + x := Range(0, niceprime) + y := Range(niceprime, 2*niceprime) + + for n := 0; n < b.N; n++ { + _vanillaVecAdd(x, y) + } +} + +func _vanillaVecSub(a, b []float32) { + for i := range a { + a[i] -= b[i] + } +} + +func BenchmarkVecSub(b *testing.B) { + x := Range(0, niceprime) + y := Range(niceprime, 2*niceprime) + + for n := 0; n < b.N; n++ { + Sub(x, y) + } +} + +func BenchmarkVanillaVecSub(b *testing.B) { + x := Range(0, niceprime) + y := Range(niceprime, 2*niceprime) + + for n := 0; n < b.N; n++ { + _vanillaVecSub(x, y) + } +} + +func _vanillaVecMul(a, b []float32) { + for i := range a { + a[i] *= b[i] + } +} + +func BenchmarkVecMul(b *testing.B) { + x := Range(0, niceprime) + y := Range(niceprime, 2*niceprime) + + for n := 0; n < b.N; n++ { + Mul(x, y) + } +} + +func BenchmarkVanillaVecMul(b *testing.B) { + x := Range(0, niceprime) + y := Range(niceprime, 2*niceprime) + + for n := 0; n < b.N; n++ { + _vanillaVecMul(x, y) + } +} + +func _vanillaVecDiv(a, b []float32) { + for i := range a { + a[i] /= b[i] + } +} + +func BenchmarkVecDiv(b *testing.B) { + x := Range(0, niceprime) + y := Range(niceprime, 2*niceprime) + + for n := 0; n < b.N; n++ { + Div(x, y) + } +} + +func BenchmarkVanillaVecDiv(b *testing.B) { + x := Range(0, niceprime) + y := Range(niceprime, 2*niceprime) + + for n := 0; n < b.N; n++ { + _vanillaVecDiv(x, y) + } +} + +func _vanillaVecSqrt(a []float32) { + for i, v := range a { + a[i] = math32.Sqrt(v) + } +} + +func BenchmarkVecSqrt(b *testing.B) { + x := Range(0, niceprime) + + for n := 0; n < b.N; n++ { + Sqrt(x) + } +} + +func BenchmarkVanillaVecSqrt(b *testing.B) { + x := Range(0, niceprime) + + for n := 0; n < b.N; n++ { + _vanillaVecSqrt(x) + } +} + +func _vanillaVecInverseSqrt(a []float32) { + for i, v := range a { + a[i] = 1.0 / math32.Sqrt(v) + } +} + +func BenchmarkVecInvSqrt(b *testing.B) { + x := Range(0, niceprime) + + for n := 0; n < b.N; n++ { + InvSqrt(x) + } +} + +func BenchmarkVanillaVecInvSqrt(b *testing.B) { + x := Range(0, niceprime) + + for n := 0; n < b.N; n++ { + _vanillaVecInverseSqrt(x) + } +} diff --git a/arith_test.go b/arith_test.go index 7ac64a6..7457ea5 100644 --- a/arith_test.go +++ b/arith_test.go @@ -298,7 +298,7 @@ func TestMinOf(t *testing.T) { correct := float32(0) got := MinOf(a) if got != correct { - t.Error("Expected %f. Got %v instead", correct, got) + t.Errorf("Expected %f. Got %v instead", correct, got) } a = []float32{} @@ -313,7 +313,7 @@ func TestArgmax(t *testing.T) { correct := 3 got := Argmax(a) if got != correct { - t.Error("Expected argmax to be %v. Got %v instead", correct, got) + t.Errorf("Expected argmax to be %v. Got %v instead", correct, got) } a = []float32{math32.Inf(-1), 2, 3, 4} diff --git a/bench.sh b/bench.sh new file mode 100755 index 0000000..fcfeb26 --- /dev/null +++ b/bench.sh @@ -0,0 +1,17 @@ +set -ex + +benchtime=${1:-1s} + +go test -bench . -benchtime $benchtime +go test -tags='sse' -bench . -benchtime $benchtime +go test -tags='avx' -bench . -benchtime $benchtime + +# travis compiles commands in script and then executes in bash. By adding +# set -e we are changing the travis build script's behavior, and the set +# -e lives on past the commands we are providing it. Some of the travis +# commands are supposed to exit with non zero status, but then continue +# executing. set -x makes the travis log files extremely verbose and +# difficult to understand. +# +# see travis-ci/travis-ci#5120 +set +ex