-
Notifications
You must be signed in to change notification settings - Fork 1
/
table.go
199 lines (169 loc) · 4.07 KB
/
table.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
package engine
import (
"fmt"
"github.com/mumax/3/cuda"
"github.com/mumax/3/httpfs"
"github.com/mumax/3/script"
"github.com/mumax/3/timer"
"github.com/mumax/3/util"
"io"
"sync"
"time"
)
var Table = *newTable("table") // output handle for tabular data (average magnetization etc.)
const TableAutoflushRate = 5 // auto-flush table every X seconds
func init() {
DeclFunc("TableAdd", TableAdd, "Add quantity as a column to the data table.")
DeclFunc("TableAddVar", TableAddVariable, "Add user-defined variable + name + unit to data table.")
DeclFunc("TableSave", TableSave, "Save the data table right now (appends one line).")
DeclFunc("TableAutoSave", TableAutoSave, "Auto-save the data table every period (s). Zero disables save.")
DeclFunc("TablePrint", TablePrint, "Print anyting in the data table")
Table.Add(&M)
}
type DataTable struct {
output interface {
io.Writer
Flush() error
}
info
outputs []TableData
autosave
flushlock sync.Mutex
}
func (t *DataTable) Write(p []byte) (int, error) {
n, err := t.output.Write(p)
util.FatalErr(err)
return n, err
}
func (t *DataTable) Flush() error {
if t.output == nil {
return nil
}
if cuda.Synchronous {
timer.Start("io")
}
err := t.output.Flush()
if cuda.Synchronous {
timer.Stop("io")
}
util.FatalErr(err)
return err
}
func newTable(name string) *DataTable {
t := new(DataTable)
t.name = name
return t
}
func TableAdd(col TableData) {
Table.Add(col)
}
func TableAddVariable(x script.ScalarFunction, name, unit string) {
Table.AddVariable(x, name, unit)
}
func (t *DataTable) AddVariable(x script.ScalarFunction, name, unit string) {
TableAdd(&userVar{x, name, unit})
}
type userVar struct {
value script.ScalarFunction
name, unit string
}
func (x *userVar) Name() string { return x.name }
func (x *userVar) NComp() int { return 1 }
func (x *userVar) Unit() string { return x.unit }
func (x *userVar) average() []float64 { return []float64{x.value.Float()} }
func TableSave() {
Table.Save()
}
func TableAutoSave(period float64) {
Table.autosave = autosave{period, Time, -1, nil} // count -1 allows output on t=0
}
func (t *DataTable) Add(output TableData) {
if t.inited() {
util.Fatal("data table add ", output.Name(), ": need to add quantity before table is output the first time")
}
t.outputs = append(t.outputs, output)
}
func (t *DataTable) Save() {
t.flushlock.Lock() // flush during write gives errShortWrite
defer t.flushlock.Unlock()
if cuda.Synchronous {
timer.Start("io")
}
t.init()
fprint(t, Time)
for _, o := range t.outputs {
vec := o.average()
for _, v := range vec {
fprint(t, "\t", float32(v))
}
}
fprintln(t)
//t.flush()
t.count++
if cuda.Synchronous {
timer.Stop("io")
}
}
func (t *DataTable) Println(msg ...interface{}) {
t.init()
fprintln(t, msg...)
}
func TablePrint(msg ...interface{}) {
Table.Println(msg...)
}
// open writer and write header
func (t *DataTable) init() {
if t.inited() {
return
}
f, err := httpfs.Create(OD() + t.name + ".txt")
util.FatalErr(err)
t.output = f
// write header
fprint(t, "# t (s)")
for _, o := range t.outputs {
if o.NComp() == 1 {
fprint(t, "\t", o.Name(), " (", o.Unit(), ")")
} else {
for c := 0; c < o.NComp(); c++ {
fprint(t, "\t", o.Name()+string('x'+c), " (", o.Unit(), ")")
}
}
}
fprintln(t)
t.Flush()
// periodically flush so GUI shows graph,
// but don't flush after every output for performance
// (httpfs flush is expensive)
go func() {
for {
time.Sleep(TableAutoflushRate * time.Second)
Table.flush()
}
}()
}
func (t *DataTable) inited() bool {
return t.output != nil
}
func (t *DataTable) flush() {
t.flushlock.Lock()
defer t.flushlock.Unlock()
t.Flush()
}
// can be saved in table
type TableData interface {
average() []float64
Name() string
Unit() string
NComp() int
}
// Safe fmt.Fprint, will fail on error
func fprint(out io.Writer, x ...interface{}) {
_, err := fmt.Fprint(out, x...)
util.FatalErr(err)
}
// Safe fmt.Fprintln, will fail on error
func fprintln(out io.Writer, x ...interface{}) {
_, err := fmt.Fprintln(out, x...)
util.FatalErr(err)
}