Search in sources :

Example 36 with Instruction

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

the class ExternalFunctionProgramBlock method getBlock2CellInstructions.

/**
 * Method to generate instructions to convert input matrices from block to
 * cell. We generate a GMR job here.
 *
 * @param inputParams list of data identifiers
 * @param unBlockedFileNames map of unblocked file names
 * @return list of instructions
 */
private ArrayList<Instruction> getBlock2CellInstructions(ArrayList<DataIdentifier> inputParams, HashMap<String, String> unBlockedFileNames) {
    ArrayList<Instruction> b2cinst = null;
    // list of input matrices
    ArrayList<DataIdentifier> matrices = new ArrayList<>();
    ArrayList<DataIdentifier> matricesNoReblock = new ArrayList<>();
    // find all inputs that are matrices
    for (int i = 0; i < inputParams.size(); i++) {
        if (inputParams.get(i).getDataType().isMatrix()) {
            if (_skipInReblock.contains(inputParams.get(i).getName()))
                matricesNoReblock.add(inputParams.get(i));
            else
                matrices.add(inputParams.get(i));
        }
    }
    if (!matrices.isEmpty()) {
        b2cinst = new ArrayList<>();
        MRJobInstruction gmrInst = new MRJobInstruction(JobType.GMR);
        TreeMap<Integer, ArrayList<String>> MRJobLineNumbers = null;
        if (DMLScript.ENABLE_DEBUG_MODE) {
            MRJobLineNumbers = new TreeMap<>();
        }
        String gmrStr = "";
        ArrayList<String> inLabels = new ArrayList<>();
        ArrayList<String> outLabels = new ArrayList<>();
        String[] outputs = new String[matrices.size()];
        byte[] resultIndex = new byte[matrices.size()];
        String scratchSpaceLoc = ConfigurationManager.getScratchSpace();
        try {
            // create a GMR job that transforms each of these matrices from block to cell
            for (int i = 0; i < matrices.size(); i++) {
                inLabels.add(matrices.get(i).getName());
                outLabels.add(matrices.get(i).getName() + "_extFnInput");
                // (matrices.size()+i);
                resultIndex[i] = (byte) i;
                outputs[i] = scratchSpaceLoc + Lop.FILE_SEPARATOR + Lop.PROCESS_PREFIX + DMLScript.getUUID() + Lop.FILE_SEPARATOR + _otherParams.get(ExternalFunctionStatement.CLASS_NAME) + _runID + "_" + i + "Input";
                unBlockedFileNames.put(matrices.get(i).getName(), outputs[i]);
                if (DMLScript.ENABLE_DEBUG_MODE) {
                    // Create a dummy gmr instruction (FOR DEBUGGER)
                    gmrStr = "MR" + Lop.OPERAND_DELIMITOR + "gmr" + Lop.OPERAND_DELIMITOR + i + Lop.DATATYPE_PREFIX + matrices.get(i).getDataType() + Lop.VALUETYPE_PREFIX + matrices.get(i).getValueType() + Lop.OPERAND_DELIMITOR + i + Lop.DATATYPE_PREFIX + matrices.get(i).getDataType() + Lop.VALUETYPE_PREFIX + matrices.get(i).getValueType() + Lop.OPERAND_DELIMITOR + ConfigurationManager.getBlocksize() + Lop.OPERAND_DELIMITOR + ConfigurationManager.getBlocksize();
                    // Set MR gmr instruction line number (FOR DEBUGGER)
                    if (!MRJobLineNumbers.containsKey(matrices.get(i).getBeginLine())) {
                        MRJobLineNumbers.put(matrices.get(i).getBeginLine(), new ArrayList<String>());
                    }
                    MRJobLineNumbers.get(matrices.get(i).getBeginLine()).add(gmrStr);
                }
                // create metadata instructions to populate symbol table
                // with variables that hold unblocked matrices
                Instruction createInst = VariableCPInstruction.prepareCreateMatrixVariableInstruction(outLabels.get(i), outputs[i], false, OutputInfo.outputInfoToString(OutputInfo.TextCellOutputInfo));
                createInst.setLocation(matrices.get(i));
                b2cinst.add(createInst);
            }
            // Finally, generate GMR instruction that performs block2cell conversion
            gmrInst.setGMRInstructions(inLabels.toArray(new String[inLabels.size()]), "", "", "", "", outLabels.toArray(new String[outLabels.size()]), resultIndex, 0, 1);
            b2cinst.add(gmrInst);
            // generate instructions that rename the output variables of GMR job
            Instruction cpInst = null, rmInst = null;
            for (int i = 0; i < matrices.size(); i++) {
                cpInst = VariableCPInstruction.prepareCopyInstruction(outLabels.get(i), matrices.get(i).getName());
                rmInst = VariableCPInstruction.prepareRemoveInstruction(outLabels.get(i));
                cpInst.setLocation(matrices.get(i));
                rmInst.setLocation(matrices.get(i));
                b2cinst.add(cpInst);
                b2cinst.add(rmInst);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        // LOG instructions
        if (LOG.isTraceEnabled()) {
            LOG.trace("\n--- Block-2-Cell Instructions ---");
            for (Instruction i : b2cinst) {
                LOG.trace(i.toString());
            }
            LOG.trace("----------------------------------");
        }
    }
    // BEGIN FUNCTION PATCH
    if (!matricesNoReblock.isEmpty()) {
        for (int i = 0; i < matricesNoReblock.size(); i++) {
            String scratchSpaceLoc = ConfigurationManager.getScratchSpace();
            String filename = scratchSpaceLoc + Lop.FILE_SEPARATOR + Lop.PROCESS_PREFIX + DMLScript.getUUID() + Lop.FILE_SEPARATOR + _otherParams.get(ExternalFunctionStatement.CLASS_NAME) + _runID + "_" + i + "Input";
            unBlockedFileNames.put(matricesNoReblock.get(i).getName(), filename);
        }
    }
    // null if no input matrices
    return b2cinst;
}
Also used : MRJobInstruction(org.apache.sysml.runtime.instructions.MRJobInstruction) DataIdentifier(org.apache.sysml.parser.DataIdentifier) ArrayList(java.util.ArrayList) 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) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 37 with Instruction

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

the class ProgramBlock method executePredicateInstructions.

protected ScalarObject executePredicateInstructions(ArrayList<Instruction> inst, ValueType retType, ExecutionContext ec) {
    // execute all instructions (indexed access required due to debug mode)
    int pos = 0;
    for (Instruction currInst : inst) {
        ec.updateDebugState(pos++);
        executeSingleInstruction(currInst, ec);
    }
    // get scalar return
    ScalarObject ret = (ScalarObject) ec.getScalarInput(PRED_VAR, retType, false);
    // check and correct scalar ret type (incl save double to int)
    if (ret.getValueType() != retType)
        switch(retType) {
            case BOOLEAN:
                ret = new BooleanObject(ret.getBooleanValue());
                break;
            case INT:
                ret = new IntObject(ret.getLongValue());
                break;
            case DOUBLE:
                ret = new DoubleObject(ret.getDoubleValue());
                break;
            case STRING:
                ret = new StringObject(ret.getStringValue());
                break;
            default:
        }
    // remove predicate variable
    ec.removeVariable(PRED_VAR);
    return ret;
}
Also used : ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) IntObject(org.apache.sysml.runtime.instructions.cp.IntObject) DoubleObject(org.apache.sysml.runtime.instructions.cp.DoubleObject) StringObject(org.apache.sysml.runtime.instructions.cp.StringObject) Instruction(org.apache.sysml.runtime.instructions.Instruction) BooleanObject(org.apache.sysml.runtime.instructions.cp.BooleanObject)

