-
Notifications
You must be signed in to change notification settings - Fork 23
/
pool.go
104 lines (92 loc) · 2.05 KB
/
pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package sparse
import (
"sync"
"github.com/james-bowman/sparse/blas"
)
const (
pooledFloatSize = 200
pooledIntSize = 200
)
var (
pool = sync.Pool{
New: func() interface{} {
return &CSR{
matrix: blas.SparseMatrix{},
}
},
}
vecPool = sync.Pool{
New: func() interface{} {
return &Vector{}
},
}
floatPool = sync.Pool{
New: func() interface{} {
return make([]float64, pooledFloatSize)
},
}
intPool = sync.Pool{
New: func() interface{} {
return make([]int, pooledIntSize)
},
}
)
func getWorkspace(r, c, nnz int, clear bool) *CSR {
w := pool.Get().(*CSR)
w.matrix.Indptr = useInts(w.matrix.Indptr, r+1, false)
w.matrix.Ind = useInts(w.matrix.Ind, nnz, false)
w.matrix.Data = useFloats(w.matrix.Data, nnz, false)
if clear {
for i := range w.matrix.Indptr {
w.matrix.Indptr[i] = 0
}
w.matrix.Ind = w.matrix.Ind[:0]
w.matrix.Data = w.matrix.Data[:0]
}
w.matrix.I = r
w.matrix.J = c
return w
}
func putWorkspace(w *CSR) {
pool.Put(w)
}
func getVecWorkspace(len, nnz int, clear bool) *Vector {
w := vecPool.Get().(*Vector)
w.ind = useInts(w.ind, nnz, false)
w.data = useFloats(w.data, nnz, false)
if clear {
w.ind = w.ind[:0]
w.data = w.data[:0]
}
w.len = len
return w
}
func putVecWorkspace(w *Vector) {
vecPool.Put(w)
}
// getFloats returns a []float64 of length l. If clear is true,
// the slice visible is zeroed.
func getFloats(l int, clear bool) []float64 {
w := floatPool.Get().([]float64)
return useFloats(w, l, clear)
}
// putFloats replaces a used []float64 into the appropriate size
// workspace pool. putFloats must not be called with a slice
// where references to the underlying data have been kept.
func putFloats(w []float64) {
if cap(w) > pooledFloatSize {
floatPool.Put(w)
}
}
// getInts returns a []ints of length l. If clear is true,
// the slice visible is zeroed.
func getInts(l int, clear bool) []int {
w := intPool.Get().([]int)
return useInts(w, l, clear)
}
// putInts replaces a used []int into the pool.
func putInts(w []int) {
if cap(w) > pooledIntSize {
intPool.Put(w)
}
}