Search in sources :

Example 1 with LocalVariableMap

use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.

the class InterProceduralAnalysis method analyzeSubProgram.

public Set<String> analyzeSubProgram(StatementBlock sb) throws HopsException, ParseException {
    DMLTranslator.resetHopsDAGVisitStatus(sb);
    //step 1: get candidates for statistics propagation into functions (if required)
    Map<String, Integer> fcandCounts = new HashMap<String, Integer>();
    Map<String, FunctionOp> fcandHops = new HashMap<String, FunctionOp>();
    Map<String, Set<Long>> fcandSafeNNZ = new HashMap<String, Set<Long>>();
    Set<String> allFCandKeys = new HashSet<String>();
    getFunctionCandidatesForStatisticPropagation(sb, fcandCounts, fcandHops);
    //cp before pruning
    allFCandKeys.addAll(fcandCounts.keySet());
    pruneFunctionCandidatesForStatisticPropagation(fcandCounts, fcandHops);
    determineFunctionCandidatesNNZPropagation(fcandHops, fcandSafeNNZ);
    DMLTranslator.resetHopsDAGVisitStatus(sb);
    if (!fcandCounts.isEmpty()) {
        //step 2: propagate statistics into functions and across DAGs
        //(callVars used to chain outputs/inputs of multiple functions calls) 
        LocalVariableMap callVars = new LocalVariableMap();
        propagateStatisticsAcrossBlock(sb, fcandCounts, callVars, fcandSafeNNZ, new HashSet<String>(), new HashSet<String>());
    }
    return fcandCounts.keySet();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) FunctionOp(org.apache.sysml.hops.FunctionOp) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) HashSet(java.util.HashSet)

Example 2 with LocalVariableMap

use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.

the class Recompiler method rRecompileProgramBlock.

