Search in sources :

Example 36 with ScalarObject

use of org.apache.sysml.runtime.instructions.cp.ScalarObject in project systemml by apache.

the class ForProgramBlock method executePredicateInstructions.

protected IntObject executePredicateInstructions(int pos, ArrayList<Instruction> instructions, ExecutionContext ec) {
    ScalarObject tmp = null;
    IntObject ret = null;
    try {
        if (_sb != null) {
            if (// set program block specific remote memory
            DMLScript.isActiveAM())
                DMLAppMasterUtils.setupProgramBlockRemoteMaxMemory(this);
            ForStatementBlock fsb = (ForStatementBlock) _sb;
            Hop predHops = null;
            boolean recompile = false;
            if (pos == 1) {
                predHops = fsb.getFromHops();
                recompile = fsb.requiresFromRecompilation();
            } else if (pos == 2) {
                predHops = fsb.getToHops();
                recompile = fsb.requiresToRecompilation();
            } else if (pos == 3) {
                predHops = fsb.getIncrementHops();
                recompile = fsb.requiresIncrementRecompilation();
            }
            tmp = (IntObject) executePredicate(instructions, predHops, recompile, ValueType.INT, ec);
        } else
            tmp = (IntObject) executePredicate(instructions, null, false, ValueType.INT, ec);
    } catch (Exception ex) {
        String predNameStr = null;
        if (pos == 1)
            predNameStr = "from";
        else if (pos == 2)
            predNameStr = "to";
        else if (pos == 3)
            predNameStr = "increment";
        throw new DMLRuntimeException(this.printBlockErrorLocation() + "Error evaluating '" + predNameStr + "' predicate", ex);
    }
    // final check of resulting int object (guaranteed to be non-null, see executePredicate)
    if (tmp instanceof IntObject)
        ret = (IntObject) tmp;
    else
        // downcast to int if necessary
        ret = new IntObject(tmp.getLongValue());
    return ret;
}
Also used : ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) IntObject(org.apache.sysml.runtime.instructions.cp.IntObject) Hop(org.apache.sysml.hops.Hop) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLScriptException(org.apache.sysml.runtime.DMLScriptException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 37 with ScalarObject

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

use of org.apache.sysml.runtime.instructions.cp.ScalarObject in project systemml by apache.

the class RewriteConstantFolding method evalScalarOperation.

/**
 * In order to (1) prevent unexpected side effects from constant folding and
 * (2) for simplicity with regard to arbitrary value type combinations,
 * we use the same compilation and runtime for constant folding as we would
 * use for actual instruction execution.
 *
 * @param bop high-level operator
 * @return literal op
 */
private LiteralOp evalScalarOperation(Hop bop) {
    // Timing time = new Timing( true );
    DataOp tmpWrite = new DataOp(TMP_VARNAME, bop.getDataType(), bop.getValueType(), bop, DataOpTypes.TRANSIENTWRITE, TMP_VARNAME);
    // generate runtime instruction
    Dag<Lop> dag = new Dag<>();
    // prevent lops reuse
    Recompiler.rClearLops(tmpWrite);
    // reconstruct lops
    Lop lops = tmpWrite.constructLops();
    lops.addToDag(dag);
    ArrayList<Instruction> inst = dag.getJobs(null, ConfigurationManager.getDMLConfig());
    // execute instructions
    ExecutionContext ec = getExecutionContext();
    ProgramBlock pb = getProgramBlock();
    pb.setInstructions(inst);
    pb.execute(ec);
    // get scalar result (check before invocation) and create literal according
    // to observed scalar output type (not hop type) for runtime consistency
    ScalarObject so = (ScalarObject) ec.getVariable(TMP_VARNAME);
    LiteralOp literal = null;
    switch(so.getValueType()) {
        case DOUBLE:
            literal = new LiteralOp(so.getDoubleValue());
            break;
        case INT:
            literal = new LiteralOp(so.getLongValue());
            break;
        case BOOLEAN:
            literal = new LiteralOp(so.getBooleanValue());
            break;
        case STRING:
            literal = new LiteralOp(so.getStringValue());
            break;
        default:
            throw new HopsException("Unsupported literal value type: " + bop.getValueType());
    }
    // cleanup
    tmpWrite.getInput().clear();
    bop.getParent().remove(tmpWrite);
    pb.setInstructions(null);
    ec.getVariables().removeAll();
    // set literal properties (scalar)
    HopRewriteUtils.setOutputParametersForScalar(literal);
    return literal;
}
Also used : ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) ExecutionContext(org.apache.sysml.runtime.controlprogram.context.ExecutionContext) ProgramBlock(org.apache.sysml.runtime.controlprogram.ProgramBlock) Dag(org.apache.sysml.lops.compile.Dag) HopsException(org.apache.sysml.hops.HopsException) Lop(org.apache.sysml.lops.Lop) Instruction(org.apache.sysml.runtime.instructions.Instruction) LiteralOp(org.apache.sysml.hops.LiteralOp) DataOp(org.apache.sysml.hops.DataOp)

Example 39 with ScalarObject

use of org.apache.sysml.runtime.instructions.cp.ScalarObject in project systemml by apache.

the class InterProceduralAnalysis method populateLocalVariableMapForFunctionCall.

