Search in sources :

Example 1 with LiteralOp

use of org.apache.sysml.hops.LiteralOp in project incubator-systemml by apache.

the class TemplateCell method rConstructCplan.

protected void rConstructCplan(Hop hop, CPlanMemoTable memo, HashMap<Long, CNode> tmp, HashSet<Hop> inHops, boolean compileLiterals) {
    //memoization for common subexpression elimination and to avoid redundant work 
    if (tmp.containsKey(hop.getHopID()))
        return;
    MemoTableEntry me = memo.getBest(hop.getHopID(), TemplateType.CellTpl);
    //recursively process required childs
    if (me != null && (me.type == TemplateType.RowTpl || me.type == TemplateType.OuterProdTpl)) {
        CNodeData cdata = TemplateUtils.createCNodeData(hop, compileLiterals);
        tmp.put(hop.getHopID(), cdata);
        inHops.add(hop);
        return;
    }
    for (int i = 0; i < hop.getInput().size(); i++) {
        Hop c = hop.getInput().get(i);
        if (me != null && me.isPlanRef(i) && !(c instanceof DataOp) && (me.type != TemplateType.MultiAggTpl || memo.contains(c.getHopID(), TemplateType.CellTpl)))
            rConstructCplan(c, memo, tmp, inHops, compileLiterals);
        else {
            CNodeData cdata = TemplateUtils.createCNodeData(c, compileLiterals);
            tmp.put(c.getHopID(), cdata);
            inHops.add(c);
        }
    }
    //construct cnode for current hop
    CNode out = null;
    if (hop instanceof UnaryOp) {
        CNode cdata1 = tmp.get(hop.getInput().get(0).getHopID());
        cdata1 = TemplateUtils.wrapLookupIfNecessary(cdata1, hop.getInput().get(0));
        String primitiveOpName = ((UnaryOp) hop).getOp().name();
        out = new CNodeUnary(cdata1, UnaryType.valueOf(primitiveOpName));
    } else if (hop instanceof BinaryOp) {
        BinaryOp bop = (BinaryOp) hop;
        CNode cdata1 = tmp.get(hop.getInput().get(0).getHopID());
        CNode cdata2 = tmp.get(hop.getInput().get(1).getHopID());
        String primitiveOpName = bop.getOp().name();
        //add lookups if required
        cdata1 = TemplateUtils.wrapLookupIfNecessary(cdata1, hop.getInput().get(0));
        cdata2 = TemplateUtils.wrapLookupIfNecessary(cdata2, hop.getInput().get(1));
        if (bop.getOp() == OpOp2.POW && cdata2.isLiteral() && cdata2.getVarname().equals("2"))
            out = new CNodeUnary(cdata1, UnaryType.POW2);
        else if (bop.getOp() == OpOp2.MULT && cdata2.isLiteral() && cdata2.getVarname().equals("2"))
            out = new CNodeUnary(cdata1, UnaryType.MULT2);
        else
            //default binary	
            out = new CNodeBinary(cdata1, cdata2, BinType.valueOf(primitiveOpName));
    } else if (hop instanceof TernaryOp) {
        TernaryOp top = (TernaryOp) hop;
        CNode cdata1 = tmp.get(hop.getInput().get(0).getHopID());
        CNode cdata2 = tmp.get(hop.getInput().get(1).getHopID());
        CNode cdata3 = tmp.get(hop.getInput().get(2).getHopID());
        //add lookups if required
        cdata1 = TemplateUtils.wrapLookupIfNecessary(cdata1, hop.getInput().get(0));
        cdata3 = TemplateUtils.wrapLookupIfNecessary(cdata3, hop.getInput().get(2));
        //construct ternary cnode, primitive operation derived from OpOp3
        out = new CNodeTernary(cdata1, cdata2, cdata3, TernaryType.valueOf(top.getOp().name()));
    } else if (hop instanceof ParameterizedBuiltinOp) {
        CNode cdata1 = tmp.get(((ParameterizedBuiltinOp) hop).getTargetHop().getHopID());
        cdata1 = TemplateUtils.wrapLookupIfNecessary(cdata1, hop.getInput().get(0));
        CNode cdata2 = tmp.get(((ParameterizedBuiltinOp) hop).getParameterHop("pattern").getHopID());
        CNode cdata3 = tmp.get(((ParameterizedBuiltinOp) hop).getParameterHop("replacement").getHopID());
        TernaryType ttype = (cdata2.isLiteral() && cdata2.getVarname().equals("Double.NaN")) ? TernaryType.REPLACE_NAN : TernaryType.REPLACE;
        out = new CNodeTernary(cdata1, cdata2, cdata3, ttype);
    } else if (hop instanceof IndexingOp) {
        CNode cdata1 = tmp.get(hop.getInput().get(0).getHopID());
        out = new CNodeTernary(cdata1, TemplateUtils.createCNodeData(new LiteralOp(hop.getInput().get(0).getDim2()), true), TemplateUtils.createCNodeData(hop.getInput().get(4), true), TernaryType.LOOKUP_RC1);
    } else if (HopRewriteUtils.isTransposeOperation(hop)) {
        out = tmp.get(hop.getInput().get(0).getHopID());
    } else if (hop instanceof AggUnaryOp) {
        //aggregation handled in template implementation (note: we do not compile 
        //^2 of SUM_SQ into the operator to simplify the detection of single operators)
        out = tmp.get(hop.getInput().get(0).getHopID());
    } else if (hop instanceof AggBinaryOp) {
        //(1) t(X)%*%X -> sum(X^2) and t(X) %*% Y -> sum(X*Y)
        if (HopRewriteUtils.isTransposeOfItself(hop.getInput().get(0), hop.getInput().get(1))) {
            CNode cdata1 = tmp.get(hop.getInput().get(1).getHopID());
            out = new CNodeUnary(cdata1, UnaryType.POW2);
        } else {
            CNode cdata1 = TemplateUtils.skipTranspose(tmp.get(hop.getInput().get(0).getHopID()), hop.getInput().get(0), tmp, compileLiterals);
            if (TemplateUtils.isColVector(cdata1))
                cdata1 = new CNodeUnary(cdata1, UnaryType.LOOKUP_R);
            CNode cdata2 = tmp.get(hop.getInput().get(1).getHopID());
            if (TemplateUtils.isColVector(cdata2))
                cdata2 = new CNodeUnary(cdata2, UnaryType.LOOKUP_R);
            out = new CNodeBinary(cdata1, cdata2, BinType.MULT);
        }
    }
    tmp.put(hop.getHopID(), out);
}
Also used : TernaryType(org.apache.sysml.hops.codegen.cplan.CNodeTernary.TernaryType) CNodeData(org.apache.sysml.hops.codegen.cplan.CNodeData) AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) UnaryOp(org.apache.sysml.hops.UnaryOp) CNodeTernary(org.apache.sysml.hops.codegen.cplan.CNodeTernary) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) Hop(org.apache.sysml.hops.Hop) CNodeBinary(org.apache.sysml.hops.codegen.cplan.CNodeBinary) TernaryOp(org.apache.sysml.hops.TernaryOp) CNode(org.apache.sysml.hops.codegen.cplan.CNode) ParameterizedBuiltinOp(org.apache.sysml.hops.ParameterizedBuiltinOp) CNodeUnary(org.apache.sysml.hops.codegen.cplan.CNodeUnary) IndexingOp(org.apache.sysml.hops.IndexingOp) AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) MemoTableEntry(org.apache.sysml.hops.codegen.template.CPlanMemoTable.MemoTableEntry) LiteralOp(org.apache.sysml.hops.LiteralOp) DataOp(org.apache.sysml.hops.DataOp) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) BinaryOp(org.apache.sysml.hops.BinaryOp)

