Search in sources :

Example 11 with ScalarOperator

use of org.apache.sysml.runtime.matrix.operators.ScalarOperator 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 12 with ScalarOperator

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

the class MatrixScalarBuiltinCPInstruction method processInstruction.

@Override
public void processInstruction(ExecutionContext ec) throws DMLRuntimeException {
    CPOperand mat = (input1.getDataType() == DataType.MATRIX) ? input1 : input2;
    CPOperand scalar = (input1.getDataType() == DataType.MATRIX) ? input2 : input1;
    MatrixBlock inBlock = ec.getMatrixInput(mat.getName());
    ScalarObject constant = (ScalarObject) ec.getScalarInput(scalar.getName(), scalar.getValueType(), scalar.isLiteral());
    ScalarOperator sc_op = (ScalarOperator) _optr;
    sc_op.setConstant(constant.getDoubleValue());
    MatrixBlock retBlock = (MatrixBlock) inBlock.scalarOperations(sc_op, new MatrixBlock());
    ec.releaseMatrixInput(mat.getName());
    // Ensure right dense/sparse output representation (guarded by released input memory)
    if (checkGuardedRepresentationChange(inBlock, retBlock)) {
        retBlock.examSparsity();
    }
    ec.setMatrixOutput(output.getName(), retBlock);
}
Also used : ScalarOperator(org.apache.sysml.runtime.matrix.operators.ScalarOperator) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock)

Example 13 with ScalarOperator

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

the class ScalarMatrixArithmeticCPInstruction method processInstruction.

@Override
public void processInstruction(ExecutionContext ec) throws DMLRuntimeException {
    CPOperand mat = (input1.getDataType() == DataType.MATRIX) ? input1 : input2;
    CPOperand scalar = (input1.getDataType() == DataType.MATRIX) ? input2 : input1;
    MatrixBlock inBlock = ec.getMatrixInput(mat.getName());
    ScalarObject constant = (ScalarObject) ec.getScalarInput(scalar.getName(), scalar.getValueType(), scalar.isLiteral());
    ScalarOperator sc_op = (ScalarOperator) _optr;
    sc_op.setConstant(constant.getDoubleValue());
    MatrixBlock retBlock = (MatrixBlock) inBlock.scalarOperations(sc_op, new MatrixBlock());
    ec.releaseMatrixInput(mat.getName());
    // Ensure right dense/sparse output representation (guarded by released input memory)
    if (checkGuardedRepresentationChange(inBlock, retBlock)) {
        retBlock.examSparsity();
    }
    ec.setMatrixOutput(output.getName(), retBlock);
}
Also used : ScalarOperator(org.apache.sysml.runtime.matrix.operators.ScalarOperator) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock)

Aggregations

ScalarOperator (org.apache.sysml.runtime.matrix.operators.ScalarOperator)13 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)8 RightScalarOperator (org.apache.sysml.runtime.matrix.operators.RightScalarOperator)4 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)3 CPOperand (org.apache.sysml.runtime.instructions.cp.CPOperand)3 ScalarObject (org.apache.sysml.runtime.instructions.cp.ScalarObject)3 CompressedMatrixBlock (org.apache.sysml.runtime.compress.CompressedMatrixBlock)2 BinaryOperator (org.apache.sysml.runtime.matrix.operators.BinaryOperator)2 LeftScalarOperator (org.apache.sysml.runtime.matrix.operators.LeftScalarOperator)2 Method (java.lang.reflect.Method)1 Pointer (jcuda.Pointer)1 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)1 LibSpoofPrimitives (org.apache.sysml.runtime.codegen.LibSpoofPrimitives)1 SparkExecutionContext (org.apache.sysml.runtime.controlprogram.context.SparkExecutionContext)1 Builtin (org.apache.sysml.runtime.functionobjects.Builtin)1 CM (org.apache.sysml.runtime.functionobjects.CM)1 IndexFunction (org.apache.sysml.runtime.functionobjects.IndexFunction)1 KahanPlus (org.apache.sysml.runtime.functionobjects.KahanPlus)1 KahanPlusSq (org.apache.sysml.runtime.functionobjects.KahanPlusSq)1 Mean (org.apache.sysml.runtime.functionobjects.Mean)1