Search in sources :

Example 1 with Instruction

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

the class Recompiler method recompileHopsDag.

/**
	 * A) Recompile basic program block hop DAG.
	 * 	
	 * We support to basic types inplace or via deep copy. Deep copy is the default and is required 
	 * in order to apply non-reversible rewrites. In-place is required in order to modify the existing
	 * hops (e.g., for parfor pre-recompilation).
	 * 
	 * @param sb statement block
	 * @param hops high-level operators
	 * @param vars local variable map
	 * @param status the recompile status
	 * @param inplace true if in place
	 * @param litreplace true if literal replacement
	 * @param tid thread id
	 * @return list of instructions
	 * @throws DMLRuntimeException if DMLRuntimeException occurs
	 * @throws HopsException if HopsException occurs
	 * @throws LopsException if LopsException occurs
	 * @throws IOException if IOException occurs
	 */
public static ArrayList<Instruction> recompileHopsDag(StatementBlock sb, ArrayList<Hop> hops, LocalVariableMap vars, RecompileStatus status, boolean inplace, boolean litreplace, long tid) throws DMLRuntimeException, HopsException, LopsException, IOException {
    ArrayList<Instruction> newInst = null;
    //however, we create deep copies for most dags to allow for concurrent recompile
    synchronized (hops) {
        LOG.debug("\n**************** Optimizer (Recompile) *************\nMemory Budget = " + OptimizerUtils.toMB(OptimizerUtils.getLocalMemBudget()) + " MB");
        // prepare hops dag for recompile
        if (!inplace) {
            // deep copy hop dag (for non-reversable rewrites)
            hops = deepCopyHopsDag(hops);
        } else {
            // clear existing lops
            Hop.resetVisitStatus(hops);
            for (Hop hopRoot : hops) rClearLops(hopRoot);
        }
        // replace scalar reads with literals 
        if (!inplace && litreplace) {
            Hop.resetVisitStatus(hops);
            for (Hop hopRoot : hops) rReplaceLiterals(hopRoot, vars, false);
        }
        // refresh matrix characteristics (update stats)			
        Hop.resetVisitStatus(hops);
        for (Hop hopRoot : hops) rUpdateStatistics(hopRoot, vars);
        // dynamic hop rewrites
        if (!inplace) {
            _rewriter.get().rewriteHopDAGs(hops, null);
            //update stats after rewrites
            Hop.resetVisitStatus(hops);
            for (Hop hopRoot : hops) rUpdateStatistics(hopRoot, vars);
        }
        // refresh memory estimates (based on updated stats,
        // before: init memo table with propagated worst-case estimates,
        // after: extract worst-case estimates from memo table 
        Hop.resetVisitStatus(hops);
        MemoTable memo = new MemoTable();
        memo.init(hops, status);
        Hop.resetVisitStatus(hops);
        for (Hop hopRoot : hops) hopRoot.refreshMemEstimates(memo);
        memo.extract(hops, status);
        // codegen if enabled
        if (ConfigurationManager.getDMLConfig().getBooleanValue(DMLConfig.CODEGEN) && SpoofCompiler.RECOMPILE_CODEGEN) {
            Hop.resetVisitStatus(hops);
            hops = SpoofCompiler.optimize(hops, true);
        }
        // construct lops			
        Dag<Lop> dag = new Dag<Lop>();
        for (Hop hopRoot : hops) {
            Lop lops = hopRoot.constructLops();
            lops.addToDag(dag);
        }
        // generate runtime instructions (incl piggybacking)
        newInst = dag.getJobs(sb, ConfigurationManager.getDMLConfig());
    }
    // replace thread ids in new instructions
    if (//only in parfor context
    tid != 0)
        newInst = ProgramConverter.createDeepCopyInstructionSet(newInst, tid, -1, null, null, null, false, false);
    // explain recompiled hops / instructions
    if (DMLScript.EXPLAIN == ExplainType.RECOMPILE_HOPS) {
        LOG.info("EXPLAIN RECOMPILE \nGENERIC (lines " + sb.getBeginLine() + "-" + sb.getEndLine() + "):\n" + Explain.explainHops(hops, 1));
    }
    if (DMLScript.EXPLAIN == ExplainType.RECOMPILE_RUNTIME) {
        LOG.info("EXPLAIN RECOMPILE \nGENERIC (lines " + sb.getBeginLine() + "-" + sb.getEndLine() + "):\n" + Explain.explain(newInst, 1));
    }
    return newInst;
}
Also used : Hop(org.apache.sysml.hops.Hop) Dag(org.apache.sysml.lops.compile.Dag) MRJobInstruction(org.apache.sysml.runtime.instructions.MRJobInstruction) Instruction(org.apache.sysml.runtime.instructions.Instruction) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) SeqInstruction(org.apache.sysml.runtime.instructions.mr.SeqInstruction) RandInstruction(org.apache.sysml.runtime.instructions.mr.RandInstruction) Lop(org.apache.sysml.lops.Lop) MemoTable(org.apache.sysml.hops.MemoTable)

