1 // Copyright 2014 The Gonum Authors. All rights reserved.
2 // Use of this code is governed by a BSD-style
3 // license that can be found in the LICENSE file
5 // Script for automatic code generation of the benchmark routines.
19 var copyrightnotice = []byte(`// Copyright 2014 The Gonum Authors. All rights reserved.
20 // Use of this code is governed by a BSD-style
21 // license that can be found in the LICENSE file`)
23 var autogen = []byte("// Code generated by \"go run gonum.org/v1/gonum/blas/testblas/benchautogen/autogen_bench_level1double.go\"; DO NOT EDIT.\n")
25 var imports = []byte(`import(
26 "golang.org/x/exp/rand"
29 "gonum.org/v1/gonum/blas"
32 var randomSliceFunction = []byte(`func randomSlice(l, idx int) ([]float64) {
36 s := make([]float64, l * idx)
50 var level1Sizes = []struct {
64 upper: "MEDIUM_SLICE",
82 type level1functionStruct struct {
88 extraName string // if have a couple different cases for the same function
91 var level1Functions = []level1functionStruct{
94 sig: "n int, x []float64, incX int, y []float64, incY int",
95 call: "n, x, incX, y, incY",
100 sig: "n int, x []float64, incX int",
106 sig: "n int, x []float64, incX int",
112 sig: "n int, x []float64, incX int",
118 sig: "n int, x []float64, incX int, y []float64, incY int",
119 call: "n, x, incX, y, incY",
124 sig: "n int, x []float64, incX int, y []float64, incY int",
125 call: "n, x, incX, y, incY",
130 sig: "n int, alpha float64, x []float64, incX int, y []float64, incY int",
131 call: "n, alpha, x, incX, y, incY",
132 extraSetup: "alpha := 2.4",
137 sig: "n int, x []float64, incX int, y []float64, incY int, c, s float64",
138 call: "n, x, incX, y, incY, c, s",
139 extraSetup: "c := 0.89725836967\ns:= 0.44150585279",
144 sig: "n int, x []float64, incX int, y []float64, incY int, p blas.DrotmParams",
145 call: "n, x, incX, y, incY, p",
146 extraSetup: "p := blas.DrotmParams{Flag: blas.OffDiagonal, H: [4]float64{0, -0.625, 0.9375,0}}",
152 sig: "n int, x []float64, incX int, y []float64, incY int, p blas.DrotmParams",
153 call: "n, x, incX, y, incY, p",
154 extraSetup: "p := blas.DrotmParams{Flag: blas.OffDiagonal, H: [4]float64{5.0 / 12, 0, 0, 0.625}}",
160 sig: "n int, x []float64, incX int, y []float64, incY int, p blas.DrotmParams",
161 call: "n, x, incX, y, incY, p",
162 extraSetup: "p := blas.DrotmParams{Flag: blas.OffDiagonal, H: [4]float64{4096, -3584, 1792, 4096}}",
168 sig: "n int, alpha float64, x []float64, incX int",
169 call: "n, alpha, x, incX",
170 extraSetup: "alpha := 2.4",
176 gopath = os.Getenv("GOPATH")
178 panic("gopath not set")
183 blasPath := filepath.Join(gopath, "src", "gonum.org", "v1", "gonum", "blas")
185 pkgs := []struct{ name string }{{name: "native"}, {name: "cgo"}}
187 for _, pkg := range pkgs {
188 err := level1(filepath.Join(blasPath, pkg.name), pkg.name)
194 err = exec.Command("go", "fmt", path.Join("gonum.org", "v1", "gonum", "blas", pkg.name)).Run()
202 func printHeader(f *os.File, name string) error {
203 if _, err := f.Write(autogen); err != nil {
206 f.WriteString("\n\n")
207 f.Write(copyrightnotice)
208 f.WriteString("\n\n")
209 f.WriteString("package " + name)
210 f.WriteString("\n\n")
212 f.WriteString("\n\n")
216 // Generate the benchmark scripts for level1
217 func level1(benchPath string, pkgname string) error {
218 // Generate level 1 benchmarks
219 level1Filepath := filepath.Join(benchPath, "level1doubleBench_auto_test.go")
220 f, err := os.Create(level1Filepath)
226 printHeader(f, pkgname)
228 // Print all of the constants
229 f.WriteString("const (\n")
230 f.WriteString("\tposInc1 = " + strconv.Itoa(posInc1) + "\n")
231 f.WriteString("\tposInc2 = " + strconv.Itoa(posInc2) + "\n")
232 f.WriteString("\tnegInc1 = " + strconv.Itoa(negInc1) + "\n")
233 f.WriteString("\tnegInc2 = " + strconv.Itoa(negInc2) + "\n")
234 for _, con := range level1Sizes {
235 f.WriteString("\t" + con.upper + " = " + strconv.Itoa(con.size) + "\n")
240 // Write the randomSlice function
241 f.Write(randomSliceFunction)
242 f.WriteString("\n\n")
244 // Start writing the benchmarks
245 for _, fun := range level1Functions {
246 writeLevel1Benchmark(fun, f)
247 f.WriteString("\n/* ------------------ */ \n")
253 func writeLevel1Benchmark(fun level1functionStruct, f *os.File) {
254 // First, write the base benchmark file
255 f.WriteString("func benchmark" + fun.camel + fun.extraName + "(b *testing.B, ")
256 f.WriteString(fun.sig)
257 f.WriteString(") {\n")
259 f.WriteString("b.ResetTimer()\n")
260 f.WriteString("for i := 0; i < b.N; i++{\n")
261 f.WriteString("\timpl." + fun.camel + "(")
263 f.WriteString(fun.call)
264 f.WriteString(")\n}\n}\n")
267 // Write all of the benchmarks to call it
268 for _, sz := range level1Sizes {
269 lambda := func(incX, incY, name string, twoInput bool) {
270 f.WriteString("func Benchmark" + fun.camel + fun.extraName + sz.camel + name + "(b *testing.B){\n")
271 f.WriteString("n := " + sz.upper + "\n")
272 f.WriteString("incX := " + incX + "\n")
273 f.WriteString("x := randomSlice(n, incX)\n")
275 f.WriteString("incY := " + incY + "\n")
276 f.WriteString("y := randomSlice(n, incY)\n")
278 f.WriteString(fun.extraSetup + "\n")
279 f.WriteString("benchmark" + fun.camel + fun.extraName + "(b, " + fun.call + ")\n")
280 f.WriteString("}\n\n")
283 lambda("1", "", "UnitaryInc", false)
284 lambda("posInc1", "", "PosInc", false)
286 lambda("1", "1", "BothUnitary", true)
287 lambda("posInc1", "1", "IncUni", true)
288 lambda("1", "negInc1", "UniInc", true)
289 lambda("posInc1", "negInc1", "BothInc", true)