Search in sources :

Example 36 with DataIdentifier

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

the class HopRewriteUtils method createDataGenOpByVal.

public static Hop createDataGenOpByVal(Hop rowInput, Hop colInput, double value) {
    Hop val = new LiteralOp(value);
    HashMap<String, Hop> params = new HashMap<>();
    params.put(DataExpression.RAND_ROWS, rowInput);
    params.put(DataExpression.RAND_COLS, colInput);
    params.put(DataExpression.RAND_MIN, val);
    params.put(DataExpression.RAND_MAX, val);
    params.put(DataExpression.RAND_PDF, new LiteralOp(DataExpression.RAND_PDF_UNIFORM));
    params.put(DataExpression.RAND_LAMBDA, new LiteralOp(-1.0));
    params.put(DataExpression.RAND_SPARSITY, new LiteralOp(1.0));
    params.put(DataExpression.RAND_SEED, new LiteralOp(DataGenOp.UNSPECIFIED_SEED));
    // note internal refresh size information
    Hop datagen = new DataGenOp(DataGenMethod.RAND, new DataIdentifier("tmp"), params);
    datagen.setOutputBlocksizes(rowInput.getRowsInBlock(), colInput.getColsInBlock());
    copyLineNumbers(rowInput, datagen);
    if (value == 0)
        datagen.setNnz(0);
    return datagen;
}
Also used : DataIdentifier(org.apache.sysml.parser.DataIdentifier) HashMap(java.util.HashMap) DataGenOp(org.apache.sysml.hops.DataGenOp) Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp)

Example 37 with DataIdentifier

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

the class ProgramConverter method createDeepCopyFunctionProgramBlock.

/**
 * This creates a deep copy of a function program block. The central reference to singletons of function program blocks
 * poses the need for explicit copies in order to prevent conflicting writes of temporary variables (see ExternalFunctionProgramBlock.
 *
 * @param namespace function namespace
 * @param oldName ?
 * @param pid ?
 * @param IDPrefix ?
 * @param prog runtime program
 * @param fnStack ?
 * @param fnCreated ?
 * @param plain ?
 */
