Search in sources :

Example 6 with UnaryOperator

use of org.apache.sysml.runtime.matrix.operators.UnaryOperator 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 > operator
 * Values from vector B is used to compute RowIndexMin for <, <= and >= 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 7 with UnaryOperator

use of org.apache.sysml.runtime.matrix.operators.UnaryOperator in project incubator-systemml by apache.

the class CPlanVectorPrimitivesTest method testVectorUnaryPrimitive.

private static void testVectorUnaryPrimitive(UnaryType utype, InputType type1) {
    try {
        // generate input data
        double sparsity = (type1 == InputType.VECTOR_DENSE) ? sparsity1 : sparsity2;
        MatrixBlock in = MatrixBlock.randOperations(m, n, sparsity, -1, 1, "uniform", 7);
        // get vector primitive via reflection
        String meName = "vect" + StringUtils.camelize(utype.name().split("_")[1]) + "Write";
        Method me = (type1 == InputType.VECTOR_DENSE) ? LibSpoofPrimitives.class.getMethod(meName, new Class[] { double[].class, int.class, int.class }) : LibSpoofPrimitives.class.getMethod(meName, new Class[] { double[].class, int[].class, int.class, int.class, int.class });
        for (int i = 0; i < m; i++) {
            // execute vector primitive via reflection
            double[] ret1 = (double[]) ((type1 == InputType.VECTOR_DENSE) ? me.invoke(null, in.getDenseBlockValues(), i * n, n) : me.invoke(null, in.getSparseBlock().values(i), in.getSparseBlock().indexes(i), in.getSparseBlock().pos(i), in.getSparseBlock().size(i), n));
            // execute comparison operation
            String opcode = utype.name().split("_")[1].toLowerCase();
            UnaryOperator uop = new UnaryOperator(Builtin.getBuiltinFnObject(opcode));
            double[] ret2 = DataConverter.convertToDoubleVector(((MatrixBlock) in.slice(i, i, 0, n - 1, new MatrixBlock()).unaryOperations(uop, new MatrixBlock())), false);
            // compare results
            TestUtils.compareMatrices(ret1, ret2, eps);
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) LibSpoofPrimitives(org.apache.sysml.runtime.codegen.LibSpoofPrimitives) Method(java.lang.reflect.Method) UnaryOperator(org.apache.sysml.runtime.matrix.operators.UnaryOperator)

Example 8 with UnaryOperator

use of org.apache.sysml.runtime.matrix.operators.UnaryOperator in project incubator-systemml by apache.

the class UnaryCPInstruction method parseInstruction.

public static UnaryCPInstruction parseInstruction(String str) {
    CPOperand in = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN);
    CPOperand out = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN);
    String[] parts = InstructionUtils.getInstructionPartsWithValueType(str);
    String opcode = null;
    ValueFunction func = null;
    // print or stop or cumulative aggregates
    if (parts.length == 4) {
        opcode = parts[0];
        in.split(parts[1]);
        out.split(parts[2]);
        func = Builtin.getBuiltinFnObject(opcode);
        if (Arrays.asList(new String[] { "ucumk+", "ucum*", "ucummin", "ucummax" }).contains(opcode))
            return new UnaryMatrixCPInstruction(new UnaryOperator(func, Integer.parseInt(parts[3])), in, out, opcode, str);
        else
            return new UnaryScalarCPInstruction(null, in, out, opcode, str);
    } else {
        // 2+1, general case
        opcode = parseUnaryInstruction(str, in, out);
        if (in.getDataType() == DataType.SCALAR)
            return new UnaryScalarCPInstruction(InstructionUtils.parseUnaryOperator(opcode), in, out, opcode, str);
        else if (in.getDataType() == DataType.MATRIX)
            return new UnaryMatrixCPInstruction(LibCommonsMath.isSupportedUnaryOperation(opcode) ? null : InstructionUtils.parseUnaryOperator(opcode), in, out, opcode, str);
    }
    return null;
}
Also used : ValueFunction(org.apache.sysml.runtime.functionobjects.ValueFunction) UnaryOperator(org.apache.sysml.runtime.matrix.operators.UnaryOperator)

