From ec2787aaf66eea425283b1fbd8430d95283d2815 Mon Sep 17 00:00:00 2001 From: ram Date: Wed, 5 Feb 2025 07:19:27 +0000 Subject: [PATCH] Implement passing of kwarg --- nac3artiq/demo/min_artiq.py | 25 +++++-------------- nac3artiq/demo/rpc_kwargs_test.py | 41 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 nac3artiq/demo/rpc_kwargs_test.py diff --git a/nac3artiq/demo/min_artiq.py b/nac3artiq/demo/min_artiq.py index 8d769f05..62d32cc3 100644 --- a/nac3artiq/demo/min_artiq.py +++ b/nac3artiq/demo/min_artiq.py @@ -114,26 +114,13 @@ def extern(function): def rpc(arg=None, flags={}): - """Decorates a function to be executed on the host interpreter with kwargs support.""" - def decorator(function): - @wraps(function) - def wrapper(*args, **kwargs): - # Get function signature - sig = inspect.signature(function) - - # Validate kwargs against signature - bound_args = sig.bind(*args, **kwargs) - bound_args.apply_defaults() - - # Call RPC with both args and kwargs - return _do_rpc(function.__name__, - bound_args.args, - bound_args.kwargs) - return wrapper - + """Decorates a function or method to be executed on the host interpreter.""" if arg is None: - return decorator - return decorator(arg) + def inner_decorator(function): + return rpc(function, flags) + return inner_decorator + register_function(arg) + return arg def kernel(function_or_method): """Decorates a function or method to be executed on the core device.""" diff --git a/nac3artiq/demo/rpc_kwargs_test.py b/nac3artiq/demo/rpc_kwargs_test.py new file mode 100644 index 00000000..acc86bba --- /dev/null +++ b/nac3artiq/demo/rpc_kwargs_test.py @@ -0,0 +1,41 @@ +from min_artiq import * +from numpy import int32 + +@rpc +def sum_3(a: int32, b: int32 = 10, c: int32 = 20) -> int32: + """ + An RPC function to test NAC3's handling of positional/keyword arguments. + """ + return int32(a + b + c) + +@nac3 +class RpcKwargTest: + core: KernelInvariant[Core] + + def __init__(self): + self.core = Core() + + @kernel + def run(self): + #1) All positional => a=1, b=2, c=3 -> total=6 + s1 = sum_3(1, 2, 3) + if s1 != 6: + raise ValueError("sum_3(1,2,3) gave the wrong result.") + + #2) Use the default b=10, c=20 => a=5 => total=35 + s2 = sum_3(5) + if s2 != 35: + raise ValueError("sum_3(5) gave the wrong result.") + + #3) a=1 (positional), b=100 (keyword), omit c => c=20 => total=121 + s3 = sum_3(1, b=100) + if s3 != 121: + raise ValueError("sum_3(1, b=100) gave the wrong result.") + + #4) a=2, c=300 => b=10 (default) => total=312 + s4 = sum_3(a=2, c=300) + if s4 != 312: + raise ValueError("sum_3(a=2, c=300) gave the wrong result.") + +if __name__ == "__main__": + RpcKwargTest().run() \ No newline at end of file