Search in sources :

Example 1 with RightScalarOperator

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

the class BuiltinBinaryCPInstruction method parseInstruction.

public static BuiltinBinaryCPInstruction 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);
    checkOutputDataType(in1, in2, out);
    // Determine appropriate Function Object based on opcode
    ValueFunction func = Builtin.getBuiltinFnObject(opcode);
    if (in1.getDataType() == DataType.SCALAR && in2.getDataType() == DataType.SCALAR)
        return new ScalarScalarBuiltinCPInstruction(new BinaryOperator(func), in1, in2, out, opcode, str);
    else if (in1.getDataType() == DataType.MATRIX && in2.getDataType() == DataType.MATRIX)
        return new MatrixMatrixBuiltinCPInstruction(new BinaryOperator(func), in1, in2, out, opcode, str);
    else
        return new MatrixScalarBuiltinCPInstruction(new RightScalarOperator(func, 0), in1, in2, out, opcode, str);
}
Also used : ValueFunction(org.apache.sysml.runtime.functionobjects.ValueFunction) BinaryOperator(org.apache.sysml.runtime.matrix.operators.BinaryOperator) RightScalarOperator(org.apache.sysml.runtime.matrix.operators.RightScalarOperator)

Example 2 with RightScalarOperator

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

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

the class LibMatrixCUDA method matrixMatrixOp.

/**
 * Utility to launch binary cellwise matrix-matrix operations CUDA kernel
 *
 * @param gCtx              a valid {@link GPUContext}
 * @param ec                execution context
 * @param instName          the invoking instruction's name for record {@link Statistics}.
 * @param in1               left input matrix
 * @param in2               right input matrix
 * @param outputName        output variable name
 * @param isLeftTransposed  true if left matrix is transposed
 * @param isRightTransposed true if right matrix is transposed
 * @param op                operator
 */
private static void matrixMatrixOp(ExecutionContext ec, GPUContext gCtx, String instName, MatrixObject in1, MatrixObject in2, String outputName, boolean isLeftTransposed, boolean isRightTransposed, 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 isEmpty1 = isSparseAndEmpty(gCtx, in1);
    boolean isEmpty2 = isSparseAndEmpty(gCtx, in2);
    int rlenA = toInt(in1.getNumRows());
    int rlenB = toInt(in2.getNumRows());
    int clenA = toInt(in1.getNumColumns());
    int clenB = toInt(in2.getNumColumns());
    int vecStatusA = getVectorStatus(rlenA, clenA).code();
    int vecStatusB = getVectorStatus(rlenB, clenB).code();
    if (isLeftTransposed || isRightTransposed) {
        throw new DMLRuntimeException("Unsupported operator: GPU transposed binary op " + isLeftTransposed + " " + isRightTransposed);
    }
    long outRLen = Math.max(rlenA, rlenB);
    long outCLen = Math.max(clenA, clenB);
    if (isEmpty1 && isEmpty2) {
        MatrixObject out = ec.allocateGPUMatrixObject(outputName, outRLen, outCLen);
        // When both inputs are empty, the output is empty too (except in the case of division)
        if (op.fn instanceof Divide || op.fn instanceof IntegerDivide || op.fn instanceof Modulus) {
            out.getGPUObject(gCtx).allocateAndFillDense(Double.NaN);
        } else if (op.fn instanceof Minus1Multiply) {
            out.getGPUObject(gCtx).allocateAndFillDense(1.0);
        } else {
            out.getGPUObject(gCtx).allocateSparseAndEmpty();
        }
    } else // Check for M1 * M2 when M1 is empty; if M2 is a vector then fallback to general case
    if (isEmpty1 && clenB != 1 && rlenB != 1) {
        // C = empty_in1 op in2 ==> becomes ==> C = 0.0 op in2
        matrixScalarArithmetic(ec, gCtx, instName, in2, outputName, isRightTransposed, new LeftScalarOperator(op.fn, 0.0));
    } else // Check for M1 * M2 when M2 is empty; if M1 is a vector then fallback to general case
    if (isEmpty2 && clenA != 1 && rlenA != 1) {
        // C = in1 op empty_in2 ==> becomes ==> C = in1 op 0.0
        matrixScalarArithmetic(ec, gCtx, instName, in1, outputName, isLeftTransposed, new RightScalarOperator(op.fn, 0.0));
    } else {
        // TODO: FIXME: Implement sparse binCellSparseOp kernel
        Pointer A = getDensePointer(gCtx, in1, instName);
        // TODO: FIXME: Implement sparse binCellSparseOp kernel
        Pointer B = getDensePointer(gCtx, in2, instName);
        // Allocated the dense output matrix
        MatrixObject out = null;
        try {
            out = getDenseMatrixOutputForGPUInstruction(ec, instName, outputName, outRLen, outCLen);
        } catch (DMLRuntimeException e) {
            throw new DMLRuntimeException("Incorrect dimensions: dimA:[" + rlenA + "," + clenA + "]" + " dimB:[" + rlenB + "," + clenB + "] out:[" + outRLen + "," + outCLen + "]", e);
        }
        Pointer C = getDensePointer(gCtx, out, instName);
        int maxRlen = Math.max(rlenA, rlenB);
        int maxClen = Math.max(clenA, clenB);
        matrixMatrixOp(gCtx, instName, A, B, maxRlen, maxClen, vecStatusA, vecStatusB, C, op);
    }
}
Also used : IntegerDivide(org.apache.sysml.runtime.functionobjects.IntegerDivide) Divide(org.apache.sysml.runtime.functionobjects.Divide) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) Modulus(org.apache.sysml.runtime.functionobjects.Modulus) LeftScalarOperator(org.apache.sysml.runtime.matrix.operators.LeftScalarOperator) CSRPointer(org.apache.sysml.runtime.instructions.gpu.context.CSRPointer) Pointer(jcuda.Pointer) RightScalarOperator(org.apache.sysml.runtime.matrix.operators.RightScalarOperator) IntegerDivide(org.apache.sysml.runtime.functionobjects.IntegerDivide) Minus1Multiply(org.apache.sysml.runtime.functionobjects.Minus1Multiply) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 4 with RightScalarOperator

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

