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.
10 "gonum.org/v1/gonum/blas"
11 "gonum.org/v1/gonum/blas/blas64"
15 0x3f, 0x00, 0x3a, 0x01, 0x3b, 0x2f, 0x35, 0x02,
16 0x3c, 0x27, 0x30, 0x1b, 0x36, 0x21, 0x2a, 0x03,
17 0x3d, 0x33, 0x25, 0x28, 0x31, 0x12, 0x1c, 0x14,
18 0x37, 0x1e, 0x22, 0x0b, 0x2b, 0x0e, 0x16, 0x04,
19 0x3e, 0x39, 0x2e, 0x34, 0x26, 0x1a, 0x20, 0x29,
20 0x32, 0x24, 0x11, 0x13, 0x1d, 0x0a, 0x0d, 0x15,
21 0x38, 0x2d, 0x19, 0x1f, 0x23, 0x10, 0x09, 0x0c,
22 0x2c, 0x18, 0x0f, 0x08, 0x17, 0x07, 0x06, 0x05,
25 // bits returns the ceiling of base 2 log of v.
26 // Approach based on http://stackoverflow.com/a/11398748.
27 func bits(v uint64) byte {
39 return tab64[((v-(v>>1))*0x07EDD5E59A4E28C2)>>58] - 1
43 // pool contains size stratified workspace Dense pools.
44 // Each pool element i returns sized matrices with a data
45 // slice capped at 1<<i.
48 // poolSym is the SymDense equivalent of pool.
51 // poolTri is the TriDense equivalent of pool.
54 // poolVec is the VecDense equivalent of pool.
57 // poolFloats is the []float64 equivalent of pool.
58 poolFloats [63]sync.Pool
60 // poolInts is the []int equivalent of pool.
61 poolInts [63]sync.Pool
67 pool[i].New = func() interface{} {
68 return &Dense{mat: blas64.General{
69 Data: make([]float64, l),
72 poolSym[i].New = func() interface{} {
73 return &SymDense{mat: blas64.Symmetric{
75 Data: make([]float64, l),
78 poolTri[i].New = func() interface{} {
79 return &TriDense{mat: blas64.Triangular{
80 Data: make([]float64, l),
83 poolVec[i].New = func() interface{} {
84 return &VecDense{mat: blas64.Vector{
86 Data: make([]float64, l),
89 poolFloats[i].New = func() interface{} {
90 return make([]float64, l)
92 poolInts[i].New = func() interface{} {
98 // getWorkspace returns a *Dense of size r×c and a data slice
99 // with a cap that is less than 2*r*c. If clear is true, the
100 // data slice visible through the Matrix interface is zeroed.
101 func getWorkspace(r, c int, clear bool) *Dense {
103 w := pool[bits(l)].Get().(*Dense)
104 w.mat.Data = w.mat.Data[:l]
116 // putWorkspace replaces a used *Dense into the appropriate size
117 // workspace pool. putWorkspace must not be called with a matrix
118 // where references to the underlying data slice have been kept.
119 func putWorkspace(w *Dense) {
120 pool[bits(uint64(cap(w.mat.Data)))].Put(w)
123 // getWorkspaceSym returns a *SymDense of size n and a cap that
124 // is less than 2*n. If clear is true, the data slice visible
125 // through the Matrix interface is zeroed.
126 func getWorkspaceSym(n int, clear bool) *SymDense {
129 s := poolSym[bits(l)].Get().(*SymDense)
130 s.mat.Data = s.mat.Data[:l]
140 // putWorkspaceSym replaces a used *SymDense into the appropriate size
141 // workspace pool. putWorkspaceSym must not be called with a matrix
142 // where references to the underlying data slice have been kept.
143 func putWorkspaceSym(s *SymDense) {
144 poolSym[bits(uint64(cap(s.mat.Data)))].Put(s)
147 // getWorkspaceTri returns a *TriDense of size n and a cap that
148 // is less than 2*n. If clear is true, the data slice visible
149 // through the Matrix interface is zeroed.
150 func getWorkspaceTri(n int, kind TriKind, clear bool) *TriDense {
153 t := poolTri[bits(l)].Get().(*TriDense)
154 t.mat.Data = t.mat.Data[:l]
161 t.mat.Uplo = blas.Upper
162 } else if kind == Lower {
163 t.mat.Uplo = blas.Lower
167 t.mat.Diag = blas.NonUnit
172 // putWorkspaceTri replaces a used *TriDense into the appropriate size
173 // workspace pool. putWorkspaceTri must not be called with a matrix
174 // where references to the underlying data slice have been kept.
175 func putWorkspaceTri(t *TriDense) {
176 poolTri[bits(uint64(cap(t.mat.Data)))].Put(t)
179 // getWorkspaceVec returns a *VecDense of length n and a cap that
180 // is less than 2*n. If clear is true, the data slice visible
181 // through the Matrix interface is zeroed.
182 func getWorkspaceVec(n int, clear bool) *VecDense {
184 v := poolVec[bits(l)].Get().(*VecDense)
185 v.mat.Data = v.mat.Data[:l]
193 // putWorkspaceVec replaces a used *VecDense into the appropriate size
194 // workspace pool. putWorkspaceVec must not be called with a matrix
195 // where references to the underlying data slice have been kept.
196 func putWorkspaceVec(v *VecDense) {
197 poolVec[bits(uint64(cap(v.mat.Data)))].Put(v)
200 // getFloats returns a []float64 of length l and a cap that is
201 // less than 2*l. If clear is true, the slice visible is zeroed.
202 func getFloats(l int, clear bool) []float64 {
203 w := poolFloats[bits(uint64(l))].Get().([]float64)
211 // putFloats replaces a used []float64 into the appropriate size
212 // workspace pool. putFloats must not be called with a slice
213 // where references to the underlying data have been kept.
214 func putFloats(w []float64) {
215 poolFloats[bits(uint64(cap(w)))].Put(w)
218 // getInts returns a []ints of length l and a cap that is
219 // less than 2*l. If clear is true, the slice visible is zeroed.
220 func getInts(l int, clear bool) []int {
221 w := poolInts[bits(uint64(l))].Get().([]int)
231 // putInts replaces a used []int into the appropriate size
232 // workspace pool. putInts must not be called with a slice
233 // where references to the underlying data have been kept.
234 func putInts(w []int) {
235 poolInts[bits(uint64(cap(w)))].Put(w)