Example 2 with Instruction

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

the class Recompiler method recompileHopsDag.

/**
	 * B) Recompile predicate hop DAG (single root): 
	 * 
	 * Note: This overloaded method is required for predicate instructions because
	 * they have only a single hops DAG and we need to synchronize on the original 
	 * (shared) hops object. Hence, we cannot create any wrapper arraylist for each
	 * recompilation - this would result in race conditions for concurrent recompilation 
	 * in a parfor body. 	
	 * 
	 * Note: no statementblock passed because for predicate dags we dont have separate live variable analysis information.
	 * 
	 * @param hops high-level operator
	 * @param vars local variable map
	 * @param status recompile status
	 * @param inplace true if in place
	 * @param litreplace true if literal replacement
	 * @param tid thread id
	 * @return list of instructions
	 * @throws DMLRuntimeException if DMLRuntimeException occurs
	 * @throws HopsException if HopsException occurs
	 * @throws LopsException if LopsException occurs
	 * @throws IOException if IOException occurs
	 */
public static ArrayList<Instruction> recompileHopsDag(Hop hops, LocalVariableMap vars, RecompileStatus status, boolean inplace, boolean litreplace, long tid) throws DMLRuntimeException, HopsException, LopsException, IOException {
    ArrayList<Instruction> newInst = null;
    //need for synchronization as we do temp changes in shared hops/lops
    synchronized (hops) {
        LOG.debug("\n**************** Optimizer (Recompile) *************\nMemory Budget = " + OptimizerUtils.toMB(OptimizerUtils.getLocalMemBudget()) + " MB");
        // prepare hops dag for recompile
        if (!inplace) {
            // deep copy hop dag (for non-reversable rewrites)
            //(this also clears existing lops in the created dag) 
            hops = deepCopyHopsDag(hops);
        } else {
            // clear existing lops
            hops.resetVisitStatus();
            rClearLops(hops);
        }
        // replace scalar reads with literals 
        if (!inplace && litreplace) {
            hops.resetVisitStatus();
            rReplaceLiterals(hops, vars, false);
        }
        // refresh matrix characteristics (update stats)			
        hops.resetVisitStatus();
        rUpdateStatistics(hops, vars);
        // dynamic hop rewrites
        if (!inplace) {
            _rewriter.get().rewriteHopDAG(hops, null);
            //update stats after rewrites
            hops.resetVisitStatus();
            rUpdateStatistics(hops, vars);
        }
        // refresh memory estimates (based on updated stats)
        MemoTable memo = new MemoTable();
        hops.resetVisitStatus();
        memo.init(hops, status);
        hops.resetVisitStatus();
        hops.refreshMemEstimates(memo);
        // codegen if enabled
        if (ConfigurationManager.getDMLConfig().getBooleanValue(DMLConfig.CODEGEN) && SpoofCompiler.RECOMPILE_CODEGEN) {
            hops.resetVisitStatus();
            hops = SpoofCompiler.optimize(hops, false);
        }
        // construct lops			
        Dag<Lop> dag = new Dag<Lop>();
        Lop lops = hops.constructLops();
        lops.addToDag(dag);
        // generate runtime instructions (incl piggybacking)
        newInst = dag.getJobs(null, ConfigurationManager.getDMLConfig());
    }
    // replace thread ids in new instructions
    if (//only in parfor context
    tid != 0)
        newInst = ProgramConverter.createDeepCopyInstructionSet(newInst, tid, -1, null, null, null, false, false);
    // explain recompiled instructions
    if (DMLScript.EXPLAIN == ExplainType.RECOMPILE_HOPS)
        LOG.info("EXPLAIN RECOMPILE \nPRED (line " + hops.getBeginLine() + "):\n" + Explain.explain(hops, 1));
    if (DMLScript.EXPLAIN == ExplainType.RECOMPILE_RUNTIME)
        LOG.info("EXPLAIN RECOMPILE \nPRED (line " + hops.getBeginLine() + "):\n" + Explain.explain(newInst, 1));
    return newInst;
}
Also used : Dag(org.apache.sysml.lops.compile.Dag) MRJobInstruction(org.apache.sysml.runtime.instructions.MRJobInstruction) Instruction(org.apache.sysml.runtime.instructions.Instruction) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) SeqInstruction(org.apache.sysml.runtime.instructions.mr.SeqInstruction) RandInstruction(org.apache.sysml.runtime.instructions.mr.RandInstruction) Lop(org.apache.sysml.lops.Lop) MemoTable(org.apache.sysml.hops.MemoTable)