public static void createDeepCopyFunctionProgramBlock(String namespace, String oldName, long pid, int IDPrefix, Program prog, HashSet<String> fnStack, HashSet<String> fnCreated, boolean plain) {
    // fpb guaranteed to be non-null (checked inside getFunctionProgramBlock)
    FunctionProgramBlock fpb = prog.getFunctionProgramBlock(namespace, oldName);
    String fnameNew = (plain) ? oldName : (oldName + CP_CHILD_THREAD + pid);
    String fnameNewKey = DMLProgram.constructFunctionKey(namespace, fnameNew);
    if (prog.getFunctionProgramBlocks().containsKey(fnameNewKey))
        // prevent redundant deep copy if already existent
        return;
    // create deep copy
    FunctionProgramBlock copy = null;
    ArrayList<DataIdentifier> tmp1 = new ArrayList<>();
    ArrayList<DataIdentifier> tmp2 = new ArrayList<>();
    if (fpb.getInputParams() != null)
        tmp1.addAll(fpb.getInputParams());
    if (fpb.getOutputParams() != null)
        tmp2.addAll(fpb.getOutputParams());
    if (fpb instanceof ExternalFunctionProgramBlockCP) {
        ExternalFunctionProgramBlockCP efpb = (ExternalFunctionProgramBlockCP) fpb;
        HashMap<String, String> tmp3 = efpb.getOtherParams();
        if (IDPrefix != -1)
            copy = new ExternalFunctionProgramBlockCP(prog, tmp1, tmp2, tmp3, saveReplaceFilenameThreadID(efpb.getBaseDir(), CP_CHILD_THREAD + IDPrefix, CP_CHILD_THREAD + pid));
        else
            copy = new ExternalFunctionProgramBlockCP(prog, tmp1, tmp2, tmp3, saveReplaceFilenameThreadID(efpb.getBaseDir(), CP_ROOT_THREAD_ID, CP_CHILD_THREAD + pid));
    } else if (fpb instanceof ExternalFunctionProgramBlock) {
        ExternalFunctionProgramBlock efpb = (ExternalFunctionProgramBlock) fpb;
        HashMap<String, String> tmp3 = efpb.getOtherParams();
        if (IDPrefix != -1)
            copy = new ExternalFunctionProgramBlock(prog, tmp1, tmp2, tmp3, saveReplaceFilenameThreadID(efpb.getBaseDir(), CP_CHILD_THREAD + IDPrefix, CP_CHILD_THREAD + pid));
        else
            copy = new ExternalFunctionProgramBlock(prog, tmp1, tmp2, tmp3, saveReplaceFilenameThreadID(efpb.getBaseDir(), CP_ROOT_THREAD_ID, CP_CHILD_THREAD + pid));
    } else {
        if (!fnStack.contains(fnameNewKey)) {
            fnStack.add(fnameNewKey);
            copy = new FunctionProgramBlock(prog, tmp1, tmp2);
            copy.setChildBlocks(rcreateDeepCopyProgramBlocks(fpb.getChildBlocks(), pid, IDPrefix, fnStack, fnCreated, plain, fpb.isRecompileOnce()));
            copy.setRecompileOnce(fpb.isRecompileOnce());
            copy.setThreadID(pid);
            fnStack.remove(fnameNewKey);
        } else
            // stop deep copy for recursive function calls
            copy = fpb;
    }
    // copy.setVariables( (LocalVariableMap) fpb.getVariables() ); //implicit cloning
    // note: instructions not used by function program block
    // put
    prog.addFunctionProgramBlock(namespace, fnameNew, copy);
    fnCreated.add(DMLProgram.constructFunctionKey(namespace, fnameNew));
}
Also used : FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) ExternalFunctionProgramBlock(org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock) DataIdentifier(org.apache.sysml.parser.DataIdentifier) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExternalFunctionProgramBlock(org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock) ExternalFunctionProgramBlockCP(org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlockCP)

Example 38 with DataIdentifier

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

the class DmlSyntacticValidator method exitParForStatement.

@Override
public void exitParForStatement(ParForStatementContext ctx) {
    ParForStatement parForStmt = new ParForStatement();
    DataIdentifier iterVar = new DataIdentifier(ctx.iterVar.getText());
    HashMap<String, String> parForParamValues = new HashMap<>();
    if (ctx.parForParams != null && ctx.parForParams.size() > 0) {
        for (StrictParameterizedExpressionContext parForParamCtx : ctx.parForParams) {
            String paramVal = parForParamCtx.paramVal.getText();
            if (argVals.containsKey(paramVal))
                paramVal = argVals.get(paramVal);
            parForParamValues.put(parForParamCtx.paramName.getText(), paramVal);
        }
    }
    // 1/-1
    Expression incrementExpr = null;
    if (ctx.iterPred.info.increment != null) {
        incrementExpr = ctx.iterPred.info.increment;
    }
    IterablePredicate predicate = new IterablePredicate(ctx, iterVar, ctx.iterPred.info.from, ctx.iterPred.info.to, incrementExpr, parForParamValues, currentFile);
    parForStmt.setPredicate(predicate);
    if (ctx.body.size() > 0) {
        for (StatementContext stmtCtx : ctx.body) {
            parForStmt.addStatementBlock(getStatementBlock(stmtCtx.info.stmt));
        }
        parForStmt.mergeStatementBlocks();
    }
    ctx.info.stmt = parForStmt;
}
Also used : DataIdentifier(org.apache.sysml.parser.DataIdentifier) HashMap(java.util.HashMap) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) StrictParameterizedExpressionContext(org.apache.sysml.parser.dml.DmlParser.StrictParameterizedExpressionContext) IterablePredicate(org.apache.sysml.parser.IterablePredicate) ParForStatement(org.apache.sysml.parser.ParForStatement) ImportStatementContext(org.apache.sysml.parser.dml.DmlParser.ImportStatementContext) IfdefAssignmentStatementContext(org.apache.sysml.parser.dml.DmlParser.IfdefAssignmentStatementContext) FunctionStatementContext(org.apache.sysml.parser.dml.DmlParser.FunctionStatementContext) AccumulatorAssignmentStatementContext(org.apache.sysml.parser.dml.DmlParser.AccumulatorAssignmentStatementContext) ForStatementContext(org.apache.sysml.parser.dml.DmlParser.ForStatementContext) AssignmentStatementContext(org.apache.sysml.parser.dml.DmlParser.AssignmentStatementContext) IfStatementContext(org.apache.sysml.parser.dml.DmlParser.IfStatementContext) PathStatementContext(org.apache.sysml.parser.dml.DmlParser.PathStatementContext) WhileStatementContext(org.apache.sysml.parser.dml.DmlParser.WhileStatementContext) ParForStatementContext(org.apache.sysml.parser.dml.DmlParser.ParForStatementContext) FunctionCallAssignmentStatementContext(org.apache.sysml.parser.dml.DmlParser.FunctionCallAssignmentStatementContext) StatementContext(org.apache.sysml.parser.dml.DmlParser.StatementContext) FunctionCallMultiAssignmentStatementContext(org.apache.sysml.parser.dml.DmlParser.FunctionCallMultiAssignmentStatementContext)

