-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
156 lines (135 loc) · 3.86 KB
/
main.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
// mumax3 main command
package main
import (
"flag"
"fmt"
"github.com/mumax/3/cuda"
"github.com/mumax/3/engine"
"github.com/mumax/3/prof"
"github.com/mumax/3/script"
"github.com/mumax/3/util"
"io/ioutil"
"log"
"os"
"runtime"
"time"
)
var (
flag_version = flag.Bool("v", false, "Print version")
flag_interactive = flag.Bool("i", false, "Open interactive browser session")
flag_silent = flag.Bool("s", false, "Silent") // provided for backwards compatibility
flag_vet = flag.Bool("vet", false, "Check input files for errors, but don't run them")
flag_od = flag.String("o", "", "Override output directory")
flag_force = flag.Bool("f", true, "Force start, clean existing output directory")
flag_port = flag.String("http", ":35367", "Port to serve web gui")
flag_cpuprof = flag.Bool("cpuprof", false, "Record gopprof CPU profile")
flag_memprof = flag.Bool("memprof", false, "Recored gopprof memory profile")
flag_gpu = flag.Int("gpu", 0, "Specify GPU")
flag_sync = flag.Bool("sync", false, "Synchronize all CUDA calls (debug)")
flag_test = flag.Bool("test", false, "Cuda test (internal)")
flag_cachedir = flag.String("cache", "", "Kernel cache directory")
)
func main() {
flag.Parse()
log.SetPrefix("")
log.SetFlags(0)
if *flag_version {
printVersion()
}
cuda.Init(*flag_gpu)
runtime.GOMAXPROCS(runtime.NumCPU())
cuda.Synchronous = *flag_sync
// used by bootstrap launcher to test cuda
// successful exit means cuda was initialized fine
if *flag_test {
os.Exit(0)
}
engine.CacheDir = *flag_cachedir
if *flag_cpuprof {
prof.InitCPU(engine.OD)
}
if *flag_memprof {
prof.InitMem(engine.OD)
}
defer prof.Cleanup()
defer engine.Close() // flushes pending output, if any
if *flag_vet {
vet()
return
}
switch flag.NArg() {
case 0:
runInteractive()
case 1:
runFileAndServe(flag.Arg(0))
default:
RunQueue(flag.Args())
}
}
func runInteractive() {
fmt.Println("no input files: starting interactive session")
//initEngine()
// setup outut dir
now := time.Now()
outdir := fmt.Sprintf("mumax-%v-%02d-%02d_%02dh%02d.out", now.Year(), int(now.Month()), now.Day(), now.Hour(), now.Minute())
suggestOD(outdir)
engine.Timeout = 365 * 24 * time.Hour // forever
// set up some sensible start configuration
engine.Eval(`SetGridSize(128, 64, 1)
SetCellSize(4e-9, 4e-9, 4e-9)
Msat = 1e6
Aex = 10e-12
alpha = 1
m = RandomMag()`)
goServeGUI()
openbrowser("http://127.0.0.1" + *flag_port)
engine.RunInteractive()
}
// Runs a script file.
func runFileAndServe(fname string) {
//initEngine()
suggestOD(util.NoExt(fname) + ".out")
var code *script.BlockStmt
var err2 error
if fname != "" {
// first we compile the entire file into an executable tree
bytes, err := ioutil.ReadFile(fname)
util.FatalErr(err)
code, err2 = engine.World.Compile(string(bytes))
util.FatalErr(err2)
}
// now the parser is not used anymore so it can handle web requests
goServeGUI()
if *flag_interactive {
openbrowser("http://127.0.0.1" + *flag_port)
}
// start executing the tree, possibly injecting commands from web gui
engine.EvalFile(code)
if *flag_interactive {
engine.RunInteractive()
}
}
// start Gui server and return
func goServeGUI() {
if *flag_port == "" {
log.Println(`not starting GUI (-http="")`)
return
}
fmt.Print("starting GUI at http://127.0.0.1", *flag_port, "\n")
go engine.Serve(*flag_port)
}
// set output directory unless flag_od overrides it
func suggestOD(fname string) {
if *flag_od != "" {
fname = *flag_od
}
engine.SetOD(fname, *flag_force)
}
// print version to stdout
func printVersion() {
fmt.Print(" ", engine.UNAME, "\n")
fmt.Print(" ", cuda.GPUInfo, "\n")
fmt.Print("(c) Arne Vansteenkiste, Dynamat LAB, Ghent University, Belgium", "\n")
fmt.Print(" This is free software without any warranty. See license.txt", "\n")
fmt.Print("\n")
}