Example 2 with LiteralOp

use of org.apache.sysml.hops.LiteralOp in project incubator-systemml by apache.

the class TemplateUtils method createCNodeData.

public static CNodeData createCNodeData(Hop hop, boolean compileLiterals) {
    CNodeData cdata = new CNodeData(hop);
    cdata.setLiteral(hop instanceof LiteralOp && (compileLiterals || UtilFunctions.isIntegerNumber(((LiteralOp) hop).getStringValue())));
    return cdata;
}
Also used : CNodeData(org.apache.sysml.hops.codegen.cplan.CNodeData) LiteralOp(org.apache.sysml.hops.LiteralOp)

Example 3 with LiteralOp

use of org.apache.sysml.hops.LiteralOp in project incubator-systemml by apache.

the class Explain method explainGDFNode.

//////////////
// internal explain GDFNODE
/**
	 * Do a post-order traverse through the GDFNode DAG and explain each GDFNode.
	 * Note: nodes referring to literalops are suppressed.
	 * 
	 * @param gnode GDF node
	 * @param level offset
	 * @param memo memoization table
	 * @return string explanation
	 * @throws DMLRuntimeException if DMLRuntimeException occurs
	 */
private static String explainGDFNode(GDFNode gnode, int level, HashSet<Long> memo) throws DMLRuntimeException {
    //basic memoization via memo table since gnode has no visit status
    if (memo.contains(gnode.getID()) || gnode.getNodeType() == NodeType.HOP_NODE && gnode.getHop() instanceof LiteralOp) {
        return "";
    }
    StringBuilder sb = new StringBuilder();
    String offset = createOffset(level);
    for (GDFNode input : gnode.getInputs()) sb.append(explainGDFNode(input, level, memo));
    //indentation
    sb.append(offset);
    //hop id
    String deps = null;
    if (SHOW_DATA_DEPENDENCIES) {
        sb.append("(" + gnode.getID() + ") ");
        StringBuilder childs = new StringBuilder();
        childs.append(" (");
        boolean childAdded = false;
        for (GDFNode input : gnode.getInputs()) {
            childs.append(childAdded ? "," : "");
            childs.append(input.getID());
            childAdded = true;
        }
        childs.append(")");
        if (childAdded)
            deps = childs.toString();
    }
    //operation string
    if (//LOOP NODES
    gnode instanceof GDFLoopNode) {
        GDFLoopNode lgnode = (GDFLoopNode) gnode;
        String offset2 = createOffset(level + 1);
        //loop header
        sb.append(lgnode.explain(deps) + "\n");
        sb.append(offset2 + "PRED:\n");
        sb.append(explainGDFNode(lgnode.getLoopPredicate(), level + 2, memo));
        sb.append(offset2 + "BODY:\n");
        //note: memo table and already done child explain prevents redundancy
        for (Entry<String, GDFNode> root : lgnode.getLoopOutputs().entrySet()) {
            sb.append(explainGDFNode(root.getValue(), level + 2, memo));
        }
    } else //GENERAL CASE (BASIC/CROSSBLOCK NODES)
    {
        sb.append(gnode.explain(deps));
        sb.append('\n');
    }
    /*
		//matrix characteristics
		sb.append(" [" + hop.getDim1() + "," 
		               + hop.getDim2() + "," 
				       + hop.getRowsInBlock() + "," 
		               + hop.getColsInBlock() + "," 
				       + hop.getNnz() + "]");
		
		//memory estimates
		sb.append(" [" + showMem(hop.getInputMemEstimate(), false) + "," 
		               + showMem(hop.getIntermediateMemEstimate(), false) + "," 
				       + showMem(hop.getOutputMemEstimate(), false) + " -> " 
		               + showMem(hop.getMemEstimate(), true) + "]");
		
		//exec type
		if (hop.getExecType() != null)
			sb.append(", " + hop.getExecType());
		*/
    //memoization
    memo.add(gnode.getID());
    return sb.toString();
}
Also used : GDFLoopNode(org.apache.sysml.hops.globalopt.gdfgraph.GDFLoopNode) GDFNode(org.apache.sysml.hops.globalopt.gdfgraph.GDFNode) LiteralOp(org.apache.sysml.hops.LiteralOp)

