OSDN Git Service

new repo
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / blas / testblas / benchautogen / autogen_bench_level1double.go
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
4
5 // Script for automatic code generation of the benchmark routines.
6 package main
7
8 import (
9         "fmt"
10         "os"
11         "os/exec"
12         "path"
13         "path/filepath"
14         "strconv"
15 )
16
17 var gopath string
18
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`)
22
23 var autogen = []byte("// Code generated by \"go run gonum.org/v1/gonum/blas/testblas/benchautogen/autogen_bench_level1double.go\"; DO NOT EDIT.\n")
24
25 var imports = []byte(`import(
26         "golang.org/x/exp/rand"
27         "testing"
28
29         "gonum.org/v1/gonum/blas"
30 )`)
31
32 var randomSliceFunction = []byte(`func randomSlice(l, idx int) ([]float64) {
33         if idx < 0{
34                 idx = -idx
35         }
36         s := make([]float64, l * idx)
37         for i := range s {
38                 s[i] = rand.Float64()
39         }
40         return s
41 }`)
42
43 const (
44         posInc1 = 5
45         posInc2 = 3
46         negInc1 = -3
47         negInc2 = -4
48 )
49
50 var level1Sizes = []struct {
51         lower string
52         upper string
53         camel string
54         size  int
55 }{
56         {
57                 lower: "small",
58                 upper: "SMALL_SLICE",
59                 camel: "Small",
60                 size:  10,
61         },
62         {
63                 lower: "medium",
64                 upper: "MEDIUM_SLICE",
65                 camel: "Medium",
66                 size:  1000,
67         },
68         {
69                 lower: "large",
70                 upper: "LARGE_SLICE",
71                 camel: "Large",
72                 size:  100000,
73         },
74         {
75                 lower: "huge",
76                 upper: "HUGE_SLICE",
77                 camel: "Huge",
78                 size:  10000000,
79         },
80 }
81
82 type level1functionStruct struct {
83         camel      string
84         sig        string
85         call       string
86         extraSetup string
87         oneInput   bool
88         extraName  string // if have a couple different cases for the same function
89 }
90
91 var level1Functions = []level1functionStruct{
92         {
93                 camel:    "Ddot",
94                 sig:      "n int, x []float64, incX int, y []float64, incY int",
95                 call:     "n, x, incX, y, incY",
96                 oneInput: false,
97         },
98         {
99                 camel:    "Dnrm2",
100                 sig:      "n int, x []float64, incX int",
101                 call:     "n, x, incX",
102                 oneInput: true,
103         },
104         {
105                 camel:    "Dasum",
106                 sig:      "n int, x []float64, incX int",
107                 call:     "n, x, incX",
108                 oneInput: true,
109         },
110         {
111                 camel:    "Idamax",
112                 sig:      "n int, x []float64, incX int",
113                 call:     "n, x, incX",
114                 oneInput: true,
115         },
116         {
117                 camel:    "Dswap",
118                 sig:      "n int, x []float64, incX int, y []float64, incY int",
119                 call:     "n, x, incX, y, incY",
120                 oneInput: false,
121         },
122         {
123                 camel:    "Dcopy",
124                 sig:      "n int, x []float64, incX int, y []float64, incY int",
125                 call:     "n, x, incX, y, incY",
126                 oneInput: false,
127         },
128         {
129                 camel:      "Daxpy",
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",
133                 oneInput:   false,
134         },
135         {
136                 camel:      "Drot",
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",
140                 oneInput:   false,
141         },
142         {
143                 camel:      "Drotm",
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}}",
147                 oneInput:   false,
148                 extraName:  "OffDia",
149         },
150         {
151                 camel:      "Drotm",
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}}",
155                 oneInput:   false,
156                 extraName:  "Dia",
157         },
158         {
159                 camel:      "Drotm",
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}}",
163                 oneInput:   false,
164                 extraName:  "Resc",
165         },
166         {
167                 camel:      "Dscal",
168                 sig:        "n int, alpha float64, x []float64, incX int",
169                 call:       "n, alpha, x, incX",
170                 extraSetup: "alpha := 2.4",
171                 oneInput:   true,
172         },
173 }
174
175 func init() {
176         gopath = os.Getenv("GOPATH")
177         if gopath == "" {
178                 panic("gopath not set")
179         }
180 }
181
182 func main() {
183         blasPath := filepath.Join(gopath, "src", "gonum.org", "v1", "gonum", "blas")
184
185         pkgs := []struct{ name string }{{name: "native"}, {name: "cgo"}}
186
187         for _, pkg := range pkgs {
188                 err := level1(filepath.Join(blasPath, pkg.name), pkg.name)
189                 if err != nil {
190                         fmt.Println(err)
191                         os.Exit(1)
192                 }
193
194                 err = exec.Command("go", "fmt", path.Join("gonum.org", "v1", "gonum", "blas", pkg.name)).Run()
195                 if err != nil {
196                         fmt.Println(err)
197                         os.Exit(1)
198                 }
199         }
200 }
201
202 func printHeader(f *os.File, name string) error {
203         if _, err := f.Write(autogen); err != nil {
204                 return err
205         }
206         f.WriteString("\n\n")
207         f.Write(copyrightnotice)
208         f.WriteString("\n\n")
209         f.WriteString("package " + name)
210         f.WriteString("\n\n")
211         f.Write(imports)
212         f.WriteString("\n\n")
213         return nil
214 }
215
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)
221         if err != nil {
222                 fmt.Println(err)
223                 os.Exit(1)
224         }
225         defer f.Close()
226         printHeader(f, pkgname)
227
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")
236         }
237         f.WriteString(")\n")
238         f.WriteString("\n")
239
240         // Write the randomSlice function
241         f.Write(randomSliceFunction)
242         f.WriteString("\n\n")
243
244         // Start writing the benchmarks
245         for _, fun := range level1Functions {
246                 writeLevel1Benchmark(fun, f)
247                 f.WriteString("\n/* ------------------ */ \n")
248         }
249
250         return nil
251 }
252
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")
258
259         f.WriteString("b.ResetTimer()\n")
260         f.WriteString("for i := 0; i < b.N; i++{\n")
261         f.WriteString("\timpl." + fun.camel + "(")
262
263         f.WriteString(fun.call)
264         f.WriteString(")\n}\n}\n")
265         f.WriteString("\n")
266
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")
274                         if twoInput {
275                                 f.WriteString("incY := " + incY + "\n")
276                                 f.WriteString("y := randomSlice(n, incY)\n")
277                         }
278                         f.WriteString(fun.extraSetup + "\n")
279                         f.WriteString("benchmark" + fun.camel + fun.extraName + "(b, " + fun.call + ")\n")
280                         f.WriteString("}\n\n")
281                 }
282                 if fun.oneInput {
283                         lambda("1", "", "UnitaryInc", false)
284                         lambda("posInc1", "", "PosInc", false)
285                 } else {
286                         lambda("1", "1", "BothUnitary", true)
287                         lambda("posInc1", "1", "IncUni", true)
288                         lambda("1", "negInc1", "UniInc", true)
289                         lambda("posInc1", "negInc1", "BothInc", true)
290                 }
291         }
292 }