Search in sources :

Example 51 with ParForProgramBlock

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

the class OptTreeConverter method rCreateOptNode.

public static OptNode rCreateOptNode(ProgramBlock pb, LocalVariableMap vars, boolean topLevel, boolean storeObjs) {
    OptNode node = null;
    if (pb instanceof IfProgramBlock) {
        IfProgramBlock ipb = (IfProgramBlock) pb;
        node = new OptNode(NodeType.IF);
        if (storeObjs)
            _rtMap.putMapping(ipb, node);
        node.setExecType(ExecType.CP);
        // process if condition
        OptNode ifn = new OptNode(NodeType.GENERIC);
        node.addChilds(createOptNodes(ipb.getPredicate(), vars, storeObjs));
        node.addChild(ifn);
        for (ProgramBlock lpb : ipb.getChildBlocksIfBody()) ifn.addChild(rCreateOptNode(lpb, vars, topLevel, storeObjs));
        // process else condition
        if (ipb.getChildBlocksElseBody() != null && ipb.getChildBlocksElseBody().size() > 0) {
            OptNode efn = new OptNode(NodeType.GENERIC);
            node.addChild(efn);
            for (ProgramBlock lpb : ipb.getChildBlocksElseBody()) efn.addChild(rCreateOptNode(lpb, vars, topLevel, storeObjs));
        }
    } else if (pb instanceof WhileProgramBlock) {
        WhileProgramBlock wpb = (WhileProgramBlock) pb;
        node = new OptNode(NodeType.WHILE);
        if (storeObjs)
            _rtMap.putMapping(wpb, node);
        node.setExecType(ExecType.CP);
        // process predicate instruction
        node.addChilds(createOptNodes(wpb.getPredicate(), vars, storeObjs));
        // process body
        for (ProgramBlock lpb : wpb.getChildBlocks()) node.addChild(rCreateOptNode(lpb, vars, topLevel, storeObjs));
    } else if (pb instanceof ForProgramBlock && !(pb instanceof ParForProgramBlock)) {
        ForProgramBlock fpb = (ForProgramBlock) pb;
        node = new OptNode(NodeType.FOR);
        if (storeObjs)
            _rtMap.putMapping(fpb, node);
        node.setExecType(ExecType.CP);
        // determine number of iterations
        long N = OptimizerUtils.getNumIterations(fpb, vars, CostEstimator.FACTOR_NUM_ITERATIONS);
        node.addParam(ParamType.NUM_ITERATIONS, String.valueOf(N));
        node.addChilds(createOptNodes(fpb.getFromInstructions(), vars, storeObjs));
        node.addChilds(createOptNodes(fpb.getToInstructions(), vars, storeObjs));
        node.addChilds(createOptNodes(fpb.getIncrementInstructions(), vars, storeObjs));
        // process body
        for (ProgramBlock lpb : fpb.getChildBlocks()) node.addChild(rCreateOptNode(lpb, vars, topLevel, storeObjs));
    } else if (pb instanceof ParForProgramBlock) {
        ParForProgramBlock fpb = (ParForProgramBlock) pb;
        node = new OptNode(NodeType.PARFOR);
        if (storeObjs)
            _rtMap.putMapping(fpb, node);
        node.setK(fpb.getDegreeOfParallelism());
        long N = fpb.getNumIterations();
        node.addParam(ParamType.NUM_ITERATIONS, (N != -1) ? String.valueOf(N) : String.valueOf(CostEstimator.FACTOR_NUM_ITERATIONS));
        switch(fpb.getExecMode()) {
            case LOCAL:
                node.setExecType(ExecType.CP);
                break;
            case REMOTE_MR:
            case REMOTE_MR_DP:
                node.setExecType(ExecType.MR);
                break;
            case REMOTE_SPARK:
            case REMOTE_SPARK_DP:
                node.setExecType(ExecType.SPARK);
                break;
            default:
                node.setExecType(null);
        }
        if (!topLevel) {
            node.addChilds(createOptNodes(fpb.getFromInstructions(), vars, storeObjs));
            node.addChilds(createOptNodes(fpb.getToInstructions(), vars, storeObjs));
            node.addChilds(createOptNodes(fpb.getIncrementInstructions(), vars, storeObjs));
        }
        // process body
        for (ProgramBlock lpb : fpb.getChildBlocks()) node.addChild(rCreateOptNode(lpb, vars, false, storeObjs));
    // parameters, add required parameters
    } else // last level program block
    {
        node = new OptNode(NodeType.GENERIC);
        if (storeObjs)
            _rtMap.putMapping(pb, node);
        node.addChilds(createOptNodes(pb.getInstructions(), vars, storeObjs));
        node.setExecType(ExecType.CP);
    }
    return node;
}
Also used : IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) 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) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock)

Example 52 with ParForProgramBlock

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

the class OptTreeConverter method replaceProgramBlock.