Example 4 with LiteralOp

use of org.apache.sysml.hops.LiteralOp in project incubator-systemml by apache.

the class DMLTranslator method processIndexingExpression.

private Hop processIndexingExpression(IndexedIdentifier source, DataIdentifier target, HashMap<String, Hop> hops) throws ParseException {
    // process Hops for indexes (for source)
    Hop rowLowerHops = null, rowUpperHops = null, colLowerHops = null, colUpperHops = null;
    if (source.getRowLowerBound() != null)
        rowLowerHops = processExpression(source.getRowLowerBound(), null, hops);
    else
        rowLowerHops = new LiteralOp(1);
    if (source.getRowUpperBound() != null)
        rowUpperHops = processExpression(source.getRowUpperBound(), null, hops);
    else {
        if (source.getOrigDim1() != -1)
            rowUpperHops = new LiteralOp(source.getOrigDim1());
        else {
            rowUpperHops = new UnaryOp(source.getName(), DataType.SCALAR, ValueType.INT, Hop.OpOp1.NROW, hops.get(source.getName()));
            rowUpperHops.setAllPositions(source.getBeginLine(), source.getBeginColumn(), source.getEndLine(), source.getEndColumn());
        }
    }
    if (source.getColLowerBound() != null)
        colLowerHops = processExpression(source.getColLowerBound(), null, hops);
    else
        colLowerHops = new LiteralOp(1);
    if (source.getColUpperBound() != null)
        colUpperHops = processExpression(source.getColUpperBound(), null, hops);
    else {
        if (source.getOrigDim2() != -1)
            colUpperHops = new LiteralOp(source.getOrigDim2());
        else
            colUpperHops = new UnaryOp(source.getName(), DataType.SCALAR, ValueType.INT, Hop.OpOp1.NCOL, hops.get(source.getName()));
    }
    if (target == null) {
        target = createTarget(source);
    }
    //unknown nnz after range indexing (applies to indexing op but also
    //data dependent operations)
    target.setNnz(-1);
    Hop indexOp = new IndexingOp(target.getName(), target.getDataType(), target.getValueType(), hops.get(source.getName()), rowLowerHops, rowUpperHops, colLowerHops, colUpperHops, source.getRowLowerEqualsUpper(), source.getColLowerEqualsUpper());
    indexOp.setAllPositions(indexOp.getBeginLine(), indexOp.getBeginColumn(), indexOp.getEndLine(), indexOp.getEndColumn());
    setIdentifierParams(indexOp, target);
    return indexOp;
}
Also used : AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) UnaryOp(org.apache.sysml.hops.UnaryOp) IndexingOp(org.apache.sysml.hops.IndexingOp) LeftIndexingOp(org.apache.sysml.hops.LeftIndexingOp) Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp)

