File: benchAxpy_test.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (165 lines) | stat: -rw-r--r-- 3,894 bytes parent folder | download
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
// Copyright ©2017 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package f64_test

import (
	"fmt"
	"testing"

	. "gonum.org/v1/gonum/internal/asm/f64"
)

const testLen = 50e5

var (
	a = 2.0
	x = make([]float64, testLen)
	y = make([]float64, testLen)
	z = make([]float64, testLen)
)

func init() {
	for n := range x {
		x[n] = float64(n)
		y[n] = float64(n)
	}
}

func BenchmarkAxpyUnitary(t *testing.B) {
	naiveaxpyu := func(a float64, x, y []float64) {
		for i, v := range x {
			y[i] += a * v
		}
	}
	tests := []struct {
		name string
		f    func(a float64, x, y []float64)
	}{
		{"AxpyUnitary", AxpyUnitary},
		{"NaiveAxpyUnitary", naiveaxpyu},
	}
	for _, test := range tests {
		for _, ln := range []uintptr{1, 3, 10, 30, 1e2, 3e2, 1e3, 3e3, 1e4, 3e4, 1e5} {
			t.Run(fmt.Sprintf("%s-%d", test.name, ln), func(b *testing.B) {
				b.SetBytes(int64(64 * ln))
				x, y := x[:ln], y[:ln]
				b.ResetTimer()
				for i := 0; i < b.N; i++ {
					test.f(a, x, y)
				}
			})
		}
	}
}

func BenchmarkAxpyUnitaryTo(t *testing.B) {
	naiveaxpyut := func(d []float64, a float64, x, y []float64) {
		for i, v := range x {
			d[i] = y[i] + a*v
		}
	}
	tests := []struct {
		name string
		f    func(z []float64, a float64, x, y []float64)
	}{
		{"AxpyUnitaryTo", AxpyUnitaryTo},
		{"NaiveAxpyUnitaryTo", naiveaxpyut},
	}
	for _, test := range tests {
		for _, ln := range []uintptr{1, 3, 10, 30, 1e2, 3e2, 1e3, 3e3, 1e4, 3e4, 1e5} {
			t.Run(fmt.Sprintf("%s-%d", test.name, ln), func(b *testing.B) {
				b.SetBytes(int64(64 * ln))
				x, y, z := x[:ln], y[:ln], z[:ln]
				b.ResetTimer()
				for i := 0; i < b.N; i++ {
					test.f(z, a, x, y)
				}
			})
		}
	}
}

var incsAxpy = []struct {
	len uintptr
	inc []int
}{
	{1, []int{1}},
	{2, []int{1, 2, 4, 10}},
	{3, []int{1, 2, 4, 10}},
	{4, []int{1, 2, 4, 10}},
	{5, []int{1, 2, 4, 10}},
	{10, []int{1, 2, 4, 10}},
	{500, []int{1, 2, 4, 10}},
	{1e3, []int{1, 2, 4, 10}},
	{1e4, []int{1, 2, 4, 10, -1, -2, -4, -10}},
}

func BenchmarkAxpyInc(t *testing.B) {
	naiveaxpyinc := func(alpha float64, x, y []float64, n, incX, incY, ix, iy uintptr) {
		for i := 0; i < int(n); i++ {
			y[iy] += alpha * x[ix]
			ix += incX
			iy += incY
		}
	}
	tests := []struct {
		name string
		f    func(alpha float64, x, y []float64, n, incX, incY, ix, iy uintptr)
	}{
		{"AxpyInc", AxpyInc},
		{"NaiveAxpyInc", naiveaxpyinc},
	}
	for _, test := range tests {
		for _, tt := range incsAxpy {
			for _, inc := range tt.inc {
				t.Run(fmt.Sprintf("%s-%d-inc(%d)", test.name, tt.len, inc), func(b *testing.B) {
					b.SetBytes(int64(64 * tt.len))
					var idx, tstInc uintptr = 0, uintptr(inc)
					if inc < 0 {
						idx = uintptr((-int(tt.len) + 1) * inc)
					}
					for i := 0; i < b.N; i++ {
						test.f(a, x, y, uintptr(tt.len), tstInc, tstInc, idx, idx)
					}
				})
			}
		}
	}
}

func BenchmarkAxpyIncTo(t *testing.B) {
	naiveaxpyincto := func(dst []float64, incDst, idst uintptr, alpha float64, x, y []float64, n, incX, incY, ix, iy uintptr) {
		for i := 0; i < int(n); i++ {
			dst[idst] = alpha*x[ix] + y[iy]
			ix += incX
			iy += incY
			idst += incDst
		}
	}
	tests := []struct {
		name string
		f    func(dst []float64, incDst, idst uintptr, alpha float64, x, y []float64, n, incX, incY, ix, iy uintptr)
	}{
		{"AxpyIncTo", AxpyIncTo},
		{"NaiveAxpyIncTo", naiveaxpyincto},
	}
	for _, test := range tests {
		for _, tt := range incsAxpy {
			for _, inc := range tt.inc {
				t.Run(fmt.Sprintf("%s-%d-inc(%d)", test.name, tt.len, inc), func(b *testing.B) {
					b.SetBytes(int64(64 * tt.len))
					var idx, tstInc uintptr = 0, uintptr(inc)
					if inc < 0 {
						idx = uintptr((-int(tt.len) + 1) * inc)
					}
					for i := 0; i < b.N; i++ {
						test.f(z, tstInc, idx, a, x, y, uintptr(tt.len),
							tstInc, tstInc, idx, idx)
					}
				})
			}
		}
	}
}