Search in sources :

Example 26 with BinaryOperator

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

the class BinaryScalarScalarCPInstruction method processInstruction.

@Override
public void processInstruction(ExecutionContext ec) {
    ScalarObject so1 = ec.getScalarInput(input1.getName(), input1.getValueType(), input1.isLiteral());
    ScalarObject so2 = ec.getScalarInput(input2.getName(), input2.getValueType(), input2.isLiteral());
    String opcode = getOpcode();
    BinaryOperator dop = (BinaryOperator) _optr;
    ScalarObject sores = null;
    // compare output value, incl implicit type promotion if necessary
    if (dop.fn instanceof ValueComparisonFunction) {
        ValueComparisonFunction vcomp = (ValueComparisonFunction) dop.fn;
        if (so1 instanceof StringObject || so2 instanceof StringObject)
            sores = new BooleanObject(vcomp.compare(so1.getStringValue(), so2.getStringValue()));
        else if (so1 instanceof DoubleObject || so2 instanceof DoubleObject)
            sores = new BooleanObject(vcomp.compare(so1.getDoubleValue(), so2.getDoubleValue()));
        else if (so1 instanceof IntObject || so2 instanceof IntObject)
            sores = new BooleanObject(vcomp.compare(so1.getLongValue(), so2.getLongValue()));
        else
            // all boolean
            sores = new BooleanObject(vcomp.compare(so1.getBooleanValue(), so2.getBooleanValue()));
    } else // compute output value, incl implicit type promotion if necessary
    {
        if (so1 instanceof StringObject || so2 instanceof StringObject) {
            if (// not string concatenation
            !opcode.equals("+"))
                throw new DMLRuntimeException("Arithmetic '" + opcode + "' not supported over string inputs.");
            sores = new StringObject(dop.fn.execute(so1.getLanguageSpecificStringValue(), so2.getLanguageSpecificStringValue()));
        } else if (so1 instanceof DoubleObject || so2 instanceof DoubleObject || output.getValueType() == ValueType.DOUBLE) {
            sores = new DoubleObject(dop.fn.execute(so1.getDoubleValue(), so2.getDoubleValue()));
        } else if (so1 instanceof IntObject || so2 instanceof IntObject) {
            double tmp = dop.fn.execute(so1.getLongValue(), so2.getLongValue());
            if (// cast to long if no overflow, otherwise controlled exception
            tmp > Long.MAX_VALUE)
                throw new DMLRuntimeException("Integer operation created numerical result overflow (" + tmp + " > " + Long.MAX_VALUE + ").");
            sores = new IntObject((long) tmp);
        } else {
            // NOTE: boolean-boolean arithmetic treated as double for consistency with R
            if (opcode.equals("&&") || opcode.equals("||") || opcode.equals("xor"))
                sores = new BooleanObject(dop.fn.execute(so1.getBooleanValue(), so2.getBooleanValue()));
            else
                sores = new DoubleObject(dop.fn.execute(so1.getDoubleValue(), so2.getDoubleValue()));
        }
    }
    ec.setScalarOutput(output.getName(), sores);
}
Also used : ValueComparisonFunction(org.apache.sysml.runtime.functionobjects.ValueComparisonFunction) BinaryOperator(org.apache.sysml.runtime.matrix.operators.BinaryOperator) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 27 with BinaryOperator

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

the class CPlanVectorPrimitivesTest method testVectorBinaryPrimitive.

