forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autogen_bench_level1double.go
284 lines (258 loc) · 6.95 KB
/
autogen_bench_level1double.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2014 The Gonum Authors. All rights reserved.
// Use of this code is governed by a BSD-style
// license that can be found in the LICENSE file
// Script for automatic code generation of the benchmark routines
package main
import (
"fmt"
"os"
"path/filepath"
"strconv"
)
var gopath string
var copyrightnotice = []byte(`// Copyright 2014 The Gonum Authors. All rights reserved.
// Use of this code is governed by a BSD-style
// license that can be found in the LICENSE file`)
var autogen = []byte(`// This file is autogenerated by github.com/gonum/blas/testblas/benchautogen/autogen_bench_level1double.go`)
var imports = []byte(`import(
"math/rand"
"testing"
"github.com/gonum/blas"
)`)
var randomSliceFunction = []byte(`func randomSlice(l, idx int) ([]float64) {
if idx < 0{
idx = -idx
}
s := make([]float64, l * idx)
for i := range s {
s[i] = rand.Float64()
}
return s
}`)
const (
posInc1 = 5
posInc2 = 3
negInc1 = -3
negInc2 = -4
)
var level1Sizes = []struct {
lower string
upper string
camel string
size int
}{
{
lower: "small",
upper: "SMALL_SLICE",
camel: "Small",
size: 10,
},
{
lower: "medium",
upper: "MEDIUM_SLICE",
camel: "Medium",
size: 1000,
},
{
lower: "large",
upper: "LARGE_SLICE",
camel: "Large",
size: 100000,
},
{
lower: "huge",
upper: "HUGE_SLICE",
camel: "Huge",
size: 10000000,
},
}
type level1functionStruct struct {
camel string
sig string
call string
extraSetup string
oneInput bool
extraName string // if have a couple different cases for the same function
}
var level1Functions = []level1functionStruct{
{
camel: "Ddot",
sig: "n int, x []float64, incX int, y []float64, incY int",
call: "n, x, incX, y, incY",
oneInput: false,
},
{
camel: "Dnrm2",
sig: "n int, x []float64, incX int",
call: "n, x, incX",
oneInput: true,
},
{
camel: "Dasum",
sig: "n int, x []float64, incX int",
call: "n, x, incX",
oneInput: true,
},
{
camel: "Idamax",
sig: "n int, x []float64, incX int",
call: "n, x, incX",
oneInput: true,
},
{
camel: "Dswap",
sig: "n int, x []float64, incX int, y []float64, incY int",
call: "n, x, incX, y, incY",
oneInput: false,
},
{
camel: "Dcopy",
sig: "n int, x []float64, incX int, y []float64, incY int",
call: "n, x, incX, y, incY",
oneInput: false,
},
{
camel: "Daxpy",
sig: "n int, alpha float64, x []float64, incX int, y []float64, incY int",
call: "n, alpha, x, incX, y, incY",
extraSetup: "alpha := 2.4",
oneInput: false,
},
{
camel: "Drot",
sig: "n int, x []float64, incX int, y []float64, incY int, c, s float64",
call: "n, x, incX, y, incY, c, s",
extraSetup: "c := 0.89725836967\ns:= 0.44150585279",
oneInput: false,
},
{
camel: "Drotm",
sig: "n int, x []float64, incX int, y []float64, incY int, p blas.DrotmParams",
call: "n, x, incX, y, incY, p",
extraSetup: "p := blas.DrotmParams{Flag: blas.OffDiagonal, H: [4]float64{0, -0.625, 0.9375,0}}",
oneInput: false,
extraName: "OffDia",
},
{
camel: "Drotm",
sig: "n int, x []float64, incX int, y []float64, incY int, p blas.DrotmParams",
call: "n, x, incX, y, incY, p",
extraSetup: "p := blas.DrotmParams{Flag: blas.OffDiagonal, H: [4]float64{5.0 / 12, 0, 0, 0.625}}",
oneInput: false,
extraName: "Dia",
},
{
camel: "Drotm",
sig: "n int, x []float64, incX int, y []float64, incY int, p blas.DrotmParams",
call: "n, x, incX, y, incY, p",
extraSetup: "p := blas.DrotmParams{Flag: blas.OffDiagonal, H: [4]float64{4096, -3584, 1792, 4096}}",
oneInput: false,
extraName: "Resc",
},
{
camel: "Dscal",
sig: "n int, alpha float64, x []float64, incX int",
call: "n, alpha, x, incX",
extraSetup: "alpha := 2.4",
oneInput: true,
},
}
func init() {
gopath = os.Getenv("GOPATH")
if gopath == "" {
panic("gopath not set")
}
}
func main() {
blasPath := filepath.Join(gopath, "src", "github.com", "gonum", "blas")
pkgs := []struct{ name string }{{name: "native"}, {name: "cgo"}}
for _, pkg := range pkgs {
err := level1(filepath.Join(blasPath, pkg.name), pkg.name)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}
func printHeader(f *os.File, name string) error {
if _, err := f.Write([]byte(copyrightnotice)); err != nil {
return err
}
f.WriteString("\n\n")
f.Write(autogen)
f.WriteString("\n\n")
f.WriteString("package " + name)
f.WriteString("\n\n")
f.Write(imports)
f.WriteString("\n\n")
return nil
}
// Generate the benchmark scripts for level1
func level1(benchPath string, pkgname string) error {
// Generate level 1 benchmarks
level1Filepath := filepath.Join(benchPath, "level1doubleBench_auto_test.go")
f, err := os.Create(level1Filepath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer f.Close()
printHeader(f, pkgname)
// Print all of the constants
f.WriteString("const (\n")
f.WriteString("\tposInc1 = " + strconv.Itoa(posInc1) + "\n")
f.WriteString("\tposInc2 = " + strconv.Itoa(posInc2) + "\n")
f.WriteString("\tnegInc1 = " + strconv.Itoa(negInc1) + "\n")
f.WriteString("\tnegInc2 = " + strconv.Itoa(negInc2) + "\n")
for _, con := range level1Sizes {
f.WriteString("\t" + con.upper + " = " + strconv.Itoa(con.size) + "\n")
}
f.WriteString(")\n")
f.WriteString("\n")
// Write the randomSlice function
f.Write(randomSliceFunction)
f.WriteString("\n\n")
// Start writing the benchmarks
for _, fun := range level1Functions {
writeLevel1Benchmark(fun, f)
f.WriteString("\n/* ------------------ */ \n")
}
return nil
}
func writeLevel1Benchmark(fun level1functionStruct, f *os.File) {
// First, write the base benchmark file
f.WriteString("func benchmark" + fun.camel + fun.extraName + "(b *testing.B, ")
f.WriteString(fun.sig)
f.WriteString(") {\n")
f.WriteString("b.ResetTimer()\n")
f.WriteString("for i := 0; i < b.N; i++{\n")
f.WriteString("\timpl." + fun.camel + "(")
f.WriteString(fun.call)
f.WriteString(")\n}\n}\n")
f.WriteString("\n")
// Write all of the benchmarks to call it
for _, sz := range level1Sizes {
lambda := func(incX, incY, name string, twoInput bool) {
f.WriteString("func Benchmark" + fun.camel + fun.extraName + sz.camel + name + "(b *testing.B){\n")
f.WriteString("n := " + sz.upper + "\n")
f.WriteString("incX := " + incX + "\n")
f.WriteString("x := randomSlice(n, incX)\n")
if twoInput {
f.WriteString("incY := " + incY + "\n")
f.WriteString("y := randomSlice(n, incY)\n")
}
f.WriteString(fun.extraSetup + "\n")
f.WriteString("benchmark" + fun.camel + fun.extraName + "(b, " + fun.call + ")\n")
f.WriteString("}\n\n")
}
if fun.oneInput {
lambda("1", "", "UnitaryInc", false)
lambda("posInc1", "", "PosInc", false)
} else {
lambda("1", "1", "BothUnitary", true)
lambda("posInc1", "1", "IncUni", true)
lambda("1", "negInc1", "UniInc", true)
lambda("posInc1", "negInc1", "BothInc", true)
}
}
}