// cu.Init(), but error is fatal and does not dump stack. func tryCuInit() { defer func() { err := recover() util.FatalErr(err, "initialize GPU:") }() cu.Init(0) }
func gui(w http.ResponseWriter, r *http.Request) { // TODO: racy with engine init, mv to package engine if guiTempl == nil { guiTempl = loadTemplate("gui.html") // TODO: embed. guis.Heun = engine.Solver guis.Mesh = engine.Mesh() } util.FatalErr(guiTempl.Execute(w, guis)) }
// make sure tabout is open. func (t *dataTable) init() { if t.Writer == nil { f, err := os.OpenFile(OD+t.name+".txt", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) util.FatalErr(err) t.Writer = bufio.NewWriter(f) fmt.Fprintln(t, "# t(s) mx my mz") t.Flush() } }
func initCpuProf(OD string) { if *Flag_cpuprof { // start CPU profile to file fname := OD + "cpu.pprof" f, err := os.Create(fname) util.FatalErr(err, "CPU profile") err = pprof.StartCPUProfile(f) util.FatalErr(err, "CPU profile") log.Println("writing CPU profile to", fname) // at exit: exec go tool pprof to generate SVG output AtExit(func() { pprof.StopCPUProfile() me := procSelfExe() outfile := fname + ".svg" saveCmdOutput(outfile, "go", "tool", "pprof", "-svg", me, fname) }) } }
func parseSize(arg string) (size [3]int) { words := strings.Split(arg, "x") if len(words) != 3 { log.Fatal("resize: need N0xN1xN2 argument") } for i, w := range words { v, err := strconv.Atoi(w) util.FatalErr(err) size[i] = v } return }
// called by init() func initGPUProf(OD string) { if *Flag_gpuprof { util.PanicErr(os.Setenv("CUDA_PROFILE", "1")) util.PanicErr(os.Setenv("CUDA_PROFILE_CSV", "1")) out := OD + "gpuprofile.csv" log.Println("writing GPU profile to", out) util.PanicErr(os.Setenv("CUDA_PROFILE_LOG", out)) cfgfile := OD + "cudaprof.cfg" util.PanicErr(os.Setenv("CUDA_PROFILE_CONFIG", cfgfile)) util.FatalErr(ioutil.WriteFile(cfgfile, []byte(CUDA_PROFILE_CONFIG), 0666), "gpuprof") //AtExit(cuda.DeviceReset) } }
func Image(f *data.Slice, fmin, fmax string) *image.NRGBA { dim := f.NComp() switch dim { default: log.Fatalf("unsupported number of components: %v", dim) case 3: return drawVectors(f.Vectors()) case 1: min, max := extrema(f.Host()[0]) if fmin != "auto" { m, err := strconv.ParseFloat(fmin, 32) util.FatalErr(err) min = float32(m) } if fmax != "auto" { m, err := strconv.ParseFloat(fmax, 32) util.FatalErr(err) max = float32(m) } return drawFloats(f.Scalars(), min, max) } panic("unreachable") }
// SetOD sets the output directory where auto-saved files will be stored. func SetOD(od string, force bool) { if OD != "./" { log.Fatal("output directory already set to", OD) } OD = od if !strings.HasSuffix(OD, "/") { OD += "/" } log.Println("output directory:", OD) { // make OD wd, err := os.Getwd() util.FatalErr(err, "create output directory:") stat, err2 := os.Stat(wd) util.FatalErr(err2, "create output directory:") util.LogErr(os.Mkdir(od, stat.Mode())) // already exists is OK } // fail on non-empty OD f, err3 := os.Open(od) util.FatalErr(err3, "open output directory:") files, _ := f.Readdir(1) if !force && len(files) != 0 { log.Fatal(od, " not empty, clean it or force with -f") } // clean output dir if len(files) != 0 && OD != "." { log.Println("cleaning files in", OD) filepath.Walk(OD, func(path string, i os.FileInfo, err error) error { if path != OD { util.FatalErr(os.RemoveAll(path), "clean output directory:") } return nil }) } }
func GoServe(port string) { http.HandleFunc("/cmd/top", Command("top", "-b", "-n", "1")) http.HandleFunc("/cmd/uname", Command("uname", "-a")) http.HandleFunc("/render/", render) http.HandleFunc("/", gui) log.Print("serving http://localhost", port, "\n") go func() { cuda.LockThread() util.FatalErr(http.ListenAndServe(port, nil)) }() runtime.Gosched() }
func MustReadFile(fname string) (data *Slice, time float64) { s, t, err := ReadFile(fname) util.FatalErr(err) return s, t }
func open(fname string) *os.File { f, err := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) util.FatalErr(err) return f }
func MustWriteFile(fname string, s *Slice, time float64) { err := WriteFile(fname, s, time) util.FatalErr(err) }
func loadTemplate(fname string) *template.Template { body, err := ioutil.ReadFile(fname) util.FatalErr(err) return template.Must(template.New(fname).Parse(string(body))) }