1 // Copyright ©2014 The Gonum Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 "golang.org/x/exp/rand"
15 func TestPool(t *testing.T) {
16 for i := 1; i < 10; i++ {
17 for j := 1; j < 10; j++ {
18 m := NewDense(i, j, nil)
19 for k := 0; k < 5; k++ {
20 work := make([]*Dense, rand.Intn(10)+1)
22 w := getWorkspace(i, j, true)
23 if !reflect.DeepEqual(w.mat, m.mat) {
24 t.Error("unexpected non-zeroed matrix returned by getWorkspace")
26 if w.capRows != m.capRows {
27 t.Error("unexpected capacity matrix returned by getWorkspace")
29 if w.capCols != m.capCols {
30 t.Error("unexpected capacity matrix returned by getWorkspace")
32 if cap(w.mat.Data) >= 2*len(w.mat.Data) {
33 t.Errorf("r: %d c: %d -> len: %d cap: %d", i, j, len(w.mat.Data), cap(w.mat.Data))
35 w.Set(0, 0, math.NaN())
38 for _, w := range work {
48 func poolBenchmark(n, r, c int, clear bool) {
49 for i := 0; i < n; i++ {
50 benchmat = getWorkspace(r, c, clear)
51 putWorkspace(benchmat)
55 func newBenchmark(n, r, c int) {
56 for i := 0; i < n; i++ {
57 benchmat = NewDense(r, c, nil)
61 func BenchmarkPool10by10Uncleared(b *testing.B) { poolBenchmark(b.N, 10, 10, false) }
62 func BenchmarkPool10by10Cleared(b *testing.B) { poolBenchmark(b.N, 10, 10, true) }
63 func BenchmarkNew10by10(b *testing.B) { newBenchmark(b.N, 10, 10) }
64 func BenchmarkPool100by100Uncleared(b *testing.B) { poolBenchmark(b.N, 100, 100, false) }
65 func BenchmarkPool100by100Cleared(b *testing.B) { poolBenchmark(b.N, 100, 100, true) }
66 func BenchmarkNew100by100(b *testing.B) { newBenchmark(b.N, 100, 100) }
68 func BenchmarkMulWorkspaceDense100Half(b *testing.B) { denseMulWorkspaceBench(b, 100, 0.5) }
69 func BenchmarkMulWorkspaceDense100Tenth(b *testing.B) { denseMulWorkspaceBench(b, 100, 0.1) }
70 func BenchmarkMulWorkspaceDense1000Half(b *testing.B) { denseMulWorkspaceBench(b, 1000, 0.5) }
71 func BenchmarkMulWorkspaceDense1000Tenth(b *testing.B) { denseMulWorkspaceBench(b, 1000, 0.1) }
72 func BenchmarkMulWorkspaceDense1000Hundredth(b *testing.B) { denseMulWorkspaceBench(b, 1000, 0.01) }
73 func BenchmarkMulWorkspaceDense1000Thousandth(b *testing.B) { denseMulWorkspaceBench(b, 1000, 0.001) }
74 func denseMulWorkspaceBench(b *testing.B, size int, rho float64) {
76 a, _ := randDense(size, rho, rand.NormFloat64)
77 d, _ := randDense(size, rho, rand.NormFloat64)
79 for i := 0; i < b.N; i++ {