Search in sources :

Example 1 with HopsException

use of org.apache.sysml.hops.HopsException in project incubator-systemml by apache.

the class DMLTranslator method constructHops.

public void constructHops(StatementBlock sb) throws ParseException, LanguageException {
    if (sb instanceof WhileStatementBlock) {
        constructHopsForWhileControlBlock((WhileStatementBlock) sb);
        return;
    }
    if (sb instanceof IfStatementBlock) {
        constructHopsForIfControlBlock((IfStatementBlock) sb);
        return;
    }
    if (sb instanceof ForStatementBlock) {
        //NOTE: applies to ForStatementBlock and ParForStatementBlock
        constructHopsForForControlBlock((ForStatementBlock) sb);
        return;
    }
    if (sb instanceof FunctionStatementBlock) {
        constructHopsForFunctionControlBlock((FunctionStatementBlock) sb);
        return;
    }
    HashMap<String, Hop> ids = new HashMap<String, Hop>();
    ArrayList<Hop> output = new ArrayList<Hop>();
    VariableSet liveIn = sb.liveIn();
    VariableSet liveOut = sb.liveOut();
    VariableSet updated = sb._updated;
    VariableSet gen = sb._gen;
    VariableSet updatedLiveOut = new VariableSet();
    // handle liveout variables that are updated --> target identifiers for Assignment
    HashMap<String, Integer> liveOutToTemp = new HashMap<String, Integer>();
    for (int i = 0; i < sb.getNumStatements(); i++) {
        Statement current = sb.getStatement(i);
        if (current instanceof AssignmentStatement) {
            AssignmentStatement as = (AssignmentStatement) current;
            DataIdentifier target = as.getTarget();
            if (target != null) {
                if (liveOut.containsVariable(target.getName())) {
                    liveOutToTemp.put(target.getName(), Integer.valueOf(i));
                }
            }
        }
        if (current instanceof MultiAssignmentStatement) {
            MultiAssignmentStatement mas = (MultiAssignmentStatement) current;
            for (DataIdentifier target : mas.getTargetList()) {
                if (liveOut.containsVariable(target.getName())) {
                    liveOutToTemp.put(target.getName(), Integer.valueOf(i));
                }
            }
        }
    }
    //	(i.e., from LV analysis, updated and gen sets)
    if (!liveIn.getVariables().values().isEmpty()) {
        for (String varName : liveIn.getVariables().keySet()) {
            if (updated.containsVariable(varName) || gen.containsVariable(varName)) {
                DataIdentifier var = liveIn.getVariables().get(varName);
                long actualDim1 = (var instanceof IndexedIdentifier) ? ((IndexedIdentifier) var).getOrigDim1() : var.getDim1();
                long actualDim2 = (var instanceof IndexedIdentifier) ? ((IndexedIdentifier) var).getOrigDim2() : var.getDim2();
                DataOp read = new DataOp(var.getName(), var.getDataType(), var.getValueType(), DataOpTypes.TRANSIENTREAD, null, actualDim1, actualDim2, var.getNnz(), var.getRowsInBlock(), var.getColumnsInBlock());
                read.setAllPositions(var.getBeginLine(), var.getBeginColumn(), var.getEndLine(), var.getEndColumn());
                ids.put(varName, read);
            }
        }
    }
    for (int i = 0; i < sb.getNumStatements(); i++) {
        Statement current = sb.getStatement(i);
        if (current instanceof OutputStatement) {
            OutputStatement os = (OutputStatement) current;
            DataExpression source = os.getSource();
            DataIdentifier target = os.getIdentifier();
            //error handling unsupported indexing expression in write statement
            if (target instanceof IndexedIdentifier) {
                throw new LanguageException(source.printErrorLocation() + ": Unsupported indexing expression in write statement. " + "Please, assign the right indexing result to a variable and write this variable.");
            }
            DataOp ae = (DataOp) processExpression(source, target, ids);
            String formatName = os.getExprParam(DataExpression.FORMAT_TYPE).toString();
            ae.setInputFormatType(Expression.convertFormatType(formatName));
            if (ae.getDataType() == DataType.SCALAR) {
                ae.setOutputParams(ae.getDim1(), ae.getDim2(), ae.getNnz(), ae.getUpdateType(), -1, -1);
            } else {
                switch(ae.getInputFormatType()) {
                    case TEXT:
                    case MM:
                    case CSV:
                        // write output in textcell format
                        ae.setOutputParams(ae.getDim1(), ae.getDim2(), ae.getNnz(), ae.getUpdateType(), -1, -1);
                        break;
                    case BINARY:
                        // write output in binary block format
                        ae.setOutputParams(ae.getDim1(), ae.getDim2(), ae.getNnz(), ae.getUpdateType(), ConfigurationManager.getBlocksize(), ConfigurationManager.getBlocksize());
                        break;
                    default:
                        throw new LanguageException("Unrecognized file format: " + ae.getInputFormatType());
                }
            }
            output.add(ae);
        }
        if (current instanceof PrintStatement) {
            DataIdentifier target = createTarget();
            target.setDataType(DataType.SCALAR);
            target.setValueType(ValueType.STRING);
            target.setAllPositions(current.getFilename(), current.getBeginLine(), target.getBeginColumn(), current.getEndLine(), current.getEndColumn());
            PrintStatement ps = (PrintStatement) current;
            PRINTTYPE ptype = ps.getType();
            try {
                if (ptype == PRINTTYPE.PRINT) {
                    Hop.OpOp1 op = Hop.OpOp1.PRINT;
                    Expression source = ps.getExpressions().get(0);
                    Hop ae = processExpression(source, target, ids);
                    Hop printHop = new UnaryOp(target.getName(), target.getDataType(), target.getValueType(), op, ae);
                    printHop.setAllPositions(current.getBeginLine(), current.getBeginColumn(), current.getEndLine(), current.getEndColumn());
                    output.add(printHop);
                } else if (ptype == PRINTTYPE.STOP) {
                    Hop.OpOp1 op = Hop.OpOp1.STOP;
                    Expression source = ps.getExpressions().get(0);
                    Hop ae = processExpression(source, target, ids);
                    Hop stopHop = new UnaryOp(target.getName(), target.getDataType(), target.getValueType(), op, ae);
                    stopHop.setAllPositions(current.getBeginLine(), current.getBeginColumn(), current.getEndLine(), current.getEndColumn());
                    output.add(stopHop);
                } else if (ptype == PRINTTYPE.PRINTF) {
                    List<Expression> expressions = ps.getExpressions();
                    Hop[] inHops = new Hop[expressions.size()];
                    // Hop (ie, MultipleOp) as input Hops
                    for (int j = 0; j < expressions.size(); j++) {
                        Hop inHop = processExpression(expressions.get(j), target, ids);
                        inHops[j] = inHop;
                    }
                    target.setValueType(ValueType.STRING);
                    Hop printfHop = new MultipleOp(target.getName(), target.getDataType(), target.getValueType(), MultiInputOp.PRINTF, inHops);
                    output.add(printfHop);
                }
            } catch (HopsException e) {
                throw new LanguageException(e);
            }
        }
        if (current instanceof AssignmentStatement) {
            AssignmentStatement as = (AssignmentStatement) current;
            DataIdentifier target = as.getTarget();
            Expression source = as.getSource();
            // CASE: regular assignment statement -- source is DML expression that is NOT user-defined or external function 
            if (!(source instanceof FunctionCallIdentifier)) {
                // CASE: target is regular data identifier
                if (!(target instanceof IndexedIdentifier)) {
                    Hop ae = processExpression(source, target, ids);
                    ids.put(target.getName(), ae);
                    target.setProperties(source.getOutput());
                    Integer statementId = liveOutToTemp.get(target.getName());
                    if ((statementId != null) && (statementId.intValue() == i)) {
                        DataOp transientwrite = new DataOp(target.getName(), target.getDataType(), target.getValueType(), ae, DataOpTypes.TRANSIENTWRITE, null);
                        transientwrite.setOutputParams(ae.getDim1(), ae.getDim2(), ae.getNnz(), ae.getUpdateType(), ae.getRowsInBlock(), ae.getColsInBlock());
                        transientwrite.setAllPositions(target.getBeginLine(), target.getBeginColumn(), target.getEndLine(), target.getEndLine());
                        updatedLiveOut.addVariable(target.getName(), target);
                        output.add(transientwrite);
                    }
                } else // end if (!(target instanceof IndexedIdentifier)) {
                // CASE: target is indexed identifier (left-hand side indexed expression)
                {
                    Hop ae = processLeftIndexedExpression(source, (IndexedIdentifier) target, ids);
                    ids.put(target.getName(), ae);
                    // obtain origDim values BEFORE they are potentially updated during setProperties call
                    //	(this is incorrect for LHS Indexing)
                    long origDim1 = ((IndexedIdentifier) target).getOrigDim1();
                    long origDim2 = ((IndexedIdentifier) target).getOrigDim2();
                    target.setProperties(source.getOutput());
                    ((IndexedIdentifier) target).setOriginalDimensions(origDim1, origDim2);
                    // (required for scalar input to left indexing)					
                    if (target.getDataType() != DataType.MATRIX) {
                        target.setDataType(DataType.MATRIX);
                        target.setValueType(ValueType.DOUBLE);
                        target.setBlockDimensions(ConfigurationManager.getBlocksize(), ConfigurationManager.getBlocksize());
                    }
                    Integer statementId = liveOutToTemp.get(target.getName());
                    if ((statementId != null) && (statementId.intValue() == i)) {
                        DataOp transientwrite = new DataOp(target.getName(), target.getDataType(), target.getValueType(), ae, DataOpTypes.TRANSIENTWRITE, null);
                        transientwrite.setOutputParams(origDim1, origDim2, ae.getNnz(), ae.getUpdateType(), ae.getRowsInBlock(), ae.getColsInBlock());
                        transientwrite.setAllPositions(target.getBeginLine(), target.getBeginColumn(), target.getEndLine(), target.getEndColumn());
                        updatedLiveOut.addVariable(target.getName(), target);
                        output.add(transientwrite);
                    }
                }
            } else {
                //assignment, function call
                FunctionCallIdentifier fci = (FunctionCallIdentifier) source;
                FunctionStatementBlock fsb = this._dmlProg.getFunctionStatementBlock(fci.getNamespace(), fci.getName());
                //error handling missing function
                if (fsb == null) {
                    String error = source.printErrorLocation() + "function " + fci.getName() + " is undefined in namespace " + fci.getNamespace();
                    LOG.error(error);
                    throw new LanguageException(error);
                }
                //error handling unsupported function call in indexing expression
                if (target instanceof IndexedIdentifier) {
                    String fkey = DMLProgram.constructFunctionKey(fci.getNamespace(), fci.getName());
                    throw new LanguageException("Unsupported function call to '" + fkey + "' in left indexing expression. " + "Please, assign the function output to a variable.");
                }
                ArrayList<Hop> finputs = new ArrayList<Hop>();
                for (ParameterExpression paramName : fci.getParamExprs()) {
                    Hop in = processExpression(paramName.getExpr(), null, ids);
                    finputs.add(in);
                }
                //create function op
                FunctionType ftype = fsb.getFunctionOpType();
                FunctionOp fcall = null;
                if (target == null) {
                    fcall = new FunctionOp(ftype, fci.getNamespace(), fci.getName(), finputs, new String[] {});
                } else {
                    fcall = new FunctionOp(ftype, fci.getNamespace(), fci.getName(), finputs, new String[] { target.getName() });
                }
                output.add(fcall);
            //TODO function output dataops (phase 3)
            //DataOp trFoutput = new DataOp(target.getName(), target.getDataType(), target.getValueType(), fcall, DataOpTypes.FUNCTIONOUTPUT, null);
            //DataOp twFoutput = new DataOp(target.getName(), target.getDataType(), target.getValueType(), trFoutput, DataOpTypes.TRANSIENTWRITE, null);					
            }
        } else if (current instanceof MultiAssignmentStatement) {
            //multi-assignment, by definition a function call
            MultiAssignmentStatement mas = (MultiAssignmentStatement) current;
            Expression source = mas.getSource();
            if (source instanceof FunctionCallIdentifier) {
                FunctionCallIdentifier fci = (FunctionCallIdentifier) source;
                FunctionStatementBlock fsb = this._dmlProg.getFunctionStatementBlock(fci.getNamespace(), fci.getName());
                FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
                if (fstmt == null) {
                    LOG.error(source.printErrorLocation() + "function " + fci.getName() + " is undefined in namespace " + fci.getNamespace());
                    throw new LanguageException(source.printErrorLocation() + "function " + fci.getName() + " is undefined in namespace " + fci.getNamespace());
                }
                ArrayList<Hop> finputs = new ArrayList<Hop>();
                for (ParameterExpression paramName : fci.getParamExprs()) {
                    Hop in = processExpression(paramName.getExpr(), null, ids);
                    finputs.add(in);
                }
                //create function op
                String[] foutputs = new String[mas.getTargetList().size()];
                int count = 0;
                for (DataIdentifier paramName : mas.getTargetList()) {
                    foutputs[count++] = paramName.getName();
                }
                FunctionType ftype = fsb.getFunctionOpType();
                FunctionOp fcall = new FunctionOp(ftype, fci.getNamespace(), fci.getName(), finputs, foutputs);
                output.add(fcall);
            //TODO function output dataops (phase 3)
            /*for ( DataIdentifier paramName : mas.getTargetList() ){
						DataOp twFoutput = new DataOp(paramName.getName(), paramName.getDataType(), paramName.getValueType(), fcall, DataOpTypes.TRANSIENTWRITE, null);
						output.add(twFoutput);
					}*/
            } else if (source instanceof BuiltinFunctionExpression && ((BuiltinFunctionExpression) source).multipleReturns()) {
                // construct input hops
                Hop fcall = processMultipleReturnBuiltinFunctionExpression((BuiltinFunctionExpression) source, mas.getTargetList(), ids);
                output.add(fcall);
            } else if (source instanceof ParameterizedBuiltinFunctionExpression && ((ParameterizedBuiltinFunctionExpression) source).multipleReturns()) {
                // construct input hops
                Hop fcall = processMultipleReturnParameterizedBuiltinFunctionExpression((ParameterizedBuiltinFunctionExpression) source, mas.getTargetList(), ids);
                output.add(fcall);
            } else
                throw new LanguageException("Class \"" + source.getClass() + "\" is not supported in Multiple Assignment statements");
        }
    }
    sb.updateLiveVariablesOut(updatedLiveOut);
    sb.set_hops(output);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MultipleOp(org.apache.sysml.hops.MultipleOp) PRINTTYPE(org.apache.sysml.parser.PrintStatement.PRINTTYPE) DataOp(org.apache.sysml.hops.DataOp) AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) UnaryOp(org.apache.sysml.hops.UnaryOp) FunctionType(org.apache.sysml.hops.FunctionOp.FunctionType) Hop(org.apache.sysml.hops.Hop) ParameterizedBuiltinFunctionOp(org.apache.sysml.parser.Expression.ParameterizedBuiltinFunctionOp) BuiltinFunctionOp(org.apache.sysml.parser.Expression.BuiltinFunctionOp) FunctionOp(org.apache.sysml.hops.FunctionOp) HopsException(org.apache.sysml.hops.HopsException)