private static void testVectorBinaryPrimitive(BinType bintype, InputType type1, InputType type2) {
    try {
        // generate input data (scalar later derived if needed)
        double sparsityA = (type1 == InputType.VECTOR_DENSE) ? sparsity1 : sparsity2;
        MatrixBlock inA = MatrixBlock.randOperations(m, n, sparsityA, -5, 5, "uniform", 3);
        double sparsityB = (type2 == InputType.VECTOR_DENSE) ? sparsity1 : sparsity2;
        MatrixBlock inB = MatrixBlock.randOperations(m, n, sparsityB, -5, 5, "uniform", 7);
        // get vector primitive via reflection
        String meName = "vect" + StringUtils.camelize(bintype.name().split("_")[1]) + "Write";
        Method me = null;
        if (type1 == InputType.SCALAR && type2 == InputType.VECTOR_DENSE)
            me = LibSpoofPrimitives.class.getMethod(meName, new Class[] { double.class, double[].class, int.class, int.class });
        else if (type1 == InputType.VECTOR_DENSE && type2 == InputType.SCALAR)
            me = LibSpoofPrimitives.class.getMethod(meName, new Class[] { double[].class, double.class, int.class, int.class });
        else if (type1 == InputType.VECTOR_DENSE && type2 == InputType.VECTOR_DENSE)
            me = LibSpoofPrimitives.class.getMethod(meName, new Class[] { double[].class, double[].class, int.class, int.class, int.class });
        else if (type1 == InputType.VECTOR_SPARSE && type2 == InputType.SCALAR)
            me = LibSpoofPrimitives.class.getMethod(meName, new Class[] { double[].class, double.class, int[].class, int.class, int.class, int.class });
        else if (type1 == InputType.SCALAR && type2 == InputType.VECTOR_SPARSE)
            me = LibSpoofPrimitives.class.getMethod(meName, new Class[] { double.class, double[].class, int[].class, int.class, int.class, int.class });
        else if (type1 == InputType.VECTOR_SPARSE && type2 == InputType.VECTOR_DENSE)
            me = LibSpoofPrimitives.class.getMethod(meName, new Class[] { double[].class, double[].class, int[].class, int.class, int.class, int.class, int.class });
        for (int i = 0; i < m; i++) {
            // execute vector primitive via reflection
            double[] ret1 = null;
            if (type1 == InputType.SCALAR && type2 == InputType.VECTOR_DENSE)
                ret1 = (double[]) me.invoke(null, inA.max(), inB.getDenseBlockValues(), i * n, n);
            else if (type1 == InputType.VECTOR_DENSE && type2 == InputType.SCALAR)
                ret1 = (double[]) me.invoke(null, inA.getDenseBlockValues(), inB.max(), i * n, n);
            else if (type1 == InputType.VECTOR_DENSE && type2 == InputType.VECTOR_DENSE)
                ret1 = (double[]) me.invoke(null, inA.getDenseBlockValues(), inB.getDenseBlockValues(), i * n, i * n, n);
            else if (type1 == InputType.VECTOR_SPARSE && type2 == InputType.SCALAR)
                ret1 = (double[]) me.invoke(null, inA.getSparseBlock().values(i), inB.max(), inA.getSparseBlock().indexes(i), inA.getSparseBlock().pos(i), inA.getSparseBlock().size(i), n);
            else if (type1 == InputType.SCALAR && type2 == InputType.VECTOR_SPARSE)
                ret1 = (double[]) me.invoke(null, inA.max(), inB.getSparseBlock().values(i), inB.getSparseBlock().indexes(i), inB.getSparseBlock().pos(i), inB.getSparseBlock().size(i), n);
            else if (type1 == InputType.VECTOR_SPARSE && type2 == InputType.VECTOR_DENSE)
                ret1 = (double[]) me.invoke(null, inA.getSparseBlock().values(i), inB.getDenseBlockValues(), inA.getSparseBlock().indexes(i), inA.getSparseBlock().pos(i), i * n, inA.getSparseBlock().size(i), n);
            // execute comparison operation
            String opcode = Hop.getBinaryOpCode(OpOp2.valueOf(bintype.name().split("_")[1]));
            MatrixBlock in1 = inA.slice(i, i, 0, n - 1, new MatrixBlock());
            MatrixBlock in2 = inB.slice(i, i, 0, n - 1, new MatrixBlock());
            double[] ret2 = null;
            if (type1 == InputType.SCALAR) {
                ScalarOperator bop = InstructionUtils.parseScalarBinaryOperator(opcode, true);
                bop = bop.setConstant(inA.max());
                ret2 = DataConverter.convertToDoubleVector((MatrixBlock) in2.scalarOperations(bop, new MatrixBlock()), false);
            } else if (type2 == InputType.SCALAR) {
                ScalarOperator bop = InstructionUtils.parseScalarBinaryOperator(opcode, false);
                bop = bop.setConstant(inB.max());
                ret2 = DataConverter.convertToDoubleVector((MatrixBlock) in1.scalarOperations(bop, new MatrixBlock()), false);
            } else {
                // vector-vector
                BinaryOperator bop = InstructionUtils.parseBinaryOperator(opcode);
                ret2 = DataConverter.convertToDoubleVector((MatrixBlock) in1.binaryOperations(bop, in2, new MatrixBlock()), false);
            }
            // compare results
            TestUtils.compareMatrices(ret1, ret2, eps);
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : ScalarOperator(org.apache.sysml.runtime.matrix.operators.ScalarOperator) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) LibSpoofPrimitives(org.apache.sysml.runtime.codegen.LibSpoofPrimitives) Method(java.lang.reflect.Method) BinaryOperator(org.apache.sysml.runtime.matrix.operators.BinaryOperator)

Example 28 with BinaryOperator

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

the class PlusMultInstruction method parseInstruction.

public static PlusMultInstruction parseInstruction(String str) throws DMLRuntimeException {
    InstructionUtils.checkNumFields(str, 4);
    String[] parts = InstructionUtils.getInstructionParts(str);
    String opcode = parts[0];
    byte in1 = Byte.parseByte(parts[1]);
    double scalar = Double.parseDouble(parts[2]);
    byte in2 = Byte.parseByte(parts[3]);
    byte out = Byte.parseByte(parts[4]);
    BinaryOperator bop = InstructionUtils.parseBinaryOperator(opcode);
    ((ValueFunctionWithConstant) bop.fn).setConstant(scalar);
    return new PlusMultInstruction(bop, in1, in2, out, str);
}
Also used : ValueFunctionWithConstant(org.apache.sysml.runtime.functionobjects.ValueFunctionWithConstant) BinaryOperator(org.apache.sysml.runtime.matrix.operators.BinaryOperator)

Example 29 with BinaryOperator

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

the class BooleanBinaryCPInstruction method parseInstruction.

public static BooleanBinaryCPInstruction parseInstruction(String str) throws DMLRuntimeException {
    CPOperand in1 = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN);
    CPOperand in2 = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN);
    CPOperand out = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN);
    String opcode = parseBinaryInstruction(str, in1, in2, out);
    // Boolean operations must be performed on BOOLEAN
    ValueType vt1 = in1.getValueType();
    ValueType vt2 = in2.getValueType();
    ValueType vt3 = out.getValueType();
    if (vt1 != ValueType.BOOLEAN || vt3 != ValueType.BOOLEAN || (vt2 != null && vt2 != ValueType.BOOLEAN))
        throw new DMLRuntimeException("Unexpected ValueType in ArithmeticInstruction.");
    // Determine appropriate Function Object based on opcode	
    BinaryOperator bop = InstructionUtils.parseBinaryOperator(opcode);
    return new BooleanBinaryCPInstruction(bop, in1, in2, out, opcode, str);
}
Also used : ValueType(org.apache.sysml.parser.Expression.ValueType) BinaryOperator(org.apache.sysml.runtime.matrix.operators.BinaryOperator) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 30 with BinaryOperator

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