private static void populateLocalVariableMapForFunctionCall(FunctionStatement fstmt, FunctionOp fop, LocalVariableMap callvars, LocalVariableMap vars, FunctionCallSizeInfo fcallSizes) {
    ArrayList<DataIdentifier> inputVars = fstmt.getInputParams();
    ArrayList<Hop> inputOps = fop.getInput();
    String fkey = fop.getFunctionKey();
    for (int i = 0; i < inputVars.size(); i++) {
        // create mapping between input hops and vars
        DataIdentifier dat = inputVars.get(i);
        Hop input = inputOps.get(i);
        if (input.getDataType() == DataType.MATRIX) {
            // propagate matrix characteristics
            MatrixObject mo = new MatrixObject(ValueType.DOUBLE, null);
            MatrixCharacteristics mc = new MatrixCharacteristics(input.getDim1(), input.getDim2(), ConfigurationManager.getBlocksize(), ConfigurationManager.getBlocksize(), fcallSizes.isSafeNnz(fkey, i) ? input.getNnz() : -1);
            MetaDataFormat meta = new MetaDataFormat(mc, null, null);
            mo.setMetaData(meta);
            vars.put(dat.getName(), mo);
        } else if (input.getDataType() == DataType.SCALAR) {
            // (for multiple calls, literal equivalence already checked)
            if (input instanceof LiteralOp) {
                vars.put(dat.getName(), ScalarObjectFactory.createScalarObject(input.getValueType(), (LiteralOp) input));
            } else // and input scalar is existing variable in symbol table
            if (PROPAGATE_SCALAR_VARS_INTO_FUN && fcallSizes.getFunctionCallCount(fkey) == 1 && input instanceof DataOp) {
                Data scalar = callvars.get(input.getName());
                if (scalar != null && scalar instanceof ScalarObject) {
                    vars.put(dat.getName(), scalar);
                }
            }
        }
    }
}
Also used : MetaDataFormat(org.apache.sysml.runtime.matrix.MetaDataFormat) DataIdentifier(org.apache.sysml.parser.DataIdentifier) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) Hop(org.apache.sysml.hops.Hop) Data(org.apache.sysml.runtime.instructions.cp.Data) MatrixCharacteristics(org.apache.sysml.runtime.matrix.MatrixCharacteristics) ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) LiteralOp(org.apache.sysml.hops.LiteralOp) DataOp(org.apache.sysml.hops.DataOp)

Example 40 with ScalarObject

use of org.apache.sysml.runtime.instructions.cp.ScalarObject in project systemml by apache.

the class LiteralReplacement method getIntValueDataLiteral.

private static long getIntValueDataLiteral(Hop hop, LocalVariableMap vars) {
    long value = -1;
    try {
        if (hop instanceof LiteralOp) {
            value = HopRewriteUtils.getIntValue((LiteralOp) hop);
        } else if (hop instanceof UnaryOp && ((UnaryOp) hop).getOp() == OpOp1.NROW) {
            // get the dimension information from the matrix object because the hop
            // dimensions might not have been updated during recompile
            MatrixObject mo = (MatrixObject) vars.get(hop.getInput().get(0).getName());
            value = mo.getNumRows();
        } else if (hop instanceof UnaryOp && ((UnaryOp) hop).getOp() == OpOp1.NCOL) {
            // get the dimension information from the matrix object because the hop
            // dimensions might not have been updated during recompile
            MatrixObject mo = (MatrixObject) vars.get(hop.getInput().get(0).getName());
            value = mo.getNumColumns();
        } else {
            ScalarObject sdat = (ScalarObject) vars.get(hop.getName());
            value = sdat.getLongValue();
        }
    } catch (HopsException ex) {
        throw new DMLRuntimeException("Failed to get int value for literal replacement", ex);
    }
    return value;
}
Also used : ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) UnaryOp(org.apache.sysml.hops.UnaryOp) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) HopsException(org.apache.sysml.hops.HopsException) LiteralOp(org.apache.sysml.hops.LiteralOp) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Aggregations

ScalarObject (org.apache.sysml.runtime.instructions.cp.ScalarObject)42 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)23 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)17 CPOperand (org.apache.sysml.runtime.instructions.cp.CPOperand)14 DoubleObject (org.apache.sysml.runtime.instructions.cp.DoubleObject)12 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)12 LiteralOp (org.apache.sysml.hops.LiteralOp)11 IntObject (org.apache.sysml.runtime.instructions.cp.IntObject)10 DataOp (org.apache.sysml.hops.DataOp)9 SparkExecutionContext (org.apache.sysml.runtime.controlprogram.context.SparkExecutionContext)9 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)8 MatrixIndexes (org.apache.sysml.runtime.matrix.data.MatrixIndexes)8 Data (org.apache.sysml.runtime.instructions.cp.Data)7 ArrayList (java.util.ArrayList)6 UnaryOp (org.apache.sysml.hops.UnaryOp)6 BooleanObject (org.apache.sysml.runtime.instructions.cp.BooleanObject)6 StringObject (org.apache.sysml.runtime.instructions.cp.StringObject)6 MetaDataFormat (org.apache.sysml.runtime.matrix.MetaDataFormat)6 ScalarOperator (org.apache.sysml.runtime.matrix.operators.ScalarOperator)6 Hop (org.apache.sysml.hops.Hop)5