Search in sources :

Example 1 with BooleanObject

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

the class ProgramBlock method executePredicateInstructions.

protected ScalarObject executePredicateInstructions(ArrayList<Instruction> inst, ValueType retType, ExecutionContext ec) throws DMLRuntimeException {
    ScalarObject ret = null;
    String retName = null;
    //execute all instructions
    for (int i = 0; i < inst.size(); i++) {
        //indexed access required due to debug mode
        Instruction currInst = inst.get(i);
        if (!isRemoveVariableInstruction(currInst)) {
            //execute instruction
            ec.updateDebugState(i);
            executeSingleInstruction(currInst, ec);
            //get last return name
            if (currInst instanceof ComputationCPInstruction)
                retName = ((ComputationCPInstruction) currInst).getOutputVariableName();
            else if (currInst instanceof VariableCPInstruction && ((VariableCPInstruction) currInst).getOutputVariableName() != null)
                retName = ((VariableCPInstruction) currInst).getOutputVariableName();
        }
    }
    //get return value TODO: how do we differentiate literals and variables?
    ret = (ScalarObject) ec.getScalarInput(retName, retType, false);
    //execute rmvar instructions
    for (int i = 0; i < inst.size(); i++) {
        //indexed access required due to debug mode
        Instruction currInst = inst.get(i);
        if (isRemoveVariableInstruction(currInst)) {
            ec.updateDebugState(i);
            executeSingleInstruction(currInst, ec);
        }
    }
    //check and correct scalar ret type (incl save double to int)
    if (ret.getValueType() != retType)
        switch(retType) {
            case BOOLEAN:
                ret = new BooleanObject(ret.getName(), ret.getBooleanValue());
                break;
            case INT:
                ret = new IntObject(ret.getName(), ret.getLongValue());
                break;
            case DOUBLE:
                ret = new DoubleObject(ret.getName(), ret.getDoubleValue());
                break;
            case STRING:
                ret = new StringObject(ret.getName(), ret.getStringValue());
                break;
            default:
        }
    return ret;
}
Also used : ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) IntObject(org.apache.sysml.runtime.instructions.cp.IntObject) VariableCPInstruction(org.apache.sysml.runtime.instructions.cp.VariableCPInstruction) DoubleObject(org.apache.sysml.runtime.instructions.cp.DoubleObject) StringObject(org.apache.sysml.runtime.instructions.cp.StringObject) ComputationCPInstruction(org.apache.sysml.runtime.instructions.cp.ComputationCPInstruction) VariableCPInstruction(org.apache.sysml.runtime.instructions.cp.VariableCPInstruction) Instruction(org.apache.sysml.runtime.instructions.Instruction) ComputationCPInstruction(org.apache.sysml.runtime.instructions.cp.ComputationCPInstruction) BooleanObject(org.apache.sysml.runtime.instructions.cp.BooleanObject)

Example 2 with BooleanObject

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

the class WhileProgramBlock method executePredicate.

