pool.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. package mat
  5. import (
  6. "sync"
  7. "gonum.org/v1/gonum/blas"
  8. "gonum.org/v1/gonum/blas/blas64"
  9. )
  10. var tab64 = [64]byte{
  11. 0x3f, 0x00, 0x3a, 0x01, 0x3b, 0x2f, 0x35, 0x02,
  12. 0x3c, 0x27, 0x30, 0x1b, 0x36, 0x21, 0x2a, 0x03,
  13. 0x3d, 0x33, 0x25, 0x28, 0x31, 0x12, 0x1c, 0x14,
  14. 0x37, 0x1e, 0x22, 0x0b, 0x2b, 0x0e, 0x16, 0x04,
  15. 0x3e, 0x39, 0x2e, 0x34, 0x26, 0x1a, 0x20, 0x29,
  16. 0x32, 0x24, 0x11, 0x13, 0x1d, 0x0a, 0x0d, 0x15,
  17. 0x38, 0x2d, 0x19, 0x1f, 0x23, 0x10, 0x09, 0x0c,
  18. 0x2c, 0x18, 0x0f, 0x08, 0x17, 0x07, 0x06, 0x05,
  19. }
  20. // bits returns the ceiling of base 2 log of v.
  21. // Approach based on http://stackoverflow.com/a/11398748.
  22. func bits(v uint64) byte {
  23. if v == 0 {
  24. return 0
  25. }
  26. v <<= 2
  27. v--
  28. v |= v >> 1
  29. v |= v >> 2
  30. v |= v >> 4
  31. v |= v >> 8
  32. v |= v >> 16
  33. v |= v >> 32
  34. return tab64[((v-(v>>1))*0x07EDD5E59A4E28C2)>>58] - 1
  35. }
  36. var (
  37. // pool contains size stratified workspace Dense pools.
  38. // Each pool element i returns sized matrices with a data
  39. // slice capped at 1<<i.
  40. pool [63]sync.Pool
  41. // poolSym is the SymDense equivalent of pool.
  42. poolSym [63]sync.Pool
  43. // poolTri is the TriDense equivalent of pool.
  44. poolTri [63]sync.Pool
  45. // poolVec is the VecDense equivalent of pool.
  46. poolVec [63]sync.Pool
  47. // poolFloats is the []float64 equivalent of pool.
  48. poolFloats [63]sync.Pool
  49. // poolInts is the []int equivalent of pool.
  50. poolInts [63]sync.Pool
  51. )
  52. func init() {
  53. for i := range pool {
  54. l := 1 << uint(i)
  55. pool[i].New = func() interface{} {
  56. return &Dense{mat: blas64.General{
  57. Data: make([]float64, l),
  58. }}
  59. }
  60. poolSym[i].New = func() interface{} {
  61. return &SymDense{mat: blas64.Symmetric{
  62. Uplo: blas.Upper,
  63. Data: make([]float64, l),
  64. }}
  65. }
  66. poolTri[i].New = func() interface{} {
  67. return &TriDense{mat: blas64.Triangular{
  68. Data: make([]float64, l),
  69. }}
  70. }
  71. poolVec[i].New = func() interface{} {
  72. return &VecDense{mat: blas64.Vector{
  73. Inc: 1,
  74. Data: make([]float64, l),
  75. }}
  76. }
  77. poolFloats[i].New = func() interface{} {
  78. return make([]float64, l)
  79. }
  80. poolInts[i].New = func() interface{} {
  81. return make([]int, l)
  82. }
  83. }
  84. }
  85. // getWorkspace returns a *Dense of size r×c and a data slice
  86. // with a cap that is less than 2*r*c. If clear is true, the
  87. // data slice visible through the Matrix interface is zeroed.
  88. func getWorkspace(r, c int, clear bool) *Dense {
  89. l := uint64(r * c)
  90. w := pool[bits(l)].Get().(*Dense)
  91. w.mat.Data = w.mat.Data[:l]
  92. if clear {
  93. zero(w.mat.Data)
  94. }
  95. w.mat.Rows = r
  96. w.mat.Cols = c
  97. w.mat.Stride = c
  98. w.capRows = r
  99. w.capCols = c
  100. return w
  101. }
  102. // putWorkspace replaces a used *Dense into the appropriate size
  103. // workspace pool. putWorkspace must not be called with a matrix
  104. // where references to the underlying data slice have been kept.
  105. func putWorkspace(w *Dense) {
  106. pool[bits(uint64(cap(w.mat.Data)))].Put(w)
  107. }
  108. // getWorkspaceSym returns a *SymDense of size n and a cap that
  109. // is less than 2*n. If clear is true, the data slice visible
  110. // through the Matrix interface is zeroed.
  111. func getWorkspaceSym(n int, clear bool) *SymDense {
  112. l := uint64(n)
  113. l *= l
  114. s := poolSym[bits(l)].Get().(*SymDense)
  115. s.mat.Data = s.mat.Data[:l]
  116. if clear {
  117. zero(s.mat.Data)
  118. }
  119. s.mat.N = n
  120. s.mat.Stride = n
  121. s.cap = n
  122. return s
  123. }
  124. // putWorkspaceSym replaces a used *SymDense into the appropriate size
  125. // workspace pool. putWorkspaceSym must not be called with a matrix
  126. // where references to the underlying data slice have been kept.
  127. func putWorkspaceSym(s *SymDense) {
  128. poolSym[bits(uint64(cap(s.mat.Data)))].Put(s)
  129. }
  130. // getWorkspaceTri returns a *TriDense of size n and a cap that
  131. // is less than 2*n. If clear is true, the data slice visible
  132. // through the Matrix interface is zeroed.
  133. func getWorkspaceTri(n int, kind TriKind, clear bool) *TriDense {
  134. l := uint64(n)
  135. l *= l
  136. t := poolTri[bits(l)].Get().(*TriDense)
  137. t.mat.Data = t.mat.Data[:l]
  138. if clear {
  139. zero(t.mat.Data)
  140. }
  141. t.mat.N = n
  142. t.mat.Stride = n
  143. if kind == Upper {
  144. t.mat.Uplo = blas.Upper
  145. } else if kind == Lower {
  146. t.mat.Uplo = blas.Lower
  147. } else {
  148. panic(ErrTriangle)
  149. }
  150. t.mat.Diag = blas.NonUnit
  151. t.cap = n
  152. return t
  153. }
  154. // putWorkspaceTri replaces a used *TriDense into the appropriate size
  155. // workspace pool. putWorkspaceTri must not be called with a matrix
  156. // where references to the underlying data slice have been kept.
  157. func putWorkspaceTri(t *TriDense) {
  158. poolTri[bits(uint64(cap(t.mat.Data)))].Put(t)
  159. }
  160. // getWorkspaceVec returns a *VecDense of length n and a cap that
  161. // is less than 2*n. If clear is true, the data slice visible
  162. // through the Matrix interface is zeroed.
  163. func getWorkspaceVec(n int, clear bool) *VecDense {
  164. l := uint64(n)
  165. v := poolVec[bits(l)].Get().(*VecDense)
  166. v.mat.Data = v.mat.Data[:l]
  167. if clear {
  168. zero(v.mat.Data)
  169. }
  170. v.mat.N = n
  171. return v
  172. }
  173. // putWorkspaceVec replaces a used *VecDense into the appropriate size
  174. // workspace pool. putWorkspaceVec must not be called with a matrix
  175. // where references to the underlying data slice have been kept.
  176. func putWorkspaceVec(v *VecDense) {
  177. poolVec[bits(uint64(cap(v.mat.Data)))].Put(v)
  178. }
  179. // getFloats returns a []float64 of length l and a cap that is
  180. // less than 2*l. If clear is true, the slice visible is zeroed.
  181. func getFloats(l int, clear bool) []float64 {
  182. w := poolFloats[bits(uint64(l))].Get().([]float64)
  183. w = w[:l]
  184. if clear {
  185. zero(w)
  186. }
  187. return w
  188. }
  189. // putFloats replaces a used []float64 into the appropriate size
  190. // workspace pool. putFloats must not be called with a slice
  191. // where references to the underlying data have been kept.
  192. func putFloats(w []float64) {
  193. poolFloats[bits(uint64(cap(w)))].Put(w)
  194. }
  195. // getInts returns a []ints of length l and a cap that is
  196. // less than 2*l. If clear is true, the slice visible is zeroed.
  197. func getInts(l int, clear bool) []int {
  198. w := poolInts[bits(uint64(l))].Get().([]int)
  199. w = w[:l]
  200. if clear {
  201. for i := range w {
  202. w[i] = 0
  203. }
  204. }
  205. return w
  206. }
  207. // putInts replaces a used []int into the appropriate size
  208. // workspace pool. putInts must not be called with a slice
  209. // where references to the underlying data have been kept.
  210. func putInts(w []int) {
  211. poolInts[bits(uint64(cap(w)))].Put(w)
  212. }