-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbenchmarks_test.go
156 lines (137 loc) · 2.51 KB
/
benchmarks_test.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
package blas
import (
"fmt"
"testing"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/stat/sampleuv"
)
func BenchmarkAxpy(b *testing.B) {
nnz := 1000
dim := 10000
x := make([]float64, nnz)
indx := make([]int, nnz)
y := make([]float64, (dim)*(dim))
rnd := rand.New(rand.NewSource(0))
sampleuv.WithoutReplacement(indx, dim, rnd)
for i := range x {
x[i] = rnd.Float64()
}
for i := range y {
y[i] = rnd.Float64()
}
inputs := []struct {
name string
alpha float64
x []float64
indx []int
y []float64
incy int
}{
{
name: "inc",
alpha: 1,
x: x,
indx: indx,
y: y,
incy: dim,
},
{
name: "unitary",
alpha: 1,
x: x,
indx: indx,
y: y[:dim],
incy: 1,
},
}
benchmarks := []struct {
name string
f func(alpha float64, x []float64, indx []int, y []float64, incy int)
}{
{
name: "Naive Go",
f: func(alpha float64, x []float64, indx []int, y []float64, incy int) {
for i, index := range indx {
y[index*incy] += alpha * x[i]
}
},
},
{
name: "Asm",
f: Dusaxpy,
},
}
for _, input := range inputs {
for _, bench := range benchmarks {
b.Run(fmt.Sprintf("%s %s", input.name, bench.name), func(b *testing.B) {
for i := 0; i < b.N; i++ {
bench.f(input.alpha, input.x, input.indx, input.y, input.incy)
}
})
}
}
}
func BenchmarkDot(b *testing.B) {
nnz := 1000
dim := 10000
x := make([]float64, nnz)
indx := make([]int, nnz)
y := make([]float64, (dim)*(dim))
rnd := rand.New(rand.NewSource(0))
sampleuv.WithoutReplacement(indx, dim, rnd)
for i := range x {
x[i] = rnd.Float64()
}
for i := range y {
y[i] = rnd.Float64()
}
inputs := []struct {
name string
x []float64
indx []int
y []float64
incy int
}{
{
name: "inc",
x: x,
indx: indx,
y: y,
incy: dim,
},
{
name: "unitary",
x: x,
indx: indx,
y: y[:dim],
incy: 1,
},
}
benchmarks := []struct {
name string
f func(x []float64, indx []int, y []float64, incy int) float64
}{
{
name: "Naive Go",
f: func(x []float64, indx []int, y []float64, incy int) (dot float64) {
for i, index := range indx {
dot += x[i] * y[index*incy]
}
return
},
},
{
name: "Asm",
f: Dusdot,
},
}
for _, input := range inputs {
for _, bench := range benchmarks {
b.Run(fmt.Sprintf("%s %s", input.name, bench.name), func(b *testing.B) {
for i := 0; i < b.N; i++ {
bench.f(input.x, input.indx, input.y, input.incy)
}
})
}
}
}