Search in sources :

Example 1 with IndexedIdentifier

use of org.apache.sysml.parser.IndexedIdentifier in project incubator-systemml by apache.

the class DmlSyntacticValidator method exitIndexedExpression.

/**
	 * DML uses 1-based indexing.;
	 *
	 * @param ctx the parse tree
	 */
@Override
public void exitIndexedExpression(IndexedExpressionContext ctx) {
    boolean isRowLower = (ctx.rowLower != null && !ctx.rowLower.isEmpty() && (ctx.rowLower.info.expr != null));
    boolean isRowUpper = (ctx.rowUpper != null && !ctx.rowUpper.isEmpty() && (ctx.rowUpper.info.expr != null));
    boolean isColLower = (ctx.colLower != null && !ctx.colLower.isEmpty() && (ctx.colLower.info.expr != null));
    boolean isColUpper = (ctx.colUpper != null && !ctx.colUpper.isEmpty() && (ctx.colUpper.info.expr != null));
    ExpressionInfo rowLower = isRowLower ? ctx.rowLower.info : null;
    ExpressionInfo rowUpper = isRowUpper ? ctx.rowUpper.info : null;
    ExpressionInfo colLower = isColLower ? ctx.colLower.info : null;
    ExpressionInfo colUpper = isColUpper ? ctx.colUpper.info : null;
    ctx.dataInfo.expr = new IndexedIdentifier(ctx.name.getText(), false, false);
    setFileLineColumn(ctx.dataInfo.expr, ctx);
    try {
        ArrayList<ArrayList<Expression>> exprList = new ArrayList<ArrayList<Expression>>();
        ArrayList<Expression> rowIndices = new ArrayList<Expression>();
        ArrayList<Expression> colIndices = new ArrayList<Expression>();
        if (!isRowLower && !isRowUpper) {
            // both not set
            rowIndices.add(null);
            rowIndices.add(null);
        } else if (isRowLower && isRowUpper) {
            // both set
            rowIndices.add(rowLower.expr);
            rowIndices.add(rowUpper.expr);
        } else if (isRowLower && !isRowUpper) {
            // only row set
            rowIndices.add(rowLower.expr);
        } else {
            notifyErrorListeners("incorrect index expression for row", ctx.start);
            return;
        }
        if (!isColLower && !isColUpper) {
            // both not set
            colIndices.add(null);
            colIndices.add(null);
        } else if (isColLower && isColUpper) {
            colIndices.add(colLower.expr);
            colIndices.add(colUpper.expr);
        } else if (isColLower && !isColUpper) {
            colIndices.add(colLower.expr);
        } else {
            notifyErrorListeners("incorrect index expression for column", ctx.start);
            return;
        }
        exprList.add(rowIndices);
        exprList.add(colIndices);
        ((IndexedIdentifier) ctx.dataInfo.expr).setIndices(exprList);
    } catch (Exception e) {
        notifyErrorListeners("cannot set the indices", ctx.start);
        return;
    }
}
Also used : Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) ArrayList(java.util.ArrayList) ExpressionInfo(org.apache.sysml.parser.common.ExpressionInfo) LanguageException(org.apache.sysml.parser.LanguageException) ParseException(org.apache.sysml.parser.ParseException) IndexedIdentifier(org.apache.sysml.parser.IndexedIdentifier)

Example 2 with IndexedIdentifier

use of org.apache.sysml.parser.IndexedIdentifier in project incubator-systemml by apache.

the class RewriteInjectSparkLoopCheckpointing method rewriteStatementBlock.