Example 2 with HopsException

use of org.apache.sysml.hops.HopsException in project incubator-systemml by apache.

the class DMLTranslator method printLops.

public void printLops(StatementBlock current) throws ParseException, HopsException, LopsException {
    if (LOG.isDebugEnabled()) {
        ArrayList<Lop> lopsDAG = current.getLops();
        LOG.debug("\n********************** LOPS DAG FOR BLOCK *******************");
        if (current instanceof FunctionStatementBlock) {
            if (current.getNumStatements() > 1)
                LOG.debug("Function statement block has more than 1 stmt");
            FunctionStatement fstmt = (FunctionStatement) current.getStatement(0);
            for (StatementBlock child : fstmt.getBody()) {
                printLops(child);
            }
        }
        if (current instanceof WhileStatementBlock) {
            // print predicate lops 
            WhileStatementBlock wstb = (WhileStatementBlock) current;
            Hop predicateHops = ((WhileStatementBlock) current).getPredicateHops();
            LOG.debug("\n********************** PREDICATE LOPS *******************");
            Lop predicateLops = predicateHops.getLops();
            if (predicateLops == null)
                predicateLops = predicateHops.constructLops();
            predicateLops.printMe();
            if (wstb.getNumStatements() > 1) {
                LOG.error(wstb.printBlockErrorLocation() + "WhileStatementBlock has more than 1 statement");
                throw new HopsException(wstb.printBlockErrorLocation() + "WhileStatementBlock has more than 1 statement");
            }
            WhileStatement ws = (WhileStatement) wstb.getStatement(0);
            for (StatementBlock sb : ws.getBody()) {
                printLops(sb);
            }
        }
        if (current instanceof IfStatementBlock) {
            // print predicate lops 
            IfStatementBlock istb = (IfStatementBlock) current;
            Hop predicateHops = ((IfStatementBlock) current).getPredicateHops();
            LOG.debug("\n********************** PREDICATE LOPS *******************");
            Lop predicateLops = predicateHops.getLops();
            if (predicateLops == null)
                predicateLops = predicateHops.constructLops();
            predicateLops.printMe();
            if (istb.getNumStatements() > 1) {
                LOG.error(istb.printBlockErrorLocation() + "IfStatmentBlock has more than 1 statement");
                throw new HopsException(istb.printBlockErrorLocation() + "IfStatmentBlock has more than 1 statement");
            }
            IfStatement is = (IfStatement) istb.getStatement(0);
            LOG.debug("\n**** LOPS DAG FOR IF BODY ****");
            for (StatementBlock sb : is.getIfBody()) {
                printLops(sb);
            }
            if (!is.getElseBody().isEmpty()) {
                LOG.debug("\n**** LOPS DAG FOR IF BODY ****");
                for (StatementBlock sb : is.getElseBody()) {
                    printLops(sb);
                }
            }
        }
        if (current instanceof ForStatementBlock) {
            // print predicate lops 
            ForStatementBlock fsb = (ForStatementBlock) current;
            LOG.debug("\n********************** PREDICATE LOPS *******************");
            if (fsb.getFromHops() != null) {
                LOG.debug("FROM:");
                Lop llops = fsb.getFromLops();
                if (llops == null)
                    llops = fsb.getFromHops().constructLops();
                llops.printMe();
            }
            if (fsb.getToHops() != null) {
                LOG.debug("TO:");
                Lop llops = fsb.getToLops();
                if (llops == null)
                    llops = fsb.getToHops().constructLops();
                llops.printMe();
            }
            if (fsb.getIncrementHops() != null) {
                LOG.debug("INCREMENT:");
                Lop llops = fsb.getIncrementLops();
                if (llops == null)
                    llops = fsb.getIncrementHops().constructLops();
                llops.printMe();
            }
            if (fsb.getNumStatements() > 1) {
                LOG.error(fsb.printBlockErrorLocation() + "ForStatementBlock has more than 1 statement");
                throw new HopsException(fsb.printBlockErrorLocation() + "ForStatementBlock has more than 1 statement");
            }
            ForStatement ws = (ForStatement) fsb.getStatement(0);
            for (StatementBlock sb : ws.getBody()) {
                printLops(sb);
            }
        }
        if (lopsDAG != null && !lopsDAG.isEmpty()) {
            Iterator<Lop> iter = lopsDAG.iterator();
            while (iter.hasNext()) {
                LOG.debug("\n********************** OUTPUT LOPS *******************");
                iter.next().printMe();
            }
        }
    }
}
Also used : Hop(org.apache.sysml.hops.Hop) HopsException(org.apache.sysml.hops.HopsException) Lop(org.apache.sysml.lops.Lop)

