Search in sources :

Example 16 with ValueType

use of org.apache.sysml.parser.Expression.ValueType in project incubator-systemml by apache.

the class HopRewriteUtils method createUnary.

public static UnaryOp createUnary(Hop input, OpOp1 type) {
    DataType dt = (type == OpOp1.CAST_AS_SCALAR) ? DataType.SCALAR : (type == OpOp1.CAST_AS_MATRIX) ? DataType.MATRIX : input.getDataType();
    ValueType vt = (type == OpOp1.CAST_AS_MATRIX) ? ValueType.DOUBLE : input.getValueType();
    UnaryOp unary = new UnaryOp(input.getName(), dt, vt, type, input);
    unary.setOutputBlocksizes(input.getRowsInBlock(), input.getColsInBlock());
    if (type == OpOp1.CAST_AS_SCALAR || type == OpOp1.CAST_AS_MATRIX) {
        int dim = (type == OpOp1.CAST_AS_SCALAR) ? 0 : 1;
        int blksz = (type == OpOp1.CAST_AS_SCALAR) ? 0 : ConfigurationManager.getBlocksize();
        setOutputParameters(unary, dim, dim, blksz, blksz, -1);
    }
    copyLineNumbers(input, unary);
    unary.refreshSizeInformation();
    return unary;
}
Also used : AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) UnaryOp(org.apache.sysml.hops.UnaryOp) ValueType(org.apache.sysml.parser.Expression.ValueType) DataType(org.apache.sysml.parser.Expression.DataType)

Example 17 with ValueType

use of org.apache.sysml.parser.Expression.ValueType in project incubator-systemml by apache.

the class StatementBlock method rewriteFunctionCallStatements.

