Search in sources :

Example 1 with FunctionCallCPInstruction

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

the class ProgramConverter method rFindSerializationCandidates.

private static void rFindSerializationCandidates(ArrayList<ProgramBlock> pbs, HashSet<String> cand) {
    for (ProgramBlock pb : pbs) {
        if (pb instanceof WhileProgramBlock) {
            WhileProgramBlock wpb = (WhileProgramBlock) pb;
            rFindSerializationCandidates(wpb.getChildBlocks(), cand);
        } else if (pb instanceof ForProgramBlock || pb instanceof ParForProgramBlock) {
            ForProgramBlock fpb = (ForProgramBlock) pb;
            rFindSerializationCandidates(fpb.getChildBlocks(), cand);
        } else if (pb instanceof IfProgramBlock) {
            IfProgramBlock ipb = (IfProgramBlock) pb;
            rFindSerializationCandidates(ipb.getChildBlocksIfBody(), cand);
            if (ipb.getChildBlocksElseBody() != null)
                rFindSerializationCandidates(ipb.getChildBlocksElseBody(), cand);
        } else // all generic program blocks
        {
            for (Instruction inst : pb.getInstructions()) if (inst instanceof FunctionCallCPInstruction) {
                FunctionCallCPInstruction fci = (FunctionCallCPInstruction) inst;
                String fkey = DMLProgram.constructFunctionKey(fci.getNamespace(), fci.getFunctionName());
                if (// memoization for multiple calls, recursion
                !cand.contains(fkey)) {
                    // add to candidates
                    cand.add(fkey);
                    // investigate chains of function calls
                    FunctionProgramBlock fpb = pb.getProgram().getFunctionProgramBlock(fci.getNamespace(), fci.getFunctionName());
                    rFindSerializationCandidates(fpb.getChildBlocks(), cand);
                }
            }
        }
    }
}
Also used : IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) ExternalFunctionProgramBlock(org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) 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) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) 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) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock)

Example 2 with FunctionCallCPInstruction

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

the class ProgramConverter method createDeepCopyInstructionSet.

/**
 * Creates a deep copy of an array of instructions and replaces the placeholders of parworker
 * IDs with the concrete IDs of this parfor instance. This is a helper method uses for generating
 * deep copies of program blocks.
 *
 * @param instSet list of instructions
 * @param pid ?
 * @param IDPrefix ?
 * @param prog runtime program
 * @param fnStack ?
 * @param fnCreated ?
 * @param plain ?
 * @param cpFunctions ?
 * @return list of instructions
 */
public static ArrayList<Instruction> createDeepCopyInstructionSet(ArrayList<Instruction> instSet, long pid, int IDPrefix, Program prog, HashSet<String> fnStack, HashSet<String> fnCreated, boolean plain, boolean cpFunctions) {
    ArrayList<Instruction> tmp = new ArrayList<>();
    for (Instruction inst : instSet) {
        if (inst instanceof FunctionCallCPInstruction && cpFunctions) {
            FunctionCallCPInstruction finst = (FunctionCallCPInstruction) inst;
            createDeepCopyFunctionProgramBlock(finst.getNamespace(), finst.getFunctionName(), pid, IDPrefix, prog, fnStack, fnCreated, plain);
        }
        tmp.add(cloneInstruction(inst, pid, plain, cpFunctions));
    }
    return tmp;
}
Also used : FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) ArrayList(java.util.ArrayList) 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 3 with FunctionCallCPInstruction

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

the class OptTreePlanChecker method checkFunctionNames.

private static void checkFunctionNames(Program prog, DMLProgram dprog, Hop root, ArrayList<Instruction> inst, Set<String> fnStack) {
    // reset visit status of dag
    root.resetVisitStatus();
    // get all function op in this dag
    HashMap<String, FunctionOp> fops = new HashMap<>();
    getAllFunctionOps(root, fops);
    for (Instruction linst : inst) if (linst instanceof FunctionCallCPInstruction) {
        FunctionCallCPInstruction flinst = (FunctionCallCPInstruction) linst;
        String fnamespace = flinst.getNamespace();
        String fname = flinst.getFunctionName();
        String key = DMLProgram.constructFunctionKey(fnamespace, fname);
        // check 1: instruction name equal to hop name
        if (!fops.containsKey(key))
            throw new DMLRuntimeException("Function Check: instruction and hop names differ (" + key + ", " + fops.keySet() + ")");
        // check 2: function exists
        if (!prog.getFunctionProgramBlocks().containsKey(key))
            throw new DMLRuntimeException("Function Check: function does not exits (" + key + ")");
        // check 3: recursive program check
        FunctionProgramBlock fpb = prog.getFunctionProgramBlock(fnamespace, fname);
        FunctionStatementBlock fsb = dprog.getFunctionStatementBlock(fnamespace, fname);
        if (!fnStack.contains(key)) {
            fnStack.add(key);
            checkProgramCorrectness(fpb, fsb, fnStack);
            fnStack.remove(key);
        }
    }
}
Also used : FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) HashMap(java.util.HashMap) FunctionOp(org.apache.sysml.hops.FunctionOp) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) Instruction(org.apache.sysml.runtime.instructions.Instruction) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 4 with FunctionCallCPInstruction

use of org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction 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 5 with FunctionCallCPInstruction

use of org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction 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

FunctionCallCPInstruction (org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction)24 Instruction (org.apache.sysml.runtime.instructions.Instruction)14 MRJobInstruction (org.apache.sysml.runtime.instructions.MRJobInstruction)12 VariableCPInstruction (org.apache.sysml.runtime.instructions.cp.VariableCPInstruction)12 ArrayList (java.util.ArrayList)10 FunctionProgramBlock (org.apache.sysml.runtime.controlprogram.FunctionProgramBlock)10 CPInstruction (org.apache.sysml.runtime.instructions.cp.CPInstruction)10 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)8 ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)8 MRInstruction (org.apache.sysml.runtime.instructions.mr.MRInstruction)8 SPInstruction (org.apache.sysml.runtime.instructions.spark.SPInstruction)8 IfProgramBlock (org.apache.sysml.runtime.controlprogram.IfProgramBlock)6 ParForProgramBlock (org.apache.sysml.runtime.controlprogram.ParForProgramBlock)6 WhileProgramBlock (org.apache.sysml.runtime.controlprogram.WhileProgramBlock)6 SpoofCPInstruction (org.apache.sysml.runtime.instructions.cp.SpoofCPInstruction)6 GPUInstruction (org.apache.sysml.runtime.instructions.gpu.GPUInstruction)6 ExternalFunctionInvocationInstruction (org.apache.sysml.udf.ExternalFunctionInvocationInstruction)6 FunctionOp (org.apache.sysml.hops.FunctionOp)4 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)4 ExternalFunctionProgramBlock (org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock)4