Permalink
Browse files

[runtime][compiler] Add support for universe with queue.

  • Loading branch information...
ptal committed Dec 2, 2018
1 parent c25884a commit 588cde870d00a80f1c61c075129376092c1ebb2a
Showing with 412 additions and 70 deletions.
  1. +140 −0 data/test/run-pass/UniverseT.bonsai.java
  2. +9 −1 examples/bonsai/Simple/src/main/java/bonsai/examples/Simple.bonsai.java
  3. +10 −0 runtime/src/main/java/bonsai/runtime/synchronous/ASTNode.java
  4. +8 −5 runtime/src/main/java/bonsai/runtime/synchronous/SpaceMachine.java
  5. +3 −0 runtime/src/main/java/bonsai/runtime/synchronous/env/Environment.java
  6. +9 −0 runtime/src/main/java/bonsai/runtime/synchronous/env/Layer.java
  7. +5 −0 runtime/src/main/java/bonsai/runtime/synchronous/interfaces/Statement.java
  8. +4 −0 runtime/src/main/java/bonsai/runtime/synchronous/statements/ConjunctivePar.java
  9. +5 −0 runtime/src/main/java/bonsai/runtime/synchronous/statements/Delay.java
  10. +4 −0 runtime/src/main/java/bonsai/runtime/synchronous/statements/DisjunctivePar.java
  11. +5 −0 runtime/src/main/java/bonsai/runtime/synchronous/statements/Nothing.java
  12. +5 −0 runtime/src/main/java/bonsai/runtime/synchronous/statements/ProcedureCall.java
  13. +22 −8 runtime/src/main/java/bonsai/runtime/synchronous/statements/QFUniverse.java
  14. +22 −1 runtime/src/main/java/bonsai/runtime/synchronous/statements/Sequence.java
  15. +5 −0 runtime/src/main/java/bonsai/runtime/synchronous/statements/SpaceStmt.java
  16. +31 −13 runtime/src/main/java/bonsai/runtime/synchronous/statements/Universe.java
  17. +9 −0 runtime/src/main/java/bonsai/runtime/synchronous/statements/VarDecl.java
  18. +4 −0 src/back/compiler/expression.rs
  19. +41 −38 src/back/compiler/statement.rs
  20. +66 −0 src/back/free_variables.rs
  21. +1 −0 src/back/mod.rs
  22. +4 −4 tests/test_bonsai.rs
