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;
}
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;
}
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);
}
}
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;
}
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;
}
Aggregations