OSDN Git Service

new repo
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / mat / pool_test.go
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         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)
21                                 for l := range work {
22                                         w := getWorkspace(i, j, true)
23                                         if !reflect.DeepEqual(w.mat, m.mat) {
24                                                 t.Error("unexpected non-zeroed matrix returned by getWorkspace")
25                                         }
26                                         if w.capRows != m.capRows {
27                                                 t.Error("unexpected capacity matrix returned by getWorkspace")
28                                         }
29                                         if w.capCols != m.capCols {
30                                                 t.Error("unexpected capacity matrix returned by getWorkspace")
31                                         }
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))
34                                         }
35                                         w.Set(0, 0, math.NaN())
36                                         work[l] = w
37                                 }
38                                 for _, w := range work {
39                                         putWorkspace(w)
40                                 }
41                         }
42                 }
43         }
44 }
45
46 var benchmat *Dense
47
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)
52         }
53 }
54
55 func newBenchmark(n, r, c int) {
56         for i := 0; i < n; i++ {
57                 benchmat = NewDense(r, c, nil)
58         }
59 }
60
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) }
67
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) {
75         b.StopTimer()
76         a, _ := randDense(size, rho, rand.NormFloat64)
77         d, _ := randDense(size, rho, rand.NormFloat64)
78         b.StartTimer()
79         for i := 0; i < b.N; i++ {
80                 a.Mul(a, d)
81         }
82 }