Search in sources :

Example 21 with Instruction

use of org.apache.sysml.runtime.instructions.Instruction in project incubator-systemml by apache.

the class OptimizerRuleBased method rReplaceFunctionNames.

protected void rReplaceFunctionNames(OptNode n, String oldName, String newName) {
    if (n.getNodeType() == NodeType.FUNCCALL) {
        FunctionOp fop = (FunctionOp) OptTreeConverter.getAbstractPlanMapping().getMappedHop(n.getID());
        String[] names = n.getParam(ParamType.OPSTRING).split(Program.KEY_DELIM);
        String fnamespace = names[0];
        String fname = names[1];
        if (// newName if shared hop
        fname.equals(oldName) || fname.equals(newName)) {
            // set opttree function name
            n.addParam(ParamType.OPSTRING, DMLProgram.constructFunctionKey(fnamespace, newName));
            // set instruction function name
            long parentID = OptTreeConverter.getAbstractPlanMapping().getMappedParentID(n.getID());
            ProgramBlock pb = (ProgramBlock) OptTreeConverter.getAbstractPlanMapping().getMappedProg(parentID)[1];
            ArrayList<Instruction> instArr = pb.getInstructions();
            for (int i = 0; i < instArr.size(); i++) {
                Instruction inst = instArr.get(i);
                if (inst instanceof FunctionCallCPInstruction) {
                    FunctionCallCPInstruction fci = (FunctionCallCPInstruction) inst;
                    if (oldName.equals(fci.getFunctionName()))
                        instArr.set(i, FunctionCallCPInstruction.parseInstruction(fci.toString().replaceAll(oldName, newName)));
                }
            }
            // set hop name (for recompile)
            if (fop.getFunctionName().equals(oldName))
                fop.setFunctionName(newName);
        }
    }
    // recursive invocation
    if (!n.isLeaf())
        for (OptNode c : n.getChilds()) rReplaceFunctionNames(c, oldName, newName);
}
Also used : FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) FunctionOp(org.apache.sysml.hops.FunctionOp) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ProgramBlock(org.apache.sysml.runtime.controlprogram.ProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) Instruction(org.apache.sysml.runtime.instructions.Instruction) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction)

Example 22 with Instruction

use of org.apache.sysml.runtime.instructions.Instruction in project incubator-systemml by apache.

the class ProgramRecompiler method rFindAndRecompileIndexingHOP.

/**
 * NOTE: if force is set, we set and recompile the respective indexing hops;
 * otherwise, we release the forced exec type and recompile again. Hence,
 * any changes can be exactly reverted with the same access behavior.
 *
 * @param sb statement block
 * @param pb program block
 * @param var variable
 * @param ec execution context
 * @param force if true, set and recompile the respective indexing hops
 */