private BooleanObject executePredicate(ExecutionContext ec) throws DMLRuntimeException {
    BooleanObject result = null;
    try {
        if (_predicate != null && !_predicate.isEmpty()) {
            if (_sb != null) {
                if (//set program block specific remote memory
                DMLScript.isActiveAM())
                    DMLAppMasterUtils.setupProgramBlockRemoteMaxMemory(this);
                WhileStatementBlock wsb = (WhileStatementBlock) _sb;
                Hop predicateOp = wsb.getPredicateHops();
                boolean recompile = wsb.requiresPredicateRecompilation();
                result = (BooleanObject) executePredicate(_predicate, predicateOp, recompile, ValueType.BOOLEAN, ec);
            } else
                result = (BooleanObject) executePredicate(_predicate, null, false, ValueType.BOOLEAN, ec);
        } else {
            //get result var
            ScalarObject scalarResult = null;
            Data resultData = ec.getVariable(_predicateResultVar);
            if (resultData == null) {
                //note: resultvar is a literal (can it be of any value type other than String, hence no literal/varname conflict) 
                scalarResult = ec.getScalarInput(_predicateResultVar, ValueType.BOOLEAN, true);
            } else {
                scalarResult = ec.getScalarInput(_predicateResultVar, ValueType.BOOLEAN, false);
            }
            //check for invalid type String 
            if (scalarResult instanceof StringObject)
                throw new DMLRuntimeException(this.printBlockErrorLocation() + "\nWhile predicate variable " + _predicateResultVar + " evaluated to string " + scalarResult + " which is not allowed for predicates in DML");
            //process result
            if (scalarResult instanceof BooleanObject)
                result = (BooleanObject) scalarResult;
            else
                //auto casting
                result = new BooleanObject(scalarResult.getBooleanValue());
        }
    } catch (Exception ex) {
        throw new DMLRuntimeException(this.printBlockErrorLocation() + "Failed to evaluate the while predicate.", ex);
    }
    //(guaranteed to be non-null, see executePredicate/getScalarInput)
    return result;
}
Also used : ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) Hop(org.apache.sysml.hops.Hop) StringObject(org.apache.sysml.runtime.instructions.cp.StringObject) Data(org.apache.sysml.runtime.instructions.cp.Data) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLScriptException(org.apache.sysml.runtime.DMLScriptException) BooleanObject(org.apache.sysml.runtime.instructions.cp.BooleanObject) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 3 with BooleanObject

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

the class ExternalFunctionProgramBlock method verifyAndAttachOutputs.

/**
	 * Method to verify that function outputs match with declared outputs
	 * 
	 * @param ec execution context
	 * @param returnFunc package function
	 * @param outputParams output parameters
	 * @throws DMLRuntimeException if DMLRuntimeException occurs
	 */
protected void verifyAndAttachOutputs(ExecutionContext ec, PackageFunction returnFunc, String outputParams) throws DMLRuntimeException {
    ArrayList<String> outputs = getParameters(outputParams);
    if (outputs.size() != returnFunc.getNumFunctionOutputs()) {
        throw new DMLRuntimeException("Number of function outputs (" + returnFunc.getNumFunctionOutputs() + ") " + "does not match with declaration (" + outputs.size() + ").");
    }
    // iterate over each output and verify that type matches
    for (int i = 0; i < outputs.size(); i++) {
        StringTokenizer tk = new StringTokenizer(outputs.get(i), ":");
        ArrayList<String> tokens = new ArrayList<String>();
        while (tk.hasMoreTokens()) {
            tokens.add(tk.nextToken());
        }
        if (returnFunc.getFunctionOutput(i).getType() == FunctionParameterType.Matrix) {
            Matrix m = (Matrix) returnFunc.getFunctionOutput(i);
            if (!(tokens.get(0).equals(getFunctionParameterDataTypeString(FunctionParameterType.Matrix))) || !(tokens.get(2).equals(getMatrixValueTypeString(m.getValueType())))) {
                throw new DMLRuntimeException("Function output '" + outputs.get(i) + "' does not match with declaration.");
            }
            // add result to variableMapping
            String varName = tokens.get(1);
            MatrixObject newVar = createOutputMatrixObject(m);
            newVar.setVarName(varName);
            //getVariables().put(varName, newVar); //put/override in local symbol table
            ec.setVariable(varName, newVar);
            continue;
        }
        if (returnFunc.getFunctionOutput(i).getType() == FunctionParameterType.Scalar) {
            Scalar s = (Scalar) returnFunc.getFunctionOutput(i);
            if (!tokens.get(0).equals(getFunctionParameterDataTypeString(FunctionParameterType.Scalar)) || !tokens.get(2).equals(getScalarValueTypeString(s.getScalarType()))) {
                throw new DMLRuntimeException("Function output '" + outputs.get(i) + "' does not match with declaration.");
            }
            // allocate and set appropriate object based on type
            ScalarObject scalarObject = null;
            ScalarValueType type = s.getScalarType();
            switch(type) {
                case Integer:
                    scalarObject = new IntObject(tokens.get(1), Long.parseLong(s.getValue()));
                    break;
                case Double:
                    scalarObject = new DoubleObject(tokens.get(1), Double.parseDouble(s.getValue()));
                    break;
                case Boolean:
                    scalarObject = new BooleanObject(tokens.get(1), Boolean.parseBoolean(s.getValue()));
                    break;
                case Text:
                    scalarObject = new StringObject(tokens.get(1), s.getValue());
                    break;
                default:
                    throw new DMLRuntimeException("Unknown scalar value type '" + type + "' of output '" + outputs.get(i) + "'.");
            }
            //this.getVariables().put(tokens.get(1), scalarObject);
            ec.setVariable(tokens.get(1), scalarObject);
            continue;
        }
        if (returnFunc.getFunctionOutput(i).getType() == FunctionParameterType.Object) {
            if (!tokens.get(0).equals(getFunctionParameterDataTypeString(FunctionParameterType.Object))) {
                throw new DMLRuntimeException("Function output '" + outputs.get(i) + "' does not match with declaration.");
            }
            throw new DMLRuntimeException("Object types not yet supported");
        // continue;
        }
        throw new DMLRuntimeException("Unknown data type '" + returnFunc.getFunctionOutput(i).getType() + "' " + "of output '" + outputs.get(i) + "'.");
    }
}
Also used : MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) DoubleObject(org.apache.sysml.runtime.instructions.cp.DoubleObject) ArrayList(java.util.ArrayList) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) Scalar(org.apache.sysml.udf.Scalar) ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) StringTokenizer(java.util.StringTokenizer) Matrix(org.apache.sysml.udf.Matrix) IntObject(org.apache.sysml.runtime.instructions.cp.IntObject) ScalarValueType(org.apache.sysml.udf.Scalar.ScalarValueType) StringObject(org.apache.sysml.runtime.instructions.cp.StringObject) BooleanObject(org.apache.sysml.runtime.instructions.cp.BooleanObject)

