From e8228710e7eb896a4f8aa478e362676456dac6db Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Fri, 5 Nov 2021 17:37:01 +0800 Subject: [PATCH 1/4] min_artiq: remove unnecessary definitions --- nac3artiq/min_artiq.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/nac3artiq/min_artiq.py b/nac3artiq/min_artiq.py index 18355dcfc..a41a4f168 100644 --- a/nac3artiq/min_artiq.py +++ b/nac3artiq/min_artiq.py @@ -51,10 +51,6 @@ def portable(function): return function -def get_defined_class(method): - return vars(sys.modules[method.__module__])[method.__qualname__.split('.')[0]] - - ms = 1e-3 us = 1e-6 ns = 1e-9 @@ -83,14 +79,6 @@ def rtio_input_timestamp(timeout_mu: int64, channel: int32) -> int64: def rtio_input_data(channel: int32) -> int32: raise NotImplementedError("syscall not simulated") -def at_mu(_): - raise NotImplementedError("at_mu not simulated") - -def now_mu() -> int32: - raise NotImplementedError("now_mu not simulated") - -def delay_mu(_): - raise NotImplementedError("delay_mu not simulated") @kernel class Core: From 610448fa734f4598addd99a0cb85f680e3161d49 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Fri, 5 Nov 2021 18:07:18 +0800 Subject: [PATCH 2/4] nac3artiq: include parallel in demo --- nac3artiq/demo.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/nac3artiq/demo.py b/nac3artiq/demo.py index 993187b00..27b9d1e89 100644 --- a/nac3artiq/demo.py +++ b/nac3artiq/demo.py @@ -3,17 +3,21 @@ from min_artiq import * @kernel class Demo: core: Core - led: TTLOut + led0: TTLOut + led1: TTLOut def __init__(self): self.core = Core() - self.led = TTLOut(self.core, 19) + self.led0 = TTLOut(self.core, 18) + self.led1 = TTLOut(self.core, 19) @kernel def run_k(self): self.core.reset() while True: - self.led.pulse(100.*ms) + with parallel: + self.led0.pulse(100.*ms) + self.led1.pulse(100.*ms) self.core.delay(100.*ms) def run(self): From d6f0607ff00cd5d38f69cb349e2c2e2e7ac108c4 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Fri, 5 Nov 2021 18:07:43 +0800 Subject: [PATCH 3/4] nac3artiq: rename class decorator to nac3 --- nac3artiq/demo.py | 2 +- nac3artiq/min_artiq.py | 41 +++++++++++++++++++++++++---------------- nac3artiq/src/lib.rs | 2 +- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/nac3artiq/demo.py b/nac3artiq/demo.py index 27b9d1e89..2a4b84907 100644 --- a/nac3artiq/demo.py +++ b/nac3artiq/demo.py @@ -1,6 +1,6 @@ from min_artiq import * -@kernel +@nac3 class Demo: core: Core led0: TTLOut diff --git a/nac3artiq/min_artiq.py b/nac3artiq/min_artiq.py index a41a4f168..6bc4f114c 100644 --- a/nac3artiq/min_artiq.py +++ b/nac3artiq/min_artiq.py @@ -5,14 +5,15 @@ from numpy import int32, int64 import nac3artiq -__all__ = ["KernelInvariant", "extern", "kernel", "portable", "ms", "us", "ns", +__all__ = ["KernelInvariant", "extern", "kernel", "portable", "nac3", + "ms", "us", "ns", "Core", "TTLOut", "parallel", "sequential"] import device_db core_arguments = device_db.device_db["core"]["arguments"] -nac3 = nac3artiq.NAC3(core_arguments["target"]) +compiler = nac3artiq.NAC3(core_arguments["target"]) allow_module_registration = True registered_ids = set() @@ -26,31 +27,40 @@ def register_module_of(obj): module = getmodule(obj) module_id = id(module) if module_id not in registered_ids: - nac3.register_module(module) + compiler.register_module(module) registered_ids.add(module_id) def extern(function): + """Decorates a function declaration defined by the core device runtime.""" register_module_of(function) return function def kernel(class_or_function): + """Decorates a function or method to be executed on the core device.""" register_module_of(class_or_function) - if isclass(class_or_function): - return class_or_function - else: - @wraps(class_or_function) - def device_only(*args, **kwargs): - raise RuntimeError("Kernels must not be called directly, use core.run(kernel_function) instead") - return device_only + @wraps(class_or_function) + def device_only(*args, **kwargs): + raise RuntimeError("Kernels must not be called directly, use core.run(kernel_function) instead") + return device_only def portable(function): + """Decorates a function or method to be executed on the same device (host/core device) as the caller.""" register_module_of(function) return function +def nac3(cls): + """ + Decorates a class to be analyzed by NAC3. + All classes containing kernels or portable methods must use this decorator. + """ + register_module_of(cls) + return cls + + ms = 1e-3 us = 1e-6 ns = 1e-9 @@ -80,7 +90,7 @@ def rtio_input_data(channel: int32) -> int32: raise NotImplementedError("syscall not simulated") -@kernel +@nac3 class Core: ref_period: float @@ -90,7 +100,7 @@ class Core: def run(self, method, *args, **kwargs): global allow_module_registration if allow_module_registration: - nac3.analyze() + compiler.analyze() allow_module_registration = False if hasattr(method, "__self__"): @@ -100,7 +110,7 @@ class Core: obj = method name = "" - nac3.compile_method(obj, name, args) + compiler.compile_method(obj, name, args) @kernel def reset(self): @@ -126,7 +136,7 @@ class Core: delay_mu(self.seconds_to_mu(dt)) -@kernel +@nac3 class TTLOut: core: Core channel: int32 @@ -166,7 +176,7 @@ class TTLOut: self.core.delay(duration) self.off() -@portable +@nac3 class KernelContextManager: @kernel def __enter__(self): @@ -178,4 +188,3 @@ class KernelContextManager: parallel = KernelContextManager() sequential = KernelContextManager() - diff --git a/nac3artiq/src/lib.rs b/nac3artiq/src/lib.rs index 45a01648b..d683e8483 100644 --- a/nac3artiq/src/lib.rs +++ b/nac3artiq/src/lib.rs @@ -120,7 +120,7 @@ impl Nac3 { } => { let kernels = decorator_list.iter().any(|decorator| { if let ast::ExprKind::Name { id, .. } = decorator.node { - id.to_string() == "kernel" || id.to_string() == "portable" + id.to_string() == "nac3" } else { false } From afb94dd299a0f46fe57a5879c526329d2af797f3 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Fri, 5 Nov 2021 18:28:31 +0800 Subject: [PATCH 4/4] nac3artiq: move demo to dedicated folder --- nac3artiq/{ => demo}/demo.py | 0 nac3artiq/{ => demo}/device_db.py | 0 nac3artiq/{ => demo}/kernel.ld | 0 nac3artiq/{ => demo}/min_artiq.py | 0 nac3artiq/demo/nac3artiq.so | 1 + nac3artiq/nac3artiq.so | 1 - 6 files changed, 1 insertion(+), 1 deletion(-) rename nac3artiq/{ => demo}/demo.py (100%) rename nac3artiq/{ => demo}/device_db.py (100%) rename nac3artiq/{ => demo}/kernel.ld (100%) rename nac3artiq/{ => demo}/min_artiq.py (100%) create mode 120000 nac3artiq/demo/nac3artiq.so delete mode 120000 nac3artiq/nac3artiq.so diff --git a/nac3artiq/demo.py b/nac3artiq/demo/demo.py similarity index 100% rename from nac3artiq/demo.py rename to nac3artiq/demo/demo.py diff --git a/nac3artiq/device_db.py b/nac3artiq/demo/device_db.py similarity index 100% rename from nac3artiq/device_db.py rename to nac3artiq/demo/device_db.py diff --git a/nac3artiq/kernel.ld b/nac3artiq/demo/kernel.ld similarity index 100% rename from nac3artiq/kernel.ld rename to nac3artiq/demo/kernel.ld diff --git a/nac3artiq/min_artiq.py b/nac3artiq/demo/min_artiq.py similarity index 100% rename from nac3artiq/min_artiq.py rename to nac3artiq/demo/min_artiq.py diff --git a/nac3artiq/demo/nac3artiq.so b/nac3artiq/demo/nac3artiq.so new file mode 120000 index 000000000..d05f6c9b1 --- /dev/null +++ b/nac3artiq/demo/nac3artiq.so @@ -0,0 +1 @@ +../../target/release/libnac3artiq.so \ No newline at end of file diff --git a/nac3artiq/nac3artiq.so b/nac3artiq/nac3artiq.so deleted file mode 120000 index fbba24173..000000000 --- a/nac3artiq/nac3artiq.so +++ /dev/null @@ -1 +0,0 @@ -../target/release/libnac3artiq.so \ No newline at end of file