use of gov.sandia.n2a.language.function.Delay in project n2a by frothga.
the class JobC method generateDeclarationsLocal.
public void generateDeclarationsLocal(EquationSet s, StringBuilder result) {
BackendDataC bed = (BackendDataC) s.backendData;
// Unit class
result.append("class " + prefix(s) + " : public PartTime<" + T + ">\n");
result.append("{\n");
result.append("public:\n");
// Unit buffers
if (bed.needLocalDerivative) {
result.append(" class Derivative\n");
result.append(" {\n");
result.append(" public:\n");
for (Variable v : bed.localDerivative) {
result.append(" " + type(v) + " " + mangle(v) + ";\n");
}
result.append(" Derivative * next;\n");
result.append(" };\n");
result.append("\n");
}
if (bed.needLocalPreserve) {
result.append(" class Preserve\n");
result.append(" {\n");
result.append(" public:\n");
for (Variable v : bed.localIntegrated) {
result.append(" " + type(v) + " " + mangle(v) + ";\n");
}
for (Variable v : bed.localDerivativePreserve) {
result.append(" " + type(v) + " " + mangle(v) + ";\n");
}
for (Variable v : bed.localBufferedExternalWriteDerivative) {
result.append(" " + type(v) + " " + mangle("next_", v) + ";\n");
}
result.append(" };\n");
result.append("\n");
}
// Unit variables
if (bed.needLocalDerivative) {
result.append(" Derivative * stackDerivative;\n");
}
if (bed.needLocalPreserve) {
result.append(" Preserve * preserve;\n");
}
if (bed.pathToContainer == null) {
result.append(" " + prefix(s.container) + " * container;\n");
}
if (s.connectionBindings != null) {
for (ConnectionBinding c : s.connectionBindings) {
// we should be able to assume that s.container is non-null; ie: a connection should always operate in some larger container
result.append(" " + prefix(c.endpoint) + " * " + mangle(c.alias) + ";\n");
}
}
if (s.accountableConnections != null) {
for (EquationSet.AccountableConnection ac : s.accountableConnections) {
result.append(" int " + prefix(ac.connection) + "_" + mangle(ac.alias) + "_count;\n");
}
}
if (bed.refcount) {
result.append(" int refcount;\n");
}
if (bed.index != null) {
result.append(" int __24index;\n");
}
if (bed.lastT) {
// $lastT is for internal use only, so no need for __24 prefix.
result.append(" " + T + " lastT;\n");
}
for (Variable v : bed.localMembers) {
result.append(" " + type(v) + " " + mangle(v) + ";\n");
}
for (Variable v : bed.localBufferedExternal) {
result.append(" " + type(v) + " " + mangle("next_", v) + ";\n");
}
for (EquationSet p : s.parts) {
result.append(" " + prefix(p) + "_Population " + mangle(p.name) + ";\n");
}
for (String columnName : bed.localColumns) {
result.append(" String " + columnName + ";\n");
}
for (EventSource es : bed.eventSources) {
String eventMonitor = "eventMonitor_" + prefix(es.target.container);
if (es.monitorIndex > 0)
eventMonitor += "_" + es.monitorIndex;
result.append(" std::vector<Part<" + T + "> *> " + eventMonitor + ";\n");
}
for (EventTarget et : bed.eventTargets) {
if (!et.trackOne && et.edge != EventTarget.NONZERO) {
result.append(" " + T + " " + mangle(et.track.name) + ";\n");
}
if (et.timeIndex >= 0) {
result.append(" " + T + " eventTime" + et.timeIndex + ";\n");
}
}
if (!bed.localFlagType.isEmpty()) {
result.append(" " + bed.localFlagType + " flags;\n");
}
int i = 0;
for (Delay d : bed.delays) {
d.index = i++;
result.append(" DelayBuffer<" + T + "> delay" + d.index + ";\n");
}
result.append("\n");
// Unit functions
if (bed.needLocalCtor) {
result.append(" " + prefix(s) + " ();\n");
}
if (bed.needLocalDtor) {
result.append(" virtual ~" + prefix(s) + " ();\n");
}
if (bed.localMembers.size() > 0) {
result.append(" virtual void clear ();\n");
}
if (s.container == null) {
result.append(" virtual void setPeriod (" + T + " dt);\n");
}
if (bed.needLocalDie) {
result.append(" virtual void die ();\n");
}
if (bed.localReference.size() > 0) {
result.append(" virtual void enterSimulation ();\n");
}
result.append(" virtual void leaveSimulation ();\n");
if (bed.refcount) {
result.append(" virtual bool isFree ();\n");
}
if (bed.needLocalInit) {
result.append(" virtual void init ();\n");
}
if (bed.needLocalIntegrate) {
result.append(" virtual void integrate ();\n");
}
if (bed.needLocalUpdate) {
result.append(" virtual void update ();\n");
}
if (bed.needLocalFinalize) {
result.append(" virtual bool finalize ();\n");
}
if (bed.needLocalUpdateDerivative) {
result.append(" virtual void updateDerivative ();\n");
}
if (bed.needLocalFinalizeDerivative) {
result.append(" virtual void finalizeDerivative ();\n");
}
if (bed.needLocalPreserve) {
result.append(" virtual void snapshot ();\n");
result.append(" virtual void restore ();\n");
}
if (bed.needLocalDerivative) {
result.append(" virtual void pushDerivative ();\n");
result.append(" virtual void multiplyAddToStack (" + T + " scalar);\n");
result.append(" virtual void multiply (" + T + " scalar);\n");
result.append(" virtual void addToMembers ();\n");
}
if (bed.live != null && !bed.live.hasAttribute("constant")) {
result.append(" virtual " + T + " getLive ();\n");
}
if (bed.xyz != null && s.connected) {
result.append(" virtual void getXYZ (MatrixFixed<" + T + ",3,1> & xyz);\n");
}
if (s.connectionBindings != null) {
if (bed.p != null) {
result.append(" virtual " + T + " getP ();\n");
}
if (bed.hasProject) {
result.append(" virtual void getProject (int i, MatrixFixed<" + T + ",3,1> & xyz);\n");
}
result.append(" virtual void setPart (int i, Part<" + T + "> * part);\n");
result.append(" virtual Part<" + T + "> * getPart (int i);\n");
}
if (bed.newborn >= 0) {
result.append(" virtual bool getNewborn ();\n");
}
if (s.connectionMatrix != null && s.connectionMatrix.needsMapping) {
result.append(" virtual int mapIndex (int i, int rc);\n");
}
if (bed.eventTargets.size() > 0) {
result.append(" virtual bool eventTest (int i);\n");
if (bed.needLocalEventDelay) {
result.append(" virtual " + T + " eventDelay (int i);\n");
}
result.append(" virtual void setLatch (int i);\n");
if (bed.eventReferences.size() > 0) {
result.append(" virtual void finalizeEvent ();\n");
}
}
if (bed.accountableEndpoints.size() > 0) {
result.append(" virtual int getCount (int i);\n");
}
if (bed.needLocalPath) {
result.append(" virtual void path (String & result);\n");
}
// Conversions
Set<Conversion> conversions = s.getConversions();
for (Conversion pair : conversions) {
EquationSet source = pair.from;
EquationSet dest = pair.to;
result.append(" void " + mangle(source.name) + "_2_" + mangle(dest.name) + " (" + mangle(source.name) + " * from, int " + mangle("$type") + ");\n");
}
// Unit class trailer
result.append("};\n");
result.append("\n");
}
use of gov.sandia.n2a.language.function.Delay in project n2a by frothga.
the class InternalBackendData method analyzeEvents.
/**
* Find event() calls and collate them (in case the same signature appears several different places
* in the equation set).
* This must be done before the variables are sorted into sets according to attributes, because we
* may need to add the "externalRead" attribute to some of them.
*/
public void analyzeEvents(EquationSet s) {
analyzeEvents(s, eventTargets, eventReferences, delays);
// Allocate storage for Event
int valueIndex = -1;
int mask = 0;
int eventIndex = 0;
for (EventTarget et : eventTargets) {
if (valueIndex == -1) {
valueIndex = countLocalFloat++;
namesLocalFloat.add("eventLatch" + valueIndex);
eventLatches.add(valueIndex);
mask = 1;
}
et.valueIndex = valueIndex;
et.mask = mask;
mask <<= 1;
// Due to limitations of float-int conversion, only 23 bits are available. Allocate another float.
if (mask > 0x400000)
valueIndex = -1;
if (// We have an auxiliary variable.
!et.trackOne && et.edge != EventTarget.NONZERO) {
et.track.readIndex = et.track.writeIndex = allocateLocalFloat(et.track.name);
// Preemptively add this, because the main analyze() routine won't see it.
// We put the aux in init so that it can pick up the initial value without a call
// to EventTarget.test(). Note that dependencies have been set when the aux was
// created, so it will execute in the correct order with all the other init variables.
localInit.add(et.track);
}
// the same part. Since #2 is already a rare case, we won't worry about trapping this.
if (et.edge == EventTarget.NONZERO && (et.sources.size() > 1 || et.sources.get(0).container == et.container && (et.delay == 0 || et.delay == -2))) {
et.timeIndex = allocateLocalFloat("eventTime" + eventIndex);
}
// TODO: What if two different event targets in this part reference the same source part? What if the condition is different? The same?
for (EventSource es : et.sources) {
EquationSet sourceContainer = es.container;
InternalBackendData sourceBed = (InternalBackendData) sourceContainer.backendData;
// TODO: Consolidate monitors that share the same trigger condition.
es.monitorIndex = sourceBed.allocateLocalObject("eventMonitor_" + s.prefix());
sourceBed.eventSources.add(es);
}
eventIndex++;
}
// Allocate storage for Delay
int i = 0;
for (Delay d : delays) {
d.index = allocateLocalObject("delay" + i++);
}
}
use of gov.sandia.n2a.language.function.Delay in project n2a by frothga.
the class RendererC method render.
public boolean render(Operator op) {
for (ProvideOperator po : job.extensions) {
Boolean result = po.render(this, op);
if (result != null)
return result;
}
if (op instanceof Add) {
Add a = (Add) op;
// Check if this is a string expression
if (a.name != null) {
result.append(a.name);
return true;
}
return false;
}
if (op instanceof AccessElement) {
AccessElement ae = (AccessElement) op;
ae.operands[0].render(this);
if (ae.operands.length == 2) {
result.append("[");
ae.operands[1].render(this);
result.append("]");
} else if (ae.operands.length == 3) {
result.append("(");
ae.operands[1].render(this);
result.append(",");
ae.operands[2].render(this);
result.append(")");
}
return true;
}
if (op instanceof AccessVariable) {
AccessVariable av = (AccessVariable) op;
result.append(job.resolve(av.reference, this, false));
return true;
}
if (op instanceof Atan) {
Atan atan = (Atan) op;
int shift = atan.exponent - atan.exponentNext;
if (useExponent && shift != 0)
result.append("(");
if (atan.operands.length > 1 || useExponent)
result.append("atan2 (");
else
result.append("atan (");
Operator y = atan.operands[0];
if (y.getType() instanceof Matrix) {
y.render(this);
result.append("[1], ");
y.render(this);
result.append("[0]");
} else {
y.render(this);
if (atan.operands.length > 1) {
result.append(", ");
// x
atan.operands[1].render(this);
} else if (useExponent) {
int shiftX = Operator.MSB - y.exponent;
int x = shiftX >= 0 ? 0x1 << shiftX : 0;
result.append(", " + x);
}
}
result.append(")");
if (useExponent && shift != 0)
result.append(printShift(shift) + ")");
return true;
}
if (op instanceof BuildMatrix) {
BuildMatrix b = (BuildMatrix) op;
result.append(b.name);
return true;
}
if (op instanceof Columns) {
Columns c = (Columns) op;
c.operands[0].render(this);
result.append(".columns ()");
return true;
}
if (op instanceof Constant) {
Constant c = (Constant) op;
Type o = c.value;
if (o instanceof Scalar) {
result.append(print(((Scalar) o).value, c.exponentNext));
return true;
}
if (o instanceof Text) {
result.append("\"" + o.toString() + "\"");
return true;
}
if (o instanceof Matrix) {
result.append(c.name);
return true;
}
return false;
}
if (op instanceof Delay) {
Delay d = (Delay) op;
if (d.operands.length == 1) {
result.append("(");
d.operands[0].render(this);
result.append(")");
return true;
}
result.append("delay" + d.index + ".step (Simulator<" + job.T + ">::instance.currentEvent->t, ");
d.operands[1].render(this);
result.append(", ");
d.operands[0].render(this);
result.append(", ");
if (d.operands.length > 2)
d.operands[2].render(this);
else
result.append("0");
result.append(")");
return true;
}
if (op instanceof Event) {
Event e = (Event) op;
// The cast to bool gets rid of the specific numeric value from flags.
// If used in a numeric expression, it should convert to either 1 or 0.
result.append("((bool) (flags & (" + bed.localFlagType + ") 0x1 << " + e.eventType.valueIndex + "))");
return true;
}
if (op instanceof Exp) {
Exp e = (Exp) op;
Operator a = e.operands[0];
result.append("exp (");
a.render(this);
if (useExponent)
result.append(", " + e.exponentNext);
result.append(")");
return true;
}
if (op instanceof Gaussian) {
Gaussian g = (Gaussian) op;
result.append("gaussian<" + job.T + "> (");
if (g.operands.length > 0) {
g.operands[0].render(this);
}
result.append(")");
return true;
}
if (op instanceof Grid) {
Grid g = (Grid) op;
boolean raw = g.operands.length >= 5 && g.operands[4].getString().contains("raw");
int shift = g.exponent - g.exponentNext;
if (useExponent && shift != 0)
result.append("(");
result.append("grid");
if (raw)
result.append("Raw");
result.append("<" + job.T + "> (");
int count = Math.min(4, g.operands.length);
if (count > 0)
g.operands[0].render(this);
for (int i = 1; i < count; i++) {
result.append(", ");
g.operands[i].render(this);
}
result.append(")");
if (useExponent && shift != 0)
result.append(printShift(shift) + ")");
return true;
}
if (op instanceof HyperbolicTangent) {
HyperbolicTangent t = (HyperbolicTangent) op;
Operator a = t.operands[0];
result.append("tanh (");
a.render(this);
result.append(")");
return true;
}
if (op instanceof Input) {
Input i = (Input) op;
result.append(i.name + "->get (");
if (// return matrix
i.operands.length < 3 || i.operands[2].getDouble() < 0) {
boolean time = i.getMode().contains("time");
String defaultLine = time ? "-INFINITY" : "0";
if (i.operands.length > 1) {
Operator op1 = i.operands[1];
if (// expression for line
op1.getType() instanceof Scalar)
// expression for line
op1.render(this);
else
// op1 is probably the mode flag
result.append(defaultLine);
} else // line is not specified. We're probably just retrieving a dummy matrix to get column count.
{
result.append(defaultLine);
}
} else // return scalar
{
i.operands[1].render(this);
result.append(", ");
i.operands[2].render(this);
}
result.append(")");
return true;
}
if (op instanceof Log) {
Log l = (Log) op;
Operator a = l.operands[0];
result.append("log (");
a.render(this);
if (useExponent)
result.append(", " + a.exponentNext + ", " + l.exponentNext);
result.append(")");
return true;
}
if (op instanceof Max) {
Max m = (Max) op;
for (int i = 0; i < m.operands.length - 1; i++) {
Operator a = m.operands[i];
result.append("max (");
renderType(a);
result.append(", ");
}
Operator b = m.operands[m.operands.length - 1];
renderType(b);
for (int i = 0; i < m.operands.length - 1; i++) result.append(")");
return true;
}
if (op instanceof Min) {
Min m = (Min) op;
for (int i = 0; i < m.operands.length - 1; i++) {
Operator a = m.operands[i];
result.append("min (");
renderType(a);
result.append(", ");
}
Operator b = m.operands[m.operands.length - 1];
renderType(b);
for (int i = 0; i < m.operands.length - 1; i++) result.append(")");
return true;
}
if (op instanceof Modulo) {
Modulo m = (Modulo) op;
Operator a = m.operand0;
Operator b = m.operand1;
result.append("modFloor (");
moduloParam(a);
result.append(", ");
moduloParam(b);
result.append(")");
return true;
}
if (op instanceof Norm) {
Norm n = (Norm) op;
Operator A = n.operands[0];
result.append("norm (");
A.render(this);
result.append(", ");
n.operands[1].render(this);
if (useExponent)
result.append(", " + A.exponentNext + ", " + n.exponentNext);
result.append(")");
return true;
}
if (op instanceof Output) {
Output o = (Output) op;
result.append(o.name + "->trace (Simulator<" + job.T + ">::instance.currentEvent->t, ");
if (// column name is explicit
o.hasColumnName) {
o.operands[2].render(this);
} else // column name is generated, so use prepared string value
{
result.append(o.columnName);
}
result.append(", ");
o.operands[1].render(this);
if (useExponent)
result.append(", " + o.operands[1].exponentNext);
result.append(", ");
if (// No mode string
o.operands.length < 4) {
// null
result.append("0");
} else if (// Mode string is constant
o.operands[3] instanceof Constant) {
result.append("\"" + o.operands[3] + "\"");
} else if (// Mode string is calculated
o.operands[3] instanceof Add) {
Add a = (Add) o.operands[3];
// No need for cast or call c_str()
result.append(a.name);
}
// else badness
result.append(")");
return true;
}
if (op instanceof Power) {
Power p = (Power) op;
Operator a = p.operand0;
Operator b = p.operand1;
result.append("pow (");
a.render(this);
result.append(", ");
b.render(this);
if (useExponent)
result.append(", " + a.exponentNext + ", " + p.exponentNext);
result.append(")");
return true;
}
if (op instanceof Pulse) {
Pulse p = (Pulse) op;
Operator t = p.operands[0];
result.append("pulse (");
renderType(t);
for (int i = 1; i < p.operands.length; i++) {
result.append(", ");
renderType(p.operands[i]);
}
result.append(")");
return true;
}
if (op instanceof ReadMatrix) {
ReadMatrix r = (ReadMatrix) op;
// Currently, ReadMatrix sets its exponent = exponentNext, so we will never do a shift here.
// Any shifting should be handled by matrixHelper while loading and converting the matrix to integer.
// matrices are held in pointers, so need to dereference
result.append("*" + r.name + "->A");
return true;
}
if (op instanceof Rows) {
Rows r = (Rows) op;
r.operands[0].render(this);
result.append(".rows ()");
return true;
}
if (op instanceof Sat) {
Sat s = (Sat) op;
Operator a = s.operands[0];
Operator lower = s.operands[1];
Operator upper;
if (s.operands.length >= 3)
upper = s.operands[2];
else
upper = lower;
result.append("max (");
if (s.operands.length == 2)
result.append("-1 * (");
renderType(lower);
if (s.operands.length == 2)
result.append(")");
result.append(", min (");
renderType(upper);
result.append(", ");
renderType(a);
result.append("))");
return true;
}
if (op instanceof SquareRoot) {
SquareRoot s = (SquareRoot) op;
Operator a = s.operands[0];
result.append("sqrt (");
a.render(this);
if (useExponent)
result.append(", " + a.exponentNext + ", " + s.exponentNext);
result.append(")");
return true;
}
if (op instanceof SumSquares) {
SumSquares ss = (SumSquares) op;
Operator A = ss.operands[0];
result.append("sumSquares (");
A.render(this);
if (useExponent)
result.append(", " + A.exponentNext + ", " + ss.exponentNext);
result.append(")");
return true;
}
if (op instanceof Tangent) {
Tangent t = (Tangent) op;
Operator a = t.operands[0];
result.append("tan (");
a.render(this);
if (useExponent)
result.append(", " + a.exponentNext + ", " + t.exponentNext);
result.append(")");
return true;
}
if (op instanceof Uniform) {
Uniform u = (Uniform) op;
result.append("uniform<" + job.T + "> (");
for (int i = 0; i < u.operands.length; i++) {
if (i > 0)
result.append(", ");
u.operands[i].render(this);
}
result.append(")");
return true;
}
return super.render(op);
}
use of gov.sandia.n2a.language.function.Delay in project n2a by frothga.
the class InternalBackendData method analyzeEvents.
public static void analyzeEvents(EquationSet s, List<EventTarget> eventTargets, List<Variable> eventReferences, List<Delay> delays) {
class EventVisitor implements Visitor {
public boolean found;
public boolean visit(Operator op) {
if (op instanceof Delay) {
delays.add((Delay) op);
} else if (op instanceof Event) {
found = true;
Event de = (Event) op;
if (// this event has not yet been analyzed
de.eventType == null) {
final EventTarget et = new EventTarget(de);
int targetIndex = eventTargets.indexOf(et);
if (// event target already exists
targetIndex >= 0) {
de.eventType = eventTargets.get(targetIndex);
} else // we must create a new event target, or more properly, fill in the event target we just used as a query object
{
// Create an entry and save the index
targetIndex = eventTargets.size();
eventTargets.add(et);
de.eventType = et;
et.container = s;
// Determine edge type
if (de.operands.length < 3) {
et.edge = EventTarget.RISE;
} else if (de.operands[2] instanceof Constant) {
Constant c = (Constant) de.operands[2];
if (c.value instanceof Text) {
Text t = (Text) c.value;
if (t.value.equalsIgnoreCase("nonzero"))
et.edge = EventTarget.NONZERO;
else if (t.value.equalsIgnoreCase("change"))
et.edge = EventTarget.CHANGE;
else if (t.value.equalsIgnoreCase("fall"))
et.edge = EventTarget.FALL;
else
et.edge = EventTarget.RISE;
} else {
Backend.err.get().println("ERROR: event() edge type must be a string.");
throw new Backend.AbortRun();
}
} else {
Backend.err.get().println("ERROR: event() edge type must be constant.");
throw new Backend.AbortRun();
}
// Allocate auxiliary variable
if (de.operands[0] instanceof AccessVariable) {
AccessVariable av = (AccessVariable) de.operands[0];
VariableReference reference = av.reference;
Variable v = reference.variable;
// then the user has broken the rule that we can't see temporaries in other parts.
if (v.hasAttribute("temporary") && v.container != s) {
Backend.err.get().println("WARNING: Cannot be temporary due to event monitor: " + v.fullName() + " from " + s.name);
v.removeAttribute("temporary");
}
// so fall through to the !trackOne case below.
if (!v.hasAttribute("temporary")) {
// ensure it's buffered, so we can detect change
v.addAttribute("externalRead");
et.trackOne = true;
// just a holder for the reference
et.track = new Variable("");
et.track.reference = reference;
}
}
if (// Expression, so create auxiliary variable. Aux not needed for NONZERO, because no change detection.
!et.trackOne && et.edge != EventTarget.NONZERO) {
et.track = new Variable("$eventAux" + targetIndex);
et.track.container = s;
et.track.type = new Scalar(0);
et.track.reference = new VariableReference();
et.track.reference.variable = et.track;
// Make executable so it can be directly evaluated during the init cycle.
et.track.equations = new TreeSet<EquationEntry>();
EquationEntry ee = new EquationEntry(et.track, "");
et.track.equations.add(ee);
ee.expression = et.event.operands[0].deepCopy();
ee.expression.addDependencies(et.track);
}
// Locate any temporaries for evaluation.
// Tie into the dependency graph using a phantom variable (which can go away afterward without damaging the graph).
// TODO: for more efficiency, we could have separate lists of temporaries for the condition and delay operands
// TODO: for more efficiency, cut off search for temporaries along a given branch of the tree at the first non-temporary.
final Variable phantom = new Variable("event");
phantom.uses = new IdentityHashMap<Variable, Integer>();
phantom.container = s;
et.event.visit(new Visitor() {
public boolean visit(Operator op) {
if (op instanceof AccessVariable) {
AccessVariable av = (AccessVariable) op;
Variable v = av.reference.variable;
if (v.hasAttribute("temporary") && !phantom.uses.containsKey(v))
phantom.uses.put(v, 1);
return false;
}
return true;
}
});
// Scan all variables in equation set to see if we need them
for (Variable t : s.ordered) {
if (t.hasAttribute("temporary") && phantom.dependsOn(t) != null)
et.dependencies.add(t);
}
// Note the default is already set to -1 (no care)
class DelayVisitor implements Visitor {
TreeSet<EquationSet> containers = new TreeSet<EquationSet>();
public boolean visit(Operator op) {
if (op instanceof AccessVariable) {
AccessVariable av = (AccessVariable) op;
// could include the target part itself, if in fact we use local variables
containers.add(av.reference.variable.container);
return false;
}
return true;
}
}
DelayVisitor dv = new DelayVisitor();
if (de.operands.length >= 2) {
if (de.operands[1] instanceof Constant) {
Constant c = (Constant) de.operands[1];
et.delay = (float) ((Scalar) c.value).value;
if (et.delay < 0)
et.delay = -1;
} else {
// indicates that we need to evaluate delay at run time
et.delay = -2;
de.operands[1].visit(dv);
}
}
// Set up monitors in source parts
class ConditionVisitor implements Visitor {
TreeSet<EquationSet> containers = new TreeSet<EquationSet>();
public boolean visit(Operator op) {
if (op instanceof AccessVariable) {
AccessVariable av = (AccessVariable) op;
Variable v = av.reference.variable;
EquationSet sourceContainer = v.container;
containers.add(sourceContainer);
// Set up monitors for values that can vary during update.
if (!v.hasAttribute("constant") && !v.hasAttribute("initOnly") && !et.monitors(sourceContainer)) {
EventSource es = new EventSource(sourceContainer, et);
// null means self-reference, a special case handled in Part
if (sourceContainer != s)
es.reference = av.reference;
et.sources.add(es);
}
return false;
}
return true;
}
}
ConditionVisitor cv = new ConditionVisitor();
de.operands[0].visit(cv);
// Special case for event with no references that vary
if (et.sources.isEmpty()) {
// We can avoid creating a self monitor if we know for certain that the event will never fire
boolean neverFires = false;
if (de.operands[0] instanceof Constant) {
if (et.edge == EventTarget.NONZERO) {
Type op0 = ((Constant) de.operands[0]).value;
if (op0 instanceof Scalar) {
neverFires = ((Scalar) op0).value == 0;
} else {
Backend.err.get().println("ERROR: Condition for event() must resolve to a number.");
throw new Backend.AbortRun();
}
} else {
neverFires = true;
}
}
if (!neverFires) {
EventSource es = new EventSource(s, et);
// This is a self-reference, so es.reference should be null.
et.sources.add(es);
}
}
// Determine if monitor needs to test every target, or if one representative target is sufficient
for (EventSource source : et.sources) {
// associated with any given source instance, so every target must be evaluated separately.
if (cv.containers.size() > 1)
source.testEach = true;
if (dv.containers.size() > 1 || (dv.containers.size() == 1 && dv.containers.first() != source.container))
source.delayEach = true;
}
}
}
}
return true;
}
}
EventVisitor eventVisitor = new EventVisitor();
for (Variable v : s.variables) {
eventVisitor.found = false;
v.visit(eventVisitor);
if ((eventVisitor.found || v.dependsOnEvent()) && v.reference.variable != v)
eventReferences.add(v);
}
}
Aggregations