-
Notifications
You must be signed in to change notification settings - Fork 0
/
asyncio.go
84 lines (75 loc) · 1.91 KB
/
asyncio.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
package engine
import (
"bufio"
"github.com/mumax/3/cuda"
"github.com/mumax/3/data"
"github.com/mumax/3/oommf"
"github.com/mumax/3/util"
"os"
"path"
"strings"
)
// Save under given file name (transparant async I/O).
func SaveAs(q Quantity, fname string) {
if !path.IsAbs(fname) && !strings.HasPrefix(fname, OD) {
fname = path.Clean(OD + "/" + fname)
}
if path.Ext(fname) == "" {
fname += ".ovf"
}
buffer, recylce := q.Slice()
if recylce {
defer cuda.Recycle(buffer)
}
info := data.Meta{Time: Time, Name: q.Name(), Unit: q.Unit()}
initQue()
saveQue <- saveTask{fname, assureCPU(buffer), info}
}
// Copy to CPU, if needed
func assureCPU(s *data.Slice) *data.Slice {
if s.CPUAccess() {
return s
} else {
return s.HostCopy()
}
}
var (
saveQue chan saveTask // passes save requests from runDownloader to runSaver
done = make(chan bool) // marks output server is completely done after closing dlQue
nOutBuf int // number of output buffers actually in use (<= maxOutputQueLen)
)
const maxOutputQueLen = 16 // number of outputs that can be queued for asynchronous I/O.
func initQue() {
if saveQue == nil {
saveQue = make(chan saveTask)
go runSaver()
}
}
// output task
type saveTask struct {
fname string
output *data.Slice
info data.Meta
}
// continuously takes save tasks and flushes them to disk.
// the save queue can accommodate many outputs (stored on host).
// the rather big queue allows big output bursts to be concurrent with GPU.
func runSaver() {
for t := range saveQue {
f, err := os.OpenFile(t.fname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
util.FatalErr(err)
out := bufio.NewWriter(f)
oommf.WriteOVF2(out, t.output, "binary 4", t.info)
out.Flush()
f.Close()
}
done <- true
}
// finalizer function called upon program exit.
// waits until all asynchronous output has been saved.
func drainOutput() {
if saveQue != nil {
close(saveQue)
<-done
}
}