Permalink
Browse files

[runtime] Latest change.

  • Loading branch information...
ptal committed Aug 22, 2017
1 parent 4cb18f1 commit 201c5dd31e10d1e789fac8a569b30568e9ca1344
@@ -20,10 +20,18 @@
public class Future
{
public SpaceBranch branch;
public SnapshotST snapshotST;
public SnapshotWL snapshotWL;
/// `snapshotST` stores all the single time variables captured in a space statement.
/// These variables are shared among all the branches of the space statement.
/// Note that the branch must have access to these variables only in READ-ONLY (TODO: ensure this property in the bonsai compiler).
public HashMap<String, SingleTimeVar> snapshotST;
/// Snapshot of the labels of the world line variables.
/// This snapshot is shared between all the children nodes, and therefore, the `restore` method of must fulfil the `Shared label property`.
/// This property ensures that a label can be restored an arbitrary number of time and is not modified.
public HashMap<String, Object> snapshotWL;
public Future(SpaceBranch branch, SnapshotST snapshotST, SnapshotWL snapshotWL) {
public Future(SpaceBranch branch, HashMap<String, SingleTimeVar> snapshotST,
SnapshotWL snapshotWL)
{
this.branch = branch;
this.snapshotST = snapshotST;
this.snapshotWL = snapshotWL;
@@ -30,17 +30,11 @@ public ModuleVar(String name, String uid,
this.initValue = initValue;
}
public Object value(int time) {
if (time != 0) {
throw new RuntimeException("Module variables cannot be a stream (variable `" + uid() + "`)");
}
public Object value() {
return ref;
}
public void reset(SpaceEnvironment env) {
ref = initValue.apply(env);
}
public void save(SnapshotWL snapshotWL) {}
public void restore(SpaceEnvironment env, SnapshotWL snapshotWL) {}
}
@@ -0,0 +1,41 @@
// Copyright 2017 Pierre Talbot (IRCAM)
// 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.
package bonsai.runtime.sugarcubes;
import java.util.function.*;
import bonsai.runtime.core.*;
public class SingleTimeVar extends Variable
{
private Object ref;
private Function<SpaceEnvironment, Object> initValue;
private SingleTimeVar(Object ref, String name, String uid,
Function<SpaceEnvironment, Object> initValue)
{
super(name, uid);
this.ref = ref;
this.initValue = initValue;
}
public Object value() {
return ref;
}
public void next() {
/// WRONG!
ref = initValue.apply(env);
}
}

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -64,14 +64,11 @@ public boolean action(Environment e) {
return false;
}
public void futures(SpaceEnvironment env, SnapshotWL snapshotWL) {
SnapshotST snapshotST = new SnapshotST();
for (String varUID: singleTimeClosure) {
Object value = env.var(varUID, 0, Permission.READ);
snapshotST.saveSingleTimeVar(varUID, value);
}
for (int i = branches.size()-1; i >= 0; i--) {
env.pushFuture(new Future(branches.get(i), snapshotST, snapshotWL));
}
public ArrayList<String> singleTimeClosure() {
return singleTimeClosure;
}
public ArrayList<SpaceBranch> branches() {
return branches;
}
}
@@ -20,12 +20,14 @@
import inria.meije.rc.sugarcubes.*;
public class SpaceEnvironment extends Clock {
private HashMap<String, Variable> vars;
private HashMap<String, SingleTimeVar> varsST;
private HashMap<String, StreamVar> varsSS;
private HashMap<String, StreamVar> varsWL;
private HashMap<String, ModuleVar> varsModule;
private HashMap<String, Consumer<SpaceEnvironment>> refsUpdaters;
private ArrayDeque<Future> futures;
private ArrayList<Space> children;
// When we enter a branch of `space`, we depend on the single time variables of the current instantiated snapshot and not the one of the current environment.
private SnapshotST currentSnapshotST;
private boolean inSnapshot;
// This is updated by the statement `stop` and `pause up`.
// We need it because the byte status of SugarCubes cannot be easily extended so the status of these statements will be `STOP` (which stands for pause).
@@ -37,7 +39,11 @@ public SpaceEnvironment(ClockIdentifier clockID,
Program body)
{
super(clockID, anInternalIdentifierGenerator, body);
// vars = new HashMap(); // FIXME, cf. declareVar
varsST = new HashMap();
varsSS = new HashMap();
varsWL = new HashMap();
varsModule = new HashMap();
refsUpdaters = new HashMap();
futures = new ArrayDeque();
children = new ArrayList();
currentSnapshotST = null;
@@ -88,63 +94,95 @@ public void newInstant() {
super.newInstant();
}
/// For each branch of each space, we create a future.
/// See class `Future` for explanations on snapshots.
public void saveFutures() {
SnapshotWL snapshotWL = new SnapshotWL();
for (Variable var : vars().values()) {
var.save(snapshotWL);
}
HashMap<String, Object> snapshotWL = snapshotWL();
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).futures(this, snapshotWL);
Space child = children.get(i);
HashMap<String, SingleTimeVar> snapshotST = snapshotST(child);
ArrayList<SpaceBranch> branches = child.branches();
for (int i = branches.size()-1; i >= 0; i--) {
futures.add(new Future(branches.get(i), snapshotST, snapshotWL));
}
}
children.clear();
}
public void pushFuture(Future future) {
this.futures.add(future);
private HashMap<String, Object> snapshotWL() {
HashMap<String, Object> snapshotWL = new HashMap();
for (StreamVar var : varsWL.values()) {
snapshotWL.put(var.uid(), var.stream().label());
}
return snapshotWL;
}
private Hashmap<String, SingleTimeVar> snapshotST(Space child) {
HashMap<String, SingleTimeVar> snapshotST = new HashMap();
for (String varUID: child.singleTimeClosure()) {
snapshotST.put(varUID, varsST.get(varUID));
}
return snapshotST;
}
// Precondition: !futures.isEmpty()
public void instantiateFuture() {
Future future = futures.pop();
for(Variable var : vars().values()) {
var.restore(this, future.snapshotWL);
for(StreamVar var : varsWL.values()) {
var.stream().restore(future.snapshotWL.get(uid));
}
currentSnapshotST = future.snapshotST;
inSnapshot = true;
HashMap<String, SingleTimeVar> current = varsST;
varsST = future.snapshotST;
future.branch.prepareFor(this);
future.branch.activate(this);
inSnapshot = false;
varsST = current;
futureInstantiated = true;
}
public void pushSpace(Space space) {
children.add(space);
}
public void enterScope(Variable var) {
private <T extends Variable> void enterScope(T var, HashMap<String, T> vars) {
if (var == null) {
throw new RuntimeException("SpaceEnvironment.enterScope: null `var` parameter.");
}
Variable old = vars().put(var.uid(), var);
T old = vars.put(var.uid(), var);
if (old != null) {
throw new RuntimeException(
"SpaceEnvironment.enterScope: The variable `" + var.name() +
"` (uid: `" + var.uid() + "`) is already in scope with the name `" + old.name() + "`.");
}
}
public void exitScope(String uid) {
private <T extends Variable> void exitScope(String uid, Hashmap<String, T> vars) {
if (uid == null) {
throw new RuntimeException("SpaceEnvironment.exitScope: null `uid` parameter.");
}
Variable removed = vars().remove(uid);
Variable removed = vars.remove(uid);
if (removed == null) {
throw new RuntimeException(
"SpaceEnvironment.enterScope: Try to exit the scope of the variable " +
"SpaceEnvironment.exitScope: Try to exit the scope of the variable " +
"with uid: `" + uid + "` but it is not in scope.");
}
}
public void enterScopeST(SingleTimeVar var) { enterScope(var, this.varsST); }
public void enterScopeSS(SingleSpaceVar var) { enterScope(var, this.varsSS); }
public void enterScopeWL(WorldLineVar var) { enterScope(var, this.varsWL); }
public void enterScopeModule(ModuleVar var) { enterScope(var, this.varsModule); }
public void exitScopeST(String uid) { exitScope(uid, this.varsST); }
public void exitScopeSS(String uid) { exitScope(uid, this.varsSS); }
public void exitScopeWL(String uid) { exitScope(uid, this.varsWL); }
public void exitScopeModule(String uid) { exitScope(uid, this.varsModule); }
public void enterScopeRef(String uid, Consumer<SpaceEnvironment> refsUpdater) {
this.refsUpdaters.put(uid, refsUpdater);
}
public void exitScopeRef(String uid) {
this.refsUpdaters.pop(uid);
}
public LatticeVar latticeVar(String name, int time, Permission permission) {
Object value = var(name, time, permission);
return Cast.toLattice(name, value);
@@ -157,12 +195,20 @@ public void checkVarNull(String uid, Variable v) {
}
}
public void checkWrite(String uid, int time) {
if (time != 0) {
throw new RuntimeException("[BUG] The variable `" + uid
+ "` is READ-ONLY because it is surrounded by `pre`.");
}
}
private Object access(String uid, int time, Permission permission) {
Variable v = vars().get(uid);
checkVarNull(uid, v);
// Generate an event on this variable to indicate it might has been modified.
// Note that `generatePure` does not read the new value of the variable yet–it is just used to wake up suspended statements—so it's OK to generate it after the actual modifications.
if (time == 0 && permission != Permission.READ) {
// Note that `generatePure` does not read the new value of the variable yet–it is just used to wake up suspended statements—so it's OK to generate it before the actual modifications.
if (permission != Permission.READ) {
checkWrite(uid, time);
Event event = getDirectAccessToEvent(new StringID(uid));
event.generatePure(this);
}
@@ -180,7 +226,7 @@ public Object var(String uid, int time, Permission permission) {
}
public Object module(String uid) {
return access(uid, 0, Permission.READ);
return varsModule.get(uid);
}
public boolean isEmpty() {
Oops, something went wrong.

0 comments on commit 201c5dd

Please sign in to comment.