@Override
public ArrayList<StatementBlock> rewriteStatementBlock(StatementBlock sb, ProgramRewriteStatus status) throws HopsException {
    ArrayList<StatementBlock> ret = new ArrayList<StatementBlock>();
    if (!OptimizerUtils.isSparkExecutionMode()) {
        // nothing to do here
        ret.add(sb);
        //return original statement block
        return ret;
    }
    //1) We currently add checkpoint operations without information about the global program structure,
    //this assumes that redundant checkpointing is prevented at runtime level (instruction-level)
    //2) Also, we do not take size information into account right now. This means that all candidates
    //are checkpointed even if they are only used by CP operations.
    //block size set by reblock rewrite
    int blocksize = status.getBlocksize();
    //optimization because otherwise we would prevent remote parfor)
    if (//incl parfor 
    (sb instanceof WhileStatementBlock || sb instanceof ForStatementBlock) && (_checkCtx ? !status.isInParforContext() : true)) {
        //step 1: determine checkpointing candidates
        ArrayList<String> candidates = new ArrayList<String>();
        VariableSet read = sb.variablesRead();
        VariableSet updated = sb.variablesUpdated();
        for (String rvar : read.getVariableNames()) if (!updated.containsVariable(rvar) && read.getVariable(rvar).getDataType() == DataType.MATRIX)
            candidates.add(rvar);
        //step 2: insert statement block with checkpointing operations
        if (//existing candidates
        !candidates.isEmpty()) {
            StatementBlock sb0 = new StatementBlock();
            sb0.setDMLProg(sb.getDMLProg());
            sb0.setAllPositions(sb.getFilename(), sb.getBeginLine(), sb.getBeginColumn(), sb.getEndLine(), sb.getEndColumn());
            ArrayList<Hop> hops = new ArrayList<Hop>();
            VariableSet livein = new VariableSet();
            VariableSet liveout = new VariableSet();
            for (String var : candidates) {
                DataIdentifier dat = read.getVariable(var);
                long dim1 = (dat instanceof IndexedIdentifier) ? ((IndexedIdentifier) dat).getOrigDim1() : dat.getDim1();
                long dim2 = (dat instanceof IndexedIdentifier) ? ((IndexedIdentifier) dat).getOrigDim2() : dat.getDim2();
                DataOp tread = new DataOp(var, DataType.MATRIX, ValueType.DOUBLE, DataOpTypes.TRANSIENTREAD, dat.getFilename(), dim1, dim2, dat.getNnz(), blocksize, blocksize);
                tread.setRequiresCheckpoint(true);
                DataOp twrite = new DataOp(var, DataType.MATRIX, ValueType.DOUBLE, tread, DataOpTypes.TRANSIENTWRITE, null);
                HopRewriteUtils.setOutputParameters(twrite, dim1, dim2, blocksize, blocksize, dat.getNnz());
                hops.add(twrite);
                livein.addVariable(var, read.getVariable(var));
                liveout.addVariable(var, read.getVariable(var));
            }
            sb0.set_hops(hops);
            sb0.setLiveIn(livein);
            sb0.setLiveOut(liveout);
            ret.add(sb0);
            //maintain rewrite status
            status.setInjectedCheckpoints();
        }
    }
    //add original statement block to end
    ret.add(sb);
    return ret;
}
Also used : ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) DataIdentifier(org.apache.sysml.parser.DataIdentifier) ArrayList(java.util.ArrayList) Hop(org.apache.sysml.hops.Hop) VariableSet(org.apache.sysml.parser.VariableSet) DataOp(org.apache.sysml.hops.DataOp) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) StatementBlock(org.apache.sysml.parser.StatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) IndexedIdentifier(org.apache.sysml.parser.IndexedIdentifier)

Example 3 with IndexedIdentifier

use of org.apache.sysml.parser.IndexedIdentifier in project incubator-systemml by apache.

the class PydmlSyntacticValidator method exitIndexedExpression.

/**
	 * PyDML uses 0-based indexing, so we increment lower indices by 1
	 * when translating to DML.
	 *
	 * @param ctx the parse tree
	 */