Example 4 with BooleanObject

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

the class IfProgramBlock method executePredicate.

private BooleanObject executePredicate(ExecutionContext ec) throws DMLRuntimeException {
    BooleanObject result = null;
    try {
        if (_predicate != null && !_predicate.isEmpty()) {
            if (_sb != null) {
                if (//set program block specific remote memory
                DMLScript.isActiveAM())
                    DMLAppMasterUtils.setupProgramBlockRemoteMaxMemory(this);
                IfStatementBlock isb = (IfStatementBlock) _sb;
                Hop predicateOp = isb.getPredicateHops();
                boolean recompile = isb.requiresPredicateRecompilation();
                result = (BooleanObject) executePredicate(_predicate, predicateOp, recompile, ValueType.BOOLEAN, ec);
            } else
                result = (BooleanObject) executePredicate(_predicate, null, false, ValueType.BOOLEAN, ec);
        } else {
            //get result var
            ScalarObject scalarResult = null;
            Data resultData = ec.getVariable(_predicateResultVar);
            if (resultData == null) {
                //note: resultvar is a literal (can it be of any value type other than String, hence no literal/varname conflict) 
                scalarResult = ec.getScalarInput(_predicateResultVar, ValueType.BOOLEAN, true);
            } else {
                scalarResult = ec.getScalarInput(_predicateResultVar, ValueType.BOOLEAN, false);
            }
            //check for invalid type String 
            if (scalarResult instanceof StringObject)
                throw new DMLRuntimeException(this.printBlockErrorLocation() + "\nIf predicate variable " + _predicateResultVar + " evaluated to string " + scalarResult + " which is not allowed for predicates in DML");
            //process result
            if (scalarResult instanceof BooleanObject)
                result = (BooleanObject) scalarResult;
            else
                //auto casting
                result = new BooleanObject(scalarResult.getBooleanValue());
        }
    } catch (Exception ex) {
        throw new DMLRuntimeException(this.printBlockErrorLocation() + "Failed to evaluate the IF predicate.", ex);
    }
    //(guaranteed to be non-null, see executePredicate/getScalarInput)
    return result;
}
Also used : ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) Hop(org.apache.sysml.hops.Hop) StringObject(org.apache.sysml.runtime.instructions.cp.StringObject) Data(org.apache.sysml.runtime.instructions.cp.Data) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLScriptException(org.apache.sysml.runtime.DMLScriptException) BooleanObject(org.apache.sysml.runtime.instructions.cp.BooleanObject) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 5 with BooleanObject

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