public static void rFindAndRecompileIndexingHOP(StatementBlock sb, ProgramBlock pb, String var, ExecutionContext ec, boolean force) {
    if (pb instanceof IfProgramBlock && sb instanceof IfStatementBlock) {
        IfProgramBlock ipb = (IfProgramBlock) pb;
        IfStatementBlock isb = (IfStatementBlock) sb;
        IfStatement is = (IfStatement) sb.getStatement(0);
        // process if condition
        if (isb.getPredicateHops() != null)
            ipb.setPredicate(rFindAndRecompileIndexingHOP(isb.getPredicateHops(), ipb.getPredicate(), var, ec, force));
        // process if branch
        int len = is.getIfBody().size();
        for (int i = 0; i < ipb.getChildBlocksIfBody().size() && i < len; i++) {
            ProgramBlock lpb = ipb.getChildBlocksIfBody().get(i);
            StatementBlock lsb = is.getIfBody().get(i);
            rFindAndRecompileIndexingHOP(lsb, lpb, var, ec, force);
        }
        // process else branch
        if (ipb.getChildBlocksElseBody() != null) {
            int len2 = is.getElseBody().size();
            for (int i = 0; i < ipb.getChildBlocksElseBody().size() && i < len2; i++) {
                ProgramBlock lpb = ipb.getChildBlocksElseBody().get(i);
                StatementBlock lsb = is.getElseBody().get(i);
                rFindAndRecompileIndexingHOP(lsb, lpb, var, ec, force);
            }
        }
    } else if (pb instanceof WhileProgramBlock && sb instanceof WhileStatementBlock) {
        WhileProgramBlock wpb = (WhileProgramBlock) pb;
        WhileStatementBlock wsb = (WhileStatementBlock) sb;
        WhileStatement ws = (WhileStatement) sb.getStatement(0);
        // process while condition
        if (wsb.getPredicateHops() != null)
            wpb.setPredicate(rFindAndRecompileIndexingHOP(wsb.getPredicateHops(), wpb.getPredicate(), var, ec, force));
        // process body
        // robustness for potentially added problem blocks
        int len = ws.getBody().size();
        for (int i = 0; i < wpb.getChildBlocks().size() && i < len; i++) {
            ProgramBlock lpb = wpb.getChildBlocks().get(i);
            StatementBlock lsb = ws.getBody().get(i);
            rFindAndRecompileIndexingHOP(lsb, lpb, var, ec, force);
        }
    } else if (// for or parfor
    pb instanceof ForProgramBlock && sb instanceof ForStatementBlock) {
        ForProgramBlock fpb = (ForProgramBlock) pb;
        ForStatementBlock fsb = (ForStatementBlock) sb;
        ForStatement fs = (ForStatement) fsb.getStatement(0);
        if (fsb.getFromHops() != null)
            fpb.setFromInstructions(rFindAndRecompileIndexingHOP(fsb.getFromHops(), fpb.getFromInstructions(), var, ec, force));
        if (fsb.getToHops() != null)
            fpb.setToInstructions(rFindAndRecompileIndexingHOP(fsb.getToHops(), fpb.getToInstructions(), var, ec, force));
        if (fsb.getIncrementHops() != null)
            fpb.setIncrementInstructions(rFindAndRecompileIndexingHOP(fsb.getIncrementHops(), fpb.getIncrementInstructions(), var, ec, force));
        // process body
        // robustness for potentially added problem blocks
        int len = fs.getBody().size();
        for (int i = 0; i < fpb.getChildBlocks().size() && i < len; i++) {
            ProgramBlock lpb = fpb.getChildBlocks().get(i);
            StatementBlock lsb = fs.getBody().get(i);
            rFindAndRecompileIndexingHOP(lsb, lpb, var, ec, force);
        }
    } else // last level program block
    {
        try {
            // process actual hops
            boolean ret = false;
            Hop.resetVisitStatus(sb.getHops());
            if (force) {
                // set forced execution type
                for (Hop h : sb.getHops()) ret |= rFindAndSetCPIndexingHOP(h, var);
            } else {
                // release forced execution type
                for (Hop h : sb.getHops()) ret |= rFindAndReleaseIndexingHOP(h, var);
            }
            // recompilation on-demand
            if (ret) {
                // construct new instructions
                ArrayList<Instruction> newInst = Recompiler.recompileHopsDag(sb, sb.getHops(), ec.getVariables(), null, true, false, 0);
                pb.setInstructions(newInst);
            }
        } catch (Exception ex) {
            throw new DMLRuntimeException(ex);
        }
    }
}
Also used : IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) Hop(org.apache.sysml.hops.Hop) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) WhileStatement(org.apache.sysml.parser.WhileStatement) BinaryCPInstruction(org.apache.sysml.runtime.instructions.cp.BinaryCPInstruction) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) Instruction(org.apache.sysml.runtime.instructions.Instruction) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) IfStatement(org.apache.sysml.parser.IfStatement) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ProgramBlock(org.apache.sysml.runtime.controlprogram.ProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) ForStatement(org.apache.sysml.parser.ForStatement) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) StatementBlock(org.apache.sysml.parser.StatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock)

Example 23 with Instruction

