Search in sources :

Example 1 with NotEquals

use of org.apache.sysml.runtime.functionobjects.NotEquals in project incubator-systemml by apache.

the class LibMatrixBincell method safeBinaryScalar.

private static void safeBinaryScalar(MatrixBlock m1, MatrixBlock ret, ScalarOperator op) {
    // early abort possible since sparsesafe
    if (m1.isEmptyBlock(false)) {
        return;
    }
    // sanity check input/output sparsity
    if (m1.sparse != ret.sparse)
        throw new DMLRuntimeException("Unsupported safe binary scalar operations over different input/output representation: " + m1.sparse + " " + ret.sparse);
    boolean copyOnes = (op.fn instanceof NotEquals && op.getConstant() == 0);
    boolean allocExact = (op.fn instanceof Multiply || op.fn instanceof Multiply2 || op.fn instanceof Power2 || Builtin.isBuiltinCode(op.fn, BuiltinCode.MAX) || Builtin.isBuiltinCode(op.fn, BuiltinCode.MIN));
    if (// SPARSE <- SPARSE
    m1.sparse) {
        // allocate sparse row structure
        ret.allocateSparseRowsBlock();
        SparseBlock a = m1.sparseBlock;
        SparseBlock c = ret.sparseBlock;
        int rlen = Math.min(m1.rlen, a.numRows());
        long nnz = 0;
        for (int r = 0; r < rlen; r++) {
            if (a.isEmpty(r))
                continue;
            int apos = a.pos(r);
            int alen = a.size(r);
            int[] aix = a.indexes(r);
            double[] avals = a.values(r);
            if (copyOnes) {
                // SPECIAL CASE: e.g., (X != 0)
                // create sparse row without repeated resizing
                SparseRowVector crow = new SparseRowVector(alen);
                crow.setSize(alen);
                // memcopy/memset of indexes/values (sparseblock guarantees absence of 0s)
                System.arraycopy(aix, apos, crow.indexes(), 0, alen);
                Arrays.fill(crow.values(), 0, alen, 1);
                c.set(r, crow, false);
                nnz += alen;
            } else {
                // create sparse row without repeated resizing for specific ops
                if (allocExact)
                    c.allocate(r, alen);
                for (int j = apos; j < apos + alen; j++) {
                    double val = op.executeScalar(avals[j]);
                    c.append(r, aix[j], val);
                    nnz += (val != 0) ? 1 : 0;
                }
            }
        }
        ret.nonZeros = nnz;
    } else {
        // DENSE <- DENSE
        denseBinaryScalar(m1, ret, op);
    }
}
Also used : Multiply2(org.apache.sysml.runtime.functionobjects.Multiply2) NotEquals(org.apache.sysml.runtime.functionobjects.NotEquals) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) Multiply(org.apache.sysml.runtime.functionobjects.Multiply) MinusMultiply(org.apache.sysml.runtime.functionobjects.MinusMultiply) PlusMultiply(org.apache.sysml.runtime.functionobjects.PlusMultiply) Power2(org.apache.sysml.runtime.functionobjects.Power2)

Example 2 with NotEquals

use of org.apache.sysml.runtime.functionobjects.NotEquals in project incubator-systemml by apache.

the class LibMatrixCUDA method matrixMatrixRelational.

/**
 * Performs elementwise operation relational specified by op of two input matrices in1 and in2
 *
 * @param ec execution context
 * @param gCtx a valid {@link GPUContext}
 * @param instName the invoking instruction's name for record {@link Statistics}.
 * @param in1 input matrix 1
 * @param in2 input matrix 2
 * @param outputName output matrix name
 * @param op binary operator
 */