Example 5 with LiteralOp

use of org.apache.sysml.hops.LiteralOp in project incubator-systemml by apache.

the class DMLTranslator method processExpression.

/**
	 * Construct Hops from parse tree : Process Expression in an assignment
	 * statement
	 * 
	 * @param source source expression
	 * @param target data identifier
	 * @param hops map of high-level operators
	 * @return high-level operator
	 * @throws ParseException if ParseException occurs
	 */
private Hop processExpression(Expression source, DataIdentifier target, HashMap<String, Hop> hops) throws ParseException {
    try {
        if (source instanceof BinaryExpression)
            return processBinaryExpression((BinaryExpression) source, target, hops);
        else if (source instanceof RelationalExpression)
            return processRelationalExpression((RelationalExpression) source, target, hops);
        else if (source instanceof BooleanExpression)
            return processBooleanExpression((BooleanExpression) source, target, hops);
        else if (source instanceof BuiltinFunctionExpression)
            return processBuiltinFunctionExpression((BuiltinFunctionExpression) source, target, hops);
        else if (source instanceof ParameterizedBuiltinFunctionExpression)
            return processParameterizedBuiltinFunctionExpression((ParameterizedBuiltinFunctionExpression) source, target, hops);
        else if (source instanceof DataExpression) {
            Hop ae = (Hop) processDataExpression((DataExpression) source, target, hops);
            if (ae instanceof DataOp) {
                String formatName = ((DataExpression) source).getVarParam(DataExpression.FORMAT_TYPE).toString();
                ((DataOp) ae).setInputFormatType(Expression.convertFormatType(formatName));
            }
            return ae;
        } else if (source instanceof IndexedIdentifier)
            return processIndexingExpression((IndexedIdentifier) source, target, hops);
        else if (source instanceof IntIdentifier) {
            IntIdentifier sourceInt = (IntIdentifier) source;
            LiteralOp litop = new LiteralOp(sourceInt.getValue());
            litop.setAllPositions(sourceInt.getBeginLine(), sourceInt.getBeginColumn(), sourceInt.getEndLine(), sourceInt.getEndColumn());
            setIdentifierParams(litop, sourceInt);
            return litop;
        } else if (source instanceof DoubleIdentifier) {
            DoubleIdentifier sourceDouble = (DoubleIdentifier) source;
            LiteralOp litop = new LiteralOp(sourceDouble.getValue());
            litop.setAllPositions(sourceDouble.getBeginLine(), sourceDouble.getBeginColumn(), sourceDouble.getEndLine(), sourceDouble.getEndColumn());
            setIdentifierParams(litop, sourceDouble);
            return litop;
        } else if (source instanceof BooleanIdentifier) {
            BooleanIdentifier sourceBoolean = (BooleanIdentifier) source;
            LiteralOp litop = new LiteralOp(sourceBoolean.getValue());
            litop.setAllPositions(sourceBoolean.getBeginLine(), sourceBoolean.getBeginColumn(), sourceBoolean.getEndLine(), sourceBoolean.getEndColumn());
            setIdentifierParams(litop, sourceBoolean);
            return litop;
        } else if (source instanceof StringIdentifier) {
            StringIdentifier sourceString = (StringIdentifier) source;
            LiteralOp litop = new LiteralOp(sourceString.getValue());
            litop.setAllPositions(sourceString.getBeginLine(), sourceString.getBeginColumn(), sourceString.getEndLine(), sourceString.getEndColumn());
            setIdentifierParams(litop, sourceString);
            return litop;
        } else if (source instanceof DataIdentifier)
            return hops.get(((DataIdentifier) source).getName());
    } catch (Exception e) {
        throw new ParseException(e.getMessage());
    }
    return null;
}
Also used : Hop(org.apache.sysml.hops.Hop) HopsException(org.apache.sysml.hops.HopsException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) LopsException(org.apache.sysml.lops.LopsException) IOException(java.io.IOException) LiteralOp(org.apache.sysml.hops.LiteralOp) DataOp(org.apache.sysml.hops.DataOp)

