2021-11-11 16:08:29 +08:00
|
|
|
from inspect import getfullargspec
|
2021-10-08 23:41:41 +08:00
|
|
|
from functools import wraps
|
2021-11-06 18:41:59 +08:00
|
|
|
from types import SimpleNamespace
|
2021-10-08 23:41:41 +08:00
|
|
|
from numpy import int32, int64
|
2021-11-06 22:50:28 +08:00
|
|
|
from typing import Generic, TypeVar
|
2021-12-04 20:35:52 +08:00
|
|
|
from math import floor, ceil
|
2021-10-08 23:41:41 +08:00
|
|
|
|
|
|
|
import nac3artiq
|
2022-02-12 21:17:37 +08:00
|
|
|
from embedding_map import EmbeddingMap
|
2021-10-08 23:41:41 +08:00
|
|
|
|
2021-12-04 20:35:52 +08:00
|
|
|
|
|
|
|
__all__ = [
|
2021-12-19 11:04:53 +08:00
|
|
|
"Kernel", "KernelInvariant", "virtual",
|
2022-03-26 13:39:09 +08:00
|
|
|
"Option", "Some", "none", "UnwrapNoneError",
|
2021-12-04 20:35:52 +08:00
|
|
|
"round64", "floor64", "ceil64",
|
|
|
|
"extern", "kernel", "portable", "nac3",
|
2022-02-12 21:17:37 +08:00
|
|
|
"rpc", "ms", "us", "ns",
|
2021-12-04 20:35:52 +08:00
|
|
|
"print_int32", "print_int64",
|
|
|
|
"Core", "TTLOut",
|
|
|
|
"parallel", "sequential"
|
|
|
|
]
|
2021-10-08 23:41:41 +08:00
|
|
|
|
|
|
|
|
2021-11-11 16:08:29 +08:00
|
|
|
T = TypeVar('T')
|
2021-12-01 22:44:53 +08:00
|
|
|
|
2021-12-19 11:04:53 +08:00
|
|
|
class Kernel(Generic[T]):
|
|
|
|
pass
|
|
|
|
|
2021-11-11 16:08:29 +08:00
|
|
|
class KernelInvariant(Generic[T]):
|
|
|
|
pass
|
|
|
|
|
2021-12-01 22:49:20 +08:00
|
|
|
# The virtual class must exist before nac3artiq.NAC3 is created.
|
2021-12-01 22:44:53 +08:00
|
|
|
class virtual(Generic[T]):
|
|
|
|
pass
|
2021-11-11 16:08:29 +08:00
|
|
|
|
2022-03-17 00:39:21 +08:00
|
|
|
class Option(Generic[T]):
|
|
|
|
_nac3_option: T
|
|
|
|
|
|
|
|
def __init__(self, v: T):
|
|
|
|
self._nac3_option = v
|
|
|
|
|
|
|
|
def is_none(self):
|
|
|
|
return self._nac3_option is None
|
|
|
|
|
|
|
|
def is_some(self):
|
|
|
|
return not self.is_none()
|
|
|
|
|
|
|
|
def unwrap(self):
|
2022-03-26 01:44:50 +08:00
|
|
|
if self.is_none():
|
2022-03-26 13:39:09 +08:00
|
|
|
raise UnwrapNoneError()
|
2022-03-17 00:39:21 +08:00
|
|
|
return self._nac3_option
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
if self.is_none():
|
2022-03-23 05:10:18 +08:00
|
|
|
return "none"
|
2022-03-17 00:39:21 +08:00
|
|
|
else:
|
|
|
|
return "Some({})".format(repr(self._nac3_option))
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
if self.is_none():
|
2022-03-23 05:10:18 +08:00
|
|
|
return "none"
|
2022-03-17 00:39:21 +08:00
|
|
|
else:
|
|
|
|
return "Some({})".format(str(self._nac3_option))
|
|
|
|
|
|
|
|
def Some(v: T) -> Option[T]:
|
|
|
|
return Option(v)
|
|
|
|
|
2022-03-23 05:10:18 +08:00
|
|
|
none = Option(None)
|
2021-12-04 20:35:52 +08:00
|
|
|
|
|
|
|
def round64(x):
|
|
|
|
return round(x)
|
|
|
|
|
|
|
|
def floor64(x):
|
|
|
|
return floor(x)
|
|
|
|
|
|
|
|
def ceil64(x):
|
|
|
|
return ceil(x)
|
|
|
|
|
|
|
|
|
2021-10-10 17:45:38 +08:00
|
|
|
import device_db
|
|
|
|
core_arguments = device_db.device_db["core"]["arguments"]
|
2021-10-08 23:41:41 +08:00
|
|
|
|
2021-11-05 18:07:43 +08:00
|
|
|
compiler = nac3artiq.NAC3(core_arguments["target"])
|
2021-11-11 16:08:29 +08:00
|
|
|
allow_registration = True
|
|
|
|
# Delay NAC3 analysis until all referenced variables are supposed to exist on the CPython side.
|
|
|
|
registered_functions = set()
|
|
|
|
registered_classes = set()
|
2021-10-08 23:46:46 +08:00
|
|
|
|
2021-11-11 16:08:29 +08:00
|
|
|
def register_function(fun):
|
|
|
|
assert allow_registration
|
|
|
|
registered_functions.add(fun)
|
2021-10-08 23:46:46 +08:00
|
|
|
|
2021-11-11 16:08:29 +08:00
|
|
|
def register_class(cls):
|
|
|
|
assert allow_registration
|
|
|
|
registered_classes.add(cls)
|
2021-10-08 23:41:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
def extern(function):
|
2021-11-05 18:07:43 +08:00
|
|
|
"""Decorates a function declaration defined by the core device runtime."""
|
2021-11-11 16:08:29 +08:00
|
|
|
register_function(function)
|
2021-10-08 23:41:41 +08:00
|
|
|
return function
|
|
|
|
|
2022-02-12 21:17:37 +08:00
|
|
|
def rpc(function):
|
|
|
|
"""Decorates a function declaration defined by the core device runtime."""
|
|
|
|
register_function(function)
|
|
|
|
return function
|
2021-10-08 23:41:41 +08:00
|
|
|
|
2021-11-06 18:41:59 +08:00
|
|
|
def kernel(function_or_method):
|
2021-11-05 18:07:43 +08:00
|
|
|
"""Decorates a function or method to be executed on the core device."""
|
2021-11-11 16:08:29 +08:00
|
|
|
register_function(function_or_method)
|
2021-11-06 18:41:59 +08:00
|
|
|
argspec = getfullargspec(function_or_method)
|
|
|
|
if argspec.args and argspec.args[0] == "self":
|
|
|
|
@wraps(function_or_method)
|
|
|
|
def run_on_core(self, *args, **kwargs):
|
|
|
|
fake_method = SimpleNamespace(__self__=self, __name__=function_or_method.__name__)
|
|
|
|
self.core.run(fake_method, *args, **kwargs)
|
|
|
|
else:
|
|
|
|
@wraps(function_or_method)
|
|
|
|
def run_on_core(*args, **kwargs):
|
|
|
|
raise RuntimeError("Kernel functions need explicit core.run()")
|
|
|
|
return run_on_core
|
2021-10-08 23:41:41 +08:00
|
|
|
|
2021-10-10 16:26:01 +08:00
|
|
|
|
2021-10-08 23:41:41 +08:00
|
|
|
def portable(function):
|
2021-11-05 18:07:43 +08:00
|
|
|
"""Decorates a function or method to be executed on the same device (host/core device) as the caller."""
|
2021-11-11 16:08:29 +08:00
|
|
|
register_function(function)
|
2021-10-08 23:41:41 +08:00
|
|
|
return function
|
|
|
|
|
|
|
|
|
2021-11-05 18:07:43 +08:00
|
|
|
def nac3(cls):
|
|
|
|
"""
|
|
|
|
Decorates a class to be analyzed by NAC3.
|
|
|
|
All classes containing kernels or portable methods must use this decorator.
|
|
|
|
"""
|
2021-11-11 16:08:29 +08:00
|
|
|
register_class(cls)
|
2021-11-05 18:07:43 +08:00
|
|
|
return cls
|
|
|
|
|
|
|
|
|
2021-10-10 17:45:38 +08:00
|
|
|
ms = 1e-3
|
|
|
|
us = 1e-6
|
|
|
|
ns = 1e-9
|
|
|
|
|
2021-10-08 23:41:41 +08:00
|
|
|
@extern
|
|
|
|
def rtio_init():
|
|
|
|
raise NotImplementedError("syscall not simulated")
|
|
|
|
|
|
|
|
|
|
|
|
@extern
|
|
|
|
def rtio_get_counter() -> int64:
|
|
|
|
raise NotImplementedError("syscall not simulated")
|
|
|
|
|
|
|
|
|
|
|
|
@extern
|
|
|
|
def rtio_output(target: int32, data: int32):
|
|
|
|
raise NotImplementedError("syscall not simulated")
|
|
|
|
|
|
|
|
|
|
|
|
@extern
|
|
|
|
def rtio_input_timestamp(timeout_mu: int64, channel: int32) -> int64:
|
|
|
|
raise NotImplementedError("syscall not simulated")
|
|
|
|
|
|
|
|
|
|
|
|
@extern
|
|
|
|
def rtio_input_data(channel: int32) -> int32:
|
|
|
|
raise NotImplementedError("syscall not simulated")
|
|
|
|
|
|
|
|
|
2021-11-19 12:39:57 +08:00
|
|
|
# These is not part of ARTIQ and only available in runkernel. Defined here for convenience.
|
|
|
|
@extern
|
|
|
|
def print_int32(x: int32):
|
|
|
|
raise NotImplementedError("syscall not simulated")
|
|
|
|
|
|
|
|
|
|
|
|
@extern
|
|
|
|
def print_int64(x: int64):
|
|
|
|
raise NotImplementedError("syscall not simulated")
|
|
|
|
|
|
|
|
|
2021-11-05 18:07:43 +08:00
|
|
|
@nac3
|
2021-10-08 23:41:41 +08:00
|
|
|
class Core:
|
2021-11-06 22:50:28 +08:00
|
|
|
ref_period: KernelInvariant[float]
|
2021-10-10 17:45:38 +08:00
|
|
|
|
2021-10-09 15:51:47 +08:00
|
|
|
def __init__(self):
|
2021-10-10 17:45:38 +08:00
|
|
|
self.ref_period = core_arguments["ref_period"]
|
2021-10-09 15:51:47 +08:00
|
|
|
|
2021-10-08 23:41:41 +08:00
|
|
|
def run(self, method, *args, **kwargs):
|
2021-11-11 16:08:29 +08:00
|
|
|
global allow_registration
|
2022-02-12 21:17:37 +08:00
|
|
|
|
|
|
|
embedding = EmbeddingMap()
|
|
|
|
|
2021-11-11 16:08:29 +08:00
|
|
|
if allow_registration:
|
|
|
|
compiler.analyze(registered_functions, registered_classes)
|
|
|
|
allow_registration = False
|
2021-10-10 16:26:01 +08:00
|
|
|
|
|
|
|
if hasattr(method, "__self__"):
|
|
|
|
obj = method.__self__
|
|
|
|
name = method.__name__
|
|
|
|
else:
|
|
|
|
obj = method
|
|
|
|
name = ""
|
|
|
|
|
2022-02-12 21:17:37 +08:00
|
|
|
compiler.compile_method_to_file(obj, name, args, "module.elf", embedding)
|
2021-10-08 23:41:41 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def reset(self):
|
|
|
|
rtio_init()
|
|
|
|
at_mu(rtio_get_counter() + int64(125000))
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def break_realtime(self):
|
|
|
|
min_now = rtio_get_counter() + int64(125000)
|
|
|
|
if now_mu() < min_now:
|
|
|
|
at_mu(min_now)
|
|
|
|
|
2021-10-10 17:45:38 +08:00
|
|
|
@portable
|
|
|
|
def seconds_to_mu(self, seconds: float) -> int64:
|
|
|
|
return int64(round(seconds/self.ref_period))
|
|
|
|
|
|
|
|
@portable
|
|
|
|
def mu_to_seconds(self, mu: int64) -> float:
|
|
|
|
return float(mu)*self.ref_period
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def delay(self, dt: float):
|
|
|
|
delay_mu(self.seconds_to_mu(dt))
|
|
|
|
|
2021-10-08 23:41:41 +08:00
|
|
|
|
2021-11-05 18:07:43 +08:00
|
|
|
@nac3
|
2021-10-08 23:41:41 +08:00
|
|
|
class TTLOut:
|
2021-11-06 22:50:28 +08:00
|
|
|
core: KernelInvariant[Core]
|
|
|
|
channel: KernelInvariant[int32]
|
|
|
|
target_o: KernelInvariant[int32]
|
2021-10-08 23:41:41 +08:00
|
|
|
|
2021-10-10 17:45:38 +08:00
|
|
|
def __init__(self, core: Core, channel: int32):
|
|
|
|
self.core = core
|
2021-10-08 23:41:41 +08:00
|
|
|
self.channel = channel
|
|
|
|
self.target_o = channel << 8
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def output(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def set_o(self, o: bool):
|
|
|
|
rtio_output(self.target_o, 1 if o else 0)
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def on(self):
|
|
|
|
self.set_o(True)
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def off(self):
|
|
|
|
self.set_o(False)
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def pulse_mu(self, duration: int64):
|
|
|
|
self.on()
|
|
|
|
delay_mu(duration)
|
|
|
|
self.off()
|
2021-10-10 17:45:38 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def pulse(self, duration: float):
|
|
|
|
self.on()
|
|
|
|
self.core.delay(duration)
|
|
|
|
self.off()
|
2021-10-31 17:16:21 +08:00
|
|
|
|
2021-11-05 18:07:43 +08:00
|
|
|
@nac3
|
2021-10-31 17:16:21 +08:00
|
|
|
class KernelContextManager:
|
|
|
|
@kernel
|
|
|
|
def __enter__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def __exit__(self):
|
|
|
|
pass
|
|
|
|
|
2022-03-26 13:39:09 +08:00
|
|
|
@nac3
|
|
|
|
class UnwrapNoneError(Exception):
|
|
|
|
"""raised when unwrapping a none value"""
|
|
|
|
artiq_builtin = True
|
|
|
|
|
2021-10-31 17:16:21 +08:00
|
|
|
parallel = KernelContextManager()
|
|
|
|
sequential = KernelContextManager()
|