public static void matrixMatrixRelational(ExecutionContext ec, GPUContext gCtx, String instName, MatrixObject in1, MatrixObject in2, String outputName, BinaryOperator op) {
    if (ec.getGPUContext(0) != gCtx)
        throw new DMLRuntimeException("GPU : Invalid internal state, the GPUContext set with the ExecutionContext is not the same used to run this LibMatrixCUDA function");
    boolean in1SparseAndEmpty = isSparseAndEmpty(gCtx, in1);
    boolean in2SparseAndEmpty = isSparseAndEmpty(gCtx, in2);
    if (in1SparseAndEmpty && in2SparseAndEmpty) {
        if (op.fn instanceof LessThan || op.fn instanceof GreaterThan || op.fn instanceof NotEquals) {
            setOutputToConstant(ec, gCtx, instName, 0.0, outputName, in1.getNumRows(), in1.getNumColumns());
        } else if (op.fn instanceof LessThanEquals || op.fn instanceof GreaterThanEquals || op.fn instanceof Equals) {
            setOutputToConstant(ec, gCtx, instName, 1.0, outputName, in1.getNumRows(), in1.getNumColumns());
        }
    } else if (in1SparseAndEmpty) {
        matrixScalarRelational(ec, gCtx, instName, in2, outputName, new LeftScalarOperator(op.fn, 0.0));
    } else if (in2SparseAndEmpty) {
        matrixScalarRelational(ec, gCtx, instName, in1, outputName, new RightScalarOperator(op.fn, 0.0));
    } else {
        matrixMatrixOp(ec, gCtx, instName, in1, in2, outputName, false, false, op);
    }
}
Also used : GreaterThanEquals(org.apache.sysml.runtime.functionobjects.GreaterThanEquals) LessThan(org.apache.sysml.runtime.functionobjects.LessThan) GreaterThanEquals(org.apache.sysml.runtime.functionobjects.GreaterThanEquals) NotEquals(org.apache.sysml.runtime.functionobjects.NotEquals) LessThanEquals(org.apache.sysml.runtime.functionobjects.LessThanEquals) Equals(org.apache.sysml.runtime.functionobjects.Equals) GreaterThan(org.apache.sysml.runtime.functionobjects.GreaterThan) LeftScalarOperator(org.apache.sysml.runtime.matrix.operators.LeftScalarOperator) RightScalarOperator(org.apache.sysml.runtime.matrix.operators.RightScalarOperator) NotEquals(org.apache.sysml.runtime.functionobjects.NotEquals) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) LessThanEquals(org.apache.sysml.runtime.functionobjects.LessThanEquals)

Example 3 with NotEquals

use of org.apache.sysml.runtime.functionobjects.NotEquals in project incubator-systemml by apache.

the class LibMatrixOuterAgg method prepareRowIndicesMax.

/**
 * This function will return max indices, based on column vector data.
 * This indices will be computed based on operator.
 * These indices can be used to compute max index for a given input value in subsequent operation.
 *
 *  e.g. Right Vector has data (V1)    :                6   3   9   7   2   4   4   3
 *       Original indices for this data will be (I1):   1   2   3   4   5   6   7   8
 *
 *  Sorting this data based on value will be (V2):      2   3   3   4   4   6   7   9
 *      Then indices will be ordered as (I2):           5   2   8   6   7   1   4   3
 *
 * CumMax of I2 will be A:  (CumMin(I2))                5   5   8   8   8   8   8   8
 * CumMax of I2 in reverse order be B:                  8   8   8   7   7   4   4   3
 *
 * Values from vector A is used to compute RowIndexMax for &gt; &amp; &gt;= operators
 * Values from vector B is used to compute RowIndexMax for &lt; &amp; &lt;= operators
 * Values from I2 is used to compute RowIndexMax for == operator.
 * Original values are directly used to compute RowIndexMax for != operator
 *
 * Shifting values from vector A or B is required to compute final indices.
 * Once indices are shifted from vector A or B, their cell value corresponding to input data will be used.
 *
 * @param iCols ?
 * @param vmb ?
 * @param bOp binary operator
 * @return array of maximum row indices
 */
