forked from gorgonia/gorgonia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
187 lines (155 loc) · 3.77 KB
/
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
// +build debug
package gorgonia
import (
"bytes"
"fmt"
"log"
"os"
"strings"
"sync/atomic"
)
const DEBUG = true
// these constants are used during development time - mainly on tracing statements to see the values of certain things.
// I use these instead of say, Delve because most of the time, the larger picture has to be known. Delve tends to give small picture views
var (
compileDev = false
shapeInferenceDev = false
typeSystemDev = false
symdiffDev = false
autodiffDev = false
machineDev = false
stabilizationDev = false
solverDev = false
cudaDev = true
allocatorDev = false
)
var READMEMSTATS = true
var TABCOUNT uint32
var TRACK = false
var _logger_ = log.New(os.Stderr, "", 0)
var replacement = "\n"
func tabcount() int {
return int(atomic.LoadUint32(&TABCOUNT))
}
func enterLoggingContext() {
atomic.AddUint32(&TABCOUNT, 1)
tabcount := tabcount()
_logger_.SetPrefix(strings.Repeat("\t", tabcount))
replacement = "\n" + strings.Repeat("\t", tabcount)
}
func leaveLoggingContext() {
tabcount := tabcount()
tabcount--
if tabcount < 0 {
atomic.StoreUint32(&TABCOUNT, 0)
tabcount = 0
} else {
atomic.StoreUint32(&TABCOUNT, uint32(tabcount))
}
_logger_.SetPrefix(strings.Repeat("\t", tabcount))
replacement = "\n" + strings.Repeat("\t", tabcount)
}
func logf(format string, others ...interface{}) {
if DEBUG {
// format = strings.Replace(format, "\n", replacement, -1)
s := fmt.Sprintf(format, others...)
s = strings.Replace(s, "\n", replacement, -1)
_logger_.Println(s)
// _logger_.Printf(format, others...)
}
}
func compileLogf(format string, attrs ...interface{}) {
if compileDev {
logf(format, attrs...)
}
}
func shapeLogf(format string, attrs ...interface{}) {
if shapeInferenceDev {
logf(format, attrs...)
}
}
func typeSysLogf(format string, attrs ...interface{}) {
if typeSystemDev {
logf(format, attrs...)
}
}
func symdiffLogf(format string, attrs ...interface{}) {
if symdiffDev {
logf(format, attrs...)
}
}
func autodiffLogf(format string, attrs ...interface{}) {
if autodiffDev {
logf(format, attrs...)
}
}
func machineLogf(format string, attrs ...interface{}) {
if machineDev {
logf(format, attrs...)
}
}
func stabLogf(format string, attrs ...interface{}) {
if stabilizationDev {
logf(format, attrs...)
}
}
func solverLogf(format string, attrs ...interface{}) {
if solverDev {
logf(format, attrs...)
}
}
func cudaLogf(format string, attrs ...interface{}) {
if cudaDev {
logf(format, attrs...)
}
}
func allocatorLogf(format string, attrs ...interface{}) {
if allocatorDev {
logf(format, attrs...)
}
}
func recoverFrom(format string, attrs ...interface{}) {
if r := recover(); r != nil {
_logger_.Printf(format, attrs...)
panic(r)
}
}
/* Graph Collision related debugging code */
var nnc, cc, ec int64
func incrNN() {
atomic.AddInt64(&nnc, 1)
}
func incrCC() {
atomic.AddInt64(&cc, 1)
}
func incrEC() {
atomic.AddInt64(&ec, 1)
}
func GraphCollisionStats() (int, int, int) {
return int(atomic.LoadInt64(&nnc)), int(atomic.LoadInt64(&cc)), int(atomic.LoadInt64(&ec))
}
/* Compilation related debug utility functions/methods*/
func logCompileState(name string, g *ExprGraph, df *dataflow) {
var fname string
if name == "" {
fname = "TotallyRandomName.csv"
} else {
fname = name + ".csv"
}
f, err := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
panic(err)
}
defer f.Close()
compileState(f, g, df)
compileLogf("Written Compile State to %v", fname)
}
/* Analysis Debug Utility Functions/Methods */
func (df *dataflow) debugIntervals(sorted Nodes) {
var buf bytes.Buffer
buf.Write([]byte("Intervals:\n"))
for _, n := range sorted {
fmt.Fprintf(&buf, "\t%v:\t%v\n", n, df.intervals[n])
}
compileLogf(buf.String())
}