use of org.apache.sysml.runtime.instructions.Instruction in project incubator-systemml by apache.

the class ProgramRecompiler method rFindAndRecompileIndexingHOP.

private static ArrayList<Instruction> rFindAndRecompileIndexingHOP(Hop hop, ArrayList<Instruction> in, String var, ExecutionContext ec, boolean force) {
    ArrayList<Instruction> tmp = in;
    try {
        boolean ret = false;
        hop.resetVisitStatus();
        if (// set forced execution type
        force)
            ret = rFindAndSetCPIndexingHOP(hop, var);
        else
            // release forced execution type
            ret = rFindAndReleaseIndexingHOP(hop, var);
        // recompilation on-demand
        if (ret) {
            // construct new instructions
            tmp = Recompiler.recompileHopsDag(hop, ec.getVariables(), null, true, false, 0);
        }
    } catch (Exception ex) {
        throw new DMLRuntimeException(ex);
    }
    return tmp;
}
Also used : BinaryCPInstruction(org.apache.sysml.runtime.instructions.cp.BinaryCPInstruction) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) Instruction(org.apache.sysml.runtime.instructions.Instruction) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 24 with Instruction

use of org.apache.sysml.runtime.instructions.Instruction in project incubator-systemml by apache.

the class ProgramConverter method rParseIfProgramBlock.

private static IfProgramBlock rParseIfProgramBlock(String in, Program prog, int id) {
    String lin = in.substring(PARFOR_PB_IF.length(), in.length() - PARFOR_PB_END.length());
    HierarchyAwareStringTokenizer st = new HierarchyAwareStringTokenizer(lin, COMPONENTS_DELIM);
    // predicate instructions
    ArrayList<Instruction> inst = parseInstructions(st.nextToken(), id);
    // exit instructions
    ArrayList<Instruction> exit = parseInstructions(st.nextToken(), id);
    // program blocks: if and else
    ArrayList<ProgramBlock> pbs1 = rParseProgramBlocks(st.nextToken(), prog, id);
    ArrayList<ProgramBlock> pbs2 = rParseProgramBlocks(st.nextToken(), prog, id);
    IfProgramBlock ipb = new IfProgramBlock(prog, inst);
    ipb.setExitInstructions2(exit);
    ipb.setChildBlocksIfBody(pbs1);
    ipb.setChildBlocksElseBody(pbs2);
    return ipb;
}
Also used : IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) ExternalFunctionProgramBlock(org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock) 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) GPUInstruction(org.apache.sysml.runtime.instructions.gpu.GPUInstruction) MRJobInstruction(org.apache.sysml.runtime.instructions.MRJobInstruction) CPInstruction(org.apache.sysml.runtime.instructions.cp.CPInstruction) ExternalFunctionInvocationInstruction(org.apache.sysml.udf.ExternalFunctionInvocationInstruction) SpoofCPInstruction(org.apache.sysml.runtime.instructions.cp.SpoofCPInstruction) Instruction(org.apache.sysml.runtime.instructions.Instruction) SPInstruction(org.apache.sysml.runtime.instructions.spark.SPInstruction) VariableCPInstruction(org.apache.sysml.runtime.instructions.cp.VariableCPInstruction) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) MRInstruction(org.apache.sysml.runtime.instructions.mr.MRInstruction)

Example 25 with Instruction

use of org.apache.sysml.runtime.instructions.Instruction in project incubator-systemml by apache.

the class ProgramConverter method cloneInstruction.