public ArrayList<Statement> rewriteFunctionCallStatements(DMLProgram dmlProg, ArrayList<Statement> statements) {
    ArrayList<Statement> newStatements = new ArrayList<>();
    for (Statement current : statements) {
        if (isRewritableFunctionCall(current, dmlProg)) {
            Expression sourceExpr = null;
            if (current instanceof AssignmentStatement)
                sourceExpr = ((AssignmentStatement) current).getSource();
            else
                sourceExpr = ((MultiAssignmentStatement) current).getSource();
            FunctionCallIdentifier fcall = (FunctionCallIdentifier) sourceExpr;
            FunctionStatementBlock fblock = dmlProg.getFunctionStatementBlock(fcall.getNamespace(), fcall.getName());
            if (fblock == null) {
                fcall.raiseValidateError("function " + fcall.getName() + " is undefined in namespace " + fcall.getNamespace(), false);
            }
            FunctionStatement fstmt = (FunctionStatement) fblock.getStatement(0);
            // recursive inlining (no memo required because update-inplace of function statement blocks, so no redundant inlining)
            if (rIsInlineableFunction(fblock, dmlProg)) {
                fstmt.getBody().get(0).setStatements(rewriteFunctionCallStatements(dmlProg, fstmt.getBody().get(0).getStatements()));
            }
            // MB: we cannot use the hash since multiple interleaved inlined functions should be independent.
            // String prefix = new Integer(fblock.hashCode()).toString() + "_";
            String prefix = _seq.getNextID() + "_";
            if (fstmt.getBody().size() > 1) {
                sourceExpr.raiseValidateError("rewritable function can only have 1 statement block", false);
            }
            StatementBlock sblock = fstmt.getBody().get(0);
            if (fcall.getParamExprs().size() != fstmt.getInputParams().size()) {
                sourceExpr.raiseValidateError("Wrong number of function input arguments: " + fcall.getParamExprs().size() + " found, but " + fstmt.getInputParams().size() + " expected.");
            }
            for (int i = 0; i < fstmt.getInputParams().size(); i++) {
                DataIdentifier currFormalParam = fstmt.getInputParams().get(i);
                // create new assignment statement
                String newFormalParameterName = prefix + currFormalParam.getName();
                DataIdentifier newTarget = new DataIdentifier(currFormalParam);
                newTarget.setName(newFormalParameterName);
                Expression currCallParam = fcall.getParamExprs().get(i).getExpr();
                // auto casting of inputs on inlining (if required)
                ValueType targetVT = newTarget.getValueType();
                if (newTarget.getDataType() == DataType.SCALAR && currCallParam.getOutput() != null && targetVT != currCallParam.getOutput().getValueType() && targetVT != ValueType.STRING) {
                    currCallParam = new BuiltinFunctionExpression(BuiltinFunctionExpression.getValueTypeCastOperator(targetVT), new Expression[] { currCallParam }, newTarget);
                }
                // create the assignment statement to bind the call parameter to formal parameter
                AssignmentStatement binding = new AssignmentStatement(newTarget, currCallParam, newTarget);
                newStatements.add(binding);
            }
            for (Statement stmt : sblock._statements) {
                // rewrite the statement to use the "rewritten" name
                Statement rewrittenStmt = stmt.rewriteStatement(prefix);
                newStatements.add(rewrittenStmt);
            }
            if (current instanceof AssignmentStatement) {
                if (fstmt.getOutputParams().size() == 0) {
                    AssignmentStatement as = (AssignmentStatement) current;
                    if ((as.getTargetList().size() == 1) && (as.getTargetList().get(0) != null)) {
                        raiseValidateError("Function '" + fcall.getName() + "' does not return a value but is assigned to " + as.getTargetList().get(0), true);
                    }
                }
            } else if (current instanceof MultiAssignmentStatement) {
                if (fstmt.getOutputParams().size() == 0) {
                    MultiAssignmentStatement mas = (MultiAssignmentStatement) current;
                    raiseValidateError("Function '" + fcall.getName() + "' does not return a value but is assigned to " + mas.getTargetList(), true);
                }
            }
            // handle the return values
            for (int i = 0; i < fstmt.getOutputParams().size(); i++) {
                // get the target (return parameter from function)
                DataIdentifier currReturnParam = fstmt.getOutputParams().get(i);
                String newSourceName = prefix + currReturnParam.getName();
                DataIdentifier newSource = new DataIdentifier(currReturnParam);
                newSource.setName(newSourceName);
                // get binding
                DataIdentifier newTarget = null;
                if (current instanceof AssignmentStatement) {
                    if (i > 0) {
                        fstmt.raiseValidateError("Assignment statement cannot return multiple values", false);
                    }
                    AssignmentStatement as = (AssignmentStatement) current;
                    DataIdentifier targ = as.getTarget();
                    if (targ == null) {
                        Expression exp = as.getSource();
                        FunctionCallIdentifier fci = (FunctionCallIdentifier) exp;
                        String functionName = fci.getName();
                        fstmt.raiseValidateError(functionName + " requires LHS value", false);
                    } else {
                        newTarget = new DataIdentifier(((AssignmentStatement) current).getTarget());
                    }
                } else {
                    newTarget = new DataIdentifier(((MultiAssignmentStatement) current).getTargetList().get(i));
                }
                // auto casting of inputs on inlining (always, redundant cast removed during Hop Rewrites)
                ValueType sourceVT = newSource.getValueType();
                if (newSource.getDataType() == DataType.SCALAR && sourceVT != ValueType.STRING) {
                    newSource = new BuiltinFunctionExpression(BuiltinFunctionExpression.getValueTypeCastOperator(sourceVT), new Expression[] { newSource }, newTarget);
                }
                // create the assignment statement to bind the call parameter to formal parameter
                AssignmentStatement binding = new AssignmentStatement(newTarget, newSource, newTarget);
                newStatements.add(binding);
            }
        } else // end if (isRewritableFunctionCall(current, dmlProg)
        {
            newStatements.add(current);
        }
    }
    return newStatements;
}
Also used : ValueType(org.apache.sysml.parser.Expression.ValueType) ArrayList(java.util.ArrayList)

Example 18 with ValueType

use of org.apache.sysml.parser.Expression.ValueType 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
 */
private static void createEmptyUnscopedVariables(LocalVariableMap out, StatementBlock sb) {
    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(false);
                        break;
                    case INT:
                        dataObj = new IntObject(-1);
                        break;
                    case DOUBLE:
                        dataObj = new DoubleObject(-1d);
                        break;
                    case STRING:
                        dataObj = new StringObject("-1");
                        break;
                    default:
                        throw new DMLRuntimeException("Value type not supported: " + valuetype);
                }
                break;
            case MATRIX:
            case FRAME:
                // 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)