Example 3 with HopsException

use of org.apache.sysml.hops.HopsException in project incubator-systemml by apache.

the class DMLTranslator method constructLops.

public void constructLops(StatementBlock sb) throws HopsException, LopsException {
    if (sb instanceof WhileStatementBlock) {
        WhileStatementBlock wsb = (WhileStatementBlock) sb;
        WhileStatement whileStmt = (WhileStatement) wsb.getStatement(0);
        ArrayList<StatementBlock> body = whileStmt.getBody();
        if (sb.get_hops() != null && !sb.get_hops().isEmpty()) {
            LOG.error(sb.printBlockErrorLocation() + "WhileStatementBlock should not have hops");
            throw new HopsException(sb.printBlockErrorLocation() + "WhileStatementBlock should not have hops");
        }
        // step through stmt blocks in while stmt body
        for (StatementBlock stmtBlock : body) {
            constructLops(stmtBlock);
        }
        // handle while stmt predicate
        Lop l = wsb.getPredicateHops().constructLops();
        wsb.set_predicateLops(l);
        wsb.updatePredicateRecompilationFlag();
    } else if (sb instanceof IfStatementBlock) {
        IfStatementBlock isb = (IfStatementBlock) sb;
        IfStatement ifStmt = (IfStatement) isb.getStatement(0);
        ArrayList<StatementBlock> ifBody = ifStmt.getIfBody();
        ArrayList<StatementBlock> elseBody = ifStmt.getElseBody();
        if (sb.get_hops() != null && !sb.get_hops().isEmpty()) {
            LOG.error(sb.printBlockErrorLocation() + "IfStatementBlock should not have hops");
            throw new HopsException(sb.printBlockErrorLocation() + "IfStatementBlock should not have hops");
        }
        // step through stmt blocks in if stmt ifBody
        for (StatementBlock stmtBlock : ifBody) constructLops(stmtBlock);
        // step through stmt blocks in if stmt elseBody
        for (StatementBlock stmtBlock : elseBody) constructLops(stmtBlock);
        // handle if stmt predicate
        Lop l = isb.getPredicateHops().constructLops();
        isb.set_predicateLops(l);
        isb.updatePredicateRecompilationFlag();
    } else if (//NOTE: applies to ForStatementBlock and ParForStatementBlock
    sb instanceof ForStatementBlock) {
        ForStatementBlock fsb = (ForStatementBlock) sb;
        ForStatement fs = (ForStatement) sb.getStatement(0);
        ArrayList<StatementBlock> body = fs.getBody();
        if (sb.get_hops() != null && !sb.get_hops().isEmpty()) {
            LOG.error(sb.printBlockErrorLocation() + "ForStatementBlock should not have hops");
            throw new HopsException(sb.printBlockErrorLocation() + "ForStatementBlock should not have hops");
        }
        // step through stmt blocks in FOR stmt body
        for (StatementBlock stmtBlock : body) constructLops(stmtBlock);
        // handle for stmt predicate
        if (fsb.getFromHops() != null) {
            Lop llobs = fsb.getFromHops().constructLops();
            fsb.setFromLops(llobs);
        }
        if (fsb.getToHops() != null) {
            Lop llobs = fsb.getToHops().constructLops();
            fsb.setToLops(llobs);
        }
        if (fsb.getIncrementHops() != null) {
            Lop llobs = fsb.getIncrementHops().constructLops();
            fsb.setIncrementLops(llobs);
        }
        fsb.updatePredicateRecompilationFlags();
    } else if (sb instanceof FunctionStatementBlock) {
        FunctionStatement functStmt = (FunctionStatement) sb.getStatement(0);
        ArrayList<StatementBlock> body = functStmt.getBody();
        if (sb.get_hops() != null && !sb.get_hops().isEmpty()) {
            LOG.error(sb.printBlockErrorLocation() + "FunctionStatementBlock should not have hops");
            throw new HopsException(sb.printBlockErrorLocation() + "FunctionStatementBlock should not have hops");
        }
        // step through stmt blocks in while stmt body
        for (StatementBlock stmtBlock : body) {
            constructLops(stmtBlock);
        }
    } else // handle default case for regular StatementBlock
    {
        if (sb.get_hops() == null)
            sb.set_hops(new ArrayList<Hop>());
        ArrayList<Lop> lops = new ArrayList<Lop>();
        for (Hop hop : sb.get_hops()) {
            lops.add(hop.constructLops());
        }
        sb.setLops(lops);
        sb.updateRecompilationFlag();
    }
}
Also used : ArrayList(java.util.ArrayList) Hop(org.apache.sysml.hops.Hop) HopsException(org.apache.sysml.hops.HopsException) Lop(org.apache.sysml.lops.Lop)