Example 3 with Instruction

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

the class Recompiler method recompileWhilePredicate.

private static void recompileWhilePredicate(WhileProgramBlock wpb, WhileStatementBlock wsb, LocalVariableMap vars, RecompileStatus status, long tid, boolean resetRecompile) throws DMLRuntimeException, HopsException, LopsException, IOException {
    if (wsb != null) {
        Hop hops = wsb.getPredicateHops();
        if (hops != null) {
            ArrayList<Instruction> tmp = recompileHopsDag(hops, vars, status, true, false, tid);
            wpb.setPredicate(tmp);
            if (ParForProgramBlock.RESET_RECOMPILATION_FLAGs && resetRecompile) {
                Hop.resetRecompilationFlag(hops, ExecType.CP);
                wsb.updatePredicateRecompilationFlag();
            }
            //update predicate vars (potentially after constant folding, e.g., in parfor)
            if (hops instanceof LiteralOp)
                wpb.setPredicateResultVar(((LiteralOp) hops).getName().toLowerCase());
        }
    }
}
Also used : Hop(org.apache.sysml.hops.Hop) MRJobInstruction(org.apache.sysml.runtime.instructions.MRJobInstruction) Instruction(org.apache.sysml.runtime.instructions.Instruction) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) SeqInstruction(org.apache.sysml.runtime.instructions.mr.SeqInstruction) RandInstruction(org.apache.sysml.runtime.instructions.mr.RandInstruction) LiteralOp(org.apache.sysml.hops.LiteralOp)

Example 4 with Instruction

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

the class Recompiler method recompileForPredicates.