public static int[] prepareRowIndicesMax(int iCols, double[] vmb, BinaryOperator bOp) {
    int[] vixCumSum = null;
    int[] vix = new int[iCols];
    // sort index vector on extracted data (unstable)
    if (!(bOp.fn instanceof NotEquals)) {
        for (int i = 0; i < iCols; i++) vix[i] = i;
        SortUtils.sortByValueStable(0, iCols, vmb, vix);
    }
    if (bOp.fn instanceof LessThan || bOp.fn instanceof LessThanEquals || bOp.fn instanceof GreaterThan || bOp.fn instanceof GreaterThanEquals) {
        boolean bPrimeCumSum = false;
        if (bOp.fn instanceof LessThan || bOp.fn instanceof LessThanEquals)
            bPrimeCumSum = true;
        double[] dvix = new double[vix.length];
        if (bPrimeCumSum)
            for (int i = 0; i < vix.length; i++) dvix[vix.length - 1 - i] = vix[i];
        else
            for (int i = 0; i < vix.length; i++) dvix[i] = vix[i];
        MatrixBlock mbix = DataConverter.convertToMatrixBlock(dvix, true);
        UnaryOperator u_op = new UnaryOperator(Builtin.getBuiltinFnObject(Builtin.BuiltinCode.CUMMAX));
        MatrixBlock mbResult = (MatrixBlock) mbix.unaryOperations(u_op, new MatrixBlock());
        vixCumSum = DataConverter.convertToIntVector(mbResult);
        if (bPrimeCumSum)
            for (int i = 0; i < (vixCumSum.length + 1) / 2; i++) {
                int iTemp = vixCumSum[vixCumSum.length - 1 - i];
                vixCumSum[vixCumSum.length - 1 - i] = vixCumSum[i];
                vixCumSum[i] = iTemp;
            }
        adjustRowIndicesMax(vixCumSum, vmb, bOp);
    } else if (bOp.fn instanceof Equals || bOp.fn instanceof NotEquals) {
        adjustRowIndicesMax(vix, vmb, bOp);
        vixCumSum = vix;
    }
    return vixCumSum;
}
Also used : GreaterThanEquals(org.apache.sysml.runtime.functionobjects.GreaterThanEquals) LessThan(org.apache.sysml.runtime.functionobjects.LessThan) Equals(org.apache.sysml.runtime.functionobjects.Equals) GreaterThanEquals(org.apache.sysml.runtime.functionobjects.GreaterThanEquals) LessThanEquals(org.apache.sysml.runtime.functionobjects.LessThanEquals) NotEquals(org.apache.sysml.runtime.functionobjects.NotEquals) GreaterThan(org.apache.sysml.runtime.functionobjects.GreaterThan) AggregateUnaryOperator(org.apache.sysml.runtime.matrix.operators.AggregateUnaryOperator) UnaryOperator(org.apache.sysml.runtime.matrix.operators.UnaryOperator) NotEquals(org.apache.sysml.runtime.functionobjects.NotEquals) LessThanEquals(org.apache.sysml.runtime.functionobjects.LessThanEquals)

Example 4 with NotEquals

use of org.apache.sysml.runtime.functionobjects.NotEquals in project incubator-systemml by apache.

the class LibMatrixOuterAgg method prepareRowIndicesMin.

/**
 * This function will return min indices, based on column vector data.
 * This indices will be computed based on operator.
 * These indices can be used to compute min index for a given input value in subsequent operation.
 *
 *  e.g. Right Vector has data (V1)    :                6   3   9   7   2   4   4   3
 *       Original indices for this data will be (I1):   1   2   3   4   5   6   7   8
 *
 *  Sorting this data based on value will be (V2):      2   3   3   4   4   6   7   9
 *      Then indices will be ordered as (I2):           5   2   8   6   7   1   4   3
 *
 * CumMin of I2 will be A:  (CumMin(I2))                5   2   2   2   2   1   1   1
 * CumMin of I2 in reverse order be B:                  1   1   1   1   1   1   3   3
 *
 * Values from vector A is used to compute RowIndexMin for &gt; operator
 * Values from vector B is used to compute RowIndexMin for &lt;, &lt;= and &gt;= operators
 * Values from I2 is used to compute RowIndexMax for == operator.
 * Original values are directly used to compute RowIndexMax for != operator
 *
 * Shifting values from vector A or B is required to compute final indices.
 * Once indices are shifted from vector A or B, their cell value corresponding to input data will be used.
 *
 * @param iCols ?
 * @param vmb ?
 * @param bOp binary operator
 * @return array of minimum row indices
 */
