use of org.apache.sysml.runtime.functionobjects.Divide 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
* @throws DMLRuntimeException if DMLRuntimeException occurs
*/
private static void matrixMatrixOp(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 isEmpty1 = isSparseAndEmpty(gCtx, in1);
boolean isEmpty2 = isSparseAndEmpty(gCtx, in2);
int rlenA = (int) in1.getNumRows();
int rlenB = (int) in2.getNumRows();
int clenA = (int) in1.getNumColumns();
int clenB = (int) in2.getNumColumns();
int vecStatusA = getVectorStatus(rlenA, clenA).code();
int vecStatusB = getVectorStatus(rlenB, clenB).code();
if (isEmpty1 && isEmpty2) {
MatrixObject out = ec.getMatrixObject(outputName);
ec.allocateGPUMatrixObject(outputName);
// When both inputs are empty, the output is empty too (except in the case of division)
if (op.fn instanceof Divide) {
out.getGPUObject(gCtx).allocateAndFillDense(Double.NaN);
} 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);
MatrixObject out = ec.getMatrixObject(outputName);
// Allocated the dense output matrix
getDenseMatrixOutputForGPUInstruction(ec, instName, outputName);
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);
}
}
use of org.apache.sysml.runtime.functionobjects.Divide in project incubator-systemml by apache.
the class MatrixBlock method estimateSparsityOnBinary.
private static SparsityEstimate estimateSparsityOnBinary(MatrixBlock m1, MatrixBlock m2, BinaryOperator op) {
SparsityEstimate est = new SparsityEstimate();
//see also, special sparse-safe case for DIV in LibMatrixBincell
if (!op.sparseSafe && !(op.fn instanceof Divide)) {
est.sparse = false;
return est;
}
BinaryAccessType atype = LibMatrixBincell.getBinaryAccessType(m1, m2);
boolean outer = (atype == BinaryAccessType.OUTER_VECTOR_VECTOR);
long m = m1.getNumRows();
long n = outer ? m2.getNumColumns() : m1.getNumColumns();
long nz1 = m1.getNonZeros();
long nz2 = m2.getNonZeros();
//account for matrix vector and vector/vector
long estnnz = 0;
if (atype == BinaryAccessType.OUTER_VECTOR_VECTOR) {
//for outer vector operations the sparsity estimate is exactly known
estnnz = nz1 * nz2;
} else //DEFAULT CASE
{
if (atype == BinaryAccessType.MATRIX_COL_VECTOR)
nz2 = nz2 * n;
else if (atype == BinaryAccessType.MATRIX_ROW_VECTOR)
nz2 = nz2 * m;
//compute output sparsity consistent w/ the hop compiler
OpOp2 bop = op.getBinaryOperatorOpOp2();
double sp1 = OptimizerUtils.getSparsity(m, n, nz1);
double sp2 = OptimizerUtils.getSparsity(m, n, nz2);
double spout = OptimizerUtils.getBinaryOpSparsity(sp1, sp2, bop, true);
estnnz = UtilFunctions.toLong(spout * m * n);
}
est.sparse = evalSparseFormatInMemory(m, n, estnnz);
est.estimatedNonZeros = estnnz;
return est;
}
Aggregations