Search in sources :

Example 11 with Output

use of gov.sandia.n2a.language.function.Output in project n2a by frothga.

the class ExportJob method analyze.

/**
 *        Find references to $index in connection endpoints, and set up info for ConnectionContext.
 */
public void analyze(EquationSet s) {
    for (EquationSet p : s.parts) analyze(p);
    class ExportTransformer implements Transformer {

        Variable v;

        public Operator transform(Operator op) {
            if (op instanceof AccessVariable) {
                VariableReference r = ((AccessVariable) op).reference;
                // It is possible that some variables were not resolved.
                if (r == null)
                    return null;
                Variable rv = r.variable;
                if (rv.container != v.container && !r.resolution.isEmpty()) {
                    Object o = r.resolution.get(r.resolution.size() - 1);
                    if (o instanceof ConnectionBinding) {
                        // This is somewhat of a hack, but ConnectionContext assumes the mappings A->0 and B->1.
                        switch(((ConnectionBinding) o).alias) {
                            case "A":
                                r.index = 0;
                                break;
                            case "B":
                                r.index = 1;
                                break;
                        }
                    }
                }
                return null;
            }
            if (op instanceof Output) {
                return new OutputLEMS((Output) op);
            }
            return null;
        }
    }
    ;
    ExportTransformer xform = new ExportTransformer();
    for (final Variable v : s.variables) {
        xform.v = v;
        v.transform(xform);
        // does not call that function.
        if (v.hasUsers() || v.hasAttribute("externalWrite"))
            continue;
        if (v.name.startsWith("$") || v.name.contains(".$"))
            continue;
        for (EquationEntry e : v.equations) {
            if (e.expression.isOutput()) {
                v.addAttribute("dummy");
                break;
            }
        }
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator) EquationSet(gov.sandia.n2a.eqset.EquationSet) Variable(gov.sandia.n2a.eqset.Variable) UnresolvedVariable(gov.sandia.n2a.eqset.EquationSet.UnresolvedVariable) AccessVariable(gov.sandia.n2a.language.AccessVariable) Transformer(gov.sandia.n2a.language.Transformer) AccessVariable(gov.sandia.n2a.language.AccessVariable) VariableReference(gov.sandia.n2a.eqset.VariableReference) ConnectionBinding(gov.sandia.n2a.eqset.EquationSet.ConnectionBinding) Output(gov.sandia.n2a.language.function.Output) EquationEntry(gov.sandia.n2a.eqset.EquationEntry)

Example 12 with Output

use of gov.sandia.n2a.language.function.Output in project n2a by frothga.

the class JobC method prepareStaticObjects.

public void prepareStaticObjects(Operator op, RendererC context, String pad) {
    final BackendDataC bed = context.bed;
    Visitor visitor = new Visitor() {

        public boolean visit(Operator op) {
            for (ProvideOperator po : extensions) {
                Boolean result = po.prepareStaticObjects(op, context, pad);
                if (result != null)
                    return result;
            }
            if (op instanceof Output) {
                Output o = (Output) op;
                if (// column name is generated
                !o.hasColumnName) {
                    BackendDataC bed = (BackendDataC) context.part.backendData;
                    if (context.global ? bed.needGlobalPath : bed.needLocalPath) {
                        context.result.append(pad + "path (" + o.columnName + ");\n");
                        context.result.append(pad + o.columnName + " += \"." + o.variableName + "\";\n");
                    } else {
                        context.result.append(pad + o.columnName + " = \"" + o.variableName + "\";\n");
                    }
                }
                if (// Apply "raw" attribute now, if set.
                o.operands[0] instanceof Constant && o.operands.length > 3) {
                    if (o.operands[3] instanceof Constant) {
                        if (o.operands[3].getString().contains("raw")) {
                            context.result.append(pad + o.name + "->raw = true;\n");
                        }
                    } else if (// calculated string
                    o.operands[3] instanceof Add) {
                        // Rather than actually calculate the string, just scan for any component which contains "raw".
                        // Only a very pathological case would not have this. IE: "r" + "a" + "w"
                        Add a = (Add) o.operands[3];
                        for (Operator fa : flattenAdd(a)) {
                            if (fa.getString().contains("raw")) {
                                context.result.append(pad + o.name + "->raw = true;\n");
                                break;
                            }
                        }
                    }
                }
                // Continue to drill down, because I/O functions can be nested.
                return true;
            }
            if (op instanceof Input) {
                Input i = (Input) op;
                if (i.operands[0] instanceof Constant) {
                    String mode = i.getMode();
                    if (// Note: In the case of T==int, we don't need to set epsilon because it is already set to 1 by the constructor.
                    (mode.contains("time") || mode.contains("smooth")) && !context.global && !T.equals("int")) {
                        // TODO: This is a bad way to set time epsilon, but not sure if there is a better one.
                        // The main problem is that several different instances may all do the same initialization,
                        // and they may disagree on epsilon, perhaps even by several orders of magnitude.
                        // We could make a compile-time estimate of the smallest dt, and use dt/1000 everywhere.
                        // This is similar to the current approach for estimating time exponent for fixed-point.
                        // Read $t' as an lvalue, to ensure we get any newly-set frequency.
                        // However, can't do this if $t' is a constant. In that case, no variable exists.
                        boolean lvalue = !bed.dt.hasAttribute("constant");
                        context.result.append(pad + i.name + "->epsilon = " + resolve(bed.dt.reference, context, lvalue) + " / 1000.0");
                        if (T.equals("float"))
                            context.result.append("f");
                        context.result.append(";\n");
                    }
                }
                return true;
            }
            return true;
        }
    };
    op.visit(visitor);
}
Also used : Operator(gov.sandia.n2a.language.Operator) Add(gov.sandia.n2a.language.operator.Add) Input(gov.sandia.n2a.language.function.Input) Visitor(gov.sandia.n2a.language.Visitor) Constant(gov.sandia.n2a.language.Constant) Output(gov.sandia.n2a.language.function.Output)

Example 13 with Output

use of gov.sandia.n2a.language.function.Output in project n2a by frothga.

the class JobC method generateMainInitializers.

public void generateMainInitializers(RendererC context) {
    StringBuilder result = context.result;
    for (ProvideOperator po : extensions) po.generateMainInitializers(context);
    for (ReadMatrix r : mainMatrix) {
        result.append("    " + r.name + " = matrixHelper<" + T + "> (\"" + r.operands[0].getString() + "\"");
        if (T.equals("int"))
            result.append(", " + r.exponent);
        result.append(");\n");
    }
    for (Input i : mainInput) {
        result.append("    " + i.name + " = inputHelper<" + T + "> (\"" + i.operands[0].getString() + "\"");
        if (T.equals("int"))
            result.append(", " + i.exponent);
        result.append(");\n");
        String mode = i.getMode();
        boolean smooth = mode.contains("smooth");
        boolean time = smooth || mode.contains("time");
        if (time)
            result.append("    " + i.name + "->time = true;\n");
        if (smooth)
            result.append("    " + i.name + "->smooth = true;\n");
    }
    for (Output o : mainOutput) {
        result.append("    " + o.name + " = outputHelper<" + T + "> (\"" + o.operands[0].getString() + "\");\n");
    }
}
Also used : Input(gov.sandia.n2a.language.function.Input) Output(gov.sandia.n2a.language.function.Output) ReadMatrix(gov.sandia.n2a.language.function.ReadMatrix)

Example 14 with Output

use of gov.sandia.n2a.language.function.Output 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);
}
Also used : Add(gov.sandia.n2a.language.operator.Add) Operator(gov.sandia.n2a.language.Operator) Max(gov.sandia.n2a.language.function.Max) Constant(gov.sandia.n2a.language.Constant) SumSquares(gov.sandia.n2a.language.function.SumSquares) Grid(gov.sandia.n2a.language.function.Grid) Sat(gov.sandia.n2a.language.function.Sat) Uniform(gov.sandia.n2a.language.function.Uniform) Delay(gov.sandia.n2a.language.function.Delay) Scalar(gov.sandia.n2a.language.type.Scalar) Input(gov.sandia.n2a.language.function.Input) ReadMatrix(gov.sandia.n2a.language.function.ReadMatrix) BuildMatrix(gov.sandia.n2a.language.BuildMatrix) Matrix(gov.sandia.n2a.language.type.Matrix) HyperbolicTangent(gov.sandia.n2a.language.function.HyperbolicTangent) SquareRoot(gov.sandia.n2a.language.function.SquareRoot) BuildMatrix(gov.sandia.n2a.language.BuildMatrix) Output(gov.sandia.n2a.language.function.Output) Atan(gov.sandia.n2a.language.function.Atan) Rows(gov.sandia.n2a.language.function.Rows) AccessVariable(gov.sandia.n2a.language.AccessVariable) Log(gov.sandia.n2a.language.function.Log) Modulo(gov.sandia.n2a.language.operator.Modulo) Pulse(gov.sandia.n2a.language.function.Pulse) Columns(gov.sandia.n2a.language.function.Columns) Text(gov.sandia.n2a.language.type.Text) Tangent(gov.sandia.n2a.language.function.Tangent) HyperbolicTangent(gov.sandia.n2a.language.function.HyperbolicTangent) ReadMatrix(gov.sandia.n2a.language.function.ReadMatrix) Type(gov.sandia.n2a.language.Type) Min(gov.sandia.n2a.language.function.Min) Event(gov.sandia.n2a.language.function.Event) Norm(gov.sandia.n2a.language.function.Norm) Gaussian(gov.sandia.n2a.language.function.Gaussian) AccessElement(gov.sandia.n2a.language.AccessElement) Exp(gov.sandia.n2a.language.function.Exp) Power(gov.sandia.n2a.language.operator.Power)