Example 9 with UnaryOperator

use of org.apache.sysml.runtime.matrix.operators.UnaryOperator in project incubator-systemml by apache.

the class UnaryMatrixSPInstruction method processInstruction.

@Override
public void processInstruction(ExecutionContext ec) {
    SparkExecutionContext sec = (SparkExecutionContext) ec;
    // get input
    JavaPairRDD<MatrixIndexes, MatrixBlock> in = sec.getBinaryBlockRDDHandleForVariable(input1.getName());
    // execute unary builtin operation
    UnaryOperator uop = (UnaryOperator) _optr;
    JavaPairRDD<MatrixIndexes, MatrixBlock> out = in.mapValues(new RDDMatrixBuiltinUnaryOp(uop));
    // set output RDD
    updateUnaryOutputMatrixCharacteristics(sec);
    sec.setRDDHandleForVariable(output.getName(), out);
    sec.addLineageRDD(output.getName(), input1.getName());
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) MatrixIndexes(org.apache.sysml.runtime.matrix.data.MatrixIndexes) SparkExecutionContext(org.apache.sysml.runtime.controlprogram.context.SparkExecutionContext) UnaryOperator(org.apache.sysml.runtime.matrix.operators.UnaryOperator)

Example 10 with UnaryOperator

use of org.apache.sysml.runtime.matrix.operators.UnaryOperator in project incubator-systemml by apache.

the class UnaryInstruction method parseInstruction.

public static UnaryInstruction parseInstruction(String str) throws DMLRuntimeException {
    String opcode = InstructionUtils.getOpCode(str);
    InstructionUtils.checkNumFields(str, 2);
    String[] parts = InstructionUtils.getInstructionParts(str);
    byte in, out;
    in = Byte.parseByte(parts[1]);
    out = Byte.parseByte(parts[2]);
    // Currently, UnaryInstructions are used primarily for executing Builtins like SIN(A), LOG(A,2)
    if (InstructionUtils.isBuiltinFunction(opcode)) {
        UnaryOperator unary = new UnaryOperator(Builtin.getBuiltinFnObject(opcode));
        return new UnaryInstruction(unary, in, out, str);
    } else
        return new UnaryInstruction(null, in, out, str);
}
Also used : UnaryOperator(org.apache.sysml.runtime.matrix.operators.UnaryOperator)

Aggregations

UnaryOperator (org.apache.sysml.runtime.matrix.operators.UnaryOperator)12 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)5 ValueFunction (org.apache.sysml.runtime.functionobjects.ValueFunction)3 SparkExecutionContext (org.apache.sysml.runtime.controlprogram.context.SparkExecutionContext)2 Equals (org.apache.sysml.runtime.functionobjects.Equals)2 GreaterThan (org.apache.sysml.runtime.functionobjects.GreaterThan)2 GreaterThanEquals (org.apache.sysml.runtime.functionobjects.GreaterThanEquals)2 LessThan (org.apache.sysml.runtime.functionobjects.LessThan)2 LessThanEquals (org.apache.sysml.runtime.functionobjects.LessThanEquals)2 NotEquals (org.apache.sysml.runtime.functionobjects.NotEquals)2 MatrixIndexes (org.apache.sysml.runtime.matrix.data.MatrixIndexes)2 AggregateUnaryOperator (org.apache.sysml.runtime.matrix.operators.AggregateUnaryOperator)2 Method (java.lang.reflect.Method)1 DMLScriptException (org.apache.sysml.runtime.DMLScriptException)1 LibSpoofPrimitives (org.apache.sysml.runtime.codegen.LibSpoofPrimitives)1 CPOperand (org.apache.sysml.runtime.instructions.cp.CPOperand)1 SimpleOperator (org.apache.sysml.runtime.matrix.operators.SimpleOperator)1