/
sparse_debug.go
229 lines (201 loc) · 5 KB
/
sparse_debug.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
// Copyright 2016 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package elib
import (
"flag"
"fmt"
"math/rand"
"os"
"runtime/pprof"
"time"
)
func (s *Sparse) validate() (err error) {
if len(s.count) != len(s.valid) {
err = fmt.Errorf("len mismatch %d != %d", len(s.count), len(s.valid))
return
}
nValid := int32(0)
for i := range s.count {
if nValid != s.count[i] {
err = fmt.Errorf("n valid %d != count[%d] %d", nValid, i, s.count[i])
return
}
nValid += int32(NSetBits(Word(s.valid[i])))
}
return
}
type testSparse struct {
// Number of iterations to run
iterations Count
// Validate/print every so many iterations (zero means never).
validateEvery Count
printEvery Count
// Seed to make randomness deterministic. 0 means choose seed.
seed int64
// Number of objects to create.
nObjects Count
// Number of bits in sparse index.
log2SparseIndexMax uint
verbose int
x Sparse
objs randSparseVec
toDense map[Index]Index
unique []Word
profile string
}
type randSparse struct {
sparse, dense Index
}
//go:generate gentemplate -d Package=elib -id randSparse -d VecType=randSparseVec -d Type=randSparse vec.tmpl
func SparseTest() {
t := testSparse{
iterations: 10,
nObjects: 10,
validateEvery: 1,
verbose: 1,
}
flag.Var(&t.iterations, "iter", "Number of iterations")
flag.Var(&t.validateEvery, "valid", "Number of iterations per validate")
flag.Var(&t.printEvery, "print", "Number of iterations per print")
flag.Int64Var(&t.seed, "seed", 0, "Seed for random number generator")
flag.UintVar(&t.log2SparseIndexMax, "len", 16, "Number of bits in sparse index")
flag.Var(&t.nObjects, "objects", "Number of random objects")
flag.IntVar(&t.verbose, "verbose", 0, "Be verbose")
flag.StringVar(&t.profile, "profile", "", "Write CPU profile to file")
flag.Parse()
err := runSparseTest(&t)
if err != nil {
panic(err)
}
}
func (t *testSparse) validate(iter int) (err error) {
if t.validateEvery != 0 && iter%int(t.validateEvery) == 0 {
if err = t.x.validate(); err != nil {
if t.verbose != 0 {
fmt.Printf("iter %d: %s\n%+v\n", iter, err, t.x)
}
return
}
}
for i := 0; i < len(t.objs); i++ {
o := &t.objs[i]
d, ok := t.toDense[o.sparse]
if !ok {
panic("toDense corrupt")
}
if d != o.dense {
err = fmt.Errorf("sparse 0x%x dense %d != %d", o.sparse, o.dense, d)
return
}
}
for i := 0; i < len(t.objs); i++ {
o := &t.objs[i]
d, ok := t.x.Get(o.sparse)
if ok != (o.dense != MaxIndex) {
err = fmt.Errorf("iter %d ok %v sparse 0x%x dense %d != %d",
iter, ok, o.sparse, o.dense, d)
return
}
}
return
}
func (t *testSparse) randomSparse() (i Index) {
for {
i = Index(rand.Int() & (1<<uint(t.log2SparseIndexMax) - 1))
if !bitmapSet(t.unique, uint(i)) {
return
}
}
}
func runSparseTest(t *testSparse) (err error) {
if t.seed == 0 {
t.seed = int64(time.Now().Nanosecond())
}
rand.Seed(t.seed)
if t.verbose != 0 {
fmt.Printf("%#v\n", t)
}
t.objs.Resize(uint(t.nObjects))
t.unique = bitmapMake(1 << t.log2SparseIndexMax)
t.toDense = make(map[Index]Index)
for i := range t.objs {
t.objs[i].sparse = t.randomSparse()
t.objs[i].dense = MaxIndex
t.toDense[t.objs[i].sparse] = t.objs[i].dense
}
if t.profile != "" {
var f *os.File
f, err = os.Create(t.profile)
if err != nil {
return
}
pprof.StartCPUProfile(f)
defer func() { pprof.StopCPUProfile() }()
}
var iter int
for iter = 0; iter < int(t.iterations); iter++ {
oi := rand.Int() % len(t.objs)
o := &t.objs[oi]
if o.dense != MaxIndex {
if ok := t.x.Unset(o.sparse); !ok {
err = fmt.Errorf("not found 0x%x", o.sparse)
return
}
o.dense = MaxIndex
} else {
bitmapUnset(t.unique, uint(o.sparse))
if t.validateEvery != 0 {
delete(t.toDense, o.sparse)
}
o.sparse = t.randomSparse()
o.dense = t.x.Set(t.objs[oi].sparse)
}
t.objs[oi] = *o
if t.validateEvery != 0 {
t.toDense[o.sparse] = o.dense
}
if t.validateEvery != 0 && iter%int(t.validateEvery) == 0 {
err = t.validate(iter)
if err != nil {
err = fmt.Errorf("iter %d %v", iter, err)
return
}
}
if t.printEvery != 0 && iter%int(t.printEvery) == 0 {
fmt.Printf("%10g iterations: %s\n", float64(iter), &t.x)
}
}
if t.verbose != 0 {
fmt.Printf("%d iterations: %+v\n", iter, t.x)
}
for i := range t.objs {
o := &t.objs[i]
if o.dense == MaxIndex {
continue
}
if ok := t.x.Unset(o.sparse); !ok {
err = fmt.Errorf("not found 0x%x", o.sparse)
return
}
o.dense = MaxIndex
if t.validateEvery != 0 {
t.toDense[o.sparse] = o.dense
if iter%int(t.validateEvery) == 0 {
err = t.validate(iter)
if err != nil {
return
}
}
}
if t.printEvery != 0 && iter%int(t.printEvery) == 0 {
fmt.Printf("%10g iterations: %s\n", float64(iter), &t.x)
}
iter++
}
if t.verbose != 0 {
fmt.Printf("%d iterations: %+v\n", iter, t.x)
fmt.Printf("No errors: %d iterations\n", t.iterations)
}
return
}