pool.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. s := make([]float64, l)
  79. return &s
  80. }
  81. poolInts[i].New = func() interface{} {
  82. s := make([]int, l)
  83. return &s
  84. }
  85. }
  86. }
  87. // getWorkspace returns a *Dense of size r×c and a data slice
  88. // with a cap that is less than 2*r*c. If clear is true, the
  89. // data slice visible through the Matrix interface is zeroed.
  90. func getWorkspace(r, c int, clear bool) *Dense {
  91. l := uint64(r * c)
  92. w := pool[bits(l)].Get().(*Dense)
  93. w.mat.Data = w.mat.Data[:l]
  94. if clear {
  95. zero(w.mat.Data)
  96. }
  97. w.mat.Rows = r
  98. w.mat.Cols = c
  99. w.mat.Stride = c
  100. w.capRows = r
  101. w.capCols = c
  102. return w
  103. }
  104. // putWorkspace replaces a used *Dense into the appropriate size
  105. // workspace pool. putWorkspace must not be called with a matrix
  106. // where references to the underlying data slice have been kept.
  107. func putWorkspace(w *Dense) {
  108. pool[bits(uint64(cap(w.mat.Data)))].Put(w)
  109. }
  110. // getWorkspaceSym returns a *SymDense of size n and a cap that
  111. // is less than 2*n. If clear is true, the data slice visible
  112. // through the Matrix interface is zeroed.
  113. func getWorkspaceSym(n int, clear bool) *SymDense {
  114. l := uint64(n)
  115. l *= l
  116. s := poolSym[bits(l)].Get().(*SymDense)
  117. s.mat.Data = s.mat.Data[:l]
  118. if clear {
  119. zero(s.mat.Data)
  120. }
  121. s.mat.N = n
  122. s.mat.Stride = n
  123. s.cap = n
  124. return s
  125. }
  126. // putWorkspaceSym replaces a used *SymDense into the appropriate size
  127. // workspace pool. putWorkspaceSym must not be called with a matrix
  128. // where references to the underlying data slice have been kept.
  129. func putWorkspaceSym(s *SymDense) {
  130. poolSym[bits(uint64(cap(s.mat.Data)))].Put(s)
  131. }
  132. // getWorkspaceTri returns a *TriDense of size n and a cap that
  133. // is less than 2*n. If clear is true, the data slice visible
  134. // through the Matrix interface is zeroed.
  135. func getWorkspaceTri(n int, kind TriKind, clear bool) *TriDense {
  136. l := uint64(n)
  137. l *= l
  138. t := poolTri[bits(l)].Get().(*TriDense)
  139. t.mat.Data = t.mat.Data[:l]
  140. if clear {
  141. zero(t.mat.Data)
  142. }
  143. t.mat.N = n
  144. t.mat.Stride = n
  145. if kind == Upper {
  146. t.mat.Uplo = blas.Upper
  147. } else if kind == Lower {
  148. t.mat.Uplo = blas.Lower
  149. } else {
  150. panic(ErrTriangle)
  151. }
  152. t.mat.Diag = blas.NonUnit
  153. t.cap = n
  154. return t
  155. }
  156. // putWorkspaceTri replaces a used *TriDense into the appropriate size
  157. // workspace pool. putWorkspaceTri must not be called with a matrix
  158. // where references to the underlying data slice have been kept.
  159. func putWorkspaceTri(t *TriDense) {
  160. poolTri[bits(uint64(cap(t.mat.Data)))].Put(t)
  161. }
  162. // getWorkspaceVec returns a *VecDense of length n and a cap that
  163. // is less than 2*n. If clear is true, the data slice visible
  164. // through the Matrix interface is zeroed.
  165. func getWorkspaceVec(n int, clear bool) *VecDense {
  166. l := uint64(n)
  167. v := poolVec[bits(l)].Get().(*VecDense)
  168. v.mat.Data = v.mat.Data[:l]
  169. if clear {
  170. zero(v.mat.Data)
  171. }
  172. v.mat.N = n
  173. return v
  174. }
  175. // putWorkspaceVec replaces a used *VecDense into the appropriate size
  176. // workspace pool. putWorkspaceVec must not be called with a matrix
  177. // where references to the underlying data slice have been kept.
  178. func putWorkspaceVec(v *VecDense) {
  179. poolVec[bits(uint64(cap(v.mat.Data)))].Put(v)
  180. }
  181. // getFloats returns a []float64 of length l and a cap that is
  182. // less than 2*l. If clear is true, the slice visible is zeroed.
  183. func getFloats(l int, clear bool) []float64 {
  184. w := *poolFloats[bits(uint64(l))].Get().(*[]float64)
  185. w = w[:l]
  186. if clear {
  187. zero(w)
  188. }
  189. return w
  190. }
  191. // putFloats replaces a used []float64 into the appropriate size
  192. // workspace pool. putFloats must not be called with a slice
  193. // where references to the underlying data have been kept.
  194. func putFloats(w []float64) {
  195. poolFloats[bits(uint64(cap(w)))].Put(&w)
  196. }
  197. // getInts returns a []ints of length l and a cap that is
  198. // less than 2*l. If clear is true, the slice visible is zeroed.
  199. func getInts(l int, clear bool) []int {
  200. w := *poolInts[bits(uint64(l))].Get().(*[]int)
  201. w = w[:l]
  202. if clear {
  203. for i := range w {
  204. w[i] = 0
  205. }
  206. }
  207. return w
  208. }
  209. // putInts replaces a used []int into the appropriate size
  210. // workspace pool. putInts must not be called with a slice
  211. // where references to the underlying data have been kept.
  212. func putInts(w []int) {
  213. poolInts[bits(uint64(cap(w)))].Put(&w)
  214. }