Search in sources :

Example 1 with Matrix

use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.

the class JobC method generateStatic.

public void generateStatic(final EquationSet s, final StringBuilder result) {
    for (EquationSet p : s.parts) generateStatic(p, result);
    // Generate static definitions
    final BackendDataC bed = (BackendDataC) s.backendData;
    class CheckStatic extends Visitor {

        public boolean global;

        public boolean visit(Operator op) {
            if (op instanceof Constant) {
                Type m = ((Constant) op).value;
                if (m instanceof Matrix) {
                    Matrix A = (Matrix) m;
                    int rows = A.rows();
                    int cols = A.columns();
                    String matrixName = "Matrix" + matrixNames.size();
                    matrixNames.put(op, matrixName);
                    if (rows == 3 && cols == 1)
                        result.append("Vector3 " + matrixName + " = Matrix<float>");
                    else
                        result.append("Matrix<float> " + matrixName);
                    result.append(" (\"" + A + "\");\n");
                }
                // Don't try to descend tree from here
                return false;
            }
            if (op instanceof Function) {
                Function f = (Function) op;
                if (// We need to auto-generate the column name.
                f instanceof Output && f.operands.length < 3) {
                    String stringName = "columnName" + stringNames.size();
                    stringNames.put(op, stringName);
                    if (global) {
                        bed.setGlobalNeedPath(s);
                        bed.globalColumns.add(stringName);
                    } else {
                        bed.setLocalNeedPath(s);
                        bed.localColumns.add(stringName);
                    }
                }
                // Detect functions that need static handles
                if (f.operands.length > 0) {
                    Operator operand0 = f.operands[0];
                    if (operand0 instanceof Constant) {
                        Constant c = (Constant) operand0;
                        Type o = c.value;
                        if (o instanceof Text) {
                            String fileName = ((Text) o).value;
                            if (op instanceof ReadMatrix) {
                                if (!matrixNames.containsKey(fileName)) {
                                    String matrixName = "Matrix" + matrixNames.size();
                                    matrixNames.put(fileName, matrixName);
                                    result.append("MatrixInput * " + matrixName + " = matrixHelper (\"" + fileName + "\");\n");
                                }
                            } else if (f instanceof Input) {
                                if (!inputNames.containsKey(fileName)) {
                                    String inputName = "Input" + inputNames.size();
                                    inputNames.put(fileName, inputName);
                                    result.append("InputHolder * " + inputName + " = inputHelper (\"" + fileName + "\");\n");
                                }
                            } else if (f instanceof Output) {
                                if (!outputNames.containsKey(fileName)) {
                                    String outputName = "Output" + outputNames.size();
                                    outputNames.put(fileName, outputName);
                                    result.append("OutputHolder * " + outputName + " = outputHelper (\"" + fileName + "\");\n");
                                }
                            }
                        }
                    } else // Dynamic file name (no static handle)
                    {
                        if (f instanceof ReadMatrix) {
                            matrixNames.put(op, "Matrix" + matrixNames.size());
                            stringNames.put(operand0, "fileName" + stringNames.size());
                        } else if (f instanceof Input) {
                            inputNames.put(op, "Input" + inputNames.size());
                            stringNames.put(operand0, "fileName" + stringNames.size());
                        } else if (f instanceof Output) {
                            outputNames.put(op, "Output" + outputNames.size());
                            stringNames.put(operand0, "fileName" + stringNames.size());
                        }
                    }
                }
                // Functions could be nested, so continue descent.
                return true;
            }
            return true;
        }
    }
    CheckStatic checkStatic = new CheckStatic();
    for (Variable v : s.ordered) {
        checkStatic.global = v.hasAttribute("global");
        v.visit(checkStatic);
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator) EquationSet(gov.sandia.n2a.eqset.EquationSet) Variable(gov.sandia.n2a.eqset.Variable) AccessVariable(gov.sandia.n2a.language.AccessVariable) Visitor(gov.sandia.n2a.language.Visitor) Constant(gov.sandia.n2a.language.Constant) Text(gov.sandia.n2a.language.type.Text) ReadMatrix(gov.sandia.n2a.language.function.ReadMatrix) Function(gov.sandia.n2a.language.Function) Type(gov.sandia.n2a.language.Type) 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) Output(gov.sandia.n2a.language.function.Output)

Example 2 with Matrix

use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.

the class ReadMatrix method eval.