public static Instruction cloneInstruction(Instruction oInst, long pid, boolean plain, boolean cpFunctions) {
    Instruction inst = null;
    String tmpString = oInst.toString();
    try {
        if (oInst instanceof CPInstruction || oInst instanceof SPInstruction || oInst instanceof MRInstruction || oInst instanceof GPUInstruction) {
            if (oInst instanceof FunctionCallCPInstruction && cpFunctions) {
                FunctionCallCPInstruction tmp = (FunctionCallCPInstruction) oInst;
                if (!plain) {
                    // safe replacement because target variables might include the function name
                    // note: this is no update-in-place in order to keep the original function name as basis
                    tmpString = tmp.updateInstStringFunctionName(tmp.getFunctionName(), tmp.getFunctionName() + CP_CHILD_THREAD + pid);
                }
            // otherwise: preserve function name
            }
            inst = InstructionParser.parseSingleInstruction(tmpString);
        } else if (oInst instanceof MRJobInstruction) {
            // clone via copy constructor
            inst = new MRJobInstruction((MRJobInstruction) oInst);
        } else
            throw new DMLRuntimeException("Failed to clone instruction: " + oInst);
    } catch (Exception ex) {
        throw new DMLRuntimeException(ex);
    }
    // save replacement of thread id references in instructions
    inst = saveReplaceThreadID(inst, ProgramConverter.CP_ROOT_THREAD_ID, ProgramConverter.CP_CHILD_THREAD + pid);
    return inst;
}
Also used : SPInstruction(org.apache.sysml.runtime.instructions.spark.SPInstruction) CPInstruction(org.apache.sysml.runtime.instructions.cp.CPInstruction) SpoofCPInstruction(org.apache.sysml.runtime.instructions.cp.SpoofCPInstruction) VariableCPInstruction(org.apache.sysml.runtime.instructions.cp.VariableCPInstruction) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) GPUInstruction(org.apache.sysml.runtime.instructions.gpu.GPUInstruction) MRJobInstruction(org.apache.sysml.runtime.instructions.MRJobInstruction) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) MRInstruction(org.apache.sysml.runtime.instructions.mr.MRInstruction) GPUInstruction(org.apache.sysml.runtime.instructions.gpu.GPUInstruction) MRJobInstruction(org.apache.sysml.runtime.instructions.MRJobInstruction) CPInstruction(org.apache.sysml.runtime.instructions.cp.CPInstruction) ExternalFunctionInvocationInstruction(org.apache.sysml.udf.ExternalFunctionInvocationInstruction) SpoofCPInstruction(org.apache.sysml.runtime.instructions.cp.SpoofCPInstruction) Instruction(org.apache.sysml.runtime.instructions.Instruction) SPInstruction(org.apache.sysml.runtime.instructions.spark.SPInstruction) VariableCPInstruction(org.apache.sysml.runtime.instructions.cp.VariableCPInstruction) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) MRInstruction(org.apache.sysml.runtime.instructions.mr.MRInstruction) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Aggregations

Instruction (org.apache.sysml.runtime.instructions.Instruction)132 MRJobInstruction (org.apache.sysml.runtime.instructions.MRJobInstruction)90 FunctionCallCPInstruction (org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction)60 VariableCPInstruction (org.apache.sysml.runtime.instructions.cp.VariableCPInstruction)60 CPInstruction (org.apache.sysml.runtime.instructions.cp.CPInstruction)56 ArrayList (java.util.ArrayList)40 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)35 ExternalFunctionInvocationInstruction (org.apache.sysml.udf.ExternalFunctionInvocationInstruction)35 FunctionProgramBlock (org.apache.sysml.runtime.controlprogram.FunctionProgramBlock)33 IfProgramBlock (org.apache.sysml.runtime.controlprogram.IfProgramBlock)33 ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)33 WhileProgramBlock (org.apache.sysml.runtime.controlprogram.WhileProgramBlock)33 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)32 SPInstruction (org.apache.sysml.runtime.instructions.spark.SPInstruction)32 MRInstruction (org.apache.sysml.runtime.instructions.mr.MRInstruction)30 GPUInstruction (org.apache.sysml.runtime.instructions.gpu.GPUInstruction)28 SpoofCPInstruction (org.apache.sysml.runtime.instructions.cp.SpoofCPInstruction)26 ParForProgramBlock (org.apache.sysml.runtime.controlprogram.ParForProgramBlock)24 Lop (org.apache.sysml.lops.Lop)23 ExternalFunctionProgramBlock (org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock)19