Aggregations

Output (gov.sandia.n2a.language.function.Output)14 Operator (gov.sandia.n2a.language.Operator)13 Visitor (gov.sandia.n2a.language.Visitor)10 AccessVariable (gov.sandia.n2a.language.AccessVariable)9 Input (gov.sandia.n2a.language.function.Input)9 Constant (gov.sandia.n2a.language.Constant)8 ReadMatrix (gov.sandia.n2a.language.function.ReadMatrix)7 Variable (gov.sandia.n2a.eqset.Variable)6 BuildMatrix (gov.sandia.n2a.language.BuildMatrix)6 Add (gov.sandia.n2a.language.operator.Add)6 Text (gov.sandia.n2a.language.type.Text)6 EquationEntry (gov.sandia.n2a.eqset.EquationEntry)4 Type (gov.sandia.n2a.language.Type)4 Matrix (gov.sandia.n2a.language.type.Matrix)4 EquationSet (gov.sandia.n2a.eqset.EquationSet)3 Scalar (gov.sandia.n2a.language.type.Scalar)3 InstanceTemporaries (gov.sandia.n2a.backend.internal.InstanceTemporaries)2 Population (gov.sandia.n2a.backend.internal.Population)2 Symbol (gov.sandia.n2a.backend.xyce.netlist.Symbol)2 XyceRenderer (gov.sandia.n2a.backend.xyce.netlist.XyceRenderer)2