the class LibMatrixCUDA method squareMatrix.

/**
 * Helper method to square a matrix in GPU memory
 * @param gCtx   a valid {@link GPUContext}
 * @param instName the invoking instruction's name for record {@link Statistics}.
 * @param in		input matrix on GPU
 * @param out		output matrix on GPU
 * @param rlen	row length
 * @param clen	column length
 */
private static void squareMatrix(GPUContext gCtx, String instName, Pointer in, Pointer out, int rlen, int clen) {
    ScalarOperator power2op = new RightScalarOperator(Power.getPowerFnObject(), 2);
    matrixScalarOp(gCtx, instName, in, 2, rlen, clen, out, power2op);
}
Also used : ScalarOperator(org.apache.sysml.runtime.matrix.operators.ScalarOperator) LeftScalarOperator(org.apache.sysml.runtime.matrix.operators.LeftScalarOperator) RightScalarOperator(org.apache.sysml.runtime.matrix.operators.RightScalarOperator) RightScalarOperator(org.apache.sysml.runtime.matrix.operators.RightScalarOperator)

Example 5 with RightScalarOperator

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

the class LibMatrixCUDA method matrixScalarArithmetic.

/**
 * Entry point to perform elementwise matrix-scalar arithmetic operation specified by op
 *
 * @param ec                execution context
 * @param gCtx              a valid {@link GPUContext}
 * @param instName          the invoking instruction's name for record {@link Statistics}.
 * @param in                input matrix
 * @param outputName        output matrix name
 * @param isInputTransposed true if input transposed
 * @param op                scalar operator
 */
