nac3artiq: #33 demo

The python API is changed a bit to allow running constructor with
@kernel annotation.
This commit is contained in:
pca006132 2021-10-07 15:58:19 +08:00
parent 08947d20c2
commit c697e522d3
3 changed files with 58 additions and 13 deletions

View File

@ -63,4 +63,4 @@ class Demo:
if __name__ == "__main__": if __name__ == "__main__":
Demo().run() run_on_core(Demo().run)

View File

@ -0,0 +1,43 @@
from language import *
from artiq_builtins import *
from numpy import int32, int64
test_int = 123
test_float = 123.456
test_list = [1] * 1
test_list2 = [[1.1], [], [3.0]]
test_list_fail = [1, 2, 3.0]
test_tuple = (1, 2, 3.0)
@kernel
class Test:
a: int32
@kernel
def __init__(self, a: int32):
self.a = a
test = Test(1)
print(test.a)
@kernel
class Demo:
@kernel
def run(self):
while True:
delay_mu(round64(test_float * 2.0))
delay_mu(int64(test_int))
delay_mu(int64(test_list[0]))
# delay_mu(int64(test_list_fail[0]))
delay_mu(int64(test_tuple[0]))
delay_mu(int64(test_tuple[2]))
delay_mu(int64(test_list2[2][0]))
delay_mu(int64(test.a))
test.a = 10
delay_mu(int64(test.a))
if __name__ == "__main__":
run_on_core(Demo().run)

View File

@ -1,12 +1,13 @@
from inspect import isclass, getmodule from inspect import isclass, getmodule
from functools import wraps from functools import wraps
import sys
import nac3artiq import nac3artiq
import device_db import device_db
__all__ = ["extern", "kernel"] __all__ = ["extern", "kernel", "run_on_core"]
nac3 = nac3artiq.NAC3(device_db.device_db["core"]["arguments"]["target"]) nac3 = nac3artiq.NAC3(device_db.device_db["core"]["arguments"]["target"])
@ -35,14 +36,15 @@ def kernel(class_or_function):
nac3.register_module(module) nac3.register_module(module)
registered_ids.add(module_id) registered_ids.add(module_id)
if isclass(class_or_function): return class_or_function
return class_or_function
else: def get_defined_class(method):
@wraps(class_or_function) return vars(sys.modules[method.__module__])[method.__qualname__.split('.')[0]]
def run_on_core(self, *args, **kwargs):
global allow_module_registration def run_on_core(method, *args, **kwargs):
if allow_module_registration: global allow_module_registration
nac3.analyze() if allow_module_registration:
allow_module_registration = False nac3.analyze()
nac3.compile_method(id(self.__class__), class_or_function.__name__) allow_module_registration = False
return run_on_core nac3.compile_method(id(get_defined_class(method)), method.__name__)