2014-09-24 23:43:22 +08:00
|
|
|
import unittest
|
2014-10-08 18:02:07 +08:00
|
|
|
from operator import itemgetter
|
2014-11-05 18:47:37 +08:00
|
|
|
import os
|
2014-11-22 07:51:20 +08:00
|
|
|
from fractions import Fraction
|
2014-09-24 23:43:22 +08:00
|
|
|
|
2014-09-30 17:38:02 +08:00
|
|
|
from artiq import *
|
2014-11-23 08:56:51 +08:00
|
|
|
from artiq.language.units import *
|
2014-10-19 23:51:49 +08:00
|
|
|
from artiq.coredevice import comm_serial, core, runtime_exceptions, rtio
|
2014-09-24 23:43:22 +08:00
|
|
|
from artiq.sim import devices as sim_devices
|
|
|
|
|
|
|
|
|
2014-11-08 06:15:40 +08:00
|
|
|
no_hardware = bool(os.getenv("ARTIQ_NO_HARDWARE"))
|
2014-11-05 18:47:37 +08:00
|
|
|
|
|
|
|
|
2014-09-24 23:43:22 +08:00
|
|
|
def _run_on_device(k_class, **parameters):
|
2014-10-19 23:51:49 +08:00
|
|
|
with comm_serial.Comm() as comm:
|
|
|
|
coredev = core.Core(comm)
|
2014-09-24 23:43:22 +08:00
|
|
|
k_inst = k_class(core=coredev, **parameters)
|
|
|
|
k_inst.run()
|
|
|
|
|
|
|
|
|
|
|
|
def _run_on_host(k_class, **parameters):
|
|
|
|
coredev = sim_devices.Core()
|
|
|
|
k_inst = k_class(core=coredev, **parameters)
|
|
|
|
k_inst.run()
|
|
|
|
|
|
|
|
|
|
|
|
class _Primes(AutoContext):
|
2014-12-02 17:19:05 +08:00
|
|
|
output_list = Parameter()
|
|
|
|
maximum = Parameter()
|
2014-09-24 23:43:22 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def run(self):
|
2014-12-02 17:19:05 +08:00
|
|
|
for x in range(1, self.maximum):
|
2014-09-24 23:43:22 +08:00
|
|
|
d = 2
|
|
|
|
prime = True
|
|
|
|
while d*d <= x:
|
|
|
|
if x % d == 0:
|
|
|
|
prime = False
|
|
|
|
break
|
|
|
|
d += 1
|
|
|
|
if prime:
|
|
|
|
self.output_list.append(x)
|
|
|
|
|
|
|
|
|
2014-11-22 07:51:20 +08:00
|
|
|
class _Misc(AutoContext):
|
2014-11-03 18:04:01 +08:00
|
|
|
def build(self):
|
|
|
|
self.input = 84
|
2014-11-22 10:08:14 +08:00
|
|
|
self.inhomogeneous_units = []
|
2014-11-03 18:04:01 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def run(self):
|
2014-11-22 07:51:20 +08:00
|
|
|
self.half_input = self.input//2
|
|
|
|
decimal_fraction = Fraction("1.2")
|
|
|
|
self.decimal_fraction_n = int(decimal_fraction.numerator)
|
|
|
|
self.decimal_fraction_d = int(decimal_fraction.denominator)
|
2014-11-22 10:08:14 +08:00
|
|
|
self.inhomogeneous_units.append(Quantity(1000, "Hz"))
|
|
|
|
self.inhomogeneous_units.append(Quantity(10, "s"))
|
2014-11-03 18:04:01 +08:00
|
|
|
|
2014-11-23 08:56:51 +08:00
|
|
|
@kernel
|
|
|
|
def dimension_error1(self):
|
|
|
|
print(1*Hz + 1*s)
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def dimension_error2(self):
|
|
|
|
print(1*Hz < 1*s)
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def dimension_error3(self):
|
|
|
|
check_unit(1*Hz, "s")
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def dimension_error4(self):
|
|
|
|
delay(10*Hz)
|
|
|
|
|
2014-11-03 18:04:01 +08:00
|
|
|
|
2014-09-24 23:43:22 +08:00
|
|
|
class _PulseLogger(AutoContext):
|
2014-12-02 17:19:05 +08:00
|
|
|
output_list = Parameter()
|
|
|
|
name = Parameter()
|
2014-09-24 23:43:22 +08:00
|
|
|
|
2014-10-08 18:02:07 +08:00
|
|
|
def _append(self, t, l, f):
|
|
|
|
if not hasattr(self, "first_timestamp"):
|
|
|
|
self.first_timestamp = t
|
|
|
|
self.output_list.append((self.name, t-self.first_timestamp, l, f))
|
2014-09-24 23:43:22 +08:00
|
|
|
|
2014-10-08 18:02:07 +08:00
|
|
|
def on(self, t, f):
|
|
|
|
self._append(t, True, f)
|
|
|
|
|
|
|
|
def off(self, t):
|
|
|
|
self._append(t, False, 0)
|
2014-09-24 23:43:22 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def pulse(self, f, duration):
|
2014-10-08 18:02:07 +08:00
|
|
|
self.on(int(now().amount*1000000000), f)
|
2014-09-24 23:43:22 +08:00
|
|
|
delay(duration)
|
2014-10-08 18:02:07 +08:00
|
|
|
self.off(int(now().amount*1000000000))
|
2014-09-24 23:43:22 +08:00
|
|
|
|
|
|
|
|
2014-09-26 23:45:09 +08:00
|
|
|
class _Pulses(AutoContext):
|
2014-12-02 17:19:05 +08:00
|
|
|
output_list = Parameter()
|
2014-10-08 18:02:07 +08:00
|
|
|
|
2014-09-24 23:43:22 +08:00
|
|
|
def build(self):
|
|
|
|
for name in "a", "b", "c", "d":
|
|
|
|
pl = _PulseLogger(self, name=name)
|
|
|
|
setattr(self, name, pl)
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def run(self):
|
|
|
|
for i in range(3):
|
|
|
|
with parallel:
|
|
|
|
with sequential:
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2014-09-26 23:45:09 +08:00
|
|
|
class _MyException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class _Exceptions(AutoContext):
|
2014-12-02 17:19:05 +08:00
|
|
|
trace = Parameter()
|
2014-09-26 23:45:09 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def run(self):
|
|
|
|
for i in range(10):
|
|
|
|
self.trace.append(i)
|
|
|
|
if i == 4:
|
|
|
|
try:
|
|
|
|
self.trace.append(10)
|
|
|
|
try:
|
|
|
|
self.trace.append(11)
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.trace.append(12)
|
|
|
|
try:
|
|
|
|
self.trace.append(13)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
except _MyException:
|
|
|
|
self.trace.append(14)
|
|
|
|
|
|
|
|
for i in range(4):
|
|
|
|
try:
|
|
|
|
self.trace.append(100)
|
|
|
|
if i == 1:
|
|
|
|
raise _MyException
|
|
|
|
elif i == 2:
|
|
|
|
raise IndexError
|
|
|
|
except (TypeError, IndexError):
|
|
|
|
self.trace.append(101)
|
|
|
|
raise
|
|
|
|
except:
|
|
|
|
self.trace.append(102)
|
|
|
|
else:
|
|
|
|
self.trace.append(103)
|
|
|
|
finally:
|
|
|
|
self.trace.append(104)
|
|
|
|
|
|
|
|
|
2014-11-08 06:15:40 +08:00
|
|
|
@unittest.skipIf(no_hardware, "no hardware")
|
2014-11-03 18:04:01 +08:00
|
|
|
class ExecutionCase(unittest.TestCase):
|
2014-09-24 23:43:22 +08:00
|
|
|
def test_primes(self):
|
|
|
|
l_device, l_host = [], []
|
2014-12-02 17:19:05 +08:00
|
|
|
_run_on_device(_Primes, maximum=100, output_list=l_device)
|
|
|
|
_run_on_host(_Primes, maximum=100, output_list=l_host)
|
2014-09-24 23:43:22 +08:00
|
|
|
self.assertEqual(l_device, l_host)
|
|
|
|
|
2014-11-22 07:51:20 +08:00
|
|
|
def test_misc(self):
|
2014-11-03 18:04:01 +08:00
|
|
|
with comm_serial.Comm() as comm:
|
|
|
|
coredev = core.Core(comm)
|
2014-11-22 07:51:20 +08:00
|
|
|
uut = _Misc(core=coredev)
|
2014-11-03 18:04:01 +08:00
|
|
|
uut.run()
|
2014-11-22 07:51:20 +08:00
|
|
|
self.assertEqual(uut.half_input, 42)
|
|
|
|
self.assertEqual(Fraction(uut.decimal_fraction_n,
|
|
|
|
uut.decimal_fraction_d),
|
|
|
|
Fraction("1.2"))
|
2014-11-22 10:08:14 +08:00
|
|
|
self.assertEqual(uut.inhomogeneous_units, [
|
|
|
|
Quantity(1000, "Hz"), Quantity(10, "s")])
|
2014-11-23 08:56:51 +08:00
|
|
|
with self.assertRaises(DimensionError):
|
|
|
|
uut.dimension_error1()
|
|
|
|
with self.assertRaises(DimensionError):
|
|
|
|
uut.dimension_error2()
|
|
|
|
with self.assertRaises(DimensionError):
|
|
|
|
uut.dimension_error3()
|
|
|
|
with self.assertRaises(DimensionError):
|
|
|
|
uut.dimension_error4()
|
2014-11-03 18:04:01 +08:00
|
|
|
|
2014-09-24 23:43:22 +08:00
|
|
|
def test_pulses(self):
|
2014-10-08 18:02:07 +08:00
|
|
|
l_device, l_host = [], []
|
|
|
|
_run_on_device(_Pulses, output_list=l_device)
|
|
|
|
_run_on_host(_Pulses, output_list=l_host)
|
|
|
|
l_host = sorted(l_host, key=itemgetter(1))
|
|
|
|
for channel in "a", "b", "c", "d":
|
|
|
|
c_device = [x for x in l_device if x[0] == channel]
|
|
|
|
c_host = [x for x in l_host if x[0] == channel]
|
|
|
|
self.assertEqual(c_device, c_host)
|
2014-09-26 23:45:09 +08:00
|
|
|
|
|
|
|
def test_exceptions(self):
|
|
|
|
t_device, t_host = [], []
|
|
|
|
with self.assertRaises(IndexError):
|
|
|
|
_run_on_device(_Exceptions, trace=t_device)
|
|
|
|
with self.assertRaises(IndexError):
|
|
|
|
_run_on_host(_Exceptions, trace=t_host)
|
|
|
|
self.assertEqual(t_device, t_host)
|
2014-09-24 23:43:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
class _RTIOLoopback(AutoContext):
|
2014-12-02 17:19:05 +08:00
|
|
|
i = Device("ttl_in")
|
|
|
|
o = Device("ttl_out")
|
|
|
|
npulses = Parameter()
|
2014-09-24 23:43:22 +08:00
|
|
|
|
|
|
|
def report(self, n):
|
|
|
|
self.result = n
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def run(self):
|
|
|
|
with parallel:
|
|
|
|
with sequential:
|
|
|
|
for i in range(self.npulses):
|
|
|
|
delay(25*ns)
|
|
|
|
self.o.pulse(25*ns)
|
2014-09-30 16:42:07 +08:00
|
|
|
self.i.gate_rising(10*us)
|
|
|
|
self.report(self.i.count())
|
2014-09-24 23:43:22 +08:00
|
|
|
|
|
|
|
|
2014-09-25 12:57:52 +08:00
|
|
|
class _RTIOUnderflow(AutoContext):
|
2014-12-02 17:19:05 +08:00
|
|
|
o = Device("ttl_out")
|
2014-09-25 12:57:52 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def run(self):
|
2014-09-26 17:21:51 +08:00
|
|
|
while True:
|
2014-09-25 12:57:52 +08:00
|
|
|
delay(25*ns)
|
|
|
|
self.o.pulse(25*ns)
|
|
|
|
|
|
|
|
|
2014-09-30 19:32:11 +08:00
|
|
|
class _RTIOSequenceError(AutoContext):
|
2014-12-02 17:19:05 +08:00
|
|
|
o = Device("ttl_out")
|
2014-09-30 19:32:11 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def run(self):
|
|
|
|
t = now()
|
|
|
|
self.o.pulse(25*us)
|
|
|
|
at(t)
|
|
|
|
self.o.pulse(25*us)
|
|
|
|
|
|
|
|
|
2014-11-08 06:15:40 +08:00
|
|
|
@unittest.skipIf(no_hardware, "no hardware")
|
2014-09-24 23:43:22 +08:00
|
|
|
class RTIOCase(unittest.TestCase):
|
2014-11-08 06:15:40 +08:00
|
|
|
# Connect channels 0 and 1 together for this test
|
|
|
|
# (C11 and C13 on Papilio Pro)
|
2014-09-24 23:43:22 +08:00
|
|
|
def test_loopback(self):
|
|
|
|
npulses = 4
|
2014-10-19 23:51:49 +08:00
|
|
|
with comm_serial.Comm() as comm:
|
|
|
|
coredev = core.Core(comm)
|
2014-09-25 12:57:52 +08:00
|
|
|
uut = _RTIOLoopback(
|
2014-09-24 23:43:22 +08:00
|
|
|
core=coredev,
|
2014-10-19 23:51:49 +08:00
|
|
|
i=rtio.RTIOIn(core=coredev, channel=0),
|
2014-11-30 15:50:57 +08:00
|
|
|
o=rtio.RTIOOut(core=coredev, channel=2),
|
2014-09-24 23:43:22 +08:00
|
|
|
npulses=npulses
|
|
|
|
)
|
2014-09-25 12:57:52 +08:00
|
|
|
uut.run()
|
|
|
|
self.assertEqual(uut.result, npulses)
|
|
|
|
|
|
|
|
def test_underflow(self):
|
2014-10-19 23:51:49 +08:00
|
|
|
with comm_serial.Comm() as comm:
|
|
|
|
coredev = core.Core(comm)
|
2014-09-25 12:57:52 +08:00
|
|
|
uut = _RTIOUnderflow(
|
|
|
|
core=coredev,
|
2014-11-30 15:50:57 +08:00
|
|
|
o=rtio.RTIOOut(core=coredev, channel=2)
|
2014-09-25 12:57:52 +08:00
|
|
|
)
|
|
|
|
with self.assertRaises(runtime_exceptions.RTIOUnderflow):
|
|
|
|
uut.run()
|
2014-09-30 19:32:11 +08:00
|
|
|
|
|
|
|
def test_sequence_error(self):
|
2014-10-19 23:51:49 +08:00
|
|
|
with comm_serial.Comm() as comm:
|
|
|
|
coredev = core.Core(comm)
|
2014-09-30 19:32:11 +08:00
|
|
|
uut = _RTIOSequenceError(
|
|
|
|
core=coredev,
|
2014-11-30 15:50:57 +08:00
|
|
|
o=rtio.RTIOOut(core=coredev, channel=2)
|
2014-09-30 19:32:11 +08:00
|
|
|
)
|
|
|
|
with self.assertRaises(runtime_exceptions.RTIOSequenceError):
|
|
|
|
uut.run()
|