-
Notifications
You must be signed in to change notification settings - Fork 19
/
d_test.go
402 lines (371 loc) · 8.25 KB
/
d_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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package blas
import (
"math"
"math/rand"
"testing"
)
func iCheck(t *testing.T, inc, N, r, e int) {
t.Logf("inc=%d N=%d : r=%d e=%d", inc, N, r, e)
if r != e {
t.FailNow()
}
}
func dCheck(t *testing.T, inc, N int, r, e float64) {
t.Logf("inc=%d N=%d : r=%f e=%f e-r=%g", inc, N, r, e, e-r)
if r != e {
t.FailNow()
}
}
var (
xd = []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9}
yd = []float64{1e17, 1e16, 1e15, 1e14, 1e13, 1e12, 1e11, 1e10, 1e9, 1e8,
1e7, 1e6, 1e5, 1e4, 1e3, 100, 10, 1}
)
func TestDdot(t *testing.T) {
for inc := 1; inc < 9; inc++ {
e := 0.0
k := 0
for N := 0; N <= len(xd)/inc; N++ {
if N > 0 {
e += xd[k] * yd[k]
k += inc
}
r := Ddot(N, xd, inc, yd, inc)
dCheck(t, inc, N, r, e)
}
}
}
func TestDnrm2(t *testing.T) {
for inc := 1; inc < 9; inc++ {
for N := 0; N <= len(xd)/inc; N++ {
e := math.Sqrt(Ddot(N, xd, inc, xd, inc))
r := Dnrm2(N, xd, inc)
dCheck(t, inc, N, r, e)
}
}
}
func TestDasum(t *testing.T) {
xd := []float64{-1, -2, 3, -4, -5, -6, 7, -8, 9, 1, -2, 3, -4, 5, -6, -7, 8}
for inc := 1; inc < 9; inc++ {
e := 0.0
k := 0
for N := 0; N <= len(xd)/inc; N++ {
if N > 0 {
e += math.Abs(xd[k])
k += inc
}
r := Dasum(N, xd, inc)
dCheck(t, inc, N, r, e)
}
}
}
func TestIdamax(t *testing.T) {
xd := []float64{-1, -2, 3, -4, -5, 0, -5, 0, 4, 2, 3, -1, 4, -2, -9, 0,
-1, 0, 0, 2, 2, -8, 2, 1, 0, 2, 4, 5, 8, 1, -7, 2, 9, 0, 1, -1}
for inc := 1; inc < 9; inc++ {
for N := 0; N <= len(xd)/inc; N++ {
i_max := 0
x_max := 0.0
for i := 0; i < N; i++ {
x := math.Abs(xd[i*inc])
if x > x_max {
x_max = x
i_max = i
}
}
r := Idamax(N, xd, inc)
iCheck(t, inc, N, r, i_max)
}
}
}
func TestDswap(t *testing.T) {
a := make([]float64, len(xd))
b := make([]float64, len(yd))
for inc := 1; inc < 9; inc++ {
for N := 0; N <= len(xd)/inc; N++ {
copy(a, xd)
copy(b, yd)
Dswap(N, a, inc, b, inc)
for i := 0; i < len(a); i++ {
if i <= inc*(N-1) && i%inc == 0 {
if a[i] != yd[i] || b[i] != xd[i] {
t.Fatalf("inc=%d N=%d", inc, N)
}
} else {
if a[i] != xd[i] || b[i] != yd[i] {
t.Fatalf("inc=%d N=%d", inc, N)
}
}
}
}
}
}
func TestDcopy(t *testing.T) {
for inc := 1; inc < 9; inc++ {
for N := 0; N <= len(xd)/inc; N++ {
a := make([]float64, len(xd))
Dcopy(N, xd, inc, a, inc)
for i := 0; i < inc*N; i++ {
if i%inc == 0 {
if a[i] != xd[i] {
t.Fatalf("inc=%d N=%d i=%d r=%f e=%f", inc, N, i, a[i],
xd[i])
}
} else {
if a[i] != 0 {
t.Fatalf("inc=%d N=%d i=%d r=%f e=0", inc, N, i, a[i])
}
}
}
}
}
}
func TestDaxpy(t *testing.T) {
for _, alpha := range []float64{0, -1, 1, 3} {
for inc := 1; inc < 9; inc++ {
for N := 0; N <= len(xd)/inc; N++ {
r := make([]float64, len(xd))
e := make([]float64, len(xd))
copy(r, xd)
copy(e, xd)
Daxpy(N, alpha, xd, inc, r, inc)
for i := 0; i < N; i++ {
e[i*inc] += alpha * xd[i*inc]
}
for i := 0; i < len(xd); i++ {
if r[i] != e[i] {
t.Fatalf("alpha=%f inc=%d N=%d i=%d r=%f e=%f",
alpha, inc, N, i, r[i], e[i])
}
}
}
}
/* This works only with assembler version.
TODO: Write general test for bounds checking
// Test bounds checks.
panicked := func(f func()) (panicked bool) {
panicked = false
defer func() {
if recover() != nil {
panicked = true
}
}()
f()
return panicked
}
d2 := []float64{1.0, 1.0}
d3 := []float64{1.0, 1.0, 1.0}
if !panicked(func() { Daxpy(3, alpha, d2, 1, d2, 1) }) {
t.Fatalf("Expected panic on index out of range.")
}
if !panicked(func() { Daxpy(2, alpha, d2, 2, d2, 1) }) {
t.Fatalf("Expected panic on index out of range.")
}
if !panicked(func() { Daxpy(2, alpha, d2, 1, d2, 2) }) {
t.Fatalf("Expected panic on index out of range.")
}
if !panicked(func() { Daxpy(3, alpha, d3, 1, d2, 1) }) {
t.Fatalf("Expected panic on index out of range.")
}
if !panicked(func() { Daxpy(3, alpha, d2, 1, d3, 2) }) {
t.Fatalf("Expected panic on index out of range.")
}*/
}
}
func TestDscal(t *testing.T) {
alpha := 3.0
for inc := 1; inc < 9; inc++ {
for N := 0; N <= len(xd)/inc; N++ {
r := make([]float64, len(xd))
e := make([]float64, len(xd))
copy(r, xd)
copy(e, xd)
Dscal(N, alpha, r, inc)
for i := 0; i < N; i++ {
e[i*inc] = alpha * xd[i*inc]
}
for i := 0; i < len(xd); i++ {
if r[i] != e[i] {
t.Fatalf("inc=%d N=%d i=%d r=%f e=%f", inc, N, i, r[i],
e[i])
}
}
}
}
}
func dEq(a, b, p float64) bool {
eps := math.SmallestNonzeroFloat64 * 2
r := math.Abs(a) + math.Abs(b)
if r <= eps {
return true
}
return math.Abs(a-b)/r < p
}
// Reference implementation of Drotg
func refDrotg(a, b float64) (c, s, r, z float64) {
roe := b
if math.Abs(a) > math.Abs(b) {
roe = a
}
scale := math.Abs(a) + math.Abs(b)
if scale == 0 {
c = 1
} else {
r = scale * math.Sqrt((a/scale)*(a/scale)+(b/scale)*(b/scale))
if math.Signbit(roe) {
r = -r
}
c = a / r
s = b / r
z = 1
if math.Abs(a) > math.Abs(b) {
z = s
}
if math.Abs(b) >= math.Abs(a) && c != 0 {
z = 1 / c
}
}
return
}
func TestDrotg(t *testing.T) {
vs := []struct{ a, b float64 }{
{0, 0}, {0, 1}, {0, -1},
{1, 0}, {1, 1}, {1, -1},
{-1, 0}, {-1, 1}, {-1, -1},
{2, 0}, {2, 1}, {2, -1},
{-2, 0}, {-2, 1}, {-2, -1},
{0, 2}, {1, 2}, {-1, 2},
{0, -2}, {1, -2}, {-1, 2},
}
for _, v := range vs {
c, s, _, _ := Drotg(v.a, v.b)
ec, es, _, _ := refDrotg(v.a, v.b)
if !dEq(c, ec, 1e-30) || !dEq(s, es, 1e-30) {
t.Fatalf("a=%g b=%g c=%g ec=%g s=%g es=%g", v.a, v.b, c, ec, s, es)
}
}
}
func TestDrot(t *testing.T) {
s2 := math.Sqrt(2)
vs := []struct{ c, s float64 }{
{0, 0}, {0, 1}, {0, -1}, {1, 0}, {-1, 0},
{s2, s2}, {s2, -s2}, {-s2, s2}, {-s2, -s2},
}
x := make([]float64, len(xd))
y := make([]float64, len(yd))
ex := make([]float64, len(xd))
ey := make([]float64, len(yd))
for _, v := range vs {
for inc := 1; inc < 9; inc++ {
for N := 0; N <= len(xd)/inc; N++ {
copy(x, xd)
copy(y, yd)
copy(ex, xd)
copy(ey, yd)
Dscal(N, v.c, ex, inc) // ex *= c
Daxpy(N, v.s, y, inc, ex, inc) // ex += s*y
Dscal(N, v.c, ey, inc) // ey *= c
Daxpy(N, -v.s, x, inc, ey, inc) // ey += (-s)*x
// (x, y) = (c*x + s*y, c*y - s*x)
Drot(N, x, inc, y, inc, v.c, v.s)
for i, _ := range x {
if !dEq(x[i], ex[i], 1e-20) || !dEq(y[i], ey[i], 1e-20) {
t.Fatalf(
"N=%d inc=%d c=%f s=%f i=%d x=%f ex=%f y=%f ey=%f",
N, inc, v.c, v.s, i, x[i], ex[i], y[i], ey[i],
)
}
}
}
}
}
}
var vd, wd []float64
func init() {
vd = make([]float64, 514)
wd = make([]float64, len(vd))
for i := 0; i < len(vd); i++ {
vd[i] = rand.Float64()
wd[i] = rand.Float64()
}
}
func BenchmarkDdot(b *testing.B) {
for i := 0; i < b.N; i++ {
Ddot(len(vd), vd, 1, wd, 1)
}
}
func BenchmarkDnrm2(b *testing.B) {
for i := 0; i < b.N; i++ {
Dnrm2(len(vd), vd, 1)
}
}
func BenchmarkDasum(b *testing.B) {
for i := 0; i < b.N; i++ {
Dasum(len(vd), vd, 1)
}
}
func BenchmarkIdamax(b *testing.B) {
for i := 0; i < b.N; i++ {
Idamax(len(vd), vd, 1)
}
}
func BenchmarkDswap(b *testing.B) {
b.StopTimer()
x := make([]float64, len(vd))
y := make([]float64, len(vd))
b.StartTimer()
for i := 0; i < b.N; i++ {
Dswap(len(x), x, 1, y, 1)
}
}
func BenchmarkDcopy(b *testing.B) {
b.StopTimer()
y := make([]float64, len(vd))
b.StartTimer()
for i := 0; i < b.N; i++ {
Dcopy(len(vd), vd, 1, y, 1)
}
}
func BenchmarkDaxpy(b *testing.B) {
b.StopTimer()
y := make([]float64, len(vd))
b.StartTimer()
for i := 0; i < b.N; i++ {
Daxpy(len(vd), -1.0, vd, 1, y, 1)
}
}
func BenchmarkDscal(b *testing.B) {
b.StopTimer()
y := make([]float64, len(vd))
copy(y, vd)
b.StartTimer()
for i := 0; i < b.N; i++ {
Dscal(len(y), -1.0, y, 1)
}
}
func BenchmarkDrotg(b *testing.B) {
for i := 0; i < b.N; i++ {
Drotg(0, 0)
Drotg(0, 1)
Drotg(0, -1)
Drotg(1, 0)
Drotg(1, 1)
Drotg(1, -1)
Drotg(-1, 0)
Drotg(-1, 1)
Drotg(-1, -1)
}
}
func BenchmarkDrot(b *testing.B) {
b.StopTimer()
x := make([]float64, len(vd))
y := make([]float64, len(vd))
copy(x, vd)
copy(y, vd)
c := math.Sqrt(2)
s := c
b.StartTimer()
for i := 0; i < b.N; i++ {
Drot(len(x), x, 1, y, 1, c, s)
}
}