Aggregations

LiteralOp (org.apache.sysml.hops.LiteralOp)81 Hop (org.apache.sysml.hops.Hop)72 AggUnaryOp (org.apache.sysml.hops.AggUnaryOp)29 BinaryOp (org.apache.sysml.hops.BinaryOp)24 AggBinaryOp (org.apache.sysml.hops.AggBinaryOp)23 UnaryOp (org.apache.sysml.hops.UnaryOp)23 DataOp (org.apache.sysml.hops.DataOp)18 IndexingOp (org.apache.sysml.hops.IndexingOp)15 DataGenOp (org.apache.sysml.hops.DataGenOp)12 LeftIndexingOp (org.apache.sysml.hops.LeftIndexingOp)11 HashMap (java.util.HashMap)9 ArrayList (java.util.ArrayList)8 ReorgOp (org.apache.sysml.hops.ReorgOp)8 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)8 HopsException (org.apache.sysml.hops.HopsException)6 ParameterizedBuiltinOp (org.apache.sysml.hops.ParameterizedBuiltinOp)6 TernaryOp (org.apache.sysml.hops.TernaryOp)6 DataIdentifier (org.apache.sysml.parser.DataIdentifier)6 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)6 QuaternaryOp (org.apache.sysml.hops.QuaternaryOp)5