@Override
public void exitIndexedExpression(IndexedExpressionContext ctx) {
    boolean isRowLower = (ctx.rowLower != null && !ctx.rowLower.isEmpty() && (ctx.rowLower.info.expr != null));
    boolean isRowUpper = (ctx.rowUpper != null && !ctx.rowUpper.isEmpty() && (ctx.rowUpper.info.expr != null));
    boolean isColLower = (ctx.colLower != null && !ctx.colLower.isEmpty() && (ctx.colLower.info.expr != null));
    boolean isColUpper = (ctx.colUpper != null && !ctx.colUpper.isEmpty() && (ctx.colUpper.info.expr != null));
    boolean isRowSliceImplicit = ctx.rowImplicitSlice != null;
    boolean isColSliceImplicit = ctx.colImplicitSlice != null;
    ExpressionInfo rowLower = isRowLower ? ctx.rowLower.info : null;
    ExpressionInfo rowUpper = isRowUpper ? ctx.rowUpper.info : null;
    ExpressionInfo colLower = isColLower ? ctx.colLower.info : null;
    ExpressionInfo colUpper = isColUpper ? ctx.colUpper.info : null;
    ctx.dataInfo.expr = new IndexedIdentifier(ctx.name.getText(), false, false);
    setFileLineColumn(ctx.dataInfo.expr, ctx);
    try {
        ArrayList<ArrayList<Expression>> exprList = new ArrayList<ArrayList<Expression>>();
        ArrayList<Expression> rowIndices = new ArrayList<Expression>();
        ArrayList<Expression> colIndices = new ArrayList<Expression>();
        if (!isRowLower && !isRowUpper) {
            // both not set
            rowIndices.add(null);
            rowIndices.add(null);
        } else if (isRowLower && isRowUpper) {
            // both set
            rowIndices.add(incrementByOne(rowLower.expr, ctx));
            rowIndices.add(rowUpper.expr);
        } else if (isRowLower && !isRowUpper) {
            // Add given lower bound
            rowIndices.add(incrementByOne(rowLower.expr, ctx));
            if (isRowSliceImplicit) {
                // Add expression for nrow(X) for implicit upper bound
                Expression.BuiltinFunctionOp bop = Expression.BuiltinFunctionOp.NROW;
                DataIdentifier x = new DataIdentifier(ctx.name.getText());
                int line = ctx.start.getLine();
                int col = ctx.start.getCharPositionInLine();
                Expression expr = new BuiltinFunctionExpression(bop, new Expression[] { x }, currentFile, line, col, line, col);
                setFileLineColumn(expr, ctx);
                rowIndices.add(expr);
            }
        } else if (!isRowLower && isRowUpper && isRowSliceImplicit) {
            // Add expression for `1` for implicit lower bound
            // Note: We go ahead and increment by 1 to convert from 0-based to 1-based indexing
            int line = ctx.start.getLine();
            int col = ctx.start.getCharPositionInLine();
            IntIdentifier one = new IntIdentifier(1, currentFile, line, col, line, col);
            setFileLineColumn(one, ctx);
            rowIndices.add(one);
            // Add given upper bound
            rowIndices.add(rowUpper.expr);
        } else {
            notifyErrorListeners("incorrect index expression for row", ctx.start);
            return;
        }
        if (!isColLower && !isColUpper) {
            // both not set
            colIndices.add(null);
            colIndices.add(null);
        } else if (isColLower && isColUpper) {
            colIndices.add(incrementByOne(colLower.expr, ctx));
            colIndices.add(colUpper.expr);
        } else if (isColLower && !isColUpper) {
            // Add given lower bound
            colIndices.add(incrementByOne(colLower.expr, ctx));
            if (isColSliceImplicit) {
                // Add expression for ncol(X) for implicit upper bound
                Expression.BuiltinFunctionOp bop = Expression.BuiltinFunctionOp.NCOL;
                DataIdentifier x = new DataIdentifier(ctx.name.getText());
                int line = ctx.start.getLine();
                int col = ctx.start.getCharPositionInLine();
                Expression expr = new BuiltinFunctionExpression(bop, new Expression[] { x }, currentFile, line, col, line, col);
                setFileLineColumn(expr, ctx);
                colIndices.add(expr);
            }
        } else if (!isColLower && isColUpper && isColSliceImplicit) {
            // Add expression for `1` for implicit lower bound
            // Note: We go ahead and increment by 1 to convert from 0-based to 1-based indexing
            int line = ctx.start.getLine();
            int col = ctx.start.getCharPositionInLine();
            IntIdentifier one = new IntIdentifier(1, currentFile, line, col, line, col);
            setFileLineColumn(one, ctx);
            colIndices.add(one);
            // Add given upper bound
            colIndices.add(colUpper.expr);
        } else {
            notifyErrorListeners("incorrect index expression for column", ctx.start);
            return;
        }
        exprList.add(rowIndices);
        exprList.add(colIndices);
        ((IndexedIdentifier) ctx.dataInfo.expr).setIndices(exprList);
    } catch (Exception e) {
        notifyErrorListeners("cannot set the indices", ctx.start);
        return;
    }
}
Also used : DataIdentifier(org.apache.sysml.parser.DataIdentifier) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) ArrayList(java.util.ArrayList) ExpressionInfo(org.apache.sysml.parser.common.ExpressionInfo) LanguageException(org.apache.sysml.parser.LanguageException) ParseException(org.apache.sysml.parser.ParseException) BinaryExpression(org.apache.sysml.parser.BinaryExpression) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) IntIdentifier(org.apache.sysml.parser.IntIdentifier) IndexedIdentifier(org.apache.sysml.parser.IndexedIdentifier)

Aggregations

ArrayList (java.util.ArrayList)3 IndexedIdentifier (org.apache.sysml.parser.IndexedIdentifier)3 DataIdentifier (org.apache.sysml.parser.DataIdentifier)2 Expression (org.apache.sysml.parser.Expression)2 LanguageException (org.apache.sysml.parser.LanguageException)2 ParameterExpression (org.apache.sysml.parser.ParameterExpression)2 ParseException (org.apache.sysml.parser.ParseException)2 ExpressionInfo (org.apache.sysml.parser.common.ExpressionInfo)2 DataOp (org.apache.sysml.hops.DataOp)1 Hop (org.apache.sysml.hops.Hop)1 BinaryExpression (org.apache.sysml.parser.BinaryExpression)1 BuiltinFunctionExpression (org.apache.sysml.parser.BuiltinFunctionExpression)1 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)1 IntIdentifier (org.apache.sysml.parser.IntIdentifier)1 StatementBlock (org.apache.sysml.parser.StatementBlock)1 VariableSet (org.apache.sysml.parser.VariableSet)1 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)1