public static void replaceProgramBlock(OptNode parent, OptNode n, ProgramBlock pbOld, ProgramBlock pbNew, boolean rtMap) {
    ProgramBlock pbParent = null;
    if (rtMap)
        pbParent = (ProgramBlock) _rtMap.getMappedObject(parent.getID());
    else {
        if (parent.getNodeType() == NodeType.FUNCCALL) {
            FunctionOp fop = (FunctionOp) _hlMap.getMappedHop(parent.getID());
            pbParent = ((Program) _hlMap.getRootProgram()[1]).getFunctionProgramBlock(fop.getFunctionNamespace(), fop.getFunctionName());
        } else
            pbParent = (ProgramBlock) _hlMap.getMappedProg(parent.getID())[1];
    }
    if (pbParent instanceof IfProgramBlock) {
        IfProgramBlock ipb = (IfProgramBlock) pbParent;
        replaceProgramBlock(ipb.getChildBlocksIfBody(), pbOld, pbNew);
        replaceProgramBlock(ipb.getChildBlocksElseBody(), pbOld, pbNew);
    } else if (pbParent instanceof WhileProgramBlock) {
        WhileProgramBlock wpb = (WhileProgramBlock) pbParent;
        replaceProgramBlock(wpb.getChildBlocks(), pbOld, pbNew);
    } else if (pbParent instanceof ForProgramBlock || pbParent instanceof ParForProgramBlock) {
        ForProgramBlock fpb = (ForProgramBlock) pbParent;
        replaceProgramBlock(fpb.getChildBlocks(), pbOld, pbNew);
    } else if (pbParent instanceof FunctionProgramBlock) {
        FunctionProgramBlock fpb = (FunctionProgramBlock) pbParent;
        replaceProgramBlock(fpb.getChildBlocks(), pbOld, pbNew);
    } else
        throw new DMLRuntimeException("Optimizer doesn't support " + pbParent.getClass().getName());
    // update repository
    if (rtMap)
        _rtMap.replaceMapping(pbNew, n);
    else
        _hlMap.replaceMapping(pbNew, n);
}
Also used : IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) 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) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) FunctionOp(org.apache.sysml.hops.FunctionOp) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 53 with ParForProgramBlock

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

the class OptimizerConstrained method rewriteSetTaskPartitioner.

// /////
// REWRITE set task partitioner
// /
@Override
protected void rewriteSetTaskPartitioner(OptNode pn, boolean flagNested, boolean flagLIX) {
    // constraint awareness
    if (!pn.getParam(ParamType.TASK_PARTITIONER).equals(PTaskPartitioner.UNSPECIFIED.toString())) {
        ParForProgramBlock pfpb = (ParForProgramBlock) OptTreeConverter.getAbstractPlanMapping().getMappedProg(pn.getID())[1];
        pfpb.setTaskPartitioner(PTaskPartitioner.valueOf(pn.getParam(ParamType.TASK_PARTITIONER)));
        String tsExt = "";
        if (pn.getParam(ParamType.TASK_SIZE) != null) {
            pfpb.setTaskSize(Integer.parseInt(pn.getParam(ParamType.TASK_SIZE)));
            tsExt += "," + pn.getParam(ParamType.TASK_SIZE);
        }
        LOG.debug(getOptMode() + " OPT: forced 'set task partitioner' - result=" + pn.getParam(ParamType.TASK_PARTITIONER) + tsExt);
    } else {
        if (pn.getParam(ParamType.TASK_SIZE) != null)
            LOG.warn("Cannot force task size without forcing task partitioner.");
        super.rewriteSetTaskPartitioner(pn, flagNested, flagLIX);
    }
}
Also used : ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock)

Example 54 with ParForProgramBlock

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

the class OptimizerConstrained method rewriteSetExecutionStategy.

// /////
// REWRITE set execution strategy
// /
@Override
protected boolean rewriteSetExecutionStategy(OptNode n, double M0, double M, double M2, double M3, boolean flagLIX) {
    boolean ret = false;
    // constraint awareness
    if (n.getExecType() != null && ConfigurationManager.isParallelParFor()) {
        ParForProgramBlock pfpb = (ParForProgramBlock) OptTreeConverter.getAbstractPlanMapping().getMappedProg(n.getID())[1];
        PExecMode mode = PExecMode.LOCAL;
        if (n.getExecType() == ExecType.MR) {
            mode = PExecMode.REMOTE_MR;
        } else if (n.getExecType() == ExecType.SPARK) {
            mode = PExecMode.REMOTE_SPARK;
        }
        pfpb.setExecMode(mode);
        LOG.debug(getOptMode() + " OPT: forced 'set execution strategy' - result=" + mode);
    } else
        ret = super.rewriteSetExecutionStategy(n, M0, M, M2, M3, flagLIX);
    return ret;
}
Also used : PExecMode(org.apache.sysml.runtime.controlprogram.ParForProgramBlock.PExecMode) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock)

