Skip to content

Commit

Permalink
start full Go input files: move flags to engine
Browse files Browse the repository at this point in the history
  • Loading branch information
barnex committed Sep 15, 2016
1 parent baed222 commit c11c84f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 40 deletions.
57 changes: 23 additions & 34 deletions cmd/mumax3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,31 @@ import (
)

var (
flag_cachedir = flag.String("cache", "/tmp", "Kernel cache directory (empty disables caching)")
flag_cpuprof = flag.Bool("cpuprof", false, "Record gopprof CPU profile")
flag_failfast = flag.Bool("failfast", false, "If one simulation fails, stop entire batch immediately")
flag_forceclean = flag.Bool("f", true, "Force start, clean existing output directory")
flag_gpu = flag.Int("gpu", 0, "Specify GPU")
flag_interactive = flag.Bool("i", false, "Open interactive browser session")
flag_launchtimeout = flag.Duration("launchtimeout", 0, "Launch timeout for CUDA calls")
flag_memprof = flag.Bool("memprof", false, "Recored gopprof memory profile")
flag_od = flag.String("o", "", "Override output directory")
flag_port = flag.String("http", ":35367", "Port to serve web gui")
flag_selftest = flag.Bool("paranoid", false, "Enable convolution self-test for cuFFT sanity.")
flag_silent = flag.Bool("s", false, "Silent") // provided for backwards compatibility
flag_sync = flag.Bool("sync", false, "Synchronize all CUDA calls (debug)")
flag_test = flag.Bool("test", false, "Cuda test (internal)")
flag_version = flag.Bool("v", true, "Print version")
flag_vet = flag.Bool("vet", false, "Check input files for errors, but don't run them")
flag_failfast = flag.Bool("failfast", false, "If one simulation fails, stop entire batch immediately")
flag_test = flag.Bool("test", false, "Cuda test (internal)")
flag_version = flag.Bool("v", true, "Print version")
flag_vet = flag.Bool("vet", false, "Check input files for errors, but don't run them")
// more flags in engine/gofiles.go
)

func main() {
flag.Parse()
log.SetPrefix("")
log.SetFlags(0)

cuda.Init(*flag_gpu)
cuda.Init(*engine.Flag_gpu)
runtime.GOMAXPROCS(runtime.NumCPU())
cuda.Synchronous = *flag_sync
cuda.Synchronous = *engine.Flag_sync
if *flag_version {
printVersion()
}

timer.Timeout = *flag_launchtimeout
if *flag_launchtimeout != 0 {
timer.Timeout = *engine.Flag_launchtimeout
if *engine.Flag_launchtimeout != 0 {
cuda.Synchronous = true
}

engine.TestDemag = *flag_selftest
engine.TestDemag = *engine.Flag_selftest

// used by bootstrap launcher to test cuda
// successful exit means cuda was initialized fine
Expand All @@ -61,18 +50,18 @@ func main() {
os.Exit(0)
}

engine.CacheDir = *flag_cachedir
if *flag_cpuprof {
engine.CacheDir = *engine.Flag_cachedir
if *engine.Flag_cpuprof {
prof.InitCPU(".")
}
if *flag_memprof {
if *engine.Flag_memprof {
prof.InitMem(".")
}
defer prof.Cleanup()
defer engine.Close() // flushes pending output, if any

defer func() {
if *flag_sync {
if *engine.Flag_sync {
timer.Print(os.Stdout)
}
}()
Expand All @@ -99,7 +88,7 @@ func runInteractive() {
// 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())
engine.InitIO(outdir, outdir, *flag_forceclean)
engine.InitIO(outdir, outdir, *engine.Flag_forceclean)

engine.Timeout = 365 * 24 * time.Hour // basically forever

Expand All @@ -119,10 +108,10 @@ func runInteractive() {
func runFileAndServe(fname string) {

outDir := util.NoExt(fname) + ".out"
if *flag_od != "" {
outDir = *flag_od
if *engine.Flag_od != "" {
outDir = *engine.Flag_od
}
engine.InitIO(fname, outDir, *flag_forceclean)
engine.InitIO(fname, outDir, *engine.Flag_forceclean)
fname = engine.InputFile

var code *script.BlockStmt
Expand All @@ -136,14 +125,14 @@ func runFileAndServe(fname string) {
// 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)
if *engine.Flag_interactive {
openbrowser("http://127.0.0.1" + *engine.Flag_port)
}

// start executing the tree, possibly injecting commands from web gui
engine.EvalFile(code)

if *flag_interactive {
if *engine.Flag_interactive {
engine.RunInteractive()
}
}
Expand All @@ -160,11 +149,11 @@ func runFileAndServe(fname string) {

// start Gui server and return server address
func goServeGUI() string {
if *flag_port == "" {
if *engine.Flag_port == "" {
log.Println(`//not starting GUI (-http="")`)
return ""
}
addr := engine.GoServe(*flag_port)
addr := engine.GoServe(*engine.Flag_port)
fmt.Print("//starting GUI at http://127.0.0.1", addr, "\n")
return addr
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/mumax3/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
func RunQueue(files []string) {
s := NewStateTab(files)
s.PrintTo(os.Stdout)
go s.ListenAndServe(*flag_port)
go s.ListenAndServe(*engine.Flag_port)
s.Run()
fmt.Println(numOK.get(), "OK, ", numFailed.get(), "failed")
os.Exit(int(exitStatus))
Expand Down
42 changes: 42 additions & 0 deletions engine/gofiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package engine

// support for running Go files as if they were mx3 files.

import (
"flag"
"github.com/mumax/3/cuda"
)

var (
// These flags are shared between cmd/mumax3 and Go input files.
Flag_cachedir = flag.String("cache", "/tmp", "Kernel cache directory (empty disables caching)")
Flag_cpuprof = flag.Bool("cpuprof", false, "Record gopprof CPU profile")
Flag_gpu = flag.Int("gpu", 0, "Specify GPU")
Flag_interactive = flag.Bool("i", false, "Open interactive browser session")
Flag_launchtimeout = flag.Duration("launchtimeout", 0, "Launch timeout for CUDA calls")
Flag_memprof = flag.Bool("memprof", false, "Recored gopprof memory profile")
Flag_od = flag.String("o", "", "Override output directory")
Flag_port = flag.String("http", ":35367", "Port to serve web gui")
Flag_selftest = flag.Bool("paranoid", false, "Enable convolution self-test for cuFFT sanity.")
Flag_silent = flag.Bool("s", false, "Silent") // provided for backwards compatibility
Flag_sync = flag.Bool("sync", false, "Synchronize all CUDA calls (debug)")
Flag_forceclean = flag.Bool("f", true, "Force start, clean existing output directory")
)

// Usage: in every Go input file, write:
//
// func main(){
// defer InitAndClose()()
// // ...
// }
//
// This initialises the GPU, output directory, etc,
// and makes sure pending output will get flushed.
func InitAndClose() func() {
cuda.Init(0)
InitIO("standardproblem4.mx3", "standardproblem4.out", true)
GoServe(":35367")
return func() {
Close()
}
}
6 changes: 1 addition & 5 deletions test/standardproblem4.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
package main

import (
"github.com/mumax/3/cuda"
. "github.com/mumax/3/engine"
)

func main() {

cuda.Init(0)
InitIO("standardproblem4.mx3", "standardproblem4.out", true)
GoServe(":35367")
defer Close()
defer InitAndClose()()

SetGridSize(128, 32, 1)
SetCellSize(500e-9/128, 125e-9/32, 3e-9)
Expand Down

0 comments on commit c11c84f

Please sign in to comment.