Search in sources :

Example 26 with ForProgramBlock

use of org.apache.sysml.runtime.controlprogram.ForProgramBlock in project systemml by apache.

the class Recompiler method rRecompileProgramBlock.

// ////////////////////////////
// private helper functions //
// ////////////////////////////
private static void rRecompileProgramBlock(ProgramBlock pb, LocalVariableMap vars, RecompileStatus status, long tid, ResetType resetRecompile) {
    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 (sb == null)
            return;
        // recompile all for stats propagation and recompile flags
        tmp = Recompiler.recompileHopsDag(sb, sb.getHops(), vars, status, true, false, tid);
        pb.setInstructions(tmp);
        // propagate stats across hops (should be executed on clone of vars)
        Recompiler.extractDAGOutputStatistics(sb.getHops(), vars);
        // reset recompilation flags (w/ special handling functions)
        if (ParForProgramBlock.RESET_RECOMPILATION_FLAGs && !containsRootFunctionOp(sb.getHops()) && resetRecompile.isReset()) {
            Hop.resetRecompilationFlag(sb.getHops(), ExecType.CP, resetRecompile);
            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 27 with ForProgramBlock

use of org.apache.sysml.runtime.controlprogram.ForProgramBlock in project systemml by apache.

the class ProgramConverter method createShallowCopyForProgramBlock.

public static ForProgramBlock createShallowCopyForProgramBlock(ForProgramBlock fpb, Program prog) {
    ForProgramBlock tmpPB = new ForProgramBlock(prog, fpb.getIterVar());
    tmpPB.setFromInstructions(fpb.getFromInstructions());
    tmpPB.setToInstructions(fpb.getToInstructions());
    tmpPB.setIncrementInstructions(fpb.getIncrementInstructions());
    tmpPB.setExitInstructions(fpb.getExitInstructions());
    tmpPB.setChildBlocks(fpb.getChildBlocks());
    return tmpPB;
}
Also used : ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock)

Example 28 with ForProgramBlock

use of org.apache.sysml.runtime.controlprogram.ForProgramBlock in project systemml by apache.

the class ProgramConverter method rSerializeProgramBlock.

private static String rSerializeProgramBlock(ProgramBlock pb, HashMap<String, byte[]> clsMap) {
    StringBuilder sb = new StringBuilder();
    // handle header
    if (pb instanceof WhileProgramBlock)
        sb.append(PARFOR_PB_WHILE);
    else if (pb instanceof ForProgramBlock && !(pb instanceof ParForProgramBlock))
        sb.append(PARFOR_PB_FOR);
    else if (pb instanceof ParForProgramBlock)
        sb.append(PARFOR_PB_PARFOR);
    else if (pb instanceof IfProgramBlock)
        sb.append(PARFOR_PB_IF);
    else if (pb instanceof FunctionProgramBlock && !(pb instanceof ExternalFunctionProgramBlock))
        sb.append(PARFOR_PB_FC);
    else if (pb instanceof ExternalFunctionProgramBlock)
        sb.append(PARFOR_PB_EFC);
    else
        // all generic program blocks
        sb.append(PARFOR_PB_BEGIN);
    // handle body
    if (pb instanceof WhileProgramBlock) {
        WhileProgramBlock wpb = (WhileProgramBlock) pb;
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(wpb.getPredicate(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(wpb.getExitInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_PBS_BEGIN);
        sb.append(rSerializeProgramBlocks(wpb.getChildBlocks(), clsMap));
        sb.append(PARFOR_PBS_END);
    } else if (pb instanceof ForProgramBlock && !(pb instanceof ParForProgramBlock)) {
        ForProgramBlock fpb = (ForProgramBlock) pb;
        sb.append(fpb.getIterVar());
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(fpb.getFromInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(fpb.getToInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(fpb.getIncrementInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(fpb.getExitInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_PBS_BEGIN);
        sb.append(rSerializeProgramBlocks(fpb.getChildBlocks(), clsMap));
        sb.append(PARFOR_PBS_END);
    } else if (pb instanceof ParForProgramBlock) {
        ParForProgramBlock pfpb = (ParForProgramBlock) pb;
        // check for nested remote ParFOR
        if (PExecMode.valueOf(pfpb.getParForParams().get(ParForStatementBlock.EXEC_MODE)) == PExecMode.REMOTE_MR)
            throw new DMLRuntimeException(NOT_SUPPORTED_MR_PARFOR);
        sb.append(pfpb.getIterVar());
        sb.append(COMPONENTS_DELIM);
        sb.append(serializeResultVariables(pfpb.getResultVariables()));
        sb.append(COMPONENTS_DELIM);
        // parameters of nested parfor
        sb.append(serializeStringHashMap(pfpb.getParForParams()));
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(pfpb.getFromInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(pfpb.getToInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(pfpb.getIncrementInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(pfpb.getExitInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_PBS_BEGIN);
        sb.append(rSerializeProgramBlocks(pfpb.getChildBlocks(), clsMap));
        sb.append(PARFOR_PBS_END);
    } else if (pb instanceof IfProgramBlock) {
        IfProgramBlock ipb = (IfProgramBlock) pb;
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(ipb.getPredicate(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(ipb.getExitInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_PBS_BEGIN);
        sb.append(rSerializeProgramBlocks(ipb.getChildBlocksIfBody(), clsMap));
        sb.append(PARFOR_PBS_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_PBS_BEGIN);
        sb.append(rSerializeProgramBlocks(ipb.getChildBlocksElseBody(), clsMap));
        sb.append(PARFOR_PBS_END);
    } else if (pb instanceof FunctionProgramBlock && !(pb instanceof ExternalFunctionProgramBlock)) {
        FunctionProgramBlock fpb = (FunctionProgramBlock) pb;
        sb.append(serializeDataIdentifiers(fpb.getInputParams()));
        sb.append(COMPONENTS_DELIM);
        sb.append(serializeDataIdentifiers(fpb.getOutputParams()));
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(fpb.getInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_PBS_BEGIN);
        sb.append(rSerializeProgramBlocks(fpb.getChildBlocks(), clsMap));
        sb.append(PARFOR_PBS_END);
        sb.append(COMPONENTS_DELIM);
    } else if (pb instanceof ExternalFunctionProgramBlock) {
        if (!(pb instanceof ExternalFunctionProgramBlockCP)) {
            throw new DMLRuntimeException(NOT_SUPPORTED_EXTERNALFUNCTION_PB);
        }
        ExternalFunctionProgramBlockCP fpb = (ExternalFunctionProgramBlockCP) pb;
        sb.append(serializeDataIdentifiers(fpb.getInputParams()));
        sb.append(COMPONENTS_DELIM);
        sb.append(serializeDataIdentifiers(fpb.getOutputParams()));
        sb.append(COMPONENTS_DELIM);
        sb.append(serializeStringHashMap(fpb.getOtherParams()));
        sb.append(COMPONENTS_DELIM);
        sb.append(fpb.getBaseDir());
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        // create on construction anyway
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_PBS_BEGIN);
        sb.append(rSerializeProgramBlocks(fpb.getChildBlocks(), clsMap));
        sb.append(PARFOR_PBS_END);
    } else // all generic program blocks
    {
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(pb.getInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
    }
    // handle end
    sb.append(PARFOR_PB_END);
    return sb.toString();
}
Also used : IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) ExternalFunctionProgramBlock(org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) ExternalFunctionProgramBlock(org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) ExternalFunctionProgramBlockCP(org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlockCP) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 29 with ForProgramBlock

use of org.apache.sysml.runtime.controlprogram.ForProgramBlock in project systemml by apache.

the class OptimizerRuleBased method removeUnnecessaryParFor.

protected int removeUnnecessaryParFor(OptNode n) {
    int count = 0;
    if (!n.isLeaf()) {
        for (OptNode sub : n.getChilds()) {
            if (sub.getNodeType() == NodeType.PARFOR && sub.getK() == 1) {
                long id = sub.getID();
                Object[] progobj = OptTreeConverter.getAbstractPlanMapping().getMappedProg(id);
                ParForStatementBlock pfsb = (ParForStatementBlock) progobj[0];
                ParForProgramBlock pfpb = (ParForProgramBlock) progobj[1];
                // create for pb as replacement
                Program prog = pfpb.getProgram();
                ForProgramBlock fpb = ProgramConverter.createShallowCopyForProgramBlock(pfpb, prog);
                // replace parfor with for, and update objectmapping
                OptTreeConverter.replaceProgramBlock(n, sub, pfpb, fpb, false);
                // update link to statement block
                fpb.setStatementBlock(pfsb);
                // update node
                sub.setNodeType(NodeType.FOR);
                sub.setK(1);
                count++;
            }
            count += removeUnnecessaryParFor(sub);
        }
    }
    return count;
}
Also used : DMLProgram(org.apache.sysml.parser.DMLProgram) Program(org.apache.sysml.runtime.controlprogram.Program) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) RDDObject(org.apache.sysml.runtime.instructions.spark.data.RDDObject) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock)

Example 30 with ForProgramBlock

use of org.apache.sysml.runtime.controlprogram.ForProgramBlock in project systemml by apache.

the class OptimizerRuleBased method removeRecursiveParFor.

protected int removeRecursiveParFor(OptNode n, HashSet<ParForProgramBlock> recPBs) {
    int count = 0;
    if (!n.isLeaf()) {
        for (OptNode sub : n.getChilds()) {
            if (sub.getNodeType() == NodeType.PARFOR) {
                long id = sub.getID();
                Object[] progobj = OptTreeConverter.getAbstractPlanMapping().getMappedProg(id);
                ParForStatementBlock pfsb = (ParForStatementBlock) progobj[0];
                ParForProgramBlock pfpb = (ParForProgramBlock) progobj[1];
                if (recPBs.contains(pfpb)) {
                    // create for pb as replacement
                    Program prog = pfpb.getProgram();
                    ForProgramBlock fpb = ProgramConverter.createShallowCopyForProgramBlock(pfpb, prog);
                    // replace parfor with for, and update objectmapping
                    OptTreeConverter.replaceProgramBlock(n, sub, pfpb, fpb, false);
                    // update link to statement block
                    fpb.setStatementBlock(pfsb);
                    // update node
                    sub.setNodeType(NodeType.FOR);
                    sub.setK(1);
                    count++;
                }
            }
            count += removeRecursiveParFor(sub, recPBs);
        }
    }
    return count;
}
Also used : DMLProgram(org.apache.sysml.parser.DMLProgram) Program(org.apache.sysml.runtime.controlprogram.Program) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) RDDObject(org.apache.sysml.runtime.instructions.spark.data.RDDObject) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock)

Aggregations

ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)69 IfProgramBlock (org.apache.sysml.runtime.controlprogram.IfProgramBlock)61 WhileProgramBlock (org.apache.sysml.runtime.controlprogram.WhileProgramBlock)61 FunctionProgramBlock (org.apache.sysml.runtime.controlprogram.FunctionProgramBlock)53 ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)47 ParForProgramBlock (org.apache.sysml.runtime.controlprogram.ParForProgramBlock)43 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)28 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)26 StatementBlock (org.apache.sysml.parser.StatementBlock)26 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)26 ExternalFunctionProgramBlock (org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock)22 ArrayList (java.util.ArrayList)21 Instruction (org.apache.sysml.runtime.instructions.Instruction)20 MRJobInstruction (org.apache.sysml.runtime.instructions.MRJobInstruction)12 DMLProgram (org.apache.sysml.parser.DMLProgram)10 ParForStatementBlock (org.apache.sysml.parser.ParForStatementBlock)10 Program (org.apache.sysml.runtime.controlprogram.Program)10 FunctionCallCPInstruction (org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction)10 Hop (org.apache.sysml.hops.Hop)9 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)9