//////////////////////////////
// private helper functions //
//////////////////////////////
private static void rRecompileProgramBlock(ProgramBlock pb, LocalVariableMap vars, RecompileStatus status, long tid, boolean resetRecompile) throws HopsException, DMLRuntimeException, LopsException, IOException {
    if (pb instanceof WhileProgramBlock) {
        WhileProgramBlock wpb = (WhileProgramBlock) pb;
        WhileStatementBlock wsb = (WhileStatementBlock) wpb.getStatementBlock();
        //recompile predicate
        recompileWhilePredicate(wpb, wsb, vars, status, tid, resetRecompile);
        //remove updated scalars because in loop
        removeUpdatedScalars(vars, wsb);
        //copy vars for later compare
        LocalVariableMap oldVars = (LocalVariableMap) vars.clone();
        RecompileStatus oldStatus = (RecompileStatus) status.clone();
        for (ProgramBlock pb2 : wpb.getChildBlocks()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
        if (reconcileUpdatedCallVarsLoops(oldVars, vars, wsb) | reconcileUpdatedCallVarsLoops(oldStatus, status, wsb)) {
            //second pass with unknowns if required
            recompileWhilePredicate(wpb, wsb, vars, status, tid, resetRecompile);
            for (ProgramBlock pb2 : wpb.getChildBlocks()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
        }
        removeUpdatedScalars(vars, wsb);
    } else if (pb instanceof IfProgramBlock) {
        IfProgramBlock ipb = (IfProgramBlock) pb;
        IfStatementBlock isb = (IfStatementBlock) ipb.getStatementBlock();
        //recompile predicate
        recompileIfPredicate(ipb, isb, vars, status, tid, resetRecompile);
        //copy vars for later compare
        LocalVariableMap oldVars = (LocalVariableMap) vars.clone();
        LocalVariableMap varsElse = (LocalVariableMap) vars.clone();
        RecompileStatus oldStatus = (RecompileStatus) status.clone();
        RecompileStatus statusElse = (RecompileStatus) status.clone();
        for (ProgramBlock pb2 : ipb.getChildBlocksIfBody()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
        for (ProgramBlock pb2 : ipb.getChildBlocksElseBody()) rRecompileProgramBlock(pb2, varsElse, statusElse, tid, resetRecompile);
        reconcileUpdatedCallVarsIf(oldVars, vars, varsElse, isb);
        reconcileUpdatedCallVarsIf(oldStatus, status, statusElse, isb);
        removeUpdatedScalars(vars, ipb.getStatementBlock());
    } else if (//includes ParFORProgramBlock
    pb instanceof ForProgramBlock) {
        ForProgramBlock fpb = (ForProgramBlock) pb;
        ForStatementBlock fsb = (ForStatementBlock) fpb.getStatementBlock();
        //recompile predicates
        recompileForPredicates(fpb, fsb, vars, status, tid, resetRecompile);
        //remove updated scalars because in loop
        removeUpdatedScalars(vars, fpb.getStatementBlock());
        //copy vars for later compare
        LocalVariableMap oldVars = (LocalVariableMap) vars.clone();
        RecompileStatus oldStatus = (RecompileStatus) status.clone();
        for (ProgramBlock pb2 : fpb.getChildBlocks()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
        if (reconcileUpdatedCallVarsLoops(oldVars, vars, fsb) | reconcileUpdatedCallVarsLoops(oldStatus, status, fsb)) {
            //second pass with unknowns if required
            recompileForPredicates(fpb, fsb, vars, status, tid, resetRecompile);
            for (ProgramBlock pb2 : fpb.getChildBlocks()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
        }
        removeUpdatedScalars(vars, fpb.getStatementBlock());
    } else if (//includes ExternalFunctionProgramBlock and ExternalFunctionProgramBlockCP
    pb instanceof FunctionProgramBlock) {
    //do nothing
    } else {
        StatementBlock sb = pb.getStatementBlock();
        ArrayList<Instruction> tmp = pb.getInstructions();
        if (//recompile all for stats propagation and recompile flags
        sb != null) //&& Recompiler.requiresRecompilation( sb.get_hops() ) 
        /*&& !Recompiler.containsNonRecompileInstructions(tmp)*/
        {
            tmp = Recompiler.recompileHopsDag(sb, sb.get_hops(), vars, status, true, false, tid);
            pb.setInstructions(tmp);
            //propagate stats across hops (should be executed on clone of vars)
            Recompiler.extractDAGOutputStatistics(sb.get_hops(), vars);
            //reset recompilation flags (w/ special handling functions)
            if (ParForProgramBlock.RESET_RECOMPILATION_FLAGs && !containsRootFunctionOp(sb.get_hops()) && resetRecompile) {
                Hop.resetRecompilationFlag(sb.get_hops(), ExecType.CP);
                sb.updateRecompilationFlag();
            }
        }
    }
}
Also used : IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) ArrayList(java.util.ArrayList) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ProgramBlock(org.apache.sysml.runtime.controlprogram.ProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) 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 3 with LocalVariableMap

use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.

the class GDFEnumOptimizer method optimize.

@Override
public GDFGraph optimize(GDFGraph gdfgraph, Summary summary) throws DMLRuntimeException, HopsException, LopsException {
    Timing time = new Timing(true);
    Program prog = gdfgraph.getRuntimeProgram();
    ExecutionContext ec = ExecutionContextFactory.createContext(prog);
    ArrayList<GDFNode> roots = gdfgraph.getGraphRootNodes();
    //Step 1: baseline costing for branch and bound costs
    double initCosts = Double.MAX_VALUE;
    if (BRANCH_AND_BOUND_PRUNING) {
        initCosts = CostEstimationWrapper.getTimeEstimate(prog, ec);
        initCosts = initCosts * (1 + BRANCH_AND_BOUND_REL_THRES);
    }
    //Step 2: dynamic programming plan generation
    //(finally, pick optimal root plans over all interesting property sets)
    ArrayList<Plan> rootPlans = new ArrayList<Plan>();
    for (GDFNode node : roots) {
        PlanSet ps = enumOpt(node, _memo, initCosts);
        Plan optPlan = ps.getPlanWithMinCosts();
        rootPlans.add(optPlan);
    }
    long enumPlanMismatch = getPlanMismatches();
    //check for final containment of independent roots and pick optimal
    HashMap<Long, Plan> memo = new HashMap<Long, Plan>();
    resetPlanMismatches();
    for (Plan p : rootPlans) rSetRuntimePlanConfig(p, memo);
    long finalPlanMismatch = getPlanMismatches();
    //generate final runtime plan (w/ optimal config)
    Recompiler.recompileProgramBlockHierarchy(prog.getProgramBlocks(), new LocalVariableMap(), 0, false);
    ec = ExecutionContextFactory.createContext(prog);
    double optCosts = CostEstimationWrapper.getTimeEstimate(prog, ec);
    //maintain optimization summary statistics
    summary.setCostsInitial(initCosts);
    summary.setCostsOptimal(optCosts);
    summary.setNumEnumPlans(_enumeratedPlans);
    summary.setNumPrunedInvalidPlans(_prunedInvalidPlans);
    summary.setNumPrunedSuboptPlans(_prunedSuboptimalPlans);
    summary.setNumCompiledPlans(_compiledPlans);
    summary.setNumCostedPlans(_costedPlans);
    summary.setNumEnumPlanMismatch(enumPlanMismatch);
    summary.setNumFinalPlanMismatch(finalPlanMismatch);
    summary.setTimeOptim(time.stop());
    return gdfgraph;
}
Also used : Program(org.apache.sysml.runtime.controlprogram.Program) HashMap(java.util.HashMap) GDFNode(org.apache.sysml.hops.globalopt.gdfgraph.GDFNode) ArrayList(java.util.ArrayList) ExecutionContext(org.apache.sysml.runtime.controlprogram.context.ExecutionContext) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) Timing(org.apache.sysml.runtime.controlprogram.parfor.stat.Timing)

Example 4 with LocalVariableMap

use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.

the class ScriptExecutor method createAndInitializeExecutionContext.

/**
 * Create an execution context and set its variables to be the symbol table
 * of the script.
 */
protected void createAndInitializeExecutionContext() {
    executionContext = ExecutionContextFactory.createContext(runtimeProgram);
    LocalVariableMap symbolTable = script.getSymbolTable();
    if (symbolTable != null)
        executionContext.setVariables(symbolTable);
    // attach registered outputs (for dynamic recompile)
    executionContext.getVariables().setRegisteredOutputs(new HashSet<String>(script.getOutputVariables()));
}
Also used : LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap)

Example 5 with LocalVariableMap

use of org.apache.sysml.runtime.controlprogram.LocalVariableMap 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 | 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)

Aggregations

LocalVariableMap (org.apache.sysml.runtime.controlprogram.LocalVariableMap)64 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)19 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)19 StatementBlock (org.apache.sysml.parser.StatementBlock)19 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)19 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)15 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)12 HashSet (java.util.HashSet)11 FunctionProgramBlock (org.apache.sysml.runtime.controlprogram.FunctionProgramBlock)11 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)11 FunctionStatement (org.apache.sysml.parser.FunctionStatement)10 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)10 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)9 IfProgramBlock (org.apache.sysml.runtime.controlprogram.IfProgramBlock)9 WhileProgramBlock (org.apache.sysml.runtime.controlprogram.WhileProgramBlock)9 Path (org.apache.hadoop.fs.Path)8 ExternalFunctionStatement (org.apache.sysml.parser.ExternalFunctionStatement)8 ExecutionContext (org.apache.sysml.runtime.controlprogram.context.ExecutionContext)8 FunctionOp (org.apache.sysml.hops.FunctionOp)7