Search in sources :

Example 1 with Binary

use of org.apache.sysml.lops.Binary in project incubator-systemml by apache.

the class AggBinaryOp method constructCPLopsMMWithLeftTransposeRewrite.

private Lop constructCPLopsMMWithLeftTransposeRewrite() throws HopsException, LopsException {
    //guaranteed to exists
    Hop X = getInput().get(0).getInput().get(0);
    Hop Y = getInput().get(1);
    int k = OptimizerUtils.getConstrainedNumThreads(_maxNumThreads);
    //right vector transpose
    Lop lY = Y.constructLops();
    Lop tY = (lY instanceof Transform && ((Transform) lY).getOperationType() == OperationTypes.Transpose) ? //if input is already a transpose, avoid redundant transpose ops
    lY.getInputs().get(0) : new Transform(lY, OperationTypes.Transpose, getDataType(), getValueType(), ExecType.CP, k);
    tY.getOutputParameters().setDimensions(Y.getDim2(), Y.getDim1(), getRowsInBlock(), getColsInBlock(), Y.getNnz());
    setLineNumbers(tY);
    //matrix mult
    Lop mult = new Binary(tY, X.constructLops(), Binary.OperationTypes.MATMULT, getDataType(), getValueType(), ExecType.CP, k);
    mult.getOutputParameters().setDimensions(Y.getDim2(), X.getDim2(), getRowsInBlock(), getColsInBlock(), getNnz());
    setLineNumbers(mult);
    //result transpose (dimensions set outside)
    Lop out = new Transform(mult, OperationTypes.Transpose, getDataType(), getValueType(), ExecType.CP, k);
    return out;
}
Also used : MultiThreadedHop(org.apache.sysml.hops.Hop.MultiThreadedHop) Binary(org.apache.sysml.lops.Binary) Lop(org.apache.sysml.lops.Lop) Transform(org.apache.sysml.lops.Transform)

Example 2 with Binary

use of org.apache.sysml.lops.Binary in project incubator-systemml by apache.

the class AggBinaryOp method constructCPLopsMM.

private void constructCPLopsMM() throws HopsException, LopsException {
    Lop matmultCP = null;
    if (DMLScript.USE_ACCELERATOR && (DMLScript.FORCE_ACCELERATOR || getMemEstimate() < OptimizerUtils.GPU_MEMORY_BUDGET)) {
        Hop h1 = getInput().get(0);
        Hop h2 = getInput().get(1);
        Lop left;
        Lop right;
        boolean isLeftTransposed;
        boolean isRightTransposed;
        if (HopRewriteUtils.isTransposeOperation(h1)) {
            isLeftTransposed = true;
            left = h1.getInput().get(0).constructLops();
        } else {
            isLeftTransposed = false;
            left = h1.constructLops();
        }
        if (HopRewriteUtils.isTransposeOperation(h2)) {
            isRightTransposed = true;
            right = h2.getInput().get(0).constructLops();
        } else {
            isRightTransposed = false;
            right = h2.constructLops();
        }
        matmultCP = new Binary(left, right, Binary.OperationTypes.MATMULT, getDataType(), getValueType(), ExecType.GPU, isLeftTransposed, isRightTransposed);
        setOutputDimensions(matmultCP);
        setNnz(-1);
    } else {
        if (isLeftTransposeRewriteApplicable(true, false)) {
            matmultCP = constructCPLopsMMWithLeftTransposeRewrite();
        } else {
            int k = OptimizerUtils.getConstrainedNumThreads(_maxNumThreads);
            matmultCP = new Binary(getInput().get(0).constructLops(), getInput().get(1).constructLops(), Binary.OperationTypes.MATMULT, getDataType(), getValueType(), ExecType.CP, k);
        }
        setOutputDimensions(matmultCP);
    }
    setLineNumbers(matmultCP);
    setLops(matmultCP);
}
Also used : MultiThreadedHop(org.apache.sysml.hops.Hop.MultiThreadedHop) Binary(org.apache.sysml.lops.Binary) Lop(org.apache.sysml.lops.Lop)

Example 3 with Binary

use of org.apache.sysml.lops.Binary in project incubator-systemml by apache.

the class BinaryOp method constructLopsBinaryDefault.