Example 38 with Instruction

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

the class ProgramBlock method executeInstructions.

protected void executeInstructions(ArrayList<Instruction> inst, ExecutionContext ec) {
    for (int i = 0; i < inst.size(); i++) {
        // indexed access required due to dynamic add
        Instruction currInst = inst.get(i);
        // execute instruction
        ec.updateDebugState(i);
        executeSingleInstruction(currInst, ec);
    }
}
Also used : Instruction(org.apache.sysml.runtime.instructions.Instruction)

Example 39 with Instruction

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

the class ProgramBlock method execute.

// ////////////////////////////////////////////////////////
// core instruction execution (program block, predicate)
// ////////////////////////////////////////////////////////
/**
 * Executes this program block (incl recompilation if required).
 *
 * @param ec execution context
 */
public void execute(ExecutionContext ec) {
    ArrayList<Instruction> tmp = _inst;
    // dynamically recompile instructions if enabled and required
    try {
        if (// set program block specific remote memory
        DMLScript.isActiveAM())
            DMLAppMasterUtils.setupProgramBlockRemoteMaxMemory(this);
        long t0 = DMLScript.STATISTICS ? System.nanoTime() : 0;
        if (ConfigurationManager.isDynamicRecompilation() && _sb != null && _sb.requiresRecompilation()) {
            tmp = Recompiler.recompileHopsDag(_sb, _sb.getHops(), ec.getVariables(), null, false, true, _tid);
        }
        if (DMLScript.STATISTICS) {
            long t1 = System.nanoTime();
            Statistics.incrementHOPRecompileTime(t1 - t0);
            if (tmp != _inst)
                Statistics.incrementHOPRecompileSB();
        }
    } catch (Exception ex) {
        throw new DMLRuntimeException("Unable to recompile program block.", ex);
    }
    // actual instruction execution
    executeInstructions(tmp, ec);
}
Also used : Instruction(org.apache.sysml.runtime.instructions.Instruction) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLScriptException(org.apache.sysml.runtime.DMLScriptException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 40 with Instruction

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

the class Explain method explainInstructions.

private static String explainInstructions(ArrayList<Instruction> instSet, int level) {
    StringBuilder sb = new StringBuilder();
    String offsetInst = createOffset(level);
    for (Instruction inst : instSet) {
        String tmp = explainGenericInstruction(inst, level);
        sb.append(offsetInst);
        sb.append(tmp);
        sb.append('\n');
    }
    return sb.toString();
}
Also used : GPUInstruction(org.apache.sysml.runtime.instructions.gpu.GPUInstruction) MRJobInstruction(org.apache.sysml.runtime.instructions.MRJobInstruction) CPInstruction(org.apache.sysml.runtime.instructions.cp.CPInstruction) Instruction(org.apache.sysml.runtime.instructions.Instruction) SPInstruction(org.apache.sysml.runtime.instructions.spark.SPInstruction) CSVReblockSPInstruction(org.apache.sysml.runtime.instructions.spark.CSVReblockSPInstruction) ReblockSPInstruction(org.apache.sysml.runtime.instructions.spark.ReblockSPInstruction)

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