gonum.org/v1/gonum@v0.14.0/mat/pool_test.go (about) 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. 4 5 package mat 6 7 import ( 8 "math" 9 "reflect" 10 "testing" 11 12 "golang.org/x/exp/rand" 13 ) 14 15 func TestPool(t *testing.T) { 16 t.Parallel() 17 for i := 1; i < 10; i++ { 18 for j := 1; j < 10; j++ { 19 m := NewDense(i, j, nil) 20 for k := 0; k < 5; k++ { 21 work := make([]*Dense, rand.Intn(10)+1) 22 for l := range work { 23 w := getDenseWorkspace(i, j, true) 24 if !reflect.DeepEqual(w.mat, m.mat) { 25 t.Error("unexpected non-zeroed matrix returned by getWorkspace") 26 } 27 if w.capRows != m.capRows { 28 t.Error("unexpected capacity matrix returned by getWorkspace") 29 } 30 if w.capCols != m.capCols { 31 t.Error("unexpected capacity matrix returned by getWorkspace") 32 } 33 if cap(w.mat.Data) >= 2*len(w.mat.Data) { 34 t.Errorf("r: %d c: %d -> len: %d cap: %d", i, j, len(w.mat.Data), cap(w.mat.Data)) 35 } 36 w.Set(0, 0, math.NaN()) 37 work[l] = w 38 } 39 for _, w := range work { 40 putDenseWorkspace(w) 41 } 42 } 43 } 44 } 45 } 46 47 var benchmat *Dense 48 49 func poolBenchmark(n, r, c int, clear bool) { 50 for i := 0; i < n; i++ { 51 benchmat = getDenseWorkspace(r, c, clear) 52 putDenseWorkspace(benchmat) 53 } 54 } 55 56 func newBenchmark(n, r, c int) { 57 for i := 0; i < n; i++ { 58 benchmat = NewDense(r, c, nil) 59 } 60 } 61 62 func BenchmarkPool10by10Uncleared(b *testing.B) { poolBenchmark(b.N, 10, 10, false) } 63 func BenchmarkPool10by10Cleared(b *testing.B) { poolBenchmark(b.N, 10, 10, true) } 64 func BenchmarkNew10by10(b *testing.B) { newBenchmark(b.N, 10, 10) } 65 func BenchmarkPool100by100Uncleared(b *testing.B) { poolBenchmark(b.N, 100, 100, false) } 66 func BenchmarkPool100by100Cleared(b *testing.B) { poolBenchmark(b.N, 100, 100, true) } 67 func BenchmarkNew100by100(b *testing.B) { newBenchmark(b.N, 100, 100) } 68 69 func BenchmarkMulWorkspaceDense100Half(b *testing.B) { denseMulWorkspaceBench(b, 100, 0.5) } 70 func BenchmarkMulWorkspaceDense100Tenth(b *testing.B) { denseMulWorkspaceBench(b, 100, 0.1) } 71 func BenchmarkMulWorkspaceDense1000Half(b *testing.B) { denseMulWorkspaceBench(b, 1000, 0.5) } 72 func BenchmarkMulWorkspaceDense1000Tenth(b *testing.B) { denseMulWorkspaceBench(b, 1000, 0.1) } 73 func BenchmarkMulWorkspaceDense1000Hundredth(b *testing.B) { denseMulWorkspaceBench(b, 1000, 0.01) } 74 func BenchmarkMulWorkspaceDense1000Thousandth(b *testing.B) { denseMulWorkspaceBench(b, 1000, 0.001) } 75 func denseMulWorkspaceBench(b *testing.B, size int, rho float64) { 76 src := rand.NewSource(1) 77 b.StopTimer() 78 a, _ := randDense(size, rho, src) 79 d, _ := randDense(size, rho, src) 80 b.StartTimer() 81 for i := 0; i < b.N; i++ { 82 a.Mul(a, d) 83 } 84 }