Example 55 with ParForProgramBlock

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

the class OptimizerRuleBased method rewriteNestedParallelism.

///////
//REWRITE enable nested parallelism
///
@SuppressWarnings("all")
protected boolean rewriteNestedParallelism(OptNode n, double M, boolean flagLIX) throws DMLRuntimeException {
    boolean nested = false;
    if (APPLY_REWRITE_NESTED_PARALLELISM && // if not applied left indexing rewrite	
    !flagLIX && // at least exploit all nodes
    _N >= _rnk && // only for 1D problems, otherwise potentially bad load balance
    !n.hasNestedParallelism(false) && // only if we can exploit full local parallelism in the map task JVM memory
    M * _lkmaxCP <= _rm) {
        //modify tree
        ArrayList<OptNode> tmpOld = n.getChilds();
        OptNode nest = new OptNode(NodeType.PARFOR, ExecType.CP);
        ArrayList<OptNode> tmpNew = new ArrayList<OptNode>();
        tmpNew.add(nest);
        n.setChilds(tmpNew);
        nest.setChilds(tmpOld);
        //modify rtprog
        long id = n.getID();
        ParForProgramBlock pfpb = (ParForProgramBlock) OptTreeConverter.getAbstractPlanMapping().getMappedProg(id)[1];
        ArrayList<ProgramBlock> tmpPBOld = pfpb.getChildBlocks();
        //create new program block structure and modify parameters (from, to, incr, types,)
        //from, to stay original
        String[] iterVars = pfpb.getIterablePredicateVars();
        //itervar, incr stay original
        String[] iterVars2 = iterVars.clone();
        int outIncr = (int) Math.ceil(((double) _N) / _rnk);
        // already checked for uniqueness in ParForStatementBlock
        iterVars[0] = ParForStatementBlock.INTERAL_FN_INDEX_ROW;
        iterVars[3] = String.valueOf(outIncr);
        //sub start
        iterVars2[1] = ParForStatementBlock.INTERAL_FN_INDEX_ROW;
        iterVars2[2] = null;
        HashMap<String, String> params = pfpb.getParForParams();
        HashMap<String, String> params2 = (HashMap<String, String>) params.clone();
        ParForProgramBlock pfpb2 = new ParForProgramBlock(pfpb.getProgram(), iterVars2, params2);
        OptTreeConverter.getAbstractPlanMapping().putProgMapping(null, pfpb2, nest);
        ArrayList<ProgramBlock> tmpPBNew = new ArrayList<ProgramBlock>();
        tmpPBNew.add(pfpb2);
        pfpb.setChildBlocks(tmpPBNew);
        pfpb.setIterablePredicateVars(iterVars);
        pfpb.setIncrementInstructions(new ArrayList<Instruction>());
        pfpb.setExecMode(PExecMode.REMOTE_MR);
        pfpb2.setChildBlocks(tmpPBOld);
        pfpb2.setResultVariables(pfpb.getResultVariables());
        pfpb2.setFromInstructions(new ArrayList<Instruction>());
        pfpb2.setToInstructions(ProgramRecompiler.createNestedParallelismToInstructionSet(ParForStatementBlock.INTERAL_FN_INDEX_ROW, String.valueOf(outIncr - 1)));
        pfpb2.setIncrementInstructions(new ArrayList<Instruction>());
        pfpb2.setExecMode(PExecMode.LOCAL);
        nested = true;
    }
    _numEvaluatedPlans++;
    LOG.debug(getOptMode() + " OPT: rewrite 'enable nested parallelism' - result=" + nested);
    return nested;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Instruction(org.apache.sysml.runtime.instructions.Instruction) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) 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)

Aggregations

ParForProgramBlock (org.apache.sysml.runtime.controlprogram.ParForProgramBlock)105 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)41 FunctionProgramBlock (org.apache.sysml.runtime.controlprogram.FunctionProgramBlock)37 ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)35 IfProgramBlock (org.apache.sysml.runtime.controlprogram.IfProgramBlock)33 WhileProgramBlock (org.apache.sysml.runtime.controlprogram.WhileProgramBlock)33 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)31 ArrayList (java.util.ArrayList)22 ParForStatementBlock (org.apache.sysml.parser.ParForStatementBlock)20 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)16 ExternalFunctionProgramBlock (org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock)16 RDDObject (org.apache.sysml.runtime.instructions.spark.data.RDDObject)16 HashSet (java.util.HashSet)15 StatementBlock (org.apache.sysml.parser.StatementBlock)11 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)10 Instruction (org.apache.sysml.runtime.instructions.Instruction)10 DMLProgram (org.apache.sysml.parser.DMLProgram)8 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)8 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)8 Program (org.apache.sysml.runtime.controlprogram.Program)8