def do_until_convergence(params): ps = sim.find_or_create_parameter_set(params) ps.create_runs_upto(4) Server.await_ps(ps) e = err(ps) while e > 0.2: print(f"results for {params} is not converged") ps.create_runs_upto(len(ps.runs()) + 4) # add four runs Server.await_ps(ps) e = err(ps) print(f"converged results for {params}, error = {e}")
from caravan import Server, Task with Server.start(): for i in range(20): Task.create(f"sleep {1+i%3}")
from caravan import Server, Task with Server.start(): tasks1 = [Task.create(f"sleep {1+i%3}") for i in range(5)] Server.await_all_tasks( tasks1) # this method blocks until all the tasks are finished print("all running tasks are complete!") for t in tasks1: print( f"task ID:{t.id()}, rc:{t.rc()}, rank:{t.rank()}, {t.start_at()}-{t.finish_at()}" )
def err(ps): runs = ps.runs() r1 = [r.output() for r in runs] n = len(runs) avg = sum(r1) / n err = math.sqrt(sum([(r - avg)**2 for r in r1]) / ((n - 1) * n)) return err def do_until_convergence(params): ps = sim.find_or_create_parameter_set(params) ps.create_runs_upto(4) Server.await_ps(ps) e = err(ps) while e > 0.2: print(f"results for {params} is not converged") ps.create_runs_upto(len(ps.runs()) + 4) # add four runs Server.await_ps(ps) e = err(ps) print(f"converged results for {params}, error = {e}") with Server.start(): for p1 in [1.0, 1.5, 2.0, 2.5]: for p2 in [0.5, 1.0]: Server.do_async(lambda param={ 'mu': p1, 'sigma': p2 }: do_until_convergence(param))
from caravan import Server, Task with Server.start(): for i in range(5): task = Task.create(f"sleep {1+i%3}") Server.await_task( task) # this method blocks until the task is finished. print( f"step {i} finished. rc: {task.rc()}, rank: {task.rank()}, {task.start_at()}-{task.finish_at()}" ) # show info of completed task
from caravan import Server, Task with Server.start(): t = Task.create("echo '[1.0,2.0,3.0]' > _output.json") Server.await_task(t) print(t.output())
def run_sequential_tasks(n): for i in range(4): task = Task.create(f"sleep {1+i%3}") Server.await_task( task) # this method blocks until the task is complete. print(f"step {i} of {n} finished") # show the progress
import functools from caravan import Server, Task def run_sequential_tasks(n): for i in range(4): task = Task.create(f"sleep {1+i%3}") Server.await_task( task) # this method blocks until the task is complete. print(f"step {i} of {n} finished") # show the progress with Server.start(): for n in range(3): Server.do_async(functools.partial(run_sequential_tasks, n))
import os, math from caravan import Server, Simulator this_dir = os.path.abspath(os.path.dirname(__file__)) sim = Simulator.create(f"python {this_dir}/mc_simulator.py > _output.json") def err(ps): runs = ps.runs() r1 = [r.output() for r in runs] n = len(runs) avg = sum(r1) / n err = math.sqrt(sum([(r - avg)**2 for r in r1]) / ((n - 1) * n)) return err with Server.start(): ps = sim.find_or_create_parameter_set({'mu': 1.0, 'sigma': 2.0}) ps.create_runs_upto(4) print("awaiting") Server.await_ps(ps) e = err(ps) while e > 0.2: print(f"error = {e}") ps.create_runs_upto(len(ps.runs()) + 4) # add four runs print("awaiting") Server.await_ps(ps) e = err(ps) print(f"error = {e}")
from caravan import Server, Tables, Task Tables.load("dump.pickle") # data are loaded Task.reset_cancelled() for t in Task.all(): print(t.to_dict()) # print Tasks with Server.start(): # restart scheduler pass print("second execution done") for t in Task.all(): print(t.to_dict()) # print Tasks
import os from caravan import Server, Simulator this_dir = os.path.abspath(os.path.dirname(__file__)) sim = Simulator.create(f"python {this_dir}/mc_simulator.py > _output.json") with Server.start(): ps = sim.find_or_create_parameter_set({ 'mu': 1.0, 'sigma': 2.0 }) # create a ParameterSet whose parameters are (mu=1.0,sigma=2.0). ps.create_runs_upto( 10 ) # create ten Runs. In the background, `make_cmd` is called to generate actual commands. Server.await_ps( ps) # wait until all the Runs of this ParameterSet finishes avg = sum([r.output() for r in ps.runs()]) / len( ps.runs()) # results are averaged over the Runs print(f"average: {avg}") for r in ps.runs(): print(f"id: {r.id()}, output: {r.output()}" ) # showing results of each Run