public Type eval(Instance context) {
    Simulator simulator = Simulator.getSimulator(context);
    // absence of simulator indicates analysis phase, so opening files is unnecessary
    if (simulator == null)
        return new Scalar(0);
    String path = ((Text) operands[0].eval(context)).value;
    Matrix A = simulator.matrices.get(path);
    if (A == null) {
        A = Matrix.factory(new File(path).getAbsoluteFile());
        simulator.matrices.put(path, A);
    }
    String mode = "";
    int lastParm = operands.length - 1;
    if (lastParm > 0) {
        Type parmValue = operands[lastParm].eval(context);
        if (parmValue instanceof Text)
            mode = ((Text) parmValue).value;
    }
    if (mode.equals("columns"))
        return new Scalar(A.columns());
    if (mode.equals("rows"))
        return new Scalar(A.rows());
    int rows = A.rows();
    int columns = A.columns();
    int lastRow = rows - 1;
    int lastColumn = columns - 1;
    double row = ((Scalar) operands[1].eval(context)).value;
    double column = ((Scalar) operands[2].eval(context)).value;
    if (mode.equals("raw")) {
        int r = (int) Math.floor(row);
        int c = (int) Math.floor(column);
        if (r < 0)
            r = 0;
        else if (r >= rows)
            r = lastRow;
        if (c < 0)
            c = 0;
        else if (c >= columns)
            c = lastColumn;
        return new Scalar(A.get(r, c));
    } else {
        row *= lastRow;
        column *= lastColumn;
        int r = (int) Math.floor(row);
        int c = (int) Math.floor(column);
        if (r < 0) {
            if (c < 0)
                return new Scalar(A.get(0, 0));
            else if (c >= lastColumn)
                return new Scalar(A.get(0, lastColumn));
            else {
                double b = column - c;
                return new Scalar((1 - b) * A.get(0, c) + b * A.get(0, c + 1));
            }
        } else if (r >= lastRow) {
            if (c < 0)
                return new Scalar(A.get(lastRow, 0));
            else if (c >= lastColumn)
                return new Scalar(A.get(lastRow, lastColumn));
            else {
                double b = column - c;
                return new Scalar((1 - b) * A.get(lastRow, c) + b * A.get(lastRow, c + 1));
            }
        } else {
            double a = row - r;
            double a1 = 1 - a;
            if (c < 0)
                return new Scalar(a1 * A.get(r, 0) + a * A.get(r + 1, 0));
            else if (c >= lastColumn)
                return new Scalar(a1 * A.get(r, lastColumn) + a * A.get(r + 1, lastColumn));
            else {
                double b = column - c;
                return new Scalar((1 - b) * (a1 * A.get(r, c) + a * A.get(r + 1, c)) + b * (a1 * A.get(r, c + 1) + a * A.get(r + 1, c + 1)));
            }
        }
    }
}
Also used : Type(gov.sandia.n2a.language.Type) Matrix(gov.sandia.n2a.language.type.Matrix) Text(gov.sandia.n2a.language.type.Text) Simulator(gov.sandia.n2a.backend.internal.Simulator) File(java.io.File) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 3 with Matrix

use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.

the class JobC method generateStatic.

