func retrieve(cmd string, args []string) { fs := newFlagSet(cmd, "[JOBID...]", "retrieve job result json files for the given job ids") fs.Parse(args) if len(fs.Args()) == 0 { log.Fatal("no job id specified") } client, err := cloudlus.Dial(*addr) fatalif(err) defer client.Close() for _, arg := range fs.Args() { uid, err := hex.DecodeString(arg) if err != nil { log.Println(err) continue } var jid cloudlus.JobId copy(jid[:], uid) j, err := client.Retrieve(jid) if err != nil { log.Println(err) continue } fname := fmt.Sprintf("result-%v.json", j.Id) err = ioutil.WriteFile(fname, saveJob(j), 0644) if err != nil { log.Println(err) } } }
func run(jobs []*cloudlus.Job, async bool) { client, err := cloudlus.Dial(*addr) fatalif(err) defer client.Close() if async { for _, j := range jobs { client.Submit(j) fmt.Printf("%v\n", j.Id) } return } ch := make(chan *cloudlus.Job, len(jobs)) for _, j := range jobs { client.Start(j, ch) } for _ = range jobs { j := <-ch if err := client.Err(); err != nil { log.Println(err) continue } fname := fmt.Sprintf("result-%v.json", j.Id) err := ioutil.WriteFile(fname, saveJob(j), 0644) if err != nil { log.Println(err) continue } func() { rc, err := client.RetrieveOutfile(j.Id) if err != nil { log.Println(err) return } defer rc.Close() fname = fmt.Sprintf("outdata-%v.zip", j.Id) f, err := os.Create(fname) if err != nil { log.Println(err) return } defer f.Close() _, err = io.Copy(f, rc) if err != nil { log.Println(err) return } }() fmt.Println(fname) } }
// RemoteTimeout is the same as Remote, but with a custom timeout rather than // the default. func RemoteTimeout(s *scen.Scenario, stdout, stderr io.Writer, addr string, timeout time.Duration) (float64, error) { client, err := cloudlus.Dial(addr) if err != nil { return math.Inf(1), err } defer client.Close() execfn := func(scn *scen.Scenario) (float64, error) { j, err := BuildRemoteJob(scn, objfile) if err != nil { return math.Inf(1), fmt.Errorf("failed to build remote job: %v", err) } j.Timeout = timeout // closing this channel might cause a send on a closed channel if the // timeout in the select below fires before the goroutine completes. done := make(chan bool, 1) go func() { j, err = client.Run(j) done <- true }() select { case <-done: if err != nil { return math.Inf(1), fmt.Errorf("job execution failed: %v", err) } case <-time.After(j.Timeout + 1*time.Hour): return math.Inf(1), fmt.Errorf("job rpc timeout limit reached") } if err := writeLogs(j, stdout, stderr); err != nil { return math.Inf(1), fmt.Errorf("job logging failed: %v", err) } data, err := client.RetrieveOutfileData(j, objfile) if err != nil { return math.Inf(1), fmt.Errorf("couldn't find objective result file: %v", err) } val, err := strconv.ParseFloat(strings.TrimSpace(string(data)), 64) if err != nil { return math.Inf(1), fmt.Errorf("invalid objective string '%s': %v", data, err) } return val, nil } return s.CalcTotalObjective(execfn) }
func submitjob(scen *scen.Scenario, j *cloudlus.Job) { if *addr == "" { runjob(scen) return } client, err := cloudlus.Dial(*addr) check(err) defer client.Close() j, err = client.Run(j) check(err) data, err := client.RetrieveOutfileData(j, *out) fmt.Printf("%s\n", data) }
func main() { var err error flag.Parse() optim.Rand = rand.New(rand.NewSource(int64(*seed))) if _, err := os.Stat(*dbname); !os.IsNotExist(err) && *restart < 0 { log.Fatalf("db file '%v' already exists", *dbname) } db, err = sql.Open("sqlite3", *dbname) check(err) defer db.Close() if *addr != "" { client, err = cloudlus.Dial(*addr) check(err) defer client.Close() } params := make([]int, flag.NArg()) for i, s := range flag.Args() { params[i], err = strconv.Atoi(s) check(err) } // load problem scen file scen := &scen.Scenario{} err = scen.Load(*scenfile) check(err) f1, err := os.Create(*objlog) check(err) defer f1.Close() f4, err := os.Create(*runlog) check(err) defer f4.Close() // create and initialize solver lb := scen.LowerBounds() ub := scen.UpperBounds() step := (ub[0] - lb[0]) / 10 var it optim.Method if *restart >= 0 { it, step = loadIter(lb, ub, *restart) } else { it = buildIter(lb, ub) } obj := &optim.ObjectiveLogger{Obj: &obj{scen, f4}, W: f1} m := &optim.BoxMesh{Mesh: &optim.InfMesh{StepSize: step}, Lower: lb, Upper: ub} // this is here so that signals goroutine can close over it solv := &optim.Solver{ Method: it, Obj: obj, Mesh: m, MaxIter: *maxiter, MaxEval: *maxeval, MaxNoImprove: *maxnoimprove, } // handle signals start := time.Now() sigs := make(chan os.Signal, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) go func() { <-sigs f1.Close() f4.Close() fmt.Println("\n*** optimizer killed early ***") final(solv, start) os.Exit(1) }() // solve and print results for solv.Next() { fmt.Printf("Iter %v (%v evals): %v\n", solv.Niter(), solv.Neval(), solv.Best()) } final(solv, start) }