Example 19 with ValueType

use of org.apache.sysml.parser.Expression.ValueType in project incubator-systemml by apache.

the class GenerateClassesForMLContext method getParamTypeAsString.

/**
 * Obtain a string representation of a parameter type, where a Matrix or
 * Frame is represented by its full class name.
 *
 * @param param
 *            the function parameter
 * @return string representation of a parameter type
 */
public static String getParamTypeAsString(DataIdentifier param) {
    DataType dt = param.getDataType();
    ValueType vt = param.getValueType();
    if ((dt == DataType.SCALAR) && (vt == ValueType.INT)) {
        return "long";
    } else if ((dt == DataType.SCALAR) && (vt == ValueType.DOUBLE)) {
        return "double";
    } else if ((dt == DataType.SCALAR) && (vt == ValueType.BOOLEAN)) {
        return "boolean";
    } else if ((dt == DataType.SCALAR) && (vt == ValueType.STRING)) {
        return "String";
    } else if (dt == DataType.MATRIX) {
        return "org.apache.sysml.api.mlcontext.Matrix";
    } else if (dt == DataType.FRAME) {
        return "org.apache.sysml.api.mlcontext.Frame";
    }
    return null;
}
Also used : ValueType(org.apache.sysml.parser.Expression.ValueType) DataType(org.apache.sysml.parser.Expression.DataType)

Example 20 with ValueType

use of org.apache.sysml.parser.Expression.ValueType in project incubator-systemml by apache.

the class GenerateClassesForMLContext method getSimpleParamTypeAsString.

/**
 * Obtain a string representation of a parameter type, where a Matrix or
 * Frame is represented by its simple class name.
 *
 * @param param
 *            the function parameter
 * @return string representation of a parameter type
 */
public static String getSimpleParamTypeAsString(DataIdentifier param) {
    DataType dt = param.getDataType();
    ValueType vt = param.getValueType();
    if ((dt == DataType.SCALAR) && (vt == ValueType.INT)) {
        return "long";
    } else if ((dt == DataType.SCALAR) && (vt == ValueType.DOUBLE)) {
        return "double";
    } else if ((dt == DataType.SCALAR) && (vt == ValueType.BOOLEAN)) {
        return "boolean";
    } else if ((dt == DataType.SCALAR) && (vt == ValueType.STRING)) {
        return "String";
    } else if (dt == DataType.MATRIX) {
        return "Matrix";
    } else if (dt == DataType.FRAME) {
        return "Frame";
    }
    return null;
}
Also used : ValueType(org.apache.sysml.parser.Expression.ValueType) DataType(org.apache.sysml.parser.Expression.DataType)

Aggregations

ValueType (org.apache.sysml.parser.Expression.ValueType)55 FrameBlock (org.apache.sysml.runtime.matrix.data.FrameBlock)23 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)19 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)18 DataType (org.apache.sysml.parser.Expression.DataType)11 MetaDataFormat (org.apache.sysml.runtime.matrix.MetaDataFormat)10 IOException (java.io.IOException)9 LongWritable (org.apache.hadoop.io.LongWritable)7 FrameObject (org.apache.sysml.runtime.controlprogram.caching.FrameObject)7 RDDObject (org.apache.sysml.runtime.instructions.spark.data.RDDObject)7 ArrayList (java.util.ArrayList)6 Text (org.apache.hadoop.io.Text)6 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)6 RUNTIME_PLATFORM (org.apache.sysml.api.DMLScript.RUNTIME_PLATFORM)5 ConvertStringToLongTextPair (org.apache.sysml.runtime.instructions.spark.functions.ConvertStringToLongTextPair)5 OutputInfo (org.apache.sysml.runtime.matrix.data.OutputInfo)5 TestConfiguration (org.apache.sysml.test.integration.TestConfiguration)5 Row (org.apache.spark.sql.Row)4 StructType (org.apache.spark.sql.types.StructType)4 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)4