Search in sources :

Example 6 with HopsException

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

the class ScriptExecutor method rewritePersistentReadsAndWrites.

/**
	 * Replace persistent reads and writes with transient reads and writes in
	 * the symbol table.
	 */
protected void rewritePersistentReadsAndWrites() {
    LocalVariableMap symbolTable = script.getSymbolTable();
    if (symbolTable != null) {
        String[] inputs = (script.getInputVariables() == null) ? new String[0] : script.getInputVariables().toArray(new String[0]);
        String[] outputs = (script.getOutputVariables() == null) ? new String[0] : script.getOutputVariables().toArray(new String[0]);
        RewriteRemovePersistentReadWrite rewrite = new RewriteRemovePersistentReadWrite(inputs, outputs, script.getSymbolTable());
        ProgramRewriter programRewriter = new ProgramRewriter(rewrite);
        try {
            programRewriter.rewriteProgramHopDAGs(dmlProgram);
        } catch (LanguageException e) {
            throw new MLContextException("Exception occurred while rewriting persistent reads and writes", e);
        } catch (HopsException e) {
            throw new MLContextException("Exception occurred while rewriting persistent reads and writes", e);
        }
    }
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) ProgramRewriter(org.apache.sysml.hops.rewrite.ProgramRewriter) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) HopsException(org.apache.sysml.hops.HopsException) RewriteRemovePersistentReadWrite(org.apache.sysml.hops.rewrite.RewriteRemovePersistentReadWrite)

Example 7 with HopsException

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

the class RewriteMatrixMultChainOptimization method clearLinksWithinChain.

private void clearLinksWithinChain(Hop hop, ArrayList<Hop> operators) throws HopsException {
    for (int i = 0; i < operators.size(); i++) {
        Hop op = operators.get(i);
        if (op.getInput().size() != 2 || (i != 0 && op.getParent().size() > 1)) {
            throw new HopsException(hop.printErrorLocation() + "Unexpected error while applying optimization on matrix-mult chain. \n");
        }
        Hop input1 = op.getInput().get(0);
        Hop input2 = op.getInput().get(1);
        op.getInput().clear();
        input1.getParent().remove(op);
        input2.getParent().remove(op);
    }
}
Also used : Hop(org.apache.sysml.hops.Hop) HopsException(org.apache.sysml.hops.HopsException)

Example 8 with HopsException

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

the class RewriteMatrixMultChainOptimization method optimizeMMChain.

/**
	 * optimizeMMChain(): It optimizes the matrix multiplication chain in which
	 * the last Hop is "this". Step-1) Identify the chain (mmChain). (Step-2) clear all
	 * links among the Hops that are involved in mmChain. (Step-3) Find the
	 * optimal ordering (dynamic programming) (Step-4) Relink the hops in
	 * mmChain.
	 * 
	 * @param hop high-level operator
	 * @throws HopsException if HopsException occurs
	 */