private void constructLopsBinaryDefault() throws HopsException, LopsException {
    /* Default behavior for BinaryOp */
    // it depends on input data types
    DataType dt1 = getInput().get(0).getDataType();
    DataType dt2 = getInput().get(1).getDataType();
    if (dt1 == dt2 && dt1 == DataType.SCALAR) {
        // Both operands scalar
        BinaryScalar binScalar1 = new BinaryScalar(getInput().get(0).constructLops(), getInput().get(1).constructLops(), HopsOpOp2LopsBS.get(op), getDataType(), getValueType());
        binScalar1.getOutputParameters().setDimensions(0, 0, 0, 0, -1);
        setLineNumbers(binScalar1);
        setLops(binScalar1);
    } else if ((dt1 == DataType.MATRIX && dt2 == DataType.SCALAR) || (dt1 == DataType.SCALAR && dt2 == DataType.MATRIX)) {
        // One operand is Matrix and the other is scalar
        ExecType et = optFindExecType();
        //select specific operator implementations
        Unary.OperationTypes ot = null;
        Hop right = getInput().get(1);
        if (op == OpOp2.POW && right instanceof LiteralOp && ((LiteralOp) right).getDoubleValue() == 2.0)
            ot = Unary.OperationTypes.POW2;
        else if (op == OpOp2.MULT && right instanceof LiteralOp && ((LiteralOp) right).getDoubleValue() == 2.0)
            ot = Unary.OperationTypes.MULTIPLY2;
        else
            //general case
            ot = HopsOpOp2LopsU.get(op);
        if (DMLScript.USE_ACCELERATOR && (DMLScript.FORCE_ACCELERATOR || getMemEstimate() < OptimizerUtils.GPU_MEMORY_BUDGET) && (op == OpOp2.MULT || op == OpOp2.PLUS || op == OpOp2.MINUS || op == OpOp2.DIV || op == OpOp2.POW)) {
            et = ExecType.GPU;
        }
        Unary unary1 = new Unary(getInput().get(0).constructLops(), getInput().get(1).constructLops(), ot, getDataType(), getValueType(), et);
        setOutputDimensions(unary1);
        setLineNumbers(unary1);
        setLops(unary1);
    } else {
        // Both operands are Matrixes
        ExecType et = optFindExecType();
        if (et == ExecType.CP) {
            if (DMLScript.USE_ACCELERATOR && (DMLScript.FORCE_ACCELERATOR || getMemEstimate() < OptimizerUtils.GPU_MEMORY_BUDGET) && (op == OpOp2.MULT || op == OpOp2.PLUS || op == OpOp2.MINUS || op == OpOp2.DIV || op == OpOp2.POW || op == OpOp2.SOLVE)) {
                et = ExecType.GPU;
            }
            Lop binary = null;
            boolean isLeftXGt = (getInput().get(0) instanceof BinaryOp) && ((BinaryOp) getInput().get(0)).getOp() == OpOp2.GREATER;
            Hop potentialZero = isLeftXGt ? ((BinaryOp) getInput().get(0)).getInput().get(1) : null;
            boolean isLeftXGt0 = isLeftXGt && potentialZero != null && potentialZero instanceof LiteralOp && ((LiteralOp) potentialZero).getDoubleValue() == 0;
            if (op == OpOp2.MULT && isLeftXGt0 && !getInput().get(0).isVector() && !getInput().get(1).isVector() && getInput().get(0).dimsKnown() && getInput().get(1).dimsKnown()) {
                binary = new ConvolutionTransform(getInput().get(0).getInput().get(0).constructLops(), getInput().get(1).constructLops(), ConvolutionTransform.OperationTypes.RELU_BACKWARD, getDataType(), getValueType(), et, -1);
            } else
                binary = new Binary(getInput().get(0).constructLops(), getInput().get(1).constructLops(), HopsOpOp2LopsB.get(op), getDataType(), getValueType(), et);
            setOutputDimensions(binary);
            setLineNumbers(binary);
            setLops(binary);
        } else if (et == ExecType.SPARK) {
            Hop left = getInput().get(0);
            Hop right = getInput().get(1);
            MMBinaryMethod mbin = optFindMMBinaryMethodSpark(left, right);
            Lop binary = null;
            if (mbin == MMBinaryMethod.MR_BINARY_UAGG_CHAIN) {
                AggUnaryOp uRight = (AggUnaryOp) right;
                binary = new BinaryUAggChain(left.constructLops(), HopsOpOp2LopsB.get(op), HopsAgg2Lops.get(uRight.getOp()), HopsDirection2Lops.get(uRight.getDirection()), getDataType(), getValueType(), et);
            } else if (mbin == MMBinaryMethod.MR_BINARY_M) {
                boolean partitioned = false;
                boolean isColVector = (right.getDim2() == 1 && left.getDim1() == right.getDim1());
                binary = new BinaryM(left.constructLops(), right.constructLops(), HopsOpOp2LopsB.get(op), getDataType(), getValueType(), et, partitioned, isColVector);
            } else {
                binary = new Binary(left.constructLops(), right.constructLops(), HopsOpOp2LopsB.get(op), getDataType(), getValueType(), et);
            }
            setOutputDimensions(binary);
            setLineNumbers(binary);
            setLops(binary);
        } else //MR
        {
            Hop left = getInput().get(0);
            Hop right = getInput().get(1);
            MMBinaryMethod mbin = optFindMMBinaryMethod(left, right);
            if (mbin == MMBinaryMethod.MR_BINARY_M) {
                boolean needPart = requiresPartitioning(right);
                Lop dcInput = right.constructLops();
                if (needPart) {
                    //right side in distributed cache
                    ExecType etPart = (OptimizerUtils.estimateSizeExactSparsity(right.getDim1(), right.getDim2(), OptimizerUtils.getSparsity(right.getDim1(), right.getDim2(), right.getNnz())) < OptimizerUtils.getLocalMemBudget()) ? ExecType.CP : //operator selection
                    ExecType.MR;
                    dcInput = new DataPartition(dcInput, DataType.MATRIX, ValueType.DOUBLE, etPart, (right.getDim2() == 1) ? PDataPartitionFormat.ROW_BLOCK_WISE_N : PDataPartitionFormat.COLUMN_BLOCK_WISE_N);
                    dcInput.getOutputParameters().setDimensions(right.getDim1(), right.getDim2(), right.getRowsInBlock(), right.getColsInBlock(), right.getNnz());
                    dcInput.setAllPositions(right.getBeginLine(), right.getBeginColumn(), right.getEndLine(), right.getEndColumn());
                }
                BinaryM binary = new BinaryM(left.constructLops(), dcInput, HopsOpOp2LopsB.get(op), getDataType(), getValueType(), ExecType.MR, needPart, (right.getDim2() == 1 && left.getDim1() == right.getDim1()));
                setOutputDimensions(binary);
                setLineNumbers(binary);
                setLops(binary);
            } else if (mbin == MMBinaryMethod.MR_BINARY_UAGG_CHAIN) {
                AggUnaryOp uRight = (AggUnaryOp) right;
                BinaryUAggChain bin = new BinaryUAggChain(left.constructLops(), HopsOpOp2LopsB.get(op), HopsAgg2Lops.get(uRight.getOp()), HopsDirection2Lops.get(uRight.getDirection()), getDataType(), getValueType(), et);
                setOutputDimensions(bin);
                setLineNumbers(bin);
                setLops(bin);
            } else if (mbin == MMBinaryMethod.MR_BINARY_OUTER_R) {
                boolean requiresRepLeft = (!right.dimsKnown() || right.getDim2() > right.getColsInBlock());
                boolean requiresRepRight = (!left.dimsKnown() || left.getDim1() > right.getRowsInBlock());
                Lop leftLop = left.constructLops();
                Lop rightLop = right.constructLops();
                if (requiresRepLeft) {
                    //ncol of right determines rep of left
                    Lop offset = createOffsetLop(right, true);
                    leftLop = new RepMat(leftLop, offset, true, left.getDataType(), left.getValueType());
                    setOutputDimensions(leftLop);
                    setLineNumbers(leftLop);
                }
                if (requiresRepRight) {
                    //nrow of right determines rep of right
                    Lop offset = createOffsetLop(left, false);
                    rightLop = new RepMat(rightLop, offset, false, right.getDataType(), right.getValueType());
                    setOutputDimensions(rightLop);
                    setLineNumbers(rightLop);
                }
                Group group1 = new Group(leftLop, Group.OperationTypes.Sort, getDataType(), getValueType());
                setLineNumbers(group1);
                setOutputDimensions(group1);
                Group group2 = new Group(rightLop, Group.OperationTypes.Sort, getDataType(), getValueType());
                setLineNumbers(group2);
                setOutputDimensions(group2);
                Binary binary = new Binary(group1, group2, HopsOpOp2LopsB.get(op), getDataType(), getValueType(), et);
                setOutputDimensions(binary);
                setLineNumbers(binary);
                setLops(binary);
            } else //MMBinaryMethod.MR_BINARY_R
            {
                boolean requiresRep = requiresReplication(left, right);
                Lop rightLop = right.constructLops();
                if (requiresRep) {
                    //ncol of left input (determines num replicates)
                    Lop offset = createOffsetLop(left, (right.getDim2() <= 1));
                    rightLop = new RepMat(rightLop, offset, (right.getDim2() <= 1), right.getDataType(), right.getValueType());
                    setOutputDimensions(rightLop);
                    setLineNumbers(rightLop);
                }
                Group group1 = new Group(getInput().get(0).constructLops(), Group.OperationTypes.Sort, getDataType(), getValueType());
                setLineNumbers(group1);
                setOutputDimensions(group1);
                Group group2 = new Group(rightLop, Group.OperationTypes.Sort, getDataType(), getValueType());
                setLineNumbers(group2);
                setOutputDimensions(group2);
                Binary binary = new Binary(group1, group2, HopsOpOp2LopsB.get(op), getDataType(), getValueType(), et);
                setLineNumbers(binary);
                setOutputDimensions(binary);
                setLops(binary);
            }
        }
    }
}
Also used : Group(org.apache.sysml.lops.Group) BinaryM(org.apache.sysml.lops.BinaryM) BinaryUAggChain(org.apache.sysml.lops.BinaryUAggChain) Lop(org.apache.sysml.lops.Lop) Unary(org.apache.sysml.lops.Unary) CombineUnary(org.apache.sysml.lops.CombineUnary) RepMat(org.apache.sysml.lops.RepMat) OperationTypes(org.apache.sysml.lops.CombineBinary.OperationTypes) DataType(org.apache.sysml.parser.Expression.DataType) ExecType(org.apache.sysml.lops.LopProperties.ExecType) Binary(org.apache.sysml.lops.Binary) CombineBinary(org.apache.sysml.lops.CombineBinary) BinaryScalar(org.apache.sysml.lops.BinaryScalar) ConvolutionTransform(org.apache.sysml.lops.ConvolutionTransform) DataPartition(org.apache.sysml.lops.DataPartition)

