forked from M-Labs/artiq
1
0
Fork 0

Threads and parallel block lowering examples

This commit is contained in:
Sebastien Bourdeauducq 2014-05-18 19:28:08 +02:00
parent 8b57e7de01
commit 1a473abecb
2 changed files with 102 additions and 0 deletions

21
examples/threads_test.py Normal file
View File

@ -0,0 +1,21 @@
from artiq.sim import *
from artiq.units import *
def threads_test():
pulse("x", 1*MHz, 10*ms)
with parallel:
with sequential:
wait_edge("a")
delay(5*ms)
pulse("a", 30*MHz, 1*us)
wait_edge("a")
delay(10*ms)
pulse("a", 60*MHz, 1*us)
with sequential:
wait_edge("b")
delay(3*ms)
pulse("b", 30*MHz, 1*us)
if __name__ == "__main__":
threads_test()
print(time_manager.format_timeline())

View File

@ -0,0 +1,81 @@
from random import Random
from collections import namedtuple
Pulse = namedtuple("Pulse", "time output frequency duration")
WaitEdge = namedtuple("WaitEdge", "time input")
class Parallel:
def __init__(self, *threads):
self.threads = threads
def threads_test(now):
yield Pulse(now, "x", 1, 10)
now += 10
def thread0(now):
now = yield WaitEdge(now, "a")
now += 5
yield Pulse(now, "a", 30, 1)
now += 1
now = yield WaitEdge(now, "a")
now += 10
yield Pulse(now, "a", 60, 1)
now += 1
return now
def thread1(now):
now = yield WaitEdge(now, "b")
now += 3
yield Pulse(now, "b", 30, 1)
now += 1
return now
now = yield Parallel(thread0(now), thread1(now))
return now
prng = Random(42)
def execute(threads):
end_time = 0
thread_replies = [None]*len(threads)
continue_execute = True
while continue_execute:
continue_execute = False
new_thread_replies = []
for thread, send_data in zip(threads, thread_replies):
try:
thread_request = thread.send(send_data)
except StopIteration as e:
if e.value > end_time:
end_time = e.value
else:
continue_execute = True
thread_reply = None
if isinstance(thread_request, Pulse):
yield thread_request
elif isinstance(thread_request, WaitEdge):
yield thread_request
edge_delay = prng.randrange(100)
print("*** Simulating input edge on '{}' after {}".format(thread_request.input, edge_delay))
thread_reply = thread_request.time + edge_delay
elif isinstance(thread_request, Parallel):
thread_reply = yield from execute(thread_request.threads)
else:
raise TypeError
new_thread_replies.append(thread_reply)
thread_replies = new_thread_replies
return end_time
if __name__ == "__main__":
ex = execute([threads_test(0)])
while True:
try:
ev = next(ex)
except StopIteration as e:
print("*** Execution ended at {}".format(e.value))
break
else:
print(ev)