-
Notifications
You must be signed in to change notification settings - Fork 2
/
queue.go
193 lines (171 loc) · 4.18 KB
/
queue.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
package main
// File que for distributing multiple input files over GPUs.
import (
"flag"
"fmt"
"github.com/mumax/3/cuda/cu"
"github.com/mumax/3/engine"
"io"
"log"
"net/http"
"os"
"os/exec"
"sync"
"sync/atomic"
)
var (
exitStatus atom = 0
numOK, numFailed atom = 0, 0
)
func RunQueue(files []string) {
s := NewStateTab(files)
s.PrintTo(os.Stdout)
go s.ListenAndServe(*flag_port)
s.Run()
fmt.Println(numOK.get(), "OK, ", numFailed.get(), "failed")
os.Exit(int(exitStatus))
}
// StateTab holds the queue state (list of jobs + statuses).
// All operations are atomic.
type stateTab struct {
lock sync.Mutex
jobs []job
next int
}
// Job info.
type job struct {
inFile string // input file to run
webAddr string // http address for gui of running process
uid int
}
// NewStateTab constructs a queue for the given input files.
// After construction, it is accessed atomically.
func NewStateTab(inFiles []string) *stateTab {
s := new(stateTab)
s.jobs = make([]job, len(inFiles))
for i, f := range inFiles {
s.jobs[i] = job{inFile: f, uid: i}
}
return s
}
// StartNext advances the next job and marks it running, setting its webAddr to indicate the GUI url.
// A copy of the job info is returned, the original remains unmodified.
// ok is false if there is no next job.
func (s *stateTab) StartNext(webAddr string) (next job, ok bool) {
s.lock.Lock()
defer s.lock.Unlock()
if s.next >= len(s.jobs) {
return job{}, false
}
s.jobs[s.next].webAddr = webAddr
jobCopy := s.jobs[s.next]
s.next++
return jobCopy, true
}
// Finish marks the job with j's uid as finished.
func (s *stateTab) Finish(j job) {
s.lock.Lock()
defer s.lock.Unlock()
s.jobs[j.uid].webAddr = ""
}
// Runs all the jobs in stateTab.
func (s *stateTab) Run() {
nGPU := cu.DeviceGetCount()
idle := initGPUs(nGPU)
for {
gpu := <-idle
addr := fmt.Sprint(":", 35368+gpu)
j, ok := s.StartNext(addr)
if !ok {
break
}
go func() {
run(j.inFile, gpu, j.webAddr)
s.Finish(j)
idle <- gpu
}()
}
// drain remaining tasks (one already done)
for i := 1; i < nGPU; i++ {
<-idle
}
}
type atom int32
func (a *atom) set(v int) { atomic.StoreInt32((*int32)(a), int32(v)) }
func (a *atom) get() int { return int(atomic.LoadInt32((*int32)(a))) }
func (a *atom) inc() { atomic.AddInt32((*int32)(a), 1) }
func run(inFile string, gpu int, webAddr string) {
// overridden flags
gpuFlag := fmt.Sprint(`-gpu=`, gpu)
httpFlag := fmt.Sprint(`-http=`, webAddr)
// pass through flags
flags := []string{gpuFlag, httpFlag}
flag.Visit(func(f *flag.Flag) {
if f.Name != "gpu" && f.Name != "http" {
flags = append(flags, fmt.Sprintf("-%v=%v", f.Name, f.Value))
}
})
flags = append(flags, inFile)
cmd := exec.Command(os.Args[0], flags...)
log.Println(os.Args[0], flags)
err := cmd.Run()
if err != nil {
log.Println(inFile, err)
exitStatus.set(1)
numFailed.inc()
if *flag_failfast {
os.Exit(1)
}
} else {
numOK.inc()
}
}
func initGPUs(nGpu int) chan int {
if nGpu == 0 {
log.Fatal("no GPUs available")
panic(0)
}
idle := make(chan int, nGpu)
for i := 0; i < nGpu; i++ {
idle <- i
}
return idle
}
func (s *stateTab) PrintTo(w io.Writer) {
s.lock.Lock()
defer s.lock.Unlock()
for i, j := range s.jobs {
fmt.Fprintf(w, "%3d %v %v\n", i, j.inFile, j.webAddr)
}
}
func (s *stateTab) RenderHTML(w io.Writer) {
s.lock.Lock()
defer s.lock.Unlock()
fmt.Fprintln(w, `
<!DOCTYPE html> <html> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="refresh" content="1">
`+engine.CSS+`
</head><body>
<span style="color:gray; font-weight:bold; font-size:1.5em"> mumax<sup>3</sup> queue status </span><br/>
<hr/>
<pre>
`)
hostname := "localhost"
hostname, _ = os.Hostname()
for _, j := range s.jobs {
if j.webAddr != "" {
fmt.Fprint(w, `<b>`, j.uid, ` <a href="`, "http://", hostname+j.webAddr, `">`, j.inFile, " ", j.webAddr, "</a></b>\n")
} else {
fmt.Fprint(w, j.uid, " ", j.inFile, "\n")
}
}
fmt.Fprintln(w, `</pre><hr/></body></html>`)
}
func (s *stateTab) ListenAndServe(addr string) {
http.Handle("/", s)
go http.ListenAndServe(addr, nil)
}
func (s *stateTab) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.RenderHTML(w)
}