private static void recompileForPredicates(ForProgramBlock fpb, ForStatementBlock fsb, LocalVariableMap vars, RecompileStatus status, long tid, boolean resetRecompile) throws DMLRuntimeException, HopsException, LopsException, IOException {
    if (fsb != null) {
        Hop fromHops = fsb.getFromHops();
        Hop toHops = fsb.getToHops();
        Hop incrHops = fsb.getIncrementHops();
        //handle recompilation flags
        if (ParForProgramBlock.RESET_RECOMPILATION_FLAGs && resetRecompile) {
            if (fromHops != null) {
                ArrayList<Instruction> tmp = recompileHopsDag(fromHops, vars, status, true, false, tid);
                fpb.setFromInstructions(tmp);
                Hop.resetRecompilationFlag(fromHops, ExecType.CP);
            }
            if (toHops != null) {
                ArrayList<Instruction> tmp = recompileHopsDag(toHops, vars, status, true, false, tid);
                fpb.setToInstructions(tmp);
                Hop.resetRecompilationFlag(toHops, ExecType.CP);
            }
            if (incrHops != null) {
                ArrayList<Instruction> tmp = recompileHopsDag(incrHops, vars, status, true, false, tid);
                fpb.setIncrementInstructions(tmp);
                Hop.resetRecompilationFlag(incrHops, ExecType.CP);
            }
            fsb.updatePredicateRecompilationFlags();
        } else //no reset of recompilation flags
        {
            if (fromHops != null) {
                ArrayList<Instruction> tmp = recompileHopsDag(fromHops, vars, status, true, false, tid);
                fpb.setFromInstructions(tmp);
            }
            if (toHops != null) {
                ArrayList<Instruction> tmp = recompileHopsDag(toHops, vars, status, true, false, tid);
                fpb.setToInstructions(tmp);
            }
            if (incrHops != null) {
                ArrayList<Instruction> tmp = recompileHopsDag(incrHops, vars, status, true, false, tid);
                fpb.setIncrementInstructions(tmp);
            }
        }
        //update predicate vars (potentially after constant folding, e.g., in parfor)
        String[] itervars = fpb.getIterablePredicateVars();
        if (fromHops != null && fromHops instanceof LiteralOp)
            itervars[1] = ((LiteralOp) fromHops).getName();
        if (toHops != null && toHops instanceof LiteralOp)
            itervars[2] = ((LiteralOp) toHops).getName();
        if (incrHops != null && incrHops instanceof LiteralOp)
            itervars[3] = ((LiteralOp) incrHops).getName();
    }
}
Also used : Hop(org.apache.sysml.hops.Hop) MRJobInstruction(org.apache.sysml.runtime.instructions.MRJobInstruction) Instruction(org.apache.sysml.runtime.instructions.Instruction) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) SeqInstruction(org.apache.sysml.runtime.instructions.mr.SeqInstruction) RandInstruction(org.apache.sysml.runtime.instructions.mr.RandInstruction) LiteralOp(org.apache.sysml.hops.LiteralOp)

Example 5 with Instruction

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

the class ExternalFunctionProgramBlock method executeInstruction.

/**
	 * Method to execute an external function invocation instruction.
	 * 
	 * @param ec execution context
	 * @param inst external function invocation instructions
	 * @throws DMLRuntimeException if DMLRuntimeException occurs
	 */
@SuppressWarnings("unchecked")
public void executeInstruction(ExecutionContext ec, ExternalFunctionInvocationInstruction inst) throws DMLRuntimeException {
    String className = inst.getClassName();
    String configFile = inst.getConfigFile();
    if (className == null)
        throw new DMLRuntimeException(this.printBlockErrorLocation() + "Class name can't be null");
    // create instance of package function.
    Object o;
    try {
        Class<Instruction> cla = (Class<Instruction>) Class.forName(className);
        o = cla.newInstance();
    } catch (Exception e) {
        throw new DMLRuntimeException(this.printBlockErrorLocation() + "Error generating package function object ", e);
    }
    if (!(o instanceof PackageFunction))
        throw new DMLRuntimeException(this.printBlockErrorLocation() + "Class is not of type PackageFunction");
    PackageFunction func = (PackageFunction) o;
    // add inputs to this package function based on input parameter
    // and their mappings.
    setupInputs(func, inst.getInputParams(), ec.getVariables());
    func.setConfiguration(configFile);
    func.setBaseDir(_baseDir);
    //executes function
    func.execute();
    // verify output of function execution matches declaration
    // and add outputs to variableMapping and Metadata
    verifyAndAttachOutputs(ec, func, inst.getOutputParams());
}
Also used : BooleanObject(org.apache.sysml.runtime.instructions.cp.BooleanObject) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) IntObject(org.apache.sysml.runtime.instructions.cp.IntObject) BinaryObject(org.apache.sysml.udf.BinaryObject) ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) DoubleObject(org.apache.sysml.runtime.instructions.cp.DoubleObject) StringObject(org.apache.sysml.runtime.instructions.cp.StringObject) PackageFunction(org.apache.sysml.udf.PackageFunction) MRJobInstruction(org.apache.sysml.runtime.instructions.MRJobInstruction) VariableCPInstruction(org.apache.sysml.runtime.instructions.cp.VariableCPInstruction) ExternalFunctionInvocationInstruction(org.apache.sysml.udf.ExternalFunctionInvocationInstruction) Instruction(org.apache.sysml.runtime.instructions.Instruction) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) CacheException(org.apache.sysml.runtime.controlprogram.caching.CacheException) 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