Search in sources :

Example 26 with HopsException

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

the class GraphBuilder method constructGDFGraph.

@SuppressWarnings("unchecked")
private static void constructGDFGraph(ProgramBlock pb, HashMap<String, GDFNode> roots) throws DMLRuntimeException, HopsException {
    if (pb instanceof FunctionProgramBlock) {
        throw new DMLRuntimeException("FunctionProgramBlocks not implemented yet.");
    } else if (pb instanceof WhileProgramBlock) {
        WhileProgramBlock wpb = (WhileProgramBlock) pb;
        WhileStatementBlock wsb = (WhileStatementBlock) pb.getStatementBlock();
        //construct predicate node (conceptually sequence of from/to/incr)
        GDFNode pred = constructGDFGraph(wsb.getPredicateHops(), wpb, new HashMap<Long, GDFNode>(), roots);
        HashMap<String, GDFNode> inputs = constructLoopInputNodes(wpb, wsb, roots);
        HashMap<String, GDFNode> lroots = (HashMap<String, GDFNode>) inputs.clone();
        //process childs blocks
        for (ProgramBlock pbc : wpb.getChildBlocks()) constructGDFGraph(pbc, lroots);
        HashMap<String, GDFNode> outputs = constructLoopOutputNodes(wsb, lroots);
        GDFLoopNode lnode = new GDFLoopNode(wpb, pred, inputs, outputs);
        //construct crossblock nodes
        constructLoopOutputCrossBlockNodes(wsb, lnode, outputs, roots, wpb);
    } else if (pb instanceof IfProgramBlock) {
        IfProgramBlock ipb = (IfProgramBlock) pb;
        IfStatementBlock isb = (IfStatementBlock) pb.getStatementBlock();
        //construct predicate
        if (isb.getPredicateHops() != null) {
            Hop pred = isb.getPredicateHops();
            roots.put(pred.getName(), constructGDFGraph(pred, ipb, new HashMap<Long, GDFNode>(), roots));
        }
        //construct if and else branch separately
        HashMap<String, GDFNode> ifRoots = (HashMap<String, GDFNode>) roots.clone();
        HashMap<String, GDFNode> elseRoots = (HashMap<String, GDFNode>) roots.clone();
        for (ProgramBlock pbc : ipb.getChildBlocksIfBody()) constructGDFGraph(pbc, ifRoots);
        if (ipb.getChildBlocksElseBody() != null)
            for (ProgramBlock pbc : ipb.getChildBlocksElseBody()) constructGDFGraph(pbc, elseRoots);
        //merge data flow roots (if no else, elseRoots refer to original roots)
        reconcileMergeIfProgramBlockOutputs(ifRoots, elseRoots, roots, ipb);
    } else if (//incl parfor
    pb instanceof ForProgramBlock) {
        ForProgramBlock fpb = (ForProgramBlock) pb;
        ForStatementBlock fsb = (ForStatementBlock) pb.getStatementBlock();
        //construct predicate node (conceptually sequence of from/to/incr)
        GDFNode pred = constructForPredicateNode(fpb, fsb, roots);
        HashMap<String, GDFNode> inputs = constructLoopInputNodes(fpb, fsb, roots);
        HashMap<String, GDFNode> lroots = (HashMap<String, GDFNode>) inputs.clone();
        //process childs blocks
        for (ProgramBlock pbc : fpb.getChildBlocks()) constructGDFGraph(pbc, lroots);
        HashMap<String, GDFNode> outputs = constructLoopOutputNodes(fsb, lroots);
        GDFLoopNode lnode = new GDFLoopNode(fpb, pred, inputs, outputs);
        //construct crossblock nodes
        constructLoopOutputCrossBlockNodes(fsb, lnode, outputs, roots, fpb);
    } else //last-level program block
    {
        StatementBlock sb = pb.getStatementBlock();
        ArrayList<Hop> hops = sb.get_hops();
        if (hops != null) {
            //create new local memo structure for local dag
            HashMap<Long, GDFNode> lmemo = new HashMap<Long, GDFNode>();
            for (Hop hop : hops) {
                //recursively construct GDF graph for hop dag root
                GDFNode root = constructGDFGraph(hop, pb, lmemo, roots);
                if (root == null)
                    throw new HopsException("GDFGraphBuilder: failed to constuct dag root for: " + Explain.explain(hop));
                //create cross block nodes for all transient writes
                if (hop instanceof DataOp && ((DataOp) hop).getDataOpType() == DataOpTypes.TRANSIENTWRITE)
                    root = new GDFCrossBlockNode(hop, pb, root, hop.getName());
                //add GDF root node to global roots 
                roots.put(hop.getName(), root);
            }
        }
    }
}
Also used : FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) HashMap(java.util.HashMap) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) Hop(org.apache.sysml.hops.Hop) ArrayList(java.util.ArrayList) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) HopsException(org.apache.sysml.hops.HopsException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) ProgramBlock(org.apache.sysml.runtime.controlprogram.ProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) DataOp(org.apache.sysml.hops.DataOp) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) StatementBlock(org.apache.sysml.parser.StatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock)