public void generateStatic(RendererC context, EquationSet s) {
    for (EquationSet p : s.parts) generateStatic(context, p);
    context.setPart(s);
    BackendDataC bed = context.bed;
    StringBuilder result = context.result;
    class CheckStatic implements Visitor {

        public boolean global;

        public boolean visit(Operator op) {
            for (ProvideOperator po : extensions) {
                Boolean result = po.generateStatic(context, op);
                if (result != null)
                    return result;
            }
            if (op instanceof BuildMatrix) {
                BuildMatrix m = (BuildMatrix) op;
                m.name = "Matrix" + matrixNames.size();
                matrixNames.put(m, m.name);
                return false;
            }
            if (op instanceof Constant) {
                Constant constant = (Constant) op;
                Type m = constant.value;
                if (m instanceof Matrix) {
                    Matrix A = (Matrix) m;
                    int rows = A.rows();
                    int cols = A.columns();
                    constant.name = "Matrix" + matrixNames.size();
                    matrixNames.put(constant, constant.name);
                    result.append("MatrixFixed<" + T + "," + rows + "," + cols + "> " + constant.name + " = {");
                    String initializer = "";
                    for (int c = 0; c < cols; c++) {
                        for (int r = 0; r < rows; r++) {
                            initializer += context.print(A.get(r, c), constant.exponent) + ", ";
                        }
                    }
                    if (initializer.length() > 2)
                        initializer = initializer.substring(0, initializer.length() - 2);
                    result.append(initializer + "};\n");
                }
                // Don't try to descend tree from here
                return false;
            }
            if (op instanceof Function) {
                Function f = (Function) op;
                if (// Handle computed strings
                f instanceof Output) {
                    Output o = (Output) f;
                    if (// We need to auto-generate the column name.
                    !o.hasColumnName) {
                        o.columnName = "columnName" + stringNames.size();
                        stringNames.put(op, o.columnName);
                        if (global) {
                            bed.setGlobalNeedPath(s);
                            bed.globalColumns.add(o.columnName);
                        } else {
                            bed.setLocalNeedPath(s);
                            bed.localColumns.add(o.columnName);
                        }
                    }
                    if (// Mode is calculated
                    o.operands.length > 3 && o.operands[3] instanceof Add) {
                        Add a = (Add) o.operands[3];
                        a.name = "columnMode" + stringNames.size();
                        stringNames.put(op, a.name);
                    }
                }
                // Detect functions that need static handles
                if (f.operands.length > 0) {
                    Operator operand0 = f.operands[0];
                    if (operand0 instanceof Constant) {
                        Constant c = (Constant) operand0;
                        if (c.value instanceof Text) {
                            String fileName = ((Text) c.value).value;
                            if (f instanceof ReadMatrix) {
                                ReadMatrix r = (ReadMatrix) f;
                                r.name = matrixNames.get(fileName);
                                if (r.name == null) {
                                    r.name = "Matrix" + matrixNames.size();
                                    matrixNames.put(fileName, r.name);
                                    mainMatrix.add(r);
                                    result.append("MatrixInput<" + T + "> * " + r.name + ";\n");
                                }
                            } else if (f instanceof Input) {
                                Input i = (Input) f;
                                i.name = inputNames.get(fileName);
                                if (i.name == null) {
                                    i.name = "Input" + inputNames.size();
                                    inputNames.put(fileName, i.name);
                                    mainInput.add(i);
                                    result.append("InputHolder<" + T + "> * " + i.name + ";\n");
                                }
                            } else if (f instanceof Output) {
                                Output o = (Output) f;
                                o.name = outputNames.get(fileName);
                                if (o.name == null) {
                                    o.name = "Output" + outputNames.size();
                                    outputNames.put(fileName, o.name);
                                    mainOutput.add(o);
                                    result.append("OutputHolder<" + T + "> * " + o.name + ";\n");
                                }
                            }
                        }
                    } else // Dynamic file name (no static handle)
                    {
                        boolean error = false;
                        if (f instanceof ReadMatrix) {
                            ReadMatrix r = (ReadMatrix) f;
                            matrixNames.put(op, r.name = "Matrix" + matrixNames.size());
                            stringNames.put(operand0, r.fileName = "fileName" + stringNames.size());
                            if (operand0 instanceof Add)
                                ((Add) operand0).name = r.fileName;
                            else
                                error = true;
                        } else if (f instanceof Input) {
                            Input i = (Input) f;
                            inputNames.put(op, i.name = "Input" + inputNames.size());
                            stringNames.put(operand0, i.fileName = "fileName" + stringNames.size());
                            if (operand0 instanceof Add)
                                ((Add) operand0).name = i.fileName;
                            else
                                error = true;
                        } else if (f instanceof Output) {
                            Output o = (Output) f;
                            outputNames.put(op, o.name = "Output" + outputNames.size());
                            stringNames.put(operand0, o.fileName = "fileName" + stringNames.size());
                            if (operand0 instanceof Add)
                                ((Add) operand0).name = o.fileName;
                            else
                                error = true;
                        }
                        if (error) {
                            Backend.err.get().println("ERROR: File name must be a string expression.");
                            throw new AbortRun();
                        }
                    }
                }
                // Functions could be nested, so continue descent.
                return true;
            }
            return true;
        }
    }
    CheckStatic checkStatic = new CheckStatic();
    for (Variable v : s.ordered) {
        checkStatic.global = v.hasAttribute("global");
        v.visit(checkStatic);
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator) Add(gov.sandia.n2a.language.operator.Add) EquationSet(gov.sandia.n2a.eqset.EquationSet) Variable(gov.sandia.n2a.eqset.Variable) AccessVariable(gov.sandia.n2a.language.AccessVariable) Visitor(gov.sandia.n2a.language.Visitor) Constant(gov.sandia.n2a.language.Constant) Text(gov.sandia.n2a.language.type.Text) ExtensionPoint(gov.sandia.n2a.plugins.ExtensionPoint) ReadMatrix(gov.sandia.n2a.language.function.ReadMatrix) AbortRun(gov.sandia.n2a.plugins.extpoints.Backend.AbortRun) Function(gov.sandia.n2a.language.Function) Type(gov.sandia.n2a.language.Type) Input(gov.sandia.n2a.language.function.Input) ReadMatrix(gov.sandia.n2a.language.function.ReadMatrix) ConnectionMatrix(gov.sandia.n2a.eqset.EquationSet.ConnectionMatrix) BuildMatrix(gov.sandia.n2a.language.BuildMatrix) Matrix(gov.sandia.n2a.language.type.Matrix) BuildMatrix(gov.sandia.n2a.language.BuildMatrix) Output(gov.sandia.n2a.language.function.Output)

Example 4 with Matrix

use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.

the class JobC method type.

public String type(Variable v) {
    if (v.type instanceof Matrix) {
        Matrix m = (Matrix) v.type;
        int rows = m.rows();
        int cols = m.columns();
        // Known dimension, so use more efficient storage.
        if (rows > 0 && cols > 0)
            return "MatrixFixed<" + T + "," + rows + "," + cols + ">";
        // Arbitrary dimension
        return "Matrix<" + T + ">";
    }
    if (v.type instanceof Text)
        return "String";
    return T;
}
Also used : ReadMatrix(gov.sandia.n2a.language.function.ReadMatrix) ConnectionMatrix(gov.sandia.n2a.eqset.EquationSet.ConnectionMatrix) BuildMatrix(gov.sandia.n2a.language.BuildMatrix) Matrix(gov.sandia.n2a.language.type.Matrix) Text(gov.sandia.n2a.language.type.Text) ExtensionPoint(gov.sandia.n2a.plugins.ExtensionPoint)

Example 5 with Matrix

use of gov.sandia.n2a.language.type.Matrix in project n2a by frothga.

the class MatrixDense method divide.

public MatrixDense divide(Type that) throws EvaluationException {
    if (that instanceof MatrixDense) {
        MatrixDense B = (MatrixDense) that;
        int oh = Math.min(rows, B.rows);
        int ow = Math.min(columns, B.columns);
        MatrixDense result = new MatrixDense(rows, columns);
        int stepA = strideC - rows * strideR;
        int stepB = B.strideC - oh * B.strideR;
        int a = offset;
        int b = B.offset;
        int r = 0;
        int end = rows * ow;
        while (r < end) {
            int overlapEnd = r + oh;
            int columnEnd = r + rows;
            while (r < overlapEnd) {
                result.data[r++] = data[a] / B.data[b];
                a += strideR;
                b += B.strideR;
            }
            while (r < columnEnd) {
                result.data[r++] = data[a];
                a += strideR;
            }
            a += stepA;
            b += stepB;
        }
        end = rows * columns;
        while (r < end) {
            int columnEnd = r + rows;
            while (r < columnEnd) {
                result.data[r++] = data[a];
                a += strideR;
            }
            a += stepA;
        }
        return result;
    }
    if (that instanceof Matrix) {
        Matrix B = (Matrix) that;
        int oh = Math.min(rows, B.rows());
        int ow = Math.min(columns, B.columns());
        MatrixDense result = new MatrixDense(rows, columns);
        int stepA = strideC - rows * strideR;
        int a = offset;
        int r = 0;
        for (int col = 0; col < ow; col++) {
            int row = 0;
            for (; row < oh; row++) {
                result.data[r++] = data[a] / B.get(row, col);
                a += strideR;
            }
            for (; row < rows; row++) {
                result.data[r++] = data[a];
                a += strideR;
            }
            a += stepA;
        }
        for (int col = ow; col < columns; col++) {
            for (int row = 0; row < rows; row++) {
                result.data[r++] = data[a];
                a += strideR;
            }
            a += stepA;
        }
        return result;
    }
    if (that instanceof Scalar) {
        double scalar = ((Scalar) that).value;
        MatrixDense result = new MatrixDense(rows, columns);
        int step = strideC - rows * strideR;
        int i = offset;
        int r = 0;
        int end = rows * columns;
        while (r < end) {
            int columnEnd = r + rows;
            while (r < columnEnd) {
                result.data[r++] = data[i] / scalar;
                i += strideR;
            }
            i += step;
        }
        return result;
    }
    throw new EvaluationException("type mismatch");
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix) EvaluationException(gov.sandia.n2a.language.EvaluationException) Scalar(gov.sandia.n2a.language.type.Scalar)

Aggregations

Matrix (gov.sandia.n2a.language.type.Matrix)37 Scalar (gov.sandia.n2a.language.type.Scalar)26 Text (gov.sandia.n2a.language.type.Text)12 Type (gov.sandia.n2a.language.Type)11 Simulator (gov.sandia.n2a.backend.internal.Simulator)9 Operator (gov.sandia.n2a.language.Operator)9 ReadMatrix (gov.sandia.n2a.language.function.ReadMatrix)8 MatrixDense (gov.sandia.n2a.linear.MatrixDense)7 BuildMatrix (gov.sandia.n2a.language.BuildMatrix)6 Constant (gov.sandia.n2a.language.Constant)6 EvaluationException (gov.sandia.n2a.language.EvaluationException)6 AccessVariable (gov.sandia.n2a.language.AccessVariable)5 Variable (gov.sandia.n2a.eqset.Variable)4 Input (gov.sandia.n2a.language.function.Input)4 Output (gov.sandia.n2a.language.function.Output)4 Add (gov.sandia.n2a.language.operator.Add)4 Function (gov.sandia.n2a.language.Function)3 Event (gov.sandia.n2a.language.function.Event)3 Modulo (gov.sandia.n2a.language.operator.Modulo)3 Random (java.util.Random)3