artiq/artiq/frontend/artiq_run.py

185 lines
5.6 KiB
Python
Raw Normal View History

2014-12-03 18:20:30 +08:00
#!/usr/bin/env python3
import argparse
import sys
2015-02-20 03:09:37 +08:00
import time
2014-12-03 18:20:30 +08:00
from operator import itemgetter
2015-01-13 19:12:19 +08:00
from itertools import chain
2014-12-03 18:20:30 +08:00
2015-02-04 18:46:55 +08:00
import h5py
from artiq.language.db import *
from artiq.language.experiment import is_experiment
from artiq.protocols import pyon
from artiq.protocols.file_db import FlatFileDB
2015-02-22 11:09:46 +08:00
from artiq.master.worker_db import DBHub, ResultDB
2015-02-04 19:09:54 +08:00
from artiq.tools import file_import, verbosity_args, init_logger
2014-12-03 18:20:30 +08:00
class ELFRunner(AutoDB):
class DBKeys:
comm = Device()
2014-12-03 18:20:30 +08:00
def run(self, filename):
2014-12-03 18:20:30 +08:00
with open(filename, "rb") as f:
binary = f.read()
comm.load(binary)
comm.run("run")
2014-12-03 18:20:30 +08:00
comm.serve(dict(), dict())
class SimpleParamLogger:
def set(self, timestamp, name, value):
print("Parameter change: {} -> {}".format(name, value))
class DummyWatchdog:
def __init__(self, t):
pass
def __enter__(self):
pass
def __exit__(self, type, value, traceback):
pass
2015-02-20 03:09:37 +08:00
class DummyScheduler:
def __init__(self):
self.next_rid = 0
self.next_trid = 0
def run_queued(self, run_params):
2015-02-20 03:09:37 +08:00
rid = self.next_rid
self.next_rid += 1
print("Queuing: {}, RID={}".format(run_params, rid))
return rid
def cancel_queued(self, rid):
print("Cancelling RID {}".format(rid))
def run_timed(self, run_params, next_run):
2015-02-20 03:09:37 +08:00
trid = self.next_trid
self.next_trid += 1
next_run_s = time.strftime("%m/%d %H:%M:%S", time.localtime(next_run))
print("Timing: {} at {}, TRID={}".format(run_params, next_run_s, trid))
return trid
def cancel_timed(self, trid):
print("Cancelling TRID {}".format(trid))
watchdog = DummyWatchdog
2015-02-20 03:09:37 +08:00
2015-01-23 00:52:13 +08:00
def get_argparser():
parser = argparse.ArgumentParser(
description="Local experiment running tool")
2014-12-03 18:20:30 +08:00
2015-02-04 19:09:54 +08:00
verbosity_args(parser)
2014-12-03 18:20:30 +08:00
parser.add_argument("-d", "--ddb", default="ddb.pyon",
help="device database file")
parser.add_argument("-p", "--pdb", default="pdb.pyon",
help="parameter database file")
parser.add_argument("-E", "--elf", default=False, action="store_true",
2014-12-03 18:20:30 +08:00
help="run ELF binary")
parser.add_argument("-e", "--experiment", default=None,
help="experiment to run")
2015-02-04 18:46:55 +08:00
parser.add_argument("-o", "--hdf5", default=None,
help="write results to specified HDF5 file"
" (default: print them)")
parser.add_argument("file",
help="file containing the experiment to run")
parser.add_argument("arguments", nargs="*",
help="run arguments")
2014-12-03 18:20:30 +08:00
2015-01-23 00:52:13 +08:00
return parser
2014-12-03 18:20:30 +08:00
def _parse_arguments(arguments):
d = {}
for argument in arguments:
name, value = argument.split("=")
d[name] = pyon.decode(value)
return d
2014-12-03 18:20:30 +08:00
def main():
2015-01-23 00:52:13 +08:00
args = get_argparser().parse_args()
2015-02-04 19:09:54 +08:00
init_logger(args)
2014-12-03 18:20:30 +08:00
ddb = FlatFileDB(args.ddb)
pdb = FlatFileDB(args.pdb)
pdb.hooks.append(SimpleParamLogger())
rdb = ResultDB(lambda description: None, lambda mod: None)
dbh = DBHub(ddb, pdb, rdb)
2014-12-03 18:20:30 +08:00
try:
if args.elf:
if args.arguments:
print("Run arguments are not supported in ELF mode")
sys.exit(1)
exp_inst = ELFRunner(dbh)
rdb.build()
exp_inst.run(args.file)
2014-12-03 18:20:30 +08:00
else:
module = file_import(args.file)
if args.experiment is None:
exps = [(k, v) for k, v in module.__dict__.items()
if is_experiment(v)]
l = len(exps)
2014-12-03 18:20:30 +08:00
if l == 0:
print("No experiments found in module")
2014-12-03 18:20:30 +08:00
sys.exit(1)
elif l > 1:
print("More than one experiment found in module:")
for k, v in sorted(experiments, key=itemgetter(0)):
if v.__doc__ is None:
print(" {}".format(k))
else:
print(" {} ({})".format(
k, v.__doc__.splitlines()[0].strip()))
print("Use -u to specify which experiment to use.")
2014-12-03 18:20:30 +08:00
sys.exit(1)
else:
exp = exps[0][1]
2014-12-03 18:20:30 +08:00
else:
exp = getattr(module, args.experiment)
try:
arguments = _parse_arguments(args.arguments)
except:
print("Failed to parse run arguments")
sys.exit(1)
run_params = {
"file": args.file,
"experiment": args.experiment,
"arguments": arguments
}
exp_inst = exp(dbh,
scheduler=DummyScheduler(),
run_params=run_params,
**run_params["arguments"])
rdb.build()
exp_inst.run()
exp_inst.analyze()
2015-02-04 18:46:55 +08:00
if args.hdf5 is not None:
f = h5py.File(args.hdf5, "w")
try:
rdb.write_hdf5(f)
finally:
f.close()
else:
if rdb.data.read or rdb.realtime_data.read:
print("Results:")
for k, v in sorted(chain(rdb.realtime_data.read.items(),
rdb.data.read.items()),
key=itemgetter(0)):
2015-02-04 18:46:55 +08:00
print("{}: {}".format(k, v))
2014-12-03 18:20:30 +08:00
finally:
dbh.close_devices()
2014-12-03 18:20:30 +08:00
if __name__ == "__main__":
main()