Example 27 with HopsException

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

the class GraphBuilder method constructLoopOutputNodes.

private static HashMap<String, GDFNode> constructLoopOutputNodes(StatementBlock fsb, HashMap<String, GDFNode> roots) throws HopsException {
    HashMap<String, GDFNode> ret = new HashMap<String, GDFNode>();
    Set<String> outvars = fsb.variablesUpdated().getVariableNames();
    for (String var : outvars) {
        GDFNode node = roots.get(var);
        //handle non-existing nodes
        if (node == null) {
            if (!IGNORE_UNBOUND_UPDATED_VARS)
                throw new HopsException("GDFGraphBuilder: failed to constuct loop output for variable: " + var);
            else
                //skip unbound updated variables	
                continue;
        }
        //add existing node to loop outputs 
        ret.put(var, node);
    }
    return ret;
}
Also used : HashMap(java.util.HashMap) HopsException(org.apache.sysml.hops.HopsException)

Example 28 with HopsException

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

the class GDFEnumOptimizer method costRuntimePlan.

private static double costRuntimePlan(Plan p) throws DMLRuntimeException {
    Program prog = p.getNode().getProgram();
    if (prog == null)
        throw new DMLRuntimeException("Program not available for runtime plan costing.");
    //put data flow configuration into program
    rSetRuntimePlanConfig(p, new HashMap<Long, Plan>());
    double costs = -1;
    if (COST_FULL_PROGRAMS || (p.getNode().getHop() == null || p.getNode().getProgramBlock() == null)) {
        //recompile entire runtime program
        Recompiler.recompileProgramBlockHierarchy(prog.getProgramBlocks(), new LocalVariableMap(), 0, false);
        _compiledPlans++;
        //cost entire runtime program
        ExecutionContext ec = ExecutionContextFactory.createContext(prog);
        costs = CostEstimationWrapper.getTimeEstimate(prog, ec);
    } else {
        Hop currentHop = p.getNode().getHop();
        ProgramBlock pb = p.getNode().getProgramBlock();
        try {
            //keep the old dag roots
            ArrayList<Hop> oldRoots = pb.getStatementBlock().get_hops();
            Hop tmpHop = null;
            if (!(currentHop instanceof DataOp && ((DataOp) currentHop).isWrite())) {
                ArrayList<Hop> newRoots = new ArrayList<Hop>();
                tmpHop = new DataOp("_tmp", currentHop.getDataType(), currentHop.getValueType(), currentHop, DataOpTypes.TRANSIENTWRITE, "tmp");
                //ensure recursive visitstatus reset on recompile
                tmpHop.setVisited();
                newRoots.add(tmpHop);
                pb.getStatementBlock().set_hops(newRoots);
            }
            //recompile modified runtime program
            Recompiler.recompileProgramBlockHierarchy(prog.getProgramBlocks(), new LocalVariableMap(), 0, false);
            _compiledPlans++;
            //cost partial runtime program up to current hop
            ExecutionContext ec = ExecutionContextFactory.createContext(prog);
            costs = CostEstimationWrapper.getTimeEstimate(prog, ec);
            //restore original hop dag
            if (tmpHop != null)
                HopRewriteUtils.removeChildReference(tmpHop, currentHop);
            pb.getStatementBlock().set_hops(oldRoots);
        } catch (HopsException ex) {
            throw new DMLRuntimeException(ex);
        }
    }
    //release forced data flow configuration from program
    rResetRuntimePlanConfig(p, new HashMap<Long, Plan>());
    _costedPlans++;
    return costs;
}
Also used : Program(org.apache.sysml.runtime.controlprogram.Program) Hop(org.apache.sysml.hops.Hop) ArrayList(java.util.ArrayList) HopsException(org.apache.sysml.hops.HopsException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) ExecutionContext(org.apache.sysml.runtime.controlprogram.context.ExecutionContext) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) ProgramBlock(org.apache.sysml.runtime.controlprogram.ProgramBlock) DataOp(org.apache.sysml.hops.DataOp)

Aggregations

HopsException (org.apache.sysml.hops.HopsException)28 Hop (org.apache.sysml.hops.Hop)14 ArrayList (java.util.ArrayList)7 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 StatementBlock (org.apache.sysml.parser.StatementBlock)5 AggUnaryOp (org.apache.sysml.hops.AggUnaryOp)4 UnaryOp (org.apache.sysml.hops.UnaryOp)4 DataIdentifier (org.apache.sysml.parser.DataIdentifier)4 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)4 HashSet (java.util.HashSet)3 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)3 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)3 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)3 LanguageException (org.apache.sysml.parser.LanguageException)3 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)3 LocalVariableMap (org.apache.sysml.runtime.controlprogram.LocalVariableMap)3 ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)3