Example 4 with HopsException

use of org.apache.sysml.hops.HopsException in project incubator-systemml by apache.

the class RewriteBlockSizeAndReblock method rule_BlockSizeAndReblock.

private void rule_BlockSizeAndReblock(Hop hop, final int blocksize) throws HopsException {
    // Go to the source(s) of the DAG
    for (Hop hi : hop.getInput()) {
        if (!hi.isVisited())
            rule_BlockSizeAndReblock(hi, blocksize);
    }
    boolean canReblock = isReblockValid();
    if (hop instanceof DataOp) {
        DataOp dop = (DataOp) hop;
        // if block size does not match
        if (canReblock && ((dop.getDataType() == DataType.MATRIX && (dop.getRowsInBlock() != blocksize || dop.getColsInBlock() != blocksize)) || (dop.getDataType() == DataType.FRAME && OptimizerUtils.isSparkExecutionMode() && (dop.getInputFormatType() == FileFormatTypes.TEXT || dop.getInputFormatType() == FileFormatTypes.CSV && OptimizerUtils.ALLOW_FRAME_CSV_REBLOCK)))) {
            if (dop.getDataOpType() == DataOp.DataOpTypes.PERSISTENTREAD) {
                // insert reblock after the hop
                dop.setRequiresReblock(true);
                dop.setOutputBlocksizes(blocksize, blocksize);
            } else if (dop.getDataOpType() == DataOp.DataOpTypes.PERSISTENTWRITE) {
                if (dop.getRowsInBlock() == -1 && dop.getColsInBlock() == -1) {
                // if this dataop is for cell output, then no reblock is needed 
                // as (A) all jobtypes can produce block2cell and cell2cell and 
                // (B) we don't generate an explicit instruction for it (the info 
                // is conveyed through OutputInfo.
                } else if (dop.getInput().get(0).requiresReblock() && dop.getInput().get(0).getParent().size() == 1) {
                    // if a reblock is feeding into this, then use it if this is
                    // the only parent, otherwise new Reblock
                    dop.getInput().get(0).setOutputBlocksizes(dop.getRowsInBlock(), dop.getColsInBlock());
                } else {
                    // insert reblock after the hop
                    dop.setRequiresReblock(true);
                    dop.setOutputBlocksizes(blocksize, blocksize);
                }
            } else if (dop.getDataOpType() == DataOp.DataOpTypes.TRANSIENTWRITE || dop.getDataOpType() == DataOp.DataOpTypes.TRANSIENTREAD) {
                if (DMLScript.rtplatform == RUNTIME_PLATFORM.SINGLE_NODE) {
                    // simply copy the values from its input
                    dop.setRowsInBlock(hop.getInput().get(0).getRowsInBlock());
                    dop.setColsInBlock(hop.getInput().get(0).getColsInBlock());
                } else {
                    // by default, all transient reads and writes are in blocked format
                    dop.setRowsInBlock(blocksize);
                    dop.setColsInBlock(blocksize);
                }
            } else {
                throw new HopsException(hop.printErrorLocation() + "unexpected non-scalar Data HOP in reblock.\n");
            }
        }
    } else //TODO remove once transform rebased to frames
    if ((hop instanceof ParameterizedBuiltinOp && ((ParameterizedBuiltinOp) hop).getOp() == ParamBuiltinOp.TRANSFORM)) {
        // check if there exists a non-csv-write output. If yes, add reblock
        boolean rblk = false;
        for (Hop out : hop.getParent()) {
            if (!(out instanceof DataOp && ((DataOp) out).getDataOpType() == DataOpTypes.PERSISTENTWRITE && ((DataOp) out).getInputFormatType() == FileFormatTypes.CSV)) {
                rblk = true;
                break;
            }
        }
        if (rblk) {
            hop.setRequiresReblock(true);
            hop.setOutputBlocksizes(blocksize, blocksize);
        }
    } else //NO DATAOP 
    {
        if (hop.requiresReblock()) {
            hop.setRowsInBlock(blocksize);
            hop.setColsInBlock(blocksize);
        } else // Constraint C2:
        if (hop.getDataType() == DataType.SCALAR) {
            hop.setRowsInBlock(-1);
            hop.setColsInBlock(-1);
        } else // Constraint C3:
        {
            if (!canReblock) {
                hop.setRowsInBlock(-1);
                hop.setColsInBlock(-1);
            } else {
                hop.setRowsInBlock(blocksize);
                hop.setColsInBlock(blocksize);
                // Reblock properties need to be set for each output.
                if (hop instanceof FunctionOp) {
                    FunctionOp fop = (FunctionOp) hop;
                    if (fop.getOutputs() != null) {
                        for (Hop out : fop.getOutputs()) {
                            out.setRowsInBlock(blocksize);
                            out.setColsInBlock(blocksize);
                        }
                    }
                }
            }
            // if any input is not blocked then the output of current Hop should not be blocked
            for (Hop h : hop.getInput()) {
                if (h.getDataType() == DataType.MATRIX && h.getRowsInBlock() == -1 && h.getColsInBlock() == -1) {
                    hop.setRowsInBlock(-1);
                    hop.setColsInBlock(-1);
                    break;
                }
            }
        }
    }
    hop.setVisited();
}
Also used : ParameterizedBuiltinOp(org.apache.sysml.hops.ParameterizedBuiltinOp) Hop(org.apache.sysml.hops.Hop) FunctionOp(org.apache.sysml.hops.FunctionOp) HopsException(org.apache.sysml.hops.HopsException) DataOp(org.apache.sysml.hops.DataOp)