the class PlusMultSPInstruction method parseInstruction.

public static PlusMultSPInstruction parseInstruction(String str) throws DMLRuntimeException {
    String[] parts = InstructionUtils.getInstructionPartsWithValueType(str);
    String opcode = parts[0];
    CPOperand operand1 = new CPOperand(parts[1]);
    //put the second matrix (parts[3]) in Operand2 to make using Binary matrix operations easier
    CPOperand operand2 = new CPOperand(parts[3]);
    CPOperand operand3 = new CPOperand(parts[2]);
    CPOperand outOperand = new CPOperand(parts[4]);
    BinaryOperator bOperator = new BinaryOperator(opcode.equals("+*") ? PlusMultiply.getPlusMultiplyFnObject() : MinusMultiply.getMinusMultiplyFnObject());
    return new PlusMultSPInstruction(bOperator, operand1, operand2, operand3, outOperand, opcode, str);
}
Also used : CPOperand(org.apache.sysml.runtime.instructions.cp.CPOperand) BinaryOperator(org.apache.sysml.runtime.matrix.operators.BinaryOperator)

Aggregations

BinaryOperator (org.apache.sysml.runtime.matrix.operators.BinaryOperator)34 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)10 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)9 CPOperand (org.apache.sysml.runtime.instructions.cp.CPOperand)5 AggregateUnaryOperator (org.apache.sysml.runtime.matrix.operators.AggregateUnaryOperator)5 AggregateOperator (org.apache.sysml.runtime.matrix.operators.AggregateOperator)4 CorrectionLocationType (org.apache.sysml.lops.PartialAggregate.CorrectionLocationType)3 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)3 ValueFunction (org.apache.sysml.runtime.functionobjects.ValueFunction)3 MatrixIndexes (org.apache.sysml.runtime.matrix.data.MatrixIndexes)3 RightScalarOperator (org.apache.sysml.runtime.matrix.operators.RightScalarOperator)3 VectorType (org.apache.sysml.lops.BinaryM.VectorType)2 SparkExecutionContext (org.apache.sysml.runtime.controlprogram.context.SparkExecutionContext)2 ValueComparisonFunction (org.apache.sysml.runtime.functionobjects.ValueComparisonFunction)2 ValueFunctionWithConstant (org.apache.sysml.runtime.functionobjects.ValueFunctionWithConstant)2 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)2 IndexedMatrixValue (org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue)2 AggregateBinaryOperator (org.apache.sysml.runtime.matrix.operators.AggregateBinaryOperator)2 ScalarOperator (org.apache.sysml.runtime.matrix.operators.ScalarOperator)2 Method (java.lang.reflect.Method)1