@@ -0,0 +1,140 @@
// Copyright 2018 Pierre Talbot
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#[run(UniverseT.printNothing, "")]
#[run(UniverseT.printNothing2, "")]
#[run(UniverseT.oneChildImmediate, "")]
#[run(UniverseT.oneNothingChild, "1")]
#[run(UniverseT.onePrintChild, "12")]
#[run(UniverseT.onePrintChild2, "1234")]
#[debug(UniverseT.twoPrintChild, "123456")]
#[debug(UniverseT.successiveChild, "123456")]
#[debug(UniverseT.successiveBinaryChild, "11a22a32b41b56")]
package test;
import java.lang.System;
import java.util.*;
import bonsai.runtime.queueing.*;
public class UniverseT
{
public proc printNothing() =
single_space StackLR stack = new StackLR();
universe with stack in
nothing
end
end
public proc printNothing2() =
single_space StackLR stack = new StackLR();
universe with stack in
pause;
System.out.print("unreachable");
end
end
public proc oneChildImmediate() =
single_space StackLR stack = new StackLR();
universe with stack in
space nothing end;
end
end
public proc oneNothingChild() =
single_space StackLR stack = new StackLR();
universe with stack in
space nothing end;
pause;
System.out.print("1");
end
end
public proc onePrintChild() =
single_space StackLR stack = new StackLR();
universe with stack in
space System.out.print("2") end;
System.out.print("1");
pause;
end
end
public proc onePrintChild2() =
single_space StackLR stack = new StackLR();
universe with stack in
space System.out.print("2") end;
System.out.print("1");
pause;
System.out.print("3");
pause;
System.out.print("unreachable");
end;
System.out.print("4")
end
public proc twoPrintChild() =
single_space StackLR stack = new StackLR();
universe with stack in
space System.out.print("2") end;
space System.out.print("4") end;
System.out.print("1");
pause;
System.out.print("3");
pause;
System.out.print("5");
pause;
System.out.print("unreachable");
end;
System.out.print("6")
end
public proc successiveChild() =
single_space StackLR stack = new StackLR();
universe with stack in
space System.out.print("2") end;
System.out.print("1");
pause;
space System.out.print("4") end;
System.out.print("3");
pause;
System.out.print("5");
pause;
System.out.print("unreachable");
end;
System.out.print("6")
end
public proc successiveBinaryChild() =
single_space StackLR stack = new StackLR();
universe with stack in
space System.out.print("1a") end;
space System.out.print("1b") end;
System.out.print("1");
pause;
space System.out.print("2a") end;
space System.out.print("2b") end;
System.out.print("2");
pause;
System.out.print("3");
pause;
System.out.print("4");
pause;
System.out.print("5");
pause;
System.out.print("unreachable");
end;
System.out.print("6")
end
}
@@ -14,9 +14,17 @@
package bonsai.examples;
import java.lang.System;
import java.util.*;
import bonsai.runtime.queueing.*;
public class Simple
{
public proc test() = nothing
public proc test() =
single_space StackLR stack = new StackLR();
universe with stack in
space nothing end;
pause;
end
end
}
@@ -49,4 +49,14 @@ protected void hasNoSubLayer(String from) {
protected static <T extends Copy> List<T> copyList(List<T> nodes) {
return nodes.stream().map(v -> (T)v.copy()).collect(Collectors.toList());
}
protected void checkNonTerminatedEOI(String nameStmt, CompletionCode k) {
if (k == CompletionCode.TERMINATE) {
throwNonTerminatedEOI(nameStmt);
}
}
protected void throwNonTerminatedEOI(String nameStmt) throws RuntimeException {
throw new RuntimeException("[BUG] `" + nameStmt + "` should not be active at the end of instant.");
}
}
@@ -49,7 +49,7 @@ private StmtResult executeLayer() {
int targetIdx = env.targetIdx();
StmtResult res = new StmtResult(CompletionCode.PAUSE);
while (res.k == CompletionCode.PAUSE) {
popQueues(targetIdx);
popQueues(targetIdx, layer);
program.canInstant(targetIdx, layer);
// We execute as much as we can of the current instant.
res = executeInstant(targetIdx, layer);
@@ -66,6 +66,9 @@ private StmtResult executeLayer() {
}
}
pushQueues(res);
if(res.k != CompletionCode.TERMINATE) {
res.k = program.endOfInstant(targetIdx, layer);
}
}
env.decTargetLayer();
return res;
@@ -77,18 +80,18 @@ private StmtResult executeLayer() {
// 2. We execute the program "p1 || p2 || ... || pn" where "pi" represents one future.
// They can communicate on single space variables.
// Since the captured space contains the same pointer to `Variable` than the current layer, the values are automatically updated in the layer.
private void popQueues(int layersRemaining) {
private void popQueues(int layersRemaining, Layer layer) {
HashSet<String> queues = program.activeQueues(layersRemaining);
if (!queues.isEmpty()) {
List<Future> futures = env.pop(queues);
Future future = Future.merge(futures);
future.space.restore();
Statement mainProgram = program;
program = future.body;
Layer layer = new Layer(future.space);
Layer encapsulatedLayer = new Layer(future.space);
program.prepare();
program.canInstant(0, layer);
StmtResult res = executeInstant(0, layer);
program.canInstant(0, encapsulatedLayer);
StmtResult res = executeInstant(0, encapsulatedLayer);
program = mainProgram;
if (res.k != CompletionCode.TERMINATE) {
throw new RuntimeException("A space statement did not terminate. (code: " + res.k + ")");
@@ -35,6 +35,9 @@ public Environment(int numLayers)
for(int i=0; i < numLayers; i++) {
layers.add(new Layer());
}
for(int i=1; i <numLayers; i++) {
layers.get(i).setParent(layers.get(i-1));
}
targetIdx = OUTERMOST_LAYER;
}
@@ -23,6 +23,7 @@
public class Layer
{
private Layer parent;
private Space space;
private Scheduler scheduler;
private Optional<String> currentQueue;
@@ -39,6 +40,14 @@ public Layer(Space space) {
this.space = space;
}
public void setParent(Layer layer) {
this.parent = layer;
}
public Layer parent() {
return parent;
}
public Variable lookUpVar(String uid) {
return space.lookUpVar(uid);
}
@@ -67,6 +67,11 @@
/// - It is not idempotent: it can be viewed as an internal transition of the statement from the current state to the next state.
StmtResult execute(int layersRemaining, Layer layer);
/// This function is called at the end of the instant, after we pushed the nodes onto the queue.
/// Purpose:
/// - Take into account queues that are empty to terminate universes.
CompletionCode endOfInstant(int layersRemaining, Layer layer);
/// We analyse the current program to prove that a write on `uid` still can happen.
/// We suppose the conditions currently suspended on `uid` to be `false` (if `inSurface` is `true`).
/// `inSurface` is `true` if the flow of control is on the statement currently analysed.
@@ -52,6 +52,10 @@ public void canInstant(int layersRemaining, Layer layer) {
throw new RuntimeException("ConjunctivePar.activeQueues: unimplemented.");
}
public CompletionCode endOfInstant(int layersRemaining, Layer layer) {
throw new RuntimeException("ConjunctivePar.terminateEmptyQueue: unimplemented.");
}
public boolean canTerminate() {
throw new RuntimeException("ConjunctivePar.canTerminate: unimplemented.");
}
@@ -49,6 +49,11 @@ public void canInstant(int layersRemaining, Layer layer) {
return new HashSet();
}
public CompletionCode endOfInstant(int layersRemaining, Layer layer) {
checkNonTerminatedEOI("delay", k);
return k;
}
public boolean canTerminate() {
return k == CompletionCode.TERMINATE || (k == kind && nextInstant);
}
@@ -52,6 +52,10 @@ public void canInstant(int layersRemaining, Layer layer) {
throw new RuntimeException("DisjunctivePar.activeQueues: unimplemented.");
}
public CompletionCode endOfInstant(int layersRemaining, Layer layer) {
throw new RuntimeException("DisjunctivePar.terminateEmptyQueue: unimplemented.");
}
public boolean canTerminate() {
throw new RuntimeException("DisjunctivePar.canTerminate: unimplemented.");
}
@@ -40,6 +40,11 @@ public void canInstant(int layersRemaining, Layer layer) {
return new HashSet();
}
public CompletionCode endOfInstant(int layersRemaining, Layer layer) {
throwNonTerminatedEOI("nothing");
return CompletionCode.TERMINATE;
}
public boolean canTerminate() {
return true;
}
@@ -50,6 +50,11 @@ public void canInstant(int layersRemaining, Layer layer) {
return new HashSet();
}
public CompletionCode endOfInstant(int layersRemaining, Layer layer) {
throwNonTerminatedEOI("call");
return CompletionCode.TERMINATE;
}
public boolean canTerminate() {
return true;
}
@@ -25,7 +25,7 @@
{
protected final Statement body;
protected StmtResult bodyRes;
protected CompletionCode k;
protected StmtResult res;
public QFUniverse(Statement body) {
super();
@@ -38,13 +38,17 @@ public QFUniverse copy() {
}
public void prepare() {
k = CompletionCode.PAUSE_DOWN;
prepareInstant();
}
public void prepareInstant() {
res = new StmtResult(CompletionCode.PAUSE_DOWN);
bodyRes = new StmtResult(CompletionCode.WAIT);
}
public void canInstant(int layersRemaining, Layer layer) {
if (layersRemaining == 0) {
prepare();
prepareInstant();
}
else {
body.canInstant(layersRemaining - 1, layer);
@@ -60,12 +64,22 @@ public void canInstant(int layersRemaining, Layer layer) {
}
}
public CompletionCode endOfInstant(int layersRemaining, Layer layer) {
checkNonTerminatedEOI("universe p end", res.k);
if(layersRemaining == 0) {
return res.k;
}
else {
return body.endOfInstant(layersRemaining - 1, layer);
}
}
public boolean canTerminate() {
return body.canTerminate();
}
public void abort(Layer layer) {
k = CompletionCode.TERMINATE;
res = new StmtResult(CompletionCode.TERMINATE);
bodyRes = new StmtResult(CompletionCode.TERMINATE);
}
@@ -75,11 +89,11 @@ public StmtResult execute(int layersRemaining, Layer layer){
if (layersRemaining == 0) {
// Promote the completion code of the body.
switch(bodyRes.k) {
case PAUSE_UP: k = CompletionCode.PAUSE; break;
case STOP: k = CompletionCode.STOP; break;
case TERMINATE: k = CompletionCode.TERMINATE; break;
case PAUSE_UP: res.k = CompletionCode.PAUSE; break;
case STOP: res.k = CompletionCode.STOP; break;
case TERMINATE: res.k = CompletionCode.TERMINATE; break;
}
return new StmtResult(k);
return res;
}
else {
bodyRes = body.execute(layersRemaining - 1, layer);
Oops, something went wrong.

0 comments on commit 588cde8

Please sign in to comment.