-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix64.go
173 lines (140 loc) · 3.82 KB
/
matrix64.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package goumem
import (
"fmt"
"github.com/exapsy/goumem/allocator"
"unsafe"
)
var (
ErrMatrixZeroSize = fmt.Errorf("goumem: matrix size cannot be zero")
)
type PointerMatrixFloat64 struct {
allocatedBlock *allocator.AllocatedBlock
rows int
cols int
}
func NewMatrixFloat64(rows, cols int) (*PointerMatrixFloat64, error) {
block, err := mem.Alloc(uintptr((rows * cols) << 3))
if err != nil {
return nil, err
}
if rows == 0 || cols == 0 {
return nil, ErrMatrixZeroSize
}
return &PointerMatrixFloat64{
allocatedBlock: block,
rows: rows,
cols: cols,
}, nil
}
func (ptr *PointerMatrixFloat64) SetRowColValue(row, col int, value float64) {
*(*float64)(unsafe.Pointer(ptr.allocatedBlock.Addr() + uintptr(row*ptr.cols+col)<<3)) = value
}
func (ptr *PointerMatrixFloat64) GetRowColValue(row, col int) float64 {
return *(*float64)(unsafe.Pointer(ptr.allocatedBlock.Addr() + uintptr(row*ptr.cols+col)<<3))
}
func (ptr *PointerMatrixFloat64) Address() uintptr {
return ptr.allocatedBlock.Addr()
}
func (ptr *PointerMatrixFloat64) Rows() int {
return ptr.rows
}
func (ptr *PointerMatrixFloat64) Cols() int {
return ptr.cols
}
func (ptr *PointerMatrixFloat64) Value() [][]float64 {
matrix := make([][]float64, ptr.rows)
flat := (*[1 << 30]float64)(unsafe.Pointer(ptr.allocatedBlock.Addr()))
for i := range matrix {
matrix[i] = flat[i*ptr.cols : (i+1)*ptr.cols]
}
return matrix
}
func (ptr *PointerMatrixFloat64) Set(matrix [][]float64) {
if len(matrix) != ptr.rows {
panic("goumem: matrix rows mismatch")
}
if len(matrix[0]) != ptr.cols {
panic("goumem: matrix cols mismatch")
}
for i := 0; i < ptr.rows; i++ {
for j := 0; j < ptr.cols; j++ {
*(*float64)(unsafe.Pointer(ptr.allocatedBlock.Addr() + uintptr(i*ptr.cols+j)<<3)) = matrix[i][j]
}
}
}
func (ptr *PointerMatrixFloat64) Free() error {
return mem.Free(ptr.allocatedBlock)
}
func (ptr *PointerMatrixFloat64) String() string {
s := "["
for i := 0; i < ptr.rows; i++ {
for j := 0; j < ptr.cols; j++ {
s += fmt.Sprintf("%f ", *(*float64)(unsafe.Pointer(ptr.allocatedBlock.Addr() + uintptr(i*ptr.cols+j)<<3)))
}
s += "\n"
}
s += "]"
return s
}
type PoolMatrixFloat64 struct {
totalMatrices int
rows int
cols int
addr uintptr
matrices []*PointerMatrixFloat64 // Preallocated matrix instances
freeList stack
}
type stack struct {
matrices []*PointerMatrixFloat64
len int
}
func (s *stack) push(matrix *PointerMatrixFloat64) {
if s.len >= len(s.matrices) {
s.matrices = append(s.matrices, matrix)
}
s.matrices[s.len] = matrix
s.len++
}
func (s *stack) pop() *PointerMatrixFloat64 {
if s.len == 0 {
panic("goumem: stack is empty")
}
s.len--
return s.matrices[s.len]
}
func NewPoolMatrix64(totalMatrices int, rows, cols int) (*PoolMatrixFloat64, error) {
if rows == 0 || cols == 0 {
return nil, ErrMatrixZeroSize
}
// Calculate the size of a matrix and the total size needed for all matrices
matrixSize := uintptr(rows*cols) << 3
totalSize := matrixSize * uintptr(totalMatrices)
block, err := mem.Alloc(totalSize)
if err != nil {
return nil, err
}
matrices := make([]*PointerMatrixFloat64, totalMatrices)
freeList := stack{matrices: make([]*PointerMatrixFloat64, totalMatrices, totalMatrices)}
for i := range matrices {
matrices[i] = &PointerMatrixFloat64{
allocatedBlock: block,
rows: rows,
cols: cols,
}
freeList.push(matrices[i])
}
return &PoolMatrixFloat64{
totalMatrices: totalMatrices,
rows: rows,
cols: cols,
addr: block.Addr(),
matrices: matrices,
freeList: freeList,
}, nil
}
func (pool *PoolMatrixFloat64) Get() *PointerMatrixFloat64 {
return pool.freeList.pop()
}
func (pool *PoolMatrixFloat64) Free(matrix *PointerMatrixFloat64) {
pool.freeList.push(matrix)
}