private void optimizeMMChain(Hop hop) throws HopsException {
    if (LOG.isTraceEnabled()) {
        LOG.trace("MM Chain Optimization for HOP: (" + hop.getClass().getSimpleName() + ", " + hop.getHopID() + ", " + hop.getName() + ")");
    }
    ArrayList<Hop> mmChain = new ArrayList<Hop>();
    ArrayList<Hop> mmOperators = new ArrayList<Hop>();
    ArrayList<Hop> tempList;
    // Step 1: Identify the chain (mmChain) & clear all links among the Hops
    // that are involved in mmChain.
    // Initialize mmChain with my inputs
    mmOperators.add(hop);
    for (Hop hi : hop.getInput()) mmChain.add(hi);
    // expand each Hop in mmChain to find the entire matrix multiplication
    // chain
    int i = 0;
    while (i < mmChain.size()) {
        boolean expandable = false;
        Hop h = mmChain.get(i);
        if (HopRewriteUtils.isMatrixMultiply(h) && !((AggBinaryOp) hop).hasLeftPMInput() && !h.isVisited()) {
            // not be expanded.
            if (h.getParent().size() > 1 || inputCount(h.getParent().get(0), h) > 1) {
                expandable = false;
                break;
            } else {
                expandable = true;
            }
        }
        h.setVisited();
        if (!expandable) {
            i = i + 1;
        } else {
            tempList = mmChain.get(i).getInput();
            if (tempList.size() != 2) {
                throw new HopsException(hop.printErrorLocation() + "Hops::rule_OptimizeMMChain(): AggBinary must have exactly two inputs.");
            }
            // add current operator to mmOperators, and its input nodes to mmChain
            mmOperators.add(mmChain.get(i));
            mmChain.set(i, tempList.get(0));
            mmChain.add(i + 1, tempList.get(1));
        }
    }
    // print the MMChain
    if (LOG.isTraceEnabled()) {
        LOG.trace("Identified MM Chain: ");
        for (Hop h : mmChain) {
            logTraceHop(h, 1);
        }
    }
    if (mmChain.size() == 2) {
        // If the chain size is 2, then there is nothing to optimize.
        return;
    } else {
        // Step 2: construct dims array
        double[] dimsArray = new double[mmChain.size() + 1];
        boolean dimsKnown = getDimsArray(hop, mmChain, dimsArray);
        if (dimsKnown) {
            // Step 3: clear the links among Hops within the identified chain
            clearLinksWithinChain(hop, mmOperators);
            // Step 4: Find the optimal ordering via dynamic programming.
            // Invoke Dynamic Programming
            int size = mmChain.size();
            int[][] split = mmChainDP(dimsArray, mmChain.size());
            // Step 5: Relink the hops using the optimal ordering (split[][]) found from DP.
            LOG.trace("Optimal MM Chain: ");
            mmChainRelinkHops(mmOperators.get(0), 0, size - 1, mmChain, mmOperators, 1, split, 1);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Hop(org.apache.sysml.hops.Hop) HopsException(org.apache.sysml.hops.HopsException)

Example 9 with HopsException

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

the class RewriteSplitDagUnknownCSVRead method rewriteStatementBlock.

@Override
public ArrayList<StatementBlock> rewriteStatementBlock(StatementBlock sb, ProgramRewriteStatus state) throws HopsException {
    ArrayList<StatementBlock> ret = new ArrayList<StatementBlock>();
    //collect all unknown csv reads hops
    ArrayList<Hop> cand = new ArrayList<Hop>();
    collectCSVReadHopsUnknownSize(sb.get_hops(), cand);
    //split hop dag on demand
    if (!cand.isEmpty()) {
        try {
            //duplicate sb incl live variable sets
            StatementBlock sb1 = new StatementBlock();
            sb1.setDMLProg(sb.getDMLProg());
            sb1.setAllPositions(sb.getFilename(), sb.getBeginLine(), sb.getBeginColumn(), sb.getEndLine(), sb.getEndColumn());
            sb1.setLiveIn(new VariableSet());
            sb1.setLiveOut(new VariableSet());
            //move csv reads incl reblock to new statement block
            //(and replace original persistent read with transient read)
            ArrayList<Hop> sb1hops = new ArrayList<Hop>();
            for (Hop c : cand) {
                Hop reblock = c;
                long rlen = reblock.getDim1();
                long clen = reblock.getDim2();
                long nnz = reblock.getNnz();
                UpdateType update = c.getUpdateType();
                long brlen = reblock.getRowsInBlock();
                long bclen = reblock.getColsInBlock();
                //create new transient read
                DataOp tread = new DataOp(reblock.getName(), reblock.getDataType(), reblock.getValueType(), DataOpTypes.TRANSIENTREAD, null, rlen, clen, nnz, update, brlen, bclen);
                HopRewriteUtils.copyLineNumbers(reblock, tread);
                //replace reblock with transient read
                ArrayList<Hop> parents = new ArrayList<Hop>(reblock.getParent());
                for (int i = 0; i < parents.size(); i++) {
                    Hop parent = parents.get(i);
                    HopRewriteUtils.replaceChildReference(parent, reblock, tread);
                }
                //add reblock sub dag to first statement block
                DataOp twrite = new DataOp(reblock.getName(), reblock.getDataType(), reblock.getValueType(), reblock, DataOpTypes.TRANSIENTWRITE, null);
                twrite.setOutputParams(rlen, clen, nnz, update, brlen, bclen);
                HopRewriteUtils.copyLineNumbers(reblock, twrite);
                sb1hops.add(twrite);
                //update live in and out of new statement block (for piggybacking)
                DataIdentifier diVar = sb.variablesRead().getVariable(reblock.getName());
                if (diVar != null) {
                    //var read should always exist because persistent read
                    sb1.liveOut().addVariable(reblock.getName(), new DataIdentifier(diVar));
                    sb.liveIn().addVariable(reblock.getName(), new DataIdentifier(diVar));
                }
            }
            sb1.set_hops(sb1hops);
            sb1.updateRecompilationFlag();
            //statement block with csv reblocks
            ret.add(sb1);
            //statement block with remaining hops
            ret.add(sb);
        } catch (Exception ex) {
            throw new HopsException("Failed to split hops dag for csv read with unknown size.", ex);
        }
        LOG.debug("Applied splitDagUnknownCSVRead.");
    } else //keep original hop dag
    {
        ret.add(sb);
    }
    return ret;
}
Also used : DataIdentifier(org.apache.sysml.parser.DataIdentifier) ArrayList(java.util.ArrayList) Hop(org.apache.sysml.hops.Hop) HopsException(org.apache.sysml.hops.HopsException) UpdateType(org.apache.sysml.runtime.controlprogram.caching.MatrixObject.UpdateType) HopsException(org.apache.sysml.hops.HopsException) VariableSet(org.apache.sysml.parser.VariableSet) DataOp(org.apache.sysml.hops.DataOp) StatementBlock(org.apache.sysml.parser.StatementBlock)

Example 10 with HopsException

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

the class FunctionCallGraph method constructFunctionCallGraph.

private void constructFunctionCallGraph(DMLProgram prog) {
    if (!prog.hasFunctionStatementBlocks())
        //early abort if prog without functions
        return;
    try {
        Stack<String> fstack = new Stack<String>();
        HashSet<String> lfset = new HashSet<String>();
        _fGraph.put(MAIN_FUNCTION_KEY, new HashSet<String>());
        for (StatementBlock sblk : prog.getStatementBlocks()) rConstructFunctionCallGraph(MAIN_FUNCTION_KEY, sblk, fstack, lfset);
    } catch (HopsException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : HopsException(org.apache.sysml.hops.HopsException) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) StatementBlock(org.apache.sysml.parser.StatementBlock) Stack(java.util.Stack) HashSet(java.util.HashSet)

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