public static void matrixScalarArithmetic(ExecutionContext ec, GPUContext gCtx, String instName, MatrixObject in, String outputName, boolean isInputTransposed, ScalarOperator 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");
    double constant = op.getConstant();
    if (LOG.isTraceEnabled()) {
        LOG.trace("GPU : matrixScalarArithmetic, scalar: " + constant + ", GPUContext=" + gCtx);
    }
    int outRLen = isInputTransposed ? (int) in.getNumColumns() : (int) in.getNumRows();
    int outCLen = isInputTransposed ? (int) in.getNumRows() : (int) in.getNumColumns();
    // if(!isCUDALibAvailable) {
    if (constant == 0) {
        if (op.fn instanceof Plus || (op.fn instanceof Minus && op instanceof RightScalarOperator) || op.fn instanceof Or) {
            deviceCopy(ec, gCtx, instName, in, outputName, isInputTransposed);
        } else if (op.fn instanceof Multiply || op.fn instanceof And) {
            setOutputToConstant(ec, gCtx, instName, 0.0, outputName, outRLen, outCLen);
        } else if (op.fn instanceof Power) {
            setOutputToConstant(ec, gCtx, instName, 1.0, outputName, outRLen, outCLen);
        } else // TODO:
        // x/0.0 is either +Infinity or -Infinity according to Java.
        // In the context of a matrix, different elements of the matrix
        // could have different values.
        // If the IEEE 754 standard defines otherwise, this logic needs
        // to be re-enabled and the Java computation logic for divide by zero
        // needs to be revisited
        // else if(op.fn instanceof Divide && isSparseAndEmpty(gCtx, in)) {
        // setOutputToConstant(ec, gCtx, instName, Double.NaN, outputName);
        // }
        // else if(op.fn instanceof Divide) {
        // //For division, IEEE 754 defines x/0.0 as INFINITY and 0.0/0.0 as NaN.
        // compareAndSet(ec, gCtx, instName, in, outputName, 0.0, 1e-6, Double.NaN, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
        // }
        {
            // TODO: Potential to optimize
            matrixScalarOp(ec, gCtx, instName, in, outputName, isInputTransposed, op);
        }
    } else if (constant == 1.0 && op.fn instanceof Or) {
        setOutputToConstant(ec, gCtx, instName, 1.0, outputName, outRLen, outCLen);
    } else if (constant == 1.0 && (op.fn instanceof And || op.fn instanceof Power)) {
        deviceCopy(ec, gCtx, instName, in, outputName, isInputTransposed);
    } else {
        matrixScalarOp(ec, gCtx, instName, in, outputName, isInputTransposed, op);
    }
// }
// else {
// double alpha = 0;
// if(op.fn instanceof Multiply) {
// alpha = op.getConstant();
// }
// else if(op.fn instanceof Divide && op instanceof RightScalarOperator) {
// alpha = Math.pow(op.getConstant(), -1.0);
// }
// else {
// throw new DMLRuntimeException("Unsupported op");
// }
// TODO: Performance optimization: Call cublasDaxpy if(in.getNumRows() == 1 || in.getNumColumns() == 1)
// C = alpha* op( A ) + beta* op ( B )
// dgeam(ec, gCtx, instName, in, in, outputName, isInputTransposed, isInputTransposed, alpha, 0.0);
// }
}
Also used : Or(org.apache.sysml.runtime.functionobjects.Or) And(org.apache.sysml.runtime.functionobjects.And) Multiply(org.apache.sysml.runtime.functionobjects.Multiply) Minus1Multiply(org.apache.sysml.runtime.functionobjects.Minus1Multiply) KahanPlus(org.apache.sysml.runtime.functionobjects.KahanPlus) Plus(org.apache.sysml.runtime.functionobjects.Plus) RightScalarOperator(org.apache.sysml.runtime.matrix.operators.RightScalarOperator) Minus(org.apache.sysml.runtime.functionobjects.Minus) Power(org.apache.sysml.runtime.functionobjects.Power) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Aggregations

RightScalarOperator (org.apache.sysml.runtime.matrix.operators.RightScalarOperator)16 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)9 LeftScalarOperator (org.apache.sysml.runtime.matrix.operators.LeftScalarOperator)8 ScalarOperator (org.apache.sysml.runtime.matrix.operators.ScalarOperator)8 Minus1Multiply (org.apache.sysml.runtime.functionobjects.Minus1Multiply)6 Pointer (jcuda.Pointer)4 CompressedMatrixBlock (org.apache.sysml.runtime.compress.CompressedMatrixBlock)4 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)4 KahanPlus (org.apache.sysml.runtime.functionobjects.KahanPlus)4 Multiply (org.apache.sysml.runtime.functionobjects.Multiply)4 Plus (org.apache.sysml.runtime.functionobjects.Plus)4 CSRPointer (org.apache.sysml.runtime.instructions.gpu.context.CSRPointer)4 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)4 BinaryOperator (org.apache.sysml.runtime.matrix.operators.BinaryOperator)4 And (org.apache.sysml.runtime.functionobjects.And)2 Builtin (org.apache.sysml.runtime.functionobjects.Builtin)2 CM (org.apache.sysml.runtime.functionobjects.CM)2 Divide (org.apache.sysml.runtime.functionobjects.Divide)2 Equals (org.apache.sysml.runtime.functionobjects.Equals)2 GreaterThan (org.apache.sysml.runtime.functionobjects.GreaterThan)2