Example 39 with DataIdentifier

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

the class DmlSyntacticValidator method getFunctionParameters.

private ArrayList<DataIdentifier> getFunctionParameters(List<TypedArgNoAssignContext> ctx) {
    ArrayList<DataIdentifier> retVal = new ArrayList<>();
    for (TypedArgNoAssignContext paramCtx : ctx) {
        DataIdentifier dataId = new DataIdentifier(paramCtx.paramName.getText());
        String dataType = null;
        String valueType = null;
        if (paramCtx.paramType == null || paramCtx.paramType.dataType() == null || paramCtx.paramType.dataType().getText() == null || paramCtx.paramType.dataType().getText().isEmpty()) {
            dataType = "scalar";
        } else {
            dataType = paramCtx.paramType.dataType().getText();
        }
        // check and assign data type
        checkValidDataType(dataType, paramCtx.start);
        if (dataType.equalsIgnoreCase("matrix"))
            dataId.setDataType(DataType.MATRIX);
        else if (dataType.equalsIgnoreCase("frame"))
            dataId.setDataType(DataType.FRAME);
        else if (dataType.equalsIgnoreCase("scalar"))
            dataId.setDataType(DataType.SCALAR);
        valueType = paramCtx.paramType.valueType().getText();
        if (valueType.equals("int") || valueType.equals("integer") || valueType.equals("Int") || valueType.equals("Integer")) {
            dataId.setValueType(ValueType.INT);
        } else if (valueType.equals("string") || valueType.equals("String")) {
            dataId.setValueType(ValueType.STRING);
        } else if (valueType.equals("boolean") || valueType.equals("Boolean")) {
            dataId.setValueType(ValueType.BOOLEAN);
        } else if (valueType.equals("double") || valueType.equals("Double")) {
            dataId.setValueType(ValueType.DOUBLE);
        } else if (valueType.equals("bool")) {
            notifyErrorListeners("invalid valuetype " + valueType + " (Quickfix: use \'boolean\' instead)", paramCtx.start);
            return null;
        } else {
            notifyErrorListeners("invalid valuetype " + valueType, paramCtx.start);
            return null;
        }
        retVal.add(dataId);
    }
    return retVal;
}
Also used : DataIdentifier(org.apache.sysml.parser.DataIdentifier) ArrayList(java.util.ArrayList) TypedArgNoAssignContext(org.apache.sysml.parser.dml.DmlParser.TypedArgNoAssignContext)

Example 40 with DataIdentifier

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

the class DmlSyntacticValidator method exitInternalFunctionDefExpression.

