-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhudd.go
331 lines (308 loc) · 9.26 KB
/
hudd.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright (c) 2021 Silvano DAL ZILIO
//
// MIT License
//go:build !buddy
// +build !buddy
package rudd
import (
"fmt"
"log"
"sync"
"sync/atomic"
"unsafe"
)
// tables corresponds to Binary Decision Diagrams based on the runtime
// hashmap. We hash a triplet (level, low, high) to a []byte and use the unique
// table to associate this triplet to an entry in the nodes table. We use more
// space but a benefit is that we can easily migrate to a concurrency-safe
// hashmap if we want to test concurrent data structures.
type tables struct {
sync.RWMutex
nodes []huddnode // List of all the BDD nodes. Constants are always kept at index 0 and 1
unique map[[huddsize]byte]int // Unicity table, used to associate each triplet to a single node
freenum int // Number of free nodes
freepos int // First free node
produced int // Total number of new nodes ever produced
hbuff [huddsize]byte // Used to compute the hash of nodes. A Buffer needs no initialization.
nodefinalizer interface{} // Finalizer used to decrement the ref count of external references
uniqueAccess int // accesses to the unique node table
uniqueHit int // entries actually found in the the unique node table
uniqueMiss int // entries not found in the the unique node table
gcstat // Information about garbage collections
configs // Configurable parameters
}
type huddnode struct {
level int32 // Order of the variable in the BDD
low int // Reference to the false branch
high int // Reference to the true branch
refcou int32 // Count the number of external references
}
func (b *tables) ismarked(n int) bool {
b.RLock()
defer b.RUnlock()
return (b.nodes[n].refcou & 0x200000) != 0
}
func (b *tables) marknode(n int) {
b.RLock()
defer b.RUnlock()
b.nodes[n].refcou |= 0x200000
}
func (b *tables) unmarknode(n int) {
b.RLock()
defer b.RUnlock()
b.nodes[n].refcou &= 0x1FFFFF
}
// New returns a new BDD based on an implementation selected with the build tag;
// meaning the 'Hudd'-style BDD by default (based on the standard runtime
// hashmap) or a 'BuDDy'-style BDD if tags buddy is set. Parameter varnum is the
// number of variables in the BDD.
//
// It is possible to set optional (configuration) parameters, such as the size
// of the initial node table (Nodesize) or the size for caches (Cachesize),
// using configs functions. The initial number of nodes is not critical since
// the table will be resized whenever there are too few nodes left after a
// garbage collection. But it does have some impact on the efficiency of the
// operations. We return a nil value if there is an error while creating the
// BDD.
func New(varnum int, options ...func(*configs)) (*BDD, error) {
b := &BDD{}
if (varnum < 1) || (varnum > int(_MAXVAR)) {
b.seterror("bad number of variable (%d)", varnum)
return nil, b.error
}
config := makeconfigs(varnum)
for _, f := range options {
f(config)
}
b.varnum = int32(varnum)
if _LOGLEVEL > 0 {
log.Printf("set varnum to %d\n", b.varnum)
}
b.varset = make([][2]int, varnum)
// We also initialize the refstack.
b.refstack = make([]int, 0, 2*varnum+4)
b.Initref()
b.error = nil
impl := &tables{}
impl.minfreenodes = config.minfreenodes
impl.maxnodeincrease = config.maxnodeincrease
// initializing the list of nodes
nodesize := config.nodesize
impl.nodes = make([]huddnode, nodesize)
for k := range impl.nodes {
impl.nodes[k] = huddnode{
level: 0,
low: -1,
high: k + 1,
refcou: 0,
}
}
impl.nodes[nodesize-1].high = 0
impl.unique = make(map[[huddsize]byte]int, nodesize)
// creating bddzero and bddone. We do not add them to the unique table.
impl.nodes[0] = huddnode{
level: int32(config.varnum),
low: 0,
high: 0,
refcou: _MAXREFCOUNT,
}
impl.nodes[1] = huddnode{
level: int32(config.varnum),
low: 1,
high: 1,
refcou: _MAXREFCOUNT,
}
impl.freepos = 2
impl.freenum = len(impl.nodes) - 2
for k := 0; k < config.varnum; k++ {
v0, _ := impl.makenode(int32(k), 0, 1, nil)
if v0 < 0 {
b.seterror("cannot allocate new variable %d in setVarnum", k)
return nil, b.error
}
impl.nodes[v0].refcou = _MAXREFCOUNT
b.Pushref(v0)
v1, _ := impl.makenode(int32(k), 1, 0, nil)
if v1 < 0 {
b.seterror("cannot allocate new variable %d in setVarnum", k)
return nil, b.error
}
impl.nodes[v1].refcou = _MAXREFCOUNT
b.Popref(1)
b.varset[k] = [2]int{v0, v1}
}
impl.gcstat.history = []gcpoint{}
impl.nodefinalizer = func(n *int) {
b.Lock()
defer b.Unlock()
if _DEBUG {
atomic.AddUint64(&(impl.gcstat.calledfinalizers), 1)
if _LOGLEVEL > 2 {
log.Printf("dec refcou %d\n", *n)
}
}
impl.nodes[*n].refcou--
}
b.tables = impl
b.cacheinit(config)
return b, nil
}
func (b *tables) huddhash(level int32, low, high int) {
b.hbuff[0] = byte(level)
b.hbuff[1] = byte(level >> 8)
b.hbuff[2] = byte(level >> 16)
b.hbuff[3] = byte(level >> 24)
b.hbuff[4] = byte(low)
b.hbuff[5] = byte(low >> 8)
b.hbuff[6] = byte(low >> 16)
b.hbuff[7] = byte(low >> 24)
if huddsize == 20 {
// 64 bits machine
b.hbuff[8] = byte(low >> 32)
b.hbuff[9] = byte(low >> 40)
b.hbuff[10] = byte(low >> 48)
b.hbuff[11] = byte(low >> 56)
b.hbuff[12] = byte(high)
b.hbuff[13] = byte(high >> 8)
b.hbuff[14] = byte(high >> 16)
b.hbuff[15] = byte(high >> 24)
b.hbuff[16] = byte(high >> 32)
b.hbuff[17] = byte(high >> 40)
b.hbuff[18] = byte(high >> 48)
b.hbuff[19] = byte(high >> 56)
return
}
// 32 bits machine
b.hbuff[8] = byte(high)
b.hbuff[9] = byte(high >> 8)
b.hbuff[10] = byte(high >> 16)
b.hbuff[11] = byte(high >> 24)
}
func (b *tables) nodehash(level int32, low, high int) (int, bool) {
b.huddhash(level, low, high)
hn, ok := b.unique[b.hbuff]
return hn, ok
}
// When a slot is unused in b.nodes, we have low set to -1 and high set to the
// next free position. The value of b.freepos gives the index of the lowest
// unused slot, except when freenum is 0, in which case it is also 0.
func (b *tables) setnode(level int32, low int, high int, count int32) int {
b.Lock()
defer b.Unlock()
b.huddhash(level, low, high)
b.freenum--
b.unique[b.hbuff] = b.freepos
res := b.freepos
b.freepos = b.nodes[b.freepos].high
b.nodes[res] = huddnode{level, low, high, count}
return res
}
func (b *tables) delnode(hn huddnode) {
b.huddhash(hn.level, hn.low, hn.high)
delete(b.unique, b.hbuff)
}
func (b *tables) size() int {
b.RLock()
defer b.RUnlock()
return len(b.nodes)
}
func (b *tables) level(n int) int32 {
b.RLock()
defer b.RUnlock()
return b.nodes[n].level
}
func (b *tables) low(n int) int {
b.RLock()
defer b.RUnlock()
return b.nodes[n].low
}
func (b *tables) high(n int) int {
b.RLock()
defer b.RUnlock()
return b.nodes[n].high
}
func (b *tables) allnodesfrom(f func(id, level, low, high int) error, n []Node) error {
for _, v := range n {
b.markrec(*v)
}
// if err := f(0, int(b.nodes[0].level), 0, 0); err != nil {
// b.unmarkall()
// return err
// }
// if err := f(1, int(b.nodes[1].level), 1, 1); err != nil {
// b.unmarkall()
// return err
// }
b.RLock()
count := len(b.nodes)
b.RUnlock()
for k := 0; k < count; k++ {
b.RLock()
if k >= len(b.nodes) {
break
}
b.RUnlock()
if b.ismarked(k) {
b.unmarknode(k)
if err := f(k, int(b.nodes[k].level), b.nodes[k].low, b.nodes[k].high); err != nil {
b.unmarkall()
return err
}
}
}
return nil
}
func (b *tables) allnodes(f func(id, level, low, high int) error) error {
// if err := f(0, int(b.nodes[0].level), 0, 0); err != nil {
// return err
// }
// if err := f(1, int(b.nodes[1].level), 1, 1); err != nil {
// return err
// }
b.RLock()
count := len(b.nodes)
b.RUnlock()
for k := 0; k < count; k++ {
b.RLock()
if k >= len(b.nodes) {
break
}
v := b.nodes[k]
b.RUnlock()
if v.low != -1 {
if err := f(k, int(v.level), v.low, v.high); err != nil {
return err
}
}
}
return nil
}
// stats returns information about the implementation
func (b *tables) stats() string {
b.RLock()
defer b.RUnlock()
res := "Impl.: Hudd\n"
res += fmt.Sprintf("Allocated: %d (%s)\n", len(b.nodes), humanSize(len(b.nodes), unsafe.Sizeof(huddnode{})))
res += fmt.Sprintf("Produced: %d\n", b.produced)
r := (float64(b.freenum) / float64(len(b.nodes))) * 100
res += fmt.Sprintf("Free: %d (%.3g %%)\n", b.freenum, r)
res += fmt.Sprintf("Used: %d (%.3g %%)\n", len(b.nodes)-b.freenum, (100.0 - r))
res += "==============\n"
res += fmt.Sprintf("# of GC: %d\n", len(b.gcstat.history))
if _DEBUG {
allocated := int(b.gcstat.setfinalizers)
reclaimed := int(b.gcstat.calledfinalizers)
for _, g := range b.gcstat.history {
allocated += g.setfinalizers
reclaimed += g.calledfinalizers
}
res += fmt.Sprintf("Ext. refs: %d\n", allocated)
res += fmt.Sprintf("Reclaimed: %d\n", reclaimed)
res += "==============\n"
res += fmt.Sprintf("Unique Access: %d\n", b.uniqueAccess)
res += fmt.Sprintf("Unique Hit: %d (%.1f%% + %.1f%%)\n", b.uniqueHit, (float64(b.uniqueHit)*100)/float64(b.uniqueAccess),
(float64(b.uniqueAccess-b.uniqueMiss-b.uniqueHit)*100)/float64(b.uniqueAccess))
res += fmt.Sprintf("Unique Miss: %d\n", b.uniqueMiss)
}
return res
}