Example 4 with Binary

use of org.apache.sysml.lops.Binary in project incubator-systemml by apache.

the class LeftIndexingOp method constructLops.

@Override
public Lop constructLops() throws HopsException, LopsException {
    //return already created lops
    if (getLops() != null)
        return getLops();
    try {
        ExecType et = optFindExecType();
        if (et == ExecType.MR) {
            //the right matrix is reindexed
            Lop top = getInput().get(2).constructLops();
            Lop bottom = getInput().get(3).constructLops();
            Lop left = getInput().get(4).constructLops();
            Lop right = getInput().get(5).constructLops();
            //right hand matrix
            Lop nrow = new UnaryCP(getInput().get(0).constructLops(), OperationTypes.NROW, DataType.SCALAR, ValueType.INT);
            Lop ncol = new UnaryCP(getInput().get(0).constructLops(), OperationTypes.NCOL, DataType.SCALAR, ValueType.INT);
            Lop rightInput = null;
            if (isRightHandSideScalar()) {
                //insert cast to matrix if necessary (for reuse MR runtime)
                rightInput = new UnaryCP(getInput().get(1).constructLops(), OperationTypes.CAST_AS_MATRIX, DataType.MATRIX, ValueType.DOUBLE);
                rightInput.getOutputParameters().setDimensions((long) 1, (long) 1, (long) ConfigurationManager.getBlocksize(), (long) ConfigurationManager.getBlocksize(), (long) -1);
            } else
                rightInput = getInput().get(1).constructLops();
            RangeBasedReIndex reindex = new RangeBasedReIndex(rightInput, top, bottom, left, right, nrow, ncol, getDataType(), getValueType(), et, true);
            reindex.getOutputParameters().setDimensions(getInput().get(0).getDim1(), getInput().get(0).getDim2(), getRowsInBlock(), getColsInBlock(), getNnz());
            setLineNumbers(reindex);
            Group group1 = new Group(reindex, Group.OperationTypes.Sort, DataType.MATRIX, getValueType());
            group1.getOutputParameters().setDimensions(getInput().get(0).getDim1(), getInput().get(0).getDim2(), getRowsInBlock(), getColsInBlock(), getNnz());
            setLineNumbers(group1);
            //the left matrix is zeroed out
            ZeroOut zeroout = new ZeroOut(getInput().get(0).constructLops(), top, bottom, left, right, getInput().get(0).getDim1(), getInput().get(0).getDim2(), getDataType(), getValueType(), et);
            zeroout.getOutputParameters().setDimensions(getInput().get(0).getDim1(), getInput().get(0).getDim2(), getRowsInBlock(), getColsInBlock(), getNnz());
            setLineNumbers(zeroout);
            Group group2 = new Group(zeroout, Group.OperationTypes.Sort, DataType.MATRIX, getValueType());
            group2.getOutputParameters().setDimensions(getInput().get(0).getDim1(), getInput().get(0).getDim2(), getRowsInBlock(), getColsInBlock(), getNnz());
            setLineNumbers(group2);
            Binary binary = new Binary(group1, group2, HopsOpOp2LopsB.get(Hop.OpOp2.PLUS), getDataType(), getValueType(), et);
            binary.getOutputParameters().setDimensions(getInput().get(0).getDim1(), getInput().get(0).getDim2(), getRowsInBlock(), getColsInBlock(), getNnz());
            setLineNumbers(binary);
            setLops(binary);
        } else if (et == ExecType.SPARK) {
            Hop left = getInput().get(0);
            Hop right = getInput().get(1);
            LeftIndexingMethod method = getOptMethodLeftIndexingMethod(left.getDim1(), left.getDim2(), left.getRowsInBlock(), left.getColsInBlock(), left.getNnz(), right.getDim1(), right.getDim2(), right.getNnz(), right.getDataType());
            //insert cast to matrix if necessary (for reuse broadcast runtime)
            Lop rightInput = right.constructLops();
            if (isRightHandSideScalar()) {
                rightInput = new UnaryCP(rightInput, (left.getDataType() == DataType.MATRIX ? OperationTypes.CAST_AS_MATRIX : OperationTypes.CAST_AS_FRAME), left.getDataType(), right.getValueType());
                long bsize = ConfigurationManager.getBlocksize();
                rightInput.getOutputParameters().setDimensions(1, 1, bsize, bsize, -1);
            }
            LeftIndex leftIndexLop = new LeftIndex(left.constructLops(), rightInput, getInput().get(2).constructLops(), getInput().get(3).constructLops(), getInput().get(4).constructLops(), getInput().get(5).constructLops(), getDataType(), getValueType(), et, getSpLixCacheType(method));
            setOutputDimensions(leftIndexLop);
            setLineNumbers(leftIndexLop);
            setLops(leftIndexLop);
        } else {
            LeftIndex left = new LeftIndex(getInput().get(0).constructLops(), getInput().get(1).constructLops(), getInput().get(2).constructLops(), getInput().get(3).constructLops(), getInput().get(4).constructLops(), getInput().get(5).constructLops(), getDataType(), getValueType(), et);
            setOutputDimensions(left);
            setLineNumbers(left);
            setLops(left);
        }
    } catch (Exception e) {
        throw new HopsException(this.printErrorLocation() + "In LeftIndexingOp Hop, error in constructing Lops ", e);
    }
    //add reblock/checkpoint lops if necessary
    constructAndSetLopsDataFlowProperties();
    return getLops();
}
Also used : Group(org.apache.sysml.lops.Group) RangeBasedReIndex(org.apache.sysml.lops.RangeBasedReIndex) ZeroOut(org.apache.sysml.lops.ZeroOut) LeftIndex(org.apache.sysml.lops.LeftIndex) ExecType(org.apache.sysml.lops.LopProperties.ExecType) Binary(org.apache.sysml.lops.Binary) Lop(org.apache.sysml.lops.Lop) LopsException(org.apache.sysml.lops.LopsException) UnaryCP(org.apache.sysml.lops.UnaryCP)

