forked from ewalker544/libsvm-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
126 lines (101 loc) · 3.86 KB
/
Copy pathcache.go
File metadata and controls
126 lines (101 loc) · 3.86 KB
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
/*
** Copyright 2014 Edward Walker
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http ://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
** Description: Caches Q matrix rows. The cache implements a LRU (Last Recently Used) eviction policy.
** @author: Ed Walker
*/
package libSvm
import (
"container/list"
"fmt"
)
// To allow the LRU cache to store values of a different type, modify BOTH cacheDataType and sizeOfCacheDataType
type cacheDataType float32
const sizeOfCacheDataType = 4
type cacheNode struct {
index int // row index for which this cacheNode is caching
refCount int // number of times this row was referenced
data []cacheDataType // valid slice from cache::cacheBuffer if in the cache
offset int // offset in cache::cacheBuffer data field is referring too
element *list.Element // list element (iterator) if in LRU list
}
type cache struct {
head []cacheNode // all the possible cached rows
rowSize int // size of each row
cacheAvail int // number of additional rows we can store
cacheBuffer []cacheDataType // pre-allocated buffer for cache
availableOffset int // next available offset in cacheBuffer
hits, misses int // cache statistics
cacheList *list.List // LRU list
}
func (c *cache) getData(i int) ([]cacheDataType, bool) {
var newData bool = true
c.head[i].refCount++ // count reference to this index
if c.head[i].offset != -1 {
h := &(c.head[i])
c.cacheList.Remove(h.element) // Remove from LRU list so we can re-insert into the back
newData = false
c.hits++
}
if newData {
var useOffset int = 0
// new data
if c.cacheAvail == 0 { // no more space in cache
// free a row
//Remove the front of the LRU list (the last used)
oldElement := c.cacheList.Front()
value := c.cacheList.Remove(oldElement)
old := value.(*cacheNode)
useOffset = old.offset // reuse its memory
old.offset = -1
c.cacheAvail++
} else {
useOffset = c.availableOffset
c.availableOffset += c.rowSize
}
c.head[i].data = c.cacheBuffer[useOffset : useOffset+c.rowSize]
c.head[i].offset = useOffset // remember which offsef this cache line has been assigned
c.cacheAvail--
c.misses++
}
h := &(c.head[i])
h.element = c.cacheList.PushBack(h) // Inserts it at the back of LRU circular list
return c.head[i].data, newData
}
func (c cache) stats() {
fmt.Printf("Cache misses: %d\n", c.misses)
fmt.Printf("Cache hits: %d\n", c.hits)
fmt.Printf("Cache efficiency: %.6f%%\n", float32(c.hits)/float32(c.hits+c.misses)*100)
}
func computeCacheSize(rowSize, cacheSize int) int {
cacheSizeBytes := cacheSize * (1 << 20) // multiply by 1 Mbytes
numCols := cacheSizeBytes / (rowSize * sizeOfCacheDataType) // num of rows we can store
numCols = maxi(2, numCols) // we should be able to store at least 2
return numCols
}
func newCache(l, rowSize, cacheSize int) *cache {
rowCacheSize := computeCacheSize(rowSize, cacheSize) // number of rows we can cache
head := make([]cacheNode, l)
for i := 0; i < l; i++ {
head[i].index = i
head[i].refCount = 0
head[i].data = nil
head[i].offset = -1
}
c := cache{head: head, rowSize: rowSize, cacheAvail: rowCacheSize, availableOffset: 0, hits: 0, misses: 0}
c.cacheBuffer = make([]cacheDataType, rowCacheSize*rowSize)
c.cacheList = list.New()
return &c
}