public static int[] prepareRowIndicesMin(int iCols, double[] vmb, BinaryOperator bOp) {
    int[] vixCumSum = null;
    int[] vix = new int[iCols];
    // sort index vector on extracted data (unstable)
    if (!(bOp.fn instanceof NotEquals || bOp.fn instanceof Equals)) {
        for (int i = 0; i < iCols; i++) vix[i] = i;
        SortUtils.sortByValueStable(0, iCols, vmb, vix);
    }
    if (bOp.fn instanceof LessThan || bOp.fn instanceof LessThanEquals || bOp.fn instanceof GreaterThan || bOp.fn instanceof GreaterThanEquals) {
        boolean bPrimeCumSum = false;
        if (bOp.fn instanceof GreaterThan || bOp.fn instanceof GreaterThanEquals)
            bPrimeCumSum = true;
        double[] dvix = new double[vix.length];
        if (bPrimeCumSum)
            for (int i = 0; i < vix.length; i++) dvix[vix.length - 1 - i] = vix[i];
        else
            for (int i = 0; i < vix.length; i++) dvix[i] = vix[i];
        MatrixBlock mbix = DataConverter.convertToMatrixBlock(dvix, true);
        UnaryOperator u_op = new UnaryOperator(Builtin.getBuiltinFnObject(Builtin.BuiltinCode.CUMMIN));
        MatrixBlock mbResult = (MatrixBlock) mbix.unaryOperations(u_op, new MatrixBlock());
        vixCumSum = DataConverter.convertToIntVector(mbResult);
        if (bPrimeCumSum)
            for (int i = 0; i < (vixCumSum.length + 1) / 2; i++) {
                int iTemp = vixCumSum[vixCumSum.length - 1 - i];
                vixCumSum[vixCumSum.length - 1 - i] = vixCumSum[i];
                vixCumSum[i] = iTemp;
            }
        adjustRowIndicesMin(vixCumSum, vmb, bOp);
    } else if (bOp.fn instanceof Equals || bOp.fn instanceof NotEquals) {
        adjustRowIndicesMin(vix, vmb, bOp);
        vixCumSum = vix;
    }
    return vixCumSum;
}
Also used : GreaterThanEquals(org.apache.sysml.runtime.functionobjects.GreaterThanEquals) Equals(org.apache.sysml.runtime.functionobjects.Equals) GreaterThanEquals(org.apache.sysml.runtime.functionobjects.GreaterThanEquals) LessThanEquals(org.apache.sysml.runtime.functionobjects.LessThanEquals) NotEquals(org.apache.sysml.runtime.functionobjects.NotEquals) LessThan(org.apache.sysml.runtime.functionobjects.LessThan) GreaterThan(org.apache.sysml.runtime.functionobjects.GreaterThan) AggregateUnaryOperator(org.apache.sysml.runtime.matrix.operators.AggregateUnaryOperator) UnaryOperator(org.apache.sysml.runtime.matrix.operators.UnaryOperator) NotEquals(org.apache.sysml.runtime.functionobjects.NotEquals) LessThanEquals(org.apache.sysml.runtime.functionobjects.LessThanEquals)

Example 5 with NotEquals

use of org.apache.sysml.runtime.functionobjects.NotEquals in project systemml by apache.

the class LibMatrixCUDA method matrixMatrixRelational.

/**
 * Performs elementwise operation relational specified by op of two input matrices in1 and in2
 *
 * @param ec execution context
 * @param gCtx a valid {@link GPUContext}
 * @param instName the invoking instruction's name for record {@link Statistics}.
 * @param in1 input matrix 1
 * @param in2 input matrix 2
 * @param outputName output matrix name
 * @param op binary operator
 */
