-
Notifications
You must be signed in to change notification settings - Fork 6
/
avx_test.go
261 lines (235 loc) · 4.87 KB
/
avx_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
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
package avx
import (
"math"
"sync"
"testing"
)
func TestMmMalloc(t *testing.T) {
for _, size := range []int{7, 8, 15} {
func(size int) {
x := MmMalloc(size)
defer MmFree(x)
if len(x) != align(size) {
t.Errorf("MmMalloc should return float slice size of %d, but size is %d", align(size), len(x))
}
}(size)
}
}
func TestAdd(t *testing.T) {
for _, size := range []int{7, 8, 15} {
func(size int) {
x := MmMalloc(size)
y := MmMalloc(size)
z := MmMalloc(size)
defer MmFree(x)
defer MmFree(y)
defer MmFree(z)
truth := make([]float32, size)
for i := 0; i < size; i++ {
x[i] = float32(i)
y[i] = float32(i + 1)
truth[i] = x[i] + y[i]
}
Add(size, x, y, z)
for i := 0; i < size; i++ {
if truth[i] != z[i] {
t.Errorf("Add should return %f in %d, but %f", truth[i], i, z[i])
}
}
}(size)
}
}
func TestSub(t *testing.T) {
for _, size := range []int{7, 8, 15} {
func(size int) {
x := MmMalloc(size)
y := MmMalloc(size)
z := MmMalloc(size)
defer MmFree(x)
defer MmFree(y)
defer MmFree(z)
truth := make([]float32, size)
for i := 0; i < size; i++ {
x[i] = float32(i)
y[i] = float32(i + 1)
truth[i] = x[i] - y[i]
}
Sub(size, x, y, z)
for i := 0; i < size; i++ {
if truth[i] != z[i] {
t.Errorf("Mul should return %f in %d, but %f", truth[i], i, z[i])
}
}
}(size)
}
}
func TestMul(t *testing.T) {
for _, size := range []int{7, 8, 15} {
func(size int) {
x := MmMalloc(size)
y := MmMalloc(size)
z := MmMalloc(size)
defer MmFree(x)
defer MmFree(y)
defer MmFree(z)
truth := make([]float32, size)
for i := 0; i < size; i++ {
x[i] = float32(i)
y[i] = float32(i + 1)
truth[i] = x[i] * y[i]
}
Mul(size, x, y, z)
for i := 0; i < size; i++ {
if truth[i] != z[i] {
t.Errorf("Mul should return %f in %d, but %f", truth[i], i, z[i])
}
}
}(size)
}
}
func TestDot(t *testing.T) {
for _, size := range []int{7, 8, 15} {
func(size int) {
x := MmMalloc(size)
y := MmMalloc(size)
defer MmFree(x)
defer MmFree(y)
var truth float32
for i := 0; i < size; i++ {
x[i] = float32(i)
y[i] = float32(i + 1)
truth += x[i] * y[i]
}
result := Dot(size, x, y)
if truth != result {
t.Errorf("Dot should return %f, but %f", truth, result)
}
}(size)
}
}
func TestEuclideanDistance(t *testing.T) {
for _, size := range []int{7, 8, 15} {
func(size int) {
x := MmMalloc(size)
y := MmMalloc(size)
defer MmFree(x)
defer MmFree(y)
var tmp float64
for i := 0; i < size; i++ {
x[i] = float32(i)
y[i] = float32(i + 1)
tmp += math.Pow(float64(x[i]-y[i]), 2.0)
}
truth := float32(math.Sqrt(float64(tmp)))
result := EuclideanDistance(size, x, y)
if truth != result {
t.Errorf("Dot should return %f, but %f", truth, result)
}
}(size)
}
}
func BenchmarkEuclideanDistanceAVX(b *testing.B) {
size := 2048
x := MmMalloc(size)
y := MmMalloc(size)
defer MmFree(x)
defer MmFree(y)
for i := 0; i < size; i++ {
x[i] = float32(i)
y[i] = float32(i + 1)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
EuclideanDistance(size, x, y)
}
}
func BenchmarkEuclideanDistanceGoroutine(b *testing.B) {
size := 2048
x := make([]float64, size)
y := make([]float64, size)
for i := 0; i < size; i++ {
x[i] = float64(i)
y[i] = float64(i + 1)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
numWorkers := 8
queue := make(chan int, numWorkers)
out := make(chan float64, numWorkers)
go func() {
for idx, _ := range x {
queue <- idx
}
close(queue)
}()
var wg sync.WaitGroup
for n := 0; n < numWorkers; n++ {
wg.Add(1)
go func() {
defer wg.Done()
for q := range queue {
out <- math.Pow(x[q]-y[q], 2)
}
}()
}
done := make(chan struct{})
result := 0.0
go func() {
for o := range out {
result += o
}
done <- struct{}{}
}()
wg.Wait()
close(out)
<-done
close(done)
}
}
func BenchmarkEuclideanDistance(b *testing.B) {
size := 2048
x := make([]float64, size)
y := make([]float64, size)
for i := 0; i < size; i++ {
x[i] = float64(i)
y[i] = float64(i + 1)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
distance := 0.0
for idx, _ := range x {
diff := x[idx] - y[idx]
distance += math.Pow(diff, 2)
}
math.Sqrt(distance)
}
}
func BenchmarkHypot(b *testing.B) {
size := 2048
x := make([]float64, size)
y := make([]float64, size)
for i := 0; i < size; i++ {
x[i] = float64(i)
y[i] = float64(i + 1)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
norm := 0.0
for idx, _ := range x {
diff := x[idx] - y[idx]
norm = math.Hypot(norm, diff)
}
}
}
func TestAlign(t *testing.T) {
expects := [][]int{
[]int{7, 8},
[]int{8, 8},
[]int{9, 16},
}
for _, expect := range expects {
if size := align(expect[0]); size != expect[1] {
t.Errorf("align should return %d, but %d", expect[1], size)
}
}
}