Example 5 with HopsException

use of org.apache.sysml.hops.HopsException in project incubator-systemml by apache.

the class RewriteConstantFolding method rConstantFoldingExpression.

private Hop rConstantFoldingExpression(Hop root) throws HopsException {
    if (root.isVisited())
        return root;
    //no iterator in order to prevent concurrent modification
    for (int i = 0; i < root.getInput().size(); i++) {
        Hop h = root.getInput().get(i);
        rConstantFoldingExpression(h);
    }
    LiteralOp literal = null;
    //fold binary op if both are literals / unary op if literal
    if (//scalar ouput
    root.getDataType() == DataType.SCALAR && (isApplicableBinaryOp(root) || isApplicableUnaryOp(root))) {
        //core constant folding via runtime instructions
        try {
            literal = evalScalarOperation(root);
        } catch (Exception ex) {
            LOG.error("Failed to execute constant folding instructions. No abort.", ex);
        }
    } else //fold conjunctive predicate if at least one input is literal 'false'
    if (isApplicableFalseConjunctivePredicate(root)) {
        literal = new LiteralOp(false);
    } else //fold disjunctive predicate if at least one input is literal 'true'
    if (isApplicableTrueDisjunctivePredicate(root)) {
        literal = new LiteralOp(true);
    }
    //replace binary operator with folded constant
    if (literal != null) {
        //reverse replacement in order to keep common subexpression elimination
        int plen = root.getParent().size();
        if (//broot is NOT a DAG root
        plen > 0) {
            for (//for all parents
            int i = 0; //for all parents
            i < root.getParent().size(); //for all parents
            i++) {
                Hop parent = root.getParent().get(i);
                for (int j = 0; j < parent.getInput().size(); j++) {
                    Hop child = parent.getInput().get(j);
                    if (root == child) {
                        //replace operator
                        //root to parent link cannot be removed within this loop, as loop iterates over list containing parents.
                        parent.getInput().remove(j);
                        HopRewriteUtils.addChildReference(parent, literal, j);
                    }
                }
            }
            root.getParent().clear();
        } else //broot IS a DAG root
        {
            root = literal;
        }
    }
    //mark processed
    root.setVisited();
    return root;
}
Also used : Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp) HopsException(org.apache.sysml.hops.HopsException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) IOException(java.io.IOException) LopsException(org.apache.sysml.lops.LopsException)

Aggregations

HopsException (org.apache.sysml.hops.HopsException)24 Hop (org.apache.sysml.hops.Hop)14 ArrayList (java.util.ArrayList)8 DataOp (org.apache.sysml.hops.DataOp)7 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)7 HashMap (java.util.HashMap)5 LiteralOp (org.apache.sysml.hops.LiteralOp)5 AggUnaryOp (org.apache.sysml.hops.AggUnaryOp)4 UnaryOp (org.apache.sysml.hops.UnaryOp)4 DataIdentifier (org.apache.sysml.parser.DataIdentifier)4 LanguageException (org.apache.sysml.parser.LanguageException)4 StatementBlock (org.apache.sysml.parser.StatementBlock)4 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)4 Lop (org.apache.sysml.lops.Lop)3 LopsException (org.apache.sysml.lops.LopsException)3 ParseException (org.apache.sysml.parser.ParseException)3 ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 FunctionOp (org.apache.sysml.hops.FunctionOp)2