public static void matrixMatrixRelational(ExecutionContext ec, GPUContext gCtx, String instName, MatrixObject in1, MatrixObject in2, String outputName, BinaryOperator op) {
    if (ec.getGPUContext(0) != gCtx)
        throw new DMLRuntimeException("GPU : Invalid internal state, the GPUContext set with the ExecutionContext is not the same used to run this LibMatrixCUDA function");
    boolean in1SparseAndEmpty = isSparseAndEmpty(gCtx, in1);
    boolean in2SparseAndEmpty = isSparseAndEmpty(gCtx, in2);
    if (in1SparseAndEmpty && in2SparseAndEmpty) {
        if (op.fn instanceof LessThan || op.fn instanceof GreaterThan || op.fn instanceof NotEquals) {
            setOutputToConstant(ec, gCtx, instName, 0.0, outputName, in1.getNumRows(), in1.getNumColumns());
        } else if (op.fn instanceof LessThanEquals || op.fn instanceof GreaterThanEquals || op.fn instanceof Equals) {
            setOutputToConstant(ec, gCtx, instName, 1.0, outputName, in1.getNumRows(), in1.getNumColumns());
        }
    } else if (in1SparseAndEmpty) {
        matrixScalarRelational(ec, gCtx, instName, in2, outputName, new LeftScalarOperator(op.fn, 0.0));
    } else if (in2SparseAndEmpty) {
        matrixScalarRelational(ec, gCtx, instName, in1, outputName, new RightScalarOperator(op.fn, 0.0));
    } else {
        matrixMatrixOp(ec, gCtx, instName, in1, in2, outputName, false, false, op);
    }
}
Also used : GreaterThanEquals(org.apache.sysml.runtime.functionobjects.GreaterThanEquals) LessThan(org.apache.sysml.runtime.functionobjects.LessThan) GreaterThanEquals(org.apache.sysml.runtime.functionobjects.GreaterThanEquals) NotEquals(org.apache.sysml.runtime.functionobjects.NotEquals) LessThanEquals(org.apache.sysml.runtime.functionobjects.LessThanEquals) Equals(org.apache.sysml.runtime.functionobjects.Equals) GreaterThan(org.apache.sysml.runtime.functionobjects.GreaterThan) LeftScalarOperator(org.apache.sysml.runtime.matrix.operators.LeftScalarOperator) RightScalarOperator(org.apache.sysml.runtime.matrix.operators.RightScalarOperator) NotEquals(org.apache.sysml.runtime.functionobjects.NotEquals) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) LessThanEquals(org.apache.sysml.runtime.functionobjects.LessThanEquals)

Aggregations

NotEquals (org.apache.sysml.runtime.functionobjects.NotEquals)8 Equals (org.apache.sysml.runtime.functionobjects.Equals)6 GreaterThan (org.apache.sysml.runtime.functionobjects.GreaterThan)6 GreaterThanEquals (org.apache.sysml.runtime.functionobjects.GreaterThanEquals)6 LessThan (org.apache.sysml.runtime.functionobjects.LessThan)6 LessThanEquals (org.apache.sysml.runtime.functionobjects.LessThanEquals)6 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)4 AggregateUnaryOperator (org.apache.sysml.runtime.matrix.operators.AggregateUnaryOperator)4 UnaryOperator (org.apache.sysml.runtime.matrix.operators.UnaryOperator)4 MinusMultiply (org.apache.sysml.runtime.functionobjects.MinusMultiply)2 Multiply (org.apache.sysml.runtime.functionobjects.Multiply)2 Multiply2 (org.apache.sysml.runtime.functionobjects.Multiply2)2 PlusMultiply (org.apache.sysml.runtime.functionobjects.PlusMultiply)2 Power2 (org.apache.sysml.runtime.functionobjects.Power2)2 LeftScalarOperator (org.apache.sysml.runtime.matrix.operators.LeftScalarOperator)2 RightScalarOperator (org.apache.sysml.runtime.matrix.operators.RightScalarOperator)2