use of org.apache.sysml.runtime.functionobjects.Minus in project incubator-systemml by apache.
the class LibMatrixCUDA method matrixMatrixArithmetic.
/**
* Performs elementwise arithmetic operation 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 isLeftTransposed true if left-transposed
* @param isRightTransposed true if right-transposed
* @param op binary operator
*/
public static void matrixMatrixArithmetic(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 isCUDALibAvailable = (op.fn instanceof Plus || op.fn instanceof Minus) && !isSparseAndEmpty(gCtx, in1) && !isSparseAndEmpty(gCtx, in2) && !isVector(in1) && !isVector(in2);
if (!isCUDALibAvailable) {
matrixMatrixOp(ec, gCtx, instName, in1, in2, outputName, isLeftTransposed, isRightTransposed, op);
} else {
double alpha;
double beta;
if (op.fn instanceof Plus) {
alpha = 1.0;
beta = 1.0;
} else if (op.fn instanceof Minus) {
alpha = 1.0;
beta = -1.0;
} else {
throw new DMLRuntimeException("Unsupported op");
}
// C = alpha* op( A ) + beta* op ( B )
dgeam(ec, gCtx, instName, in1, in2, outputName, isLeftTransposed, isRightTransposed, alpha, beta);
}
}
use of org.apache.sysml.runtime.functionobjects.Minus 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);
// }
}
use of org.apache.sysml.runtime.functionobjects.Minus in project incubator-systemml by apache.
the class LibMatrixCUDA method matrixScalarArithmetic.
/**
* Performs elementwise operation 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 isLeftTransposed true if left-transposed
* @param isRightTransposed true if right-transposed
* @param op binary operator
* @throws DMLRuntimeException if DMLRuntimeException occurs
*/
public static void matrixScalarArithmetic(ExecutionContext ec, GPUContext gCtx, String instName, MatrixObject in1, MatrixObject in2, String outputName, boolean isLeftTransposed, boolean isRightTransposed, BinaryOperator op) throws DMLRuntimeException {
if (ec.getGPUContext() != 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 isCUDALibAvailable = (op.fn instanceof Plus || op.fn instanceof Minus) && !isSparseAndEmpty(gCtx, in1) && !isSparseAndEmpty(gCtx, in2) && !isVector(in1) && !isVector(in2);
if (!isCUDALibAvailable) {
matrixMatrixOp(ec, gCtx, instName, in1, in2, outputName, isLeftTransposed, isRightTransposed, op);
} else {
double alpha;
double beta;
if (op.fn instanceof Plus) {
alpha = 1.0;
beta = 1.0;
} else if (op.fn instanceof Minus) {
alpha = 1.0;
beta = -1.0;
} else {
throw new DMLRuntimeException("Unsupported op");
}
// C = alpha* op( A ) + beta* op ( B )
dgeam(ec, gCtx, instName, in1, in2, outputName, isLeftTransposed, isRightTransposed, alpha, beta);
}
}
use of org.apache.sysml.runtime.functionobjects.Minus in project systemml by apache.
the class LibMatrixCUDA method matrixMatrixArithmetic.
/**
* Performs elementwise arithmetic operation 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 isLeftTransposed true if left-transposed
* @param isRightTransposed true if right-transposed
* @param op binary operator
*/
public static void matrixMatrixArithmetic(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 isCUDALibAvailable = (op.fn instanceof Plus || op.fn instanceof Minus) && !isSparseAndEmpty(gCtx, in1) && !isSparseAndEmpty(gCtx, in2) && !isVector(in1) && !isVector(in2);
if (!isCUDALibAvailable) {
matrixMatrixOp(ec, gCtx, instName, in1, in2, outputName, isLeftTransposed, isRightTransposed, op);
} else {
double alpha;
double beta;
if (op.fn instanceof Plus) {
alpha = 1.0;
beta = 1.0;
} else if (op.fn instanceof Minus) {
alpha = 1.0;
beta = -1.0;
} else {
throw new DMLRuntimeException("Unsupported op");
}
// C = alpha* op( A ) + beta* op ( B )
dgeam(ec, gCtx, instName, in1, in2, outputName, isLeftTransposed, isRightTransposed, alpha, beta);
}
}
use of org.apache.sysml.runtime.functionobjects.Minus in project 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);
// }
}
Aggregations