the class ParForProgramBlock method createEmptyUnscopedVariables.

/**
	 * Create empty matrix objects and scalars for all unscoped vars 
	 * (created within the parfor).
	 * 
	 * NOTE: parfor gives no guarantees on the values of those objects - hence
	 * we return -1 for sclars and empty matrix objects.
	 * 
	 * @param out local variable map
	 * @param sb statement block
	 * @throws DMLRuntimeException if DMLRuntimeException occurs
	 */
private void createEmptyUnscopedVariables(LocalVariableMap out, StatementBlock sb) throws DMLRuntimeException {
    VariableSet updated = sb.variablesUpdated();
    VariableSet livein = sb.liveIn();
    //for all vars IN <updated> AND NOT IN <livein>
    for (String var : updated.getVariableNames()) if (!livein.containsVariable(var)) {
        //create empty output
        DataIdentifier dat = updated.getVariable(var);
        DataType datatype = dat.getDataType();
        ValueType valuetype = dat.getValueType();
        Data dataObj = null;
        switch(datatype) {
            case SCALAR:
                switch(valuetype) {
                    case BOOLEAN:
                        dataObj = new BooleanObject(var, false);
                        break;
                    case INT:
                        dataObj = new IntObject(var, -1);
                        break;
                    case DOUBLE:
                        dataObj = new DoubleObject(var, -1d);
                        break;
                    case STRING:
                        dataObj = new StringObject(var, "-1");
                        break;
                    default:
                        throw new DMLRuntimeException("Value type not supported: " + valuetype);
                }
                break;
            case MATRIX:
                //because metadata (e.g., outputinfo) not known at this place.
                break;
            case UNKNOWN:
                break;
            default:
                throw new DMLRuntimeException("Data type not supported: " + datatype);
        }
        if (dataObj != null)
            out.put(var, dataObj);
    }
}
Also used : VariableSet(org.apache.sysml.parser.VariableSet) DataIdentifier(org.apache.sysml.parser.DataIdentifier) IntObject(org.apache.sysml.runtime.instructions.cp.IntObject) ValueType(org.apache.sysml.parser.Expression.ValueType) DoubleObject(org.apache.sysml.runtime.instructions.cp.DoubleObject) StringObject(org.apache.sysml.runtime.instructions.cp.StringObject) DataType(org.apache.sysml.parser.Expression.DataType) Data(org.apache.sysml.runtime.instructions.cp.Data) BooleanObject(org.apache.sysml.runtime.instructions.cp.BooleanObject) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Aggregations

BooleanObject (org.apache.sysml.runtime.instructions.cp.BooleanObject)9 StringObject (org.apache.sysml.runtime.instructions.cp.StringObject)7 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)6 DoubleObject (org.apache.sysml.runtime.instructions.cp.DoubleObject)5 IntObject (org.apache.sysml.runtime.instructions.cp.IntObject)5 ScalarObject (org.apache.sysml.runtime.instructions.cp.ScalarObject)5 Data (org.apache.sysml.runtime.instructions.cp.Data)4 DMLScriptException (org.apache.sysml.runtime.DMLScriptException)3 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)3 StringTokenizer (java.util.StringTokenizer)2 Hop (org.apache.sysml.hops.Hop)2 DataType (org.apache.sysml.parser.Expression.DataType)2 ValueType (org.apache.sysml.parser.Expression.ValueType)2 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)2 FrameBlock (org.apache.sysml.runtime.matrix.data.FrameBlock)2 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)2 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 JavaPairRDD (org.apache.spark.api.java.JavaPairRDD)1 JavaRDD (org.apache.spark.api.java.JavaRDD)1