// -----------------------------------------------------------------
// Internal & External Functions Definitions
// -----------------------------------------------------------------
@Override
public void exitInternalFunctionDefExpression(InternalFunctionDefExpressionContext ctx) {
    FunctionStatement functionStmt = new FunctionStatement();
    ArrayList<DataIdentifier> functionInputs = getFunctionParameters(ctx.inputParams);
    functionStmt.setInputParams(functionInputs);
    // set function outputs
    ArrayList<DataIdentifier> functionOutputs = getFunctionParameters(ctx.outputParams);
    functionStmt.setOutputParams(functionOutputs);
    // set function name
    functionStmt.setName(ctx.name.getText());
    if (ctx.body.size() > 0) {
        // handle function body
        // Create arraylist of one statement block
        ArrayList<StatementBlock> body = new ArrayList<>();
        for (StatementContext stmtCtx : ctx.body) {
            body.add(getStatementBlock(stmtCtx.info.stmt));
        }
        functionStmt.setBody(body);
        functionStmt.mergeStatementBlocks();
    } else {
        notifyErrorListeners("functions with no statements are not allowed", ctx.start);
        return;
    }
    ctx.info.stmt = functionStmt;
    setFileLineColumn(ctx.info.stmt, ctx);
    ctx.info.functionName = ctx.name.getText();
}
Also used : ExternalFunctionStatement(org.apache.sysml.parser.ExternalFunctionStatement) FunctionStatement(org.apache.sysml.parser.FunctionStatement) DataIdentifier(org.apache.sysml.parser.DataIdentifier) ArrayList(java.util.ArrayList) StatementBlock(org.apache.sysml.parser.StatementBlock) ImportStatementContext(org.apache.sysml.parser.dml.DmlParser.ImportStatementContext) IfdefAssignmentStatementContext(org.apache.sysml.parser.dml.DmlParser.IfdefAssignmentStatementContext) FunctionStatementContext(org.apache.sysml.parser.dml.DmlParser.FunctionStatementContext) AccumulatorAssignmentStatementContext(org.apache.sysml.parser.dml.DmlParser.AccumulatorAssignmentStatementContext) ForStatementContext(org.apache.sysml.parser.dml.DmlParser.ForStatementContext) AssignmentStatementContext(org.apache.sysml.parser.dml.DmlParser.AssignmentStatementContext) IfStatementContext(org.apache.sysml.parser.dml.DmlParser.IfStatementContext) PathStatementContext(org.apache.sysml.parser.dml.DmlParser.PathStatementContext) WhileStatementContext(org.apache.sysml.parser.dml.DmlParser.WhileStatementContext) ParForStatementContext(org.apache.sysml.parser.dml.DmlParser.ParForStatementContext) FunctionCallAssignmentStatementContext(org.apache.sysml.parser.dml.DmlParser.FunctionCallAssignmentStatementContext) StatementContext(org.apache.sysml.parser.dml.DmlParser.StatementContext) FunctionCallMultiAssignmentStatementContext(org.apache.sysml.parser.dml.DmlParser.FunctionCallMultiAssignmentStatementContext)

Aggregations

DataIdentifier (org.apache.sysml.parser.DataIdentifier)56 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)13 ParameterExpression (org.apache.sysml.parser.ParameterExpression)13 Hop (org.apache.sysml.hops.Hop)12 Expression (org.apache.sysml.parser.Expression)12 LiteralOp (org.apache.sysml.hops.LiteralOp)10 BinaryExpression (org.apache.sysml.parser.BinaryExpression)8 BuiltinFunctionExpression (org.apache.sysml.parser.BuiltinFunctionExpression)8 Data (org.apache.sysml.runtime.instructions.cp.Data)7 DataGenOp (org.apache.sysml.hops.DataGenOp)6 DataOp (org.apache.sysml.hops.DataOp)6 StatementBlock (org.apache.sysml.parser.StatementBlock)6 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)6 HopsException (org.apache.sysml.hops.HopsException)4 DataType (org.apache.sysml.parser.Expression.DataType)4 ExternalFunctionStatement (org.apache.sysml.parser.ExternalFunctionStatement)4 IterablePredicate (org.apache.sysml.parser.IterablePredicate)4 LanguageException (org.apache.sysml.parser.LanguageException)4 ParForStatement (org.apache.sysml.parser.ParForStatement)4