Aggregations

Binary (org.apache.sysml.lops.Binary)4 Lop (org.apache.sysml.lops.Lop)4 MultiThreadedHop (org.apache.sysml.hops.Hop.MultiThreadedHop)2 Group (org.apache.sysml.lops.Group)2 ExecType (org.apache.sysml.lops.LopProperties.ExecType)2 BinaryM (org.apache.sysml.lops.BinaryM)1 BinaryScalar (org.apache.sysml.lops.BinaryScalar)1 BinaryUAggChain (org.apache.sysml.lops.BinaryUAggChain)1 CombineBinary (org.apache.sysml.lops.CombineBinary)1 OperationTypes (org.apache.sysml.lops.CombineBinary.OperationTypes)1 CombineUnary (org.apache.sysml.lops.CombineUnary)1 ConvolutionTransform (org.apache.sysml.lops.ConvolutionTransform)1 DataPartition (org.apache.sysml.lops.DataPartition)1 LeftIndex (org.apache.sysml.lops.LeftIndex)1 LopsException (org.apache.sysml.lops.LopsException)1 RangeBasedReIndex (org.apache.sysml.lops.RangeBasedReIndex)1 RepMat (org.apache.sysml.lops.RepMat)1 Transform (org.apache.sysml.lops.Transform)1 Unary (org.apache.sysml.lops.Unary)1 UnaryCP (org.apache.sysml.lops.UnaryCP)1