test/portability: port to NAC3

This commit is contained in:
Sébastien Bourdeauducq 2024-08-30 14:29:40 +08:00
parent c763b9b717
commit 9f664afb00
1 changed files with 77 additions and 22 deletions

View File

@ -1,6 +1,8 @@
from operator import itemgetter
from fractions import Fraction
from numpy import int32, int64
from artiq.coredevice.core import Core
from artiq.experiment import *
from artiq.sim import devices as sim_devices
from artiq.test.hardware_testbench import ExperimentCase
@ -16,13 +18,18 @@ def _run_on_host(k_class, *args, **kwargs):
return k_inst
@nac3
class _Primes(EnvExperiment):
core: KernelInvariant[Core]
maximum: KernelInvariant[int32]
def build(self, output_list, maximum):
self.setattr_device("core")
self.output_list = output_list
self.maximum = maximum
def _add_output(self, x):
@rpc
def _add_output(self, x: int32):
self.output_list.append(x)
@kernel
@ -39,8 +46,11 @@ class _Primes(EnvExperiment):
self._add_output(x)
@nac3
class _Math(EnvExperiment):
"""Test kernel math"""
core: KernelInvariant[Core]
x: KernelInvariant[float]
x_sqrt: Kernel[float]
def build(self):
self.setattr_device("core")
@ -52,7 +62,18 @@ class _Math(EnvExperiment):
self.x_sqrt = self.x**0.5
@nac3
class _Misc(EnvExperiment):
core: KernelInvariant[Core]
input: KernelInvariant[int32]
al: KernelInvariant[list[int32]]
list_copy_in: KernelInvariant[list[float]]
half_input: Kernel[int32]
acc: Kernel[int32]
list_copy_out: Kernel[list[float]]
def build(self):
self.setattr_device("core")
@ -73,7 +94,10 @@ class _Misc(EnvExperiment):
self.list_copy_out = self.list_copy_in
@nac3
class _PulseLogger(EnvExperiment):
core: KernelInvariant[Core]
def build(self, parent_test, name):
self.setattr_device("core")
self.parent_test = parent_test
@ -86,20 +110,29 @@ class _PulseLogger(EnvExperiment):
t_usec = round(self.core.mu_to_seconds(t-origin)*1000000)
self.parent_test.output_list.append((self.name, t_usec, l, f))
def on(self, t, f):
@rpc
def on(self, t: int64, f: int32):
self._append(t, True, f)
def off(self, t):
@rpc
def off(self, t: int64):
self._append(t, False, 0)
@kernel
def pulse(self, f, duration):
def pulse(self, f: int32, duration: float):
self.on(now_mu(), f)
delay(duration)
self.core.delay(duration)
self.off(now_mu())
@nac3
class _Pulses(EnvExperiment):
core: KernelInvariant[Core]
a: KernelInvariant[_PulseLogger]
b: KernelInvariant[_PulseLogger]
c: KernelInvariant[_PulseLogger]
d: KernelInvariant[_PulseLogger]
def build(self, output_list):
self.setattr_device("core")
self.output_list = output_list
@ -115,22 +148,28 @@ class _Pulses(EnvExperiment):
for i in range(3):
with parallel:
with sequential:
self.a.pulse(100+i, 20*us)
self.b.pulse(200+i, 20*us)
self.a.pulse(100+i, 20.*us)
self.b.pulse(200+i, 20.*us)
with sequential:
self.c.pulse(300+i, 10*us)
self.d.pulse(400+i, 20*us)
self.c.pulse(300+i, 10.*us)
self.d.pulse(400+i, 20.*us)
@nac3
class _MyException(Exception):
pass
@nac3
class _NestedFinally(EnvExperiment):
core: KernelInvariant[Core]
def build(self, trace):
self.setattr_device("core")
self.trace = trace
def _trace(self, i):
@rpc
def _trace(self, i: int32):
self.trace.append(i)
@kernel
@ -148,12 +187,17 @@ class _NestedFinally(EnvExperiment):
finally:
self._trace(2)
@nac3
class _NestedExceptions(EnvExperiment):
core: KernelInvariant[Core]
def build(self, trace):
self.setattr_device("core")
self.trace = trace
def _trace(self, i):
@rpc
def _trace(self, i: int32):
self.trace.append(i)
@kernel
@ -177,12 +221,17 @@ class _NestedExceptions(EnvExperiment):
finally:
self._trace(4)
@nac3
class _Exceptions(EnvExperiment):
core: KernelInvariant[Core]
def build(self, trace):
self.setattr_device("core")
self.trace = trace
def _trace(self, i):
@rpc
def _trace(self, i: int32):
self.trace.append(i)
@kernel
@ -224,13 +273,19 @@ class _Exceptions(EnvExperiment):
self._trace(104)
@nac3
class _RPCExceptions(EnvExperiment):
core: KernelInvariant[Core]
catch: KernelInvariant[bool]
success: Kernel[bool]
def build(self, catch):
self.setattr_device("core")
self.catch = catch
self.success = False
@rpc
def exception_raiser(self):
raise _MyException
@ -253,13 +308,18 @@ class _RPCExceptions(EnvExperiment):
self.success = True
@nac3
class _Keywords(EnvExperiment):
core: KernelInvariant[Core]
value: KernelInvariant[int32]
def build(self, value, output):
self.setattr_device("core")
self.value = value
self.output = output
def rpc(self, kw):
@rpc
def rpc(self, kw: int32):
self.output.append(kw)
@kernel
@ -323,6 +383,7 @@ class HostVsDeviceCase(ExperimentCase):
uut = self.execute(_RPCExceptions, catch=True)
self.assertTrue(uut.success)
@unittest.skip("NAC3TODO https://git.m-labs.hk/M-Labs/nac3/issues/533")
def test_keywords(self):
for f in self.execute, _run_on_host:
output = []
@ -331,9 +392,3 @@ class HostVsDeviceCase(ExperimentCase):
output = []
f(_Keywords, value=1, output=output)
self.assertEqual(output, [1])
output = []
f(_Keywords, value=False, output=output)
self.assertEqual(output, [False])
output = []
f(_Keywords, value=True, output=output)
self.assertEqual(output, [True])