Skip to content

Commit bc81be1

Browse files
author
whitequark
committedFeb 22, 2016
Implement dumb 'with parallel' (#265).
1 parent 51a5910 commit bc81be1

File tree

10 files changed

+52
-2
lines changed

10 files changed

+52
-2
lines changed
 

‎artiq/compiler/builtins.py

+3
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ def fn_print():
144144
def fn_kernel():
145145
return types.TBuiltinFunction("kernel")
146146

147+
def obj_parallel():
148+
return types.TBuiltin("parallel")
149+
147150
def obj_interleave():
148151
return types.TBuiltin("interleave")
149152

‎artiq/compiler/prelude.py

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def globals():
3030
"portable": builtins.fn_kernel(),
3131

3232
# ARTIQ context managers
33+
"parallel": builtins.obj_parallel(),
3334
"interleave": builtins.obj_interleave(),
3435
"sequential": builtins.obj_sequential(),
3536
"watchdog": builtins.fn_watchdog(),

‎artiq/compiler/transforms/artiq_ir_generator.py

+22
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,28 @@ def visit_With(self, node):
765765
if not tail.is_terminated():
766766
tail.append(ir.Branch(self.current_block))
767767
return
768+
elif types.is_builtin(context_expr_node.type, "parallel"):
769+
start_mu = self.append(ir.Builtin("now_mu", [], builtins.TInt64()))
770+
end_mu = start_mu
771+
772+
for stmt in node.body:
773+
self.append(ir.Builtin("at_mu", [start_mu], builtins.TNone()))
774+
775+
block = self.add_block()
776+
if self.current_block.is_terminated():
777+
self.warn_unreachable(stmt[0])
778+
else:
779+
self.append(ir.Branch(block))
780+
self.current_block = block
781+
782+
self.visit(stmt)
783+
784+
mid_mu = self.append(ir.Builtin("now_mu", [], builtins.TInt64()))
785+
gt_mu = self.append(ir.Compare(ast.Gt(loc=None), mid_mu, end_mu))
786+
end_mu = self.append(ir.Select(gt_mu, mid_mu, end_mu))
787+
788+
self.append(ir.Builtin("at_mu", [end_mu], builtins.TNone()))
789+
return
768790

769791
cleanup = []
770792
for item_node in node.items:

‎artiq/compiler/transforms/inferencer.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,7 @@ def visit_withitemT(self, node):
994994

995995
typ = node.context_expr.type
996996
if (types.is_builtin(typ, "interleave") or types.is_builtin(typ, "sequential") or
997+
types.is_builtin(typ, "parallel") or
997998
(isinstance(node.context_expr, asttyped.CallT) and
998999
types.is_builtin(node.context_expr.func.type, "watchdog"))):
9991000
# builtin context managers
@@ -1092,8 +1093,8 @@ def visit_With(self, node):
10921093

10931094
for item_node in node.items:
10941095
typ = item_node.context_expr.type.find()
1095-
if (types.is_builtin(typ, "interleave") or types.is_builtin(typ, "sequential")) and \
1096-
len(node.items) != 1:
1096+
if (types.is_builtin(typ, "parallel") or types.is_builtin(typ, "interleave") or
1097+
types.is_builtin(typ, "sequential")) and len(node.items) != 1:
10971098
diag = diagnostic.Diagnostic("error",
10981099
"the '{kind}' context manager must be the only one in a 'with' statement",
10991100
{"kind": typ.name},

‎artiq/test/lit/lit.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ if os.name == "posix":
3434
config.environment["LIBARTIQ_SUPPORT"] = support_lib
3535

3636
config.available_features.add("exceptions")
37+
config.available_features.add("time")

‎artiq/test/lit/time/advance.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: %python -m artiq.compiler.testbench.jit %s
2+
# REQUIRES: time
23

34
assert now() == 0.0
45
delay(100.0)

‎artiq/test/lit/time/advance_mu.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: %python -m artiq.compiler.testbench.jit %s
2+
# REQUIRES: time
23

34
assert now_mu() == 0
45
delay_mu(100)

‎artiq/test/lit/time/conversion.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: %python -m artiq.compiler.testbench.jit %s
2+
# REQUIRES: time
23

34
assert seconds_to_mu(2.0) == 2000000
45
assert mu_to_seconds(1500000) == 1.5

‎artiq/test/lit/time/parallel.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# RUN: %python -m artiq.compiler.testbench.jit %s
2+
# REQUIRES: time
3+
4+
with parallel:
5+
with sequential:
6+
assert now_mu() == 0
7+
delay_mu(10)
8+
assert now_mu() == 10
9+
with sequential:
10+
assert now_mu() == 0
11+
delay_mu(20)
12+
assert now_mu() == 20
13+
with sequential:
14+
assert now_mu() == 0
15+
delay_mu(15)
16+
assert now_mu() == 15
17+
assert now_mu() == 0
18+
assert now_mu() == 20

‎artiq/test/lit/time/watchdog.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# RUN: %python -m artiq.compiler.testbench.jit %s >%t
22
# RUN: OutputCheck %s --file-to-check=%t
3+
# REQUIRES: time
34

45
def f():
56
with watchdog(1.